none
Cannot convert const char* (variable) to LPCWSTR

    Question

  • Hi, i'm making a program in VC++ 2008 express to work as a runas tool; below is my source:

    #include <windows.h>
    #include <stdio.h> 
    #include <ntsecapi.h>
    
    int goRun(LPCWSTR user,LPCWSTR password,LPCWSTR program)
    {
    	STARTUPINFOW su_info;
    	ZeroMemory(&su_info, sizeof(STARTUPINFOW));
    	su_info.cb = sizeof(STARTUPINFOW);
    
    	PROCESS_INFORMATION pi;
    	ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
    	CreateProcessWithLogonW(user, L"localhost", password, 0, 
    		program , NULL, 0, NULL, NULL, &su_info, &pi);
    	return 0;
    }
    
    int runAs(const char* sUser, const char* sPassword, const char* sProgram)
    {
    	const LPCWSTR user = LPCWSTR(sUser);
    	const LPCWSTR password = LPCWSTR(sPassword);
    	const LPCWSTR program = LPCWSTR(sProgram);
    
    	goRun(user, password, program);
    	return 0;
    }
    
    int main()
    {
    	runAs("chuck","norris","C:\\windows\\notepad.exe");
    	return 0;
    }


    eventually this will be used as a dll so the function main resembles the calling program.

    Thanks in advance :)
    ---
    weirdy
    Sunday, February 21, 2010 5:03 PM

Answers

All replies

  • So why not ditch your runAs function and just use Unicode strings
    throughout?

    Dave
    Sunday, February 21, 2010 5:10 PM
  • Why are you mixing chars (8 bit characters) and wide chars (16 bit characters)?  They are not the same thing and they are not interchangeable.  So if you are passed a const char* sUser (which is a pointer to narrow characters) you cannot simply cast it to LPCWSTR (which is a pointer to wide characters).  It is possible to convert the characters, but why are you making such conversions necessary?  Decide which character set your program will use, then use it consistently.

    Sunday, February 21, 2010 5:14 PM
  • I updated my code to this:

    #include <windows.h>
    #include <stdio.h> 
    #include <ntsecapi.h>
    
    int goRun(LPCWSTR user,LPCWSTR password,LPCWSTR program)
    {
    	STARTUPINFOW su_info;
    	ZeroMemory(&su_info, sizeof(STARTUPINFOW));
    	su_info.cb = sizeof(STARTUPINFOW);
    
    	PROCESS_INFORMATION pi;
    	ZeroMemory(&pi, sizeof(PROCESS_INFORMATION));
    	CreateProcessWithLogonW(user, L"localhost", password, 0, 
    		program , NULL, 0, NULL, NULL, &su_info, &pi);
    	return 0;
    }
    
    int main()
    {
    	goRun("chuck","norris","C:\\windows\\notepad.exe");
    	return 0;
    }

    I get the error about not being able to convert between const char and LPCWSTR at this point; what can I do to make them the same?

    I'll add that the calling program doesnt have support for LPCWSTR's, so i can only pass const char's into the function
    • Edited by Weirdy Sunday, February 21, 2010 5:21 PM Updated code block
    Sunday, February 21, 2010 5:19 PM
  • Well, this is why I think wide characters should be taught at the same time as char.

    Try

    goRun(L"chuck", L"norris",L"c:\\windows\\notepad.exe");

    This will explicitly tell the compiler that the strings are to be seen as wide character strings.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    Sunday, February 21, 2010 5:23 PM
  • Thanks for the help, but they can't change as those resemble values passed into the program from another tool, which doesnt support LPCWSTR's

    Sunday, February 21, 2010 5:27 PM
  • Then you are going to have to use MultiByteToWideChar to convert your strings to wchar_t* before you carry on.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
    • Marked as answer by Nancy Shao Monday, March 01, 2010 6:46 AM
    Sunday, February 21, 2010 5:32 PM
  • Thanks for the help!
    Sunday, February 21, 2010 5:33 PM
  • > Thanks for the help, but they can't change as those resemble values
    > passed into the program from another tool, which doesnt support LPCWSTR's

    First, make 100% sure this other interface can only support ANSI (char)
    characters. Full unicode (wchar_t/LPCWSTR) is by far the best way to go
    here if it's at all possible.

    If it really isn't possible to use Unicode, convert using
    MultiByteToWideChar - or more simply using the CA2CW macros that you'll
    find in VC++ - see the "ATL and MFC String Conversion Macros" topic in
    MSDN for more information.

    Dave
    Sunday, February 21, 2010 6:26 PM