CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Posts
    230

    win32 CreateProcess cannot convert parameter 2 from 'char *__w64 ' to 'LPWSTR' error

    hello all i have code from open source project that im integrating into my code . now my code settings in visual studio 2008 character set is Unicode . and the external code is Multi-Byte Character Set. when i change the character setting in my application after adding the new source im getting other errors in my code .
    so reading from the net i guess i need to make some changes to the external code to support unicode. here is my code :


    string FullPathToExe = c:\\foo\\boo.exe;
    vector<char> str2(FullPathToExe.begin(), FullPathToExe.end());
    str2.push_back('\0');
    if (!CreateProcess(NULL,
    &str2[0],
    NULL,
    NULL,
    TRUE,
    0,
    NULL,
    NULL,
    &si,
    &pi))
    and the error is :
    : error C2664: 'CreateProcessW' : cannot convert parameter 2 from 'char *__w64 ' to 'LPWSTR'

    the second error is : the code:

    CHAR szPrintBuffer[512];
    wsprintf(szPrintBuffer,
    "ERROR: API = %s.\n error code = %d.\n message = %s.\n",
    pszAPI, GetLastError(), (char *)lpvMessageBuffer);
    and the error is :
    error C2664: 'wsprintfW' : cannot convert parameter 1 from 'CHAR [512]' to 'LPWSTR'
    1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

    im not win32 programmer and this is new to me. Thanks for helping
    c++ winapi Unicode

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: win32 CreateProcess cannot convert parameter 2 from 'char *__w64 ' to 'LPWSTR' er

    Since you're compiling for unicode you need to change every char to wchar_t and every string literal to L"MyString".

    If you ever want to compile for multibyte use TCHAR for every char/wchar_t and _T("MyString") for every string literal.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: win32 CreateProcess cannot convert parameter 2 from 'char *__w64 ' to 'LPWSTR' er

    Along with using the _T("") macro, consider using the CString class instead of the stl string or wstring classes. It correctly handles ANSI and UNICODE based on the compilers build settings.

    The newer CString class is a templated class and doesn't rely on MFC (unlike the old CString class in VC6). To use it, just #include <atlstr.h>.

    Code:
    #include <atlstr.h>
    
    CString sFilePath = _T("c:\\foo\\boo.exe");
    
    if (!CreateProcess(NULL,
      sFilePath,
      NULL,
      NULL,
      TRUE,
      0,
      NULL,
      NULL,
      &si,
      &pi))

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured