[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

CreateProcess

Posted on 2006-05-15
7
Medium Priority
?
2,010 Views
Last Modified: 2013-11-20
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]
0
Comment
Question by:zeurx
  • 3
  • 2
  • 2
7 Comments
 
LVL 1

Accepted Solution

by:
TiCpu earned 375 total points
ID: 16687505
Try using the _T("calc.exe") macro so the compiler automatically chooses between ASCII and Unicode.
0
 

Author Comment

by:zeurx
ID: 16687743
[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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16687909
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:mahesh1402
ID: 16687961
are you sure just making _T() solved your problem ?

-MAHESH
0
 
LVL 1

Expert Comment

by:TiCpu
ID: 16687979
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
 
LVL 22

Expert Comment

by:mahesh1402
ID: 16687988
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
 

Author Comment

by:zeurx
ID: 16689596
     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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question