sửa giúp mình lỗi này với

Thảo luận trong 'Lập trình & Đồ hoạ' bắt đầu bởi Mr KOP, 13/12/09.

  1. Mr KOP

    Mr KOP T.E.T.Я.I.S

    Tham gia ngày:
    11/3/07
    Bài viết:
    507
    Nơi ở:
    Saigon
    mình có đoạn code sau

    Mã:
    
    #include <stdio.h>
    #include <conio.h>
    #include <windows.h>
    
    char path[] = "C:\\WINDOWS\\system32\\notepad.exe" ;
    
    int main()
    {
        PROCESS_INFORMATION pif;
        STARTUPINFO si;
    
        printf("Current Process ID = %d\n", GetCurrentProcessId());
        
        ZeroMemory(&si,sizeof(si));
        si.cb = sizeof(si);
    
        // creat a process to run notepad
        printf("Creat a process to run notepad\n");
        CreateProcess( path, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pif); 
        printf("process notepad id: %d\n", GetProcessId(pif.hProcess));
        printf("Press any key to terminate notepad ...\n");
        getch();
        TerminateProcess(pif.hProcess,0);
        CloseHandle(pif.hProcess);
        CloseHandle(pif.hThread);
    
        ZeroMemory(&si,sizeof(si));
        si.cb = sizeof(si);
        printf("Creat a process to run notepad\n");
        CreateProcess( path, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pif);
        printf("Waiting for notepad terminated\n");
        WaitForSingleObject(pif.hProcess, INFINITE);
        CloseHandle(pif.hProcess);
        CloseHandle(pif.hThread);
        return 0;
    }
    
    khi chạy thì nó báo không thể chuyển kiểu char qua LPCWSTR

    nguyên văn như sauerror C2664: 'CreateProcessW' : cannot convert parameter 1 from 'char [31]' to 'LPCWSTR'

    Mấy bro giải thích và sửa hộ với
    cảm ơn trước

    ps: mình có tìm trên mấy trang nước ngoài cũng có nói về chuyển qua lại giữa chả và LPCWSTR nhưng đọc không hiểu :(
     
  2. kakashidinho

    kakashidinho C O N T R A Lão Làng GVN

    Tham gia ngày:
    3/10/07
    Bài viết:
    1,812
    Nếu bạn dùng visual studio thì thử bấm chuột phải vào project chọn properties ->Configuration Properties->General, chỉnh Character Set field thành Use Multi-Byte Character Set.

    Lưu ý:mình dùng visual studio 2008,bản trước đó có thể chỉnh kiểu khác.

    Hoặc có thể dùng cách này :
    thay char path[] = "C:\\WINDOWS\\system32\\notepad.exe" ;
    thành
    wchar_t path[] = L"C:\\WINDOWS\\system32\\notepad.exe" ;
     

Chia sẻ trang này