CreateProcess

Today i started a program that creates a new desktop and runs a program.  well i got the new desktop to create and switch back after the program but i cannot get the process started.  this causes the program to lock into the other desktop which is only able to be switched back by a reset.  when i build in a CreateProcess it keeps telling me i need to convert char* to LPWSTR but when i LPWSTR("string"); it does compile but does not run the program, Please Help!!!

[code]
        STARTUPINFO      si;
      PROCESS_INFORMATION pi;
      si.cb = sizeof(si);
      si.lpTitle = "Test";
      si.lpDesktop = "Test";
      CreateProcess(NULL, "c:\\windows\\system32\\calc.exe", NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
      WaitForSingleObject(pi.hProcess,INFINITE);
[/code]

[error]
Error      1      error C2440: '=' : cannot convert from 'const char [5]' to 'LPWSTR'      line 63
Error      2      error C2664: 'CreateProcessW' : cannot convert parameter 2 from 'const char [29]' to 'LPWSTR'      line 64
[/error]
zeurxAsked:
Who is Participating?
 
TiCpuConnect With a Mentor Commented:
Try using the _T("calc.exe") macro so the compiler automatically chooses between ASCII and Unicode.
0
 
zeurxAuthor Commented:
[debug]
Unhandled exception at 0x7c825352 in Switch Desktop.exe: 0xC0000005: Access violation writing location 0x0041be30.
[/debug]
[code]
      STARTUPINFO      si;
      PROCESS_INFORMATION pi;
      ZeroMemory(&si, sizeof(si));
      si.cb = sizeof(si);
      si.lpTitle = _T("Test");
      si.lpDesktop = _T("Test");
      ZeroMemory(&pi, sizeof(pi));
      CreateProcess(NULL, _T("c:\\windows\\system32\\calc.exe"), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
      WaitForSingleObject(pi.hProcess,10000);
[/code]
it craps at the CreateProcess when it jumps to the wincore.cpp file
[code]
            // cache hit
            lpEntry = pMsgCache->lpEntry;
            winMsgLock.Unlock();
            if (lpEntry == NULL)
                  return FALSE;
[/code]
0
 
mahesh1402Commented:
Why you are initializing si.lpDesktop = _T("Test")... ?

Here is correct way to do this...

     STARTUPINFO     si;
     PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));  //Add this

     si.cb = sizeof(STARTUPINFO);
     si.lpTitle = _T("Test");
     si.lpDesktop = NULL;  //assign NULL

     CreateProcess(NULL, "c:\\windows\\system32\\calc.exe", 0, 0, 0, CREATE_NEW_CONSOLE, 0, 0, &si, &pi);
     WaitForSingleObject(pi.hProcess,INFINITE);

-MAHESH
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
mahesh1402Commented:
are you sure just making _T() solved your problem ?

-MAHESH
0
 
TiCpuCommented:
I told him over IM to initialize a variable with a copy of the string for the command line instead of using a const string, because in CreateProcess the command line is supposed to be in/out not just in hehe.
0
 
mahesh1402Commented:
even if you use _T you will get memory errors with above code...... By assigning NULL to si.lpDesktop only solves above memory related bug..

-MAHESH
0
 
zeurxAuthor Commented:
     HDESK hOriginalThread;
      HDESK hOriginalInput;
      HDESK hNewDesktop;

      hOriginalThread = GetThreadDesktop(GetCurrentThreadId());
      hOriginalInput= OpenInputDesktop(0, FALSE, DESKTOP_SWITCHDESKTOP);

      hNewDesktop = CreateDesktop(_T("Test"), NULL, NULL, 0, GENERIC_ALL, NULL);
      SetThreadDesktop(hNewDesktop);
      SwitchDesktop(hNewDesktop);

      STARTUPINFO      si;
      PROCESS_INFORMATION pi;
      ZeroMemory(&si, sizeof(si));
      si.cb = sizeof(si);
      si.lpTitle = _T("Test");
      si.lpDesktop = _T("Test");
      ZeroMemory(&pi, sizeof(pi));
      LPTSTR sCmdLine = _tcsdup(_T("Lock.exe"));
      CreateProcess(NULL, sCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
      WaitForSingleObject(pi.hProcess,INFINITE);

      SwitchDesktop(hOriginalInput);
      SetThreadDesktop(hOriginalThread);

      CloseDesktop(hNewDesktop);

      ExitProcess(0);
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.