Question on CreateProcess 
Author Message
 Question on CreateProcess

Hi,

I have problems on how to use CreateProcess.  My code is as follows:
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    CString Temp = getenv("TEMP");

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;

    BOOL Running = CreateProcess(NULL,  "sysval.exe " + Temp + "
1000000000",
                                                     NULL, NULL, FALSE,
0, NULL, NULL, &si, &pi);
    if (Running == TRUE)
    {
  // Close these so the sysytem knows we don't care about the process
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
 }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Sysval.exe is a Dos program for license checking.  It needs two
parameters to run.  The first one is the
directory name.  The second one is a random number.  I can't compile the

program.  I got an error message, something like "can't convert CString
to char*  in the second parameter of CreateProcess", but even I hard
code the command line in CreateProcess, it doesn't work either.  My
question is how to use CreateProcess to call an executable which needs
two command line parameters.  I can go to DOS, and type in "sysval
c:\temp 10000000" to run the sysval program, but I tried lots of
parameter combinations for CreateProcess, I still can't run sysval.  It
is really frustrating to use CreateProcess.  Your insights on this
function are very appreciated.

Thank you very much in advance.

Bing Wang



Sat, 06 Nov 1999 03:00:00 GMT  
 Question on CreateProcess

Quote:
>>BOOL Running = CreateProcess(NULL,  "sysval.exe " + Temp + "

1000000000",NULL, NULL, FALSE,0, NULL, NULL, &si, &pi);

What is this "sysval.exe " + Temp + " 1000000000"
Its not valid C++ code.  CreateProcess needs a char*, a CStirng can not
be converted to a cahr*.  Hence you need to use the GetBuffer and
ReleaseBuffer calls

You need to do this before the call, for example

    CString Temp = getenv("TEMP");

    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;

    CString TempString;
    TempString = "sysval.exe ";
    TempString += Temp;
    TempString += " 1000000";

    char* CmdLine = TempString.GetBuffer();
    BOOL Running = CreateProcess(NULL,  CmdLine ,NULL, NULL, FALSE,0,
NULL, NULL, &si, &pi);

    TempString.ReleaseBuffer;

    if (Running == TRUE)
    {
  // Close these so the sysytem knows we don't care about the process
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
 }



Sat, 06 Nov 1999 03:00:00 GMT  
 Question on CreateProcess

You're most of the way there.

The main problem right now is that you can't build a string this way:

        "sysval.exe " + Temp + " 1000000000"

The first argument in the phrase above is a pointer.  The + operator in
this context will attempt to do pointer arithmetic.  A CString object
cannot be used as the second argument there.

You can append to a CString using the + or += operators:

        CString cmdLine = "sysval.exe " ;
        cmdLine += Temp ;
        cmdLine += " 1000000000" ;

(That can be shortened into one statement.)

You can then pass "cmdLine" as the second argument to CreateProcess,
since CString knows how to convert itself into a const char *.

If the call to CreateProcess fails, you should call GetLastError() to
determine why the call failed.  Error codes are defined in WINERROR.H.

HTH

Quote:

> Hi,

> I have problems on how to use CreateProcess.  My code is as follows:

> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

>     CString Temp = getenv("TEMP");

>     STARTUPINFO si;
>     PROCESS_INFORMATION pi;

>     ZeroMemory(&si, sizeof(STARTUPINFO));
>     si.cb = sizeof(STARTUPINFO);
>     si.dwFlags = STARTF_USESHOWWINDOW;
>     si.wShowWindow = SW_HIDE;

>     BOOL Running = CreateProcess(NULL,  "sysval.exe " + Temp + "
> 1000000000",
>                                                      NULL, NULL,
> FALSE,
> 0, NULL, NULL, &si, &pi);
>     if (Running == TRUE)
>     {
>   // Close these so the sysytem knows we don't care about the process
>         CloseHandle(pi.hThread);
>         CloseHandle(pi.hProcess);
>  }

> //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

> Sysval.exe is a Dos program for license checking.  It needs two
> parameters to run.  The first one is the
> directory name.  The second one is a random number.  I can't compile
> the

> program.  I got an error message, something like "can't convert
> CString
> to char*  in the second parameter of CreateProcess", but even I hard
> code the command line in CreateProcess, it doesn't work either.  My
> question is how to use CreateProcess to call an executable which needs
> two command line parameters.  I can go to DOS, and type in "sysval
> c:\temp 10000000" to run the sysval program, but I tried lots of
> parameter combinations for CreateProcess, I still can't run sysval.
> It
> is really frustrating to use CreateProcess.  Your insights on this
> function are very appreciated.

> Thank you very much in advance.

> Bing Wang

-- Aaron

---------------------------------
Aaron J Margosis

Work phone:  703-873-2622 (703-USE-A-MAC ??!!)



Sat, 06 Nov 1999 03:00:00 GMT  
 Question on CreateProcess

Quote:

>         CString cmdLine = "sysval.exe " ;
>         cmdLine += Temp ;
>         cmdLine += " 1000000000" ;
[...]
> You can then pass "cmdLine" as the second argument to CreateProcess,
> since CString knows how to convert itself into a const char *.

[...]

Jim Marshall's right -- CreateProcess' 2nd param is "char *", not "const
char *".  A CString can't be implicitly converted to a non-const char
*.  You either have to use GetBuffer or build the string in your own
char buffer.

-- Aaron

---------------------------------
Aaron J Margosis

Work phone:  703-873-2622 (703-USE-A-MAC ??!!)



Sat, 06 Nov 1999 03:00:00 GMT  
 Question on CreateProcess

--------------6F93D2A770DDFEF8EC54987F
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Quote:

> >>BOOL Running = CreateProcess(NULL,  "sysval.exe " + Temp + "
> 1000000000",NULL, NULL, FALSE,0, NULL, NULL, &si, &pi);

> What is this "sysval.exe " + Temp + " 1000000000"
> Its not valid C++ code.  CreateProcess needs a char*, a CStirng can
> not
> be converted to a cahr*.  Hence you need to use the GetBuffer and
> ReleaseBuffer calls

> You need to do this before the call, for example

>     CString Temp = getenv("TEMP");

>     STARTUPINFO si;
>     PROCESS_INFORMATION pi;

>     ZeroMemory(&si, sizeof(STARTUPINFO));
>     si.cb = sizeof(STARTUPINFO);
>     si.dwFlags = STARTF_USESHOWWINDOW;
>     si.wShowWindow = SW_HIDE;

>     CString TempString;
>     TempString = "sysval.exe ";
>     TempString += Temp;
>     TempString += " 1000000";

>     char* CmdLine = TempString.GetBuffer();
>     BOOL Running = CreateProcess(NULL,  CmdLine ,NULL, NULL,
> FALSE,0,
> NULL, NULL, &si, &pi);

>     TempString.ReleaseBuffer;

>     if (Running == TRUE)
>     {
>   // Close these so the sysytem knows we don't care about the
> process
>         CloseHandle(pi.hThread);
>         CloseHandle(pi.hProcess);
>  }

 Dear Jim,

Thank you very much for your answer.  Yes, it works.  But I got another
problem.  This sysval.exe will generate a tempory file which is called
something like "*.sc" which the name is random but alway with extention
.sc.  After this CreateProcess call, I will rename this file to a
certain name (sysval.sc), then I will open it and read it.  But the
problem is that the rename happens before the sysval program finishes.
I guess I have created a multithreading issue here.  I have attached my
code as follows:

     STARTUPINFO si;
     PROCESS_INFORMATION pi;

     ZeroMemory(&si, sizeof(STARTUPINFO));
     si.cb = sizeof(STARTUPINFO);
     si.dwFlags = STARTF_USESHOWWINDOW;
     si.wShowWindow = SW_HIDE;

     CString TempString;
     TempString = "sysval.exe ";
     TempString += Temp;
     TempString += " 10000000";

     char* CmdLine = TempString.GetBuffer(30);

 BOOL Running = CreateProcess(NULL, CmdLine, NULL, NULL, FALSE, 0, NULL,

NULL, &si, &pi);

 TempString.ReleaseBuffer();
    if (Running == TRUE)
    {
  // Close these so the sysytem knows we don't care about the process
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
 }

//////////////////////////////////////////////////////////////////////////////////////////////

 CString FileName = Temp + "\\*.sc";
 CString DstFileName = Temp + "\\sysval.sc";
 FindHandle = FindFirstFile(FileName, &FindFileData);

 if (FindHandle != INVALID_HANDLE_VALUE) {
  MoveFile(Temp + "\\" +FindFileData.cFileName, DstFileName);
        FindClose( FindHandle );
 }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

I think I have to set some parameter in the CreateProcess to suspend the
main thread, but I don't know how to do it.  The Sysval.exe program is a
DOS program for license checking,  I call in the InitInstance in CWinApp
class to check valid license.  It usually should be hide while running
if the license is valid, but if the license is not valid, it should be
showed to ask some question to the user.  I have set the dwShowWindow
parameter to SW_HIDE, will it alway hides the Dos Window.  I appreciate
very much if you could help me out.  I am really struggling with
CreateProcess.

Thank you again.

Bing Wang

--------------6F93D2A770DDFEF8EC54987F
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit

<HTML><BODY>

<BLOCKQUOTE TYPE=CITE><I>&gt;&gt;BOOL Running = CreateProcess(NULL,&nbsp; "sysval.exe
" + Temp + "</I>
<BR>1000000000",NULL, NULL, FALSE,0, NULL, NULL, &amp;si, &amp;pi);
<BR>
<BR>What is this "sysval.exe " + Temp + " 1000000000"
<BR>Its not valid C++ code.&nbsp; CreateProcess needs a char*, a CStirng can
not
<BR>be converted to a cahr*.&nbsp; Hence you need to use the GetBuffer and
<BR>ReleaseBuffer calls
<BR>
<BR>You need to do this before the call, for example
<BR>
<BR>&nbsp;&nbsp;&nbsp; CString Temp = getenv("TEMP");
<BR>
<BR>&nbsp;&nbsp;&nbsp; STARTUPINFO si;
<BR>&nbsp;&nbsp;&nbsp; PROCESS_INFORMATION pi;
<BR>
<BR>&nbsp;&nbsp;&nbsp; ZeroMemory(&amp;si, sizeof(STARTUPINFO));
<BR>&nbsp;&nbsp;&nbsp; si.cb = sizeof(STARTUPINFO);
<BR>&nbsp;&nbsp;&nbsp; si.dwFlags = STARTF_USESHOWWINDOW;
<BR>&nbsp;&nbsp;&nbsp; si.wShowWindow = SW_HIDE;
<BR>
<BR>&nbsp;&nbsp;&nbsp; CString TempString;
<BR>&nbsp;&nbsp;&nbsp; TempString = "sysval.exe ";
<BR>&nbsp;&nbsp;&nbsp; TempString += Temp;
<BR>&nbsp;&nbsp;&nbsp; TempString += " 1000000";
<BR>
<BR>&nbsp;&nbsp;&nbsp; char* CmdLine = TempString.GetBuffer();
<BR>&nbsp;&nbsp;&nbsp; BOOL Running = CreateProcess(NULL,&nbsp; CmdLine ,NULL,
NULL, FALSE,0,
<BR>NULL, NULL, &amp;si, &amp;pi);
<BR>
<BR>&nbsp;&nbsp;&nbsp; TempString.ReleaseBuffer;
<BR>
<BR>&nbsp;&nbsp;&nbsp; if (Running == TRUE)
<BR>&nbsp;&nbsp;&nbsp; {
<BR>&nbsp; // Close these so the sysytem knows we don't care about the process
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseHandle(pi.hThread);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseHandle(pi.hProcess);
<BR>&nbsp;}
</BLOCKQUOTE>
&nbsp;Dear Jim,
<BR>
<BR>Thank you very much for your answer.&nbsp; Yes, it works.&nbsp; But I got
another problem.&nbsp; This sysval.exe will generate a tempory file which
is called something like "*.sc" which the name is random but alway with
extention .sc.&nbsp; After this CreateProcess call, I will rename this
file to a certain name (sysval.sc), then I will open it and read it.&nbsp;
But the problem is that the rename happens before the sysval program finishes.&nbsp;
I guess I have created a multithreading issue here.&nbsp; I have attached
my code as follows:
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; STARTUPINFO si;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; PROCESS_INFORMATION pi;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; ZeroMemory(&amp;si, sizeof(STARTUPINFO));
<BR>&nbsp;&nbsp;&nbsp;&nbsp; si.cb = sizeof(STARTUPINFO);
<BR>&nbsp;&nbsp;&nbsp;&nbsp; si.dwFlags = STARTF_USESHOWWINDOW;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; si.wShowWindow = SW_HIDE;
<BR>
<BR>&nbsp;&nbsp;&nbsp;&nbsp; CString TempString;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; TempString = "sysval.exe ";
<BR>&nbsp;&nbsp;&nbsp;&nbsp; TempString += Temp;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; TempString += " 10000000";
<BR>&nbsp;
<BR>&nbsp;&nbsp;&nbsp;&nbsp; char* CmdLine = TempString.GetBuffer(30);
<BR>
<BR>&nbsp;BOOL Running = CreateProcess(NULL, CmdLine, NULL, NULL, FALSE, 0,
NULL,
<BR>NULL, &amp;si, &amp;pi);
<BR>
<BR>&nbsp;TempString.ReleaseBuffer();
<BR>&nbsp;&nbsp;&nbsp; if (Running == TRUE)
<BR>&nbsp;&nbsp;&nbsp; {
<BR>&nbsp; // Close these so the sysytem knows we don't care about the process
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseHandle(pi.hThread);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; CloseHandle(pi.hProcess);
<BR>&nbsp;}
<BR>
<BR>
<BR>//////////////////////////////////////////////////////////////////////////////////////////////
<BR>
<BR>&nbsp;CString FileName = Temp + "\\*.sc";
<BR>&nbsp;CString DstFileName = Temp + "\\sysval.sc";
<BR>&nbsp;FindHandle = FindFirstFile(FileName, &amp;FindFileData);
<BR>
<BR>&nbsp;if (FindHandle != INVALID_HANDLE_VALUE) {
<BR>&nbsp; MoveFile(Temp + "\\" +FindFileData.cFileName, DstFileName);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FindClose( FindHandle );
<BR>&nbsp;}
<BR>
<BR>/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<BR>
<BR>I think I have to set some parameter in the CreateProcess to suspend the
main thread, but I don't know how to do it.&nbsp; The Sysval.exe program
is a DOS program for license checking,&nbsp; I call in the InitInstance
in CWinApp class to check valid license.&nbsp; It usually should be hide
while running if the license is valid, but if the license is not valid,
it should be showed to ask some question to the user.&nbsp; I have set
the dwShowWindow parameter to SW_HIDE, will it alway hides the Dos Window.&nbsp;
I appreciate very much if you could help me out.&nbsp; I am really struggling
with CreateProcess.
<BR>
<BR>Thank you again.
<BR>
<BR>Bing Wang

</BODY>
</HTML>

--------------6F93D2A770DDFEF8EC54987F--



Sat, 06 Nov 1999 03:00:00 GMT  
 Question on CreateProcess

You need a call to WaitForSingleObject  for example

if (Running == TRUE)
    {
  // Close these so the sysytem knows we don't care about the process
        WaitForSingleObject(pi.hThread, INFINITE)
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
    }

Note: the INFINITE part will make your app hang for ever until
sysval.exe exits



Sun, 07 Nov 1999 03:00:00 GMT  
 Question on CreateProcess

Quote:

> You need a call to WaitForSingleObject  for example

> if (Running == TRUE)
>     {
>   // Close these so the sysytem knows we don't care about the
> process
>         WaitForSingleObject(pi.hThread, INFINITE)
>         CloseHandle(pi.hThread);
>         CloseHandle(pi.hProcess);
>     }

> Note: the INFINITE part will make your app hang for ever until
> sysval.exe exits

Hi, Jim:

Thank you very much for your help.  I added the "WaitForSingleObject..."
function and it works.
But Since I hide the Dos window for sysval.exe, if the licence is
invalid, I need to answer some questions in the dos window for
sysval.exe, so the program is waiting forever.  How to kill the thread?
I want to kill it and use CreateProcess again to regenerate the
sysval.exe with dos window shown, so that I can answer some questions in
this dos window, and then the application will be terminated due to
invalid licensing.  I know it is kind of complicated.  I know I should
rewrite the sysval.exe, but I really don't have time now.  I want to use
CreateProcess to hide the Dos window when the licence is valid, and I
want the dos window to be shown for invalid licence.  Sysval.exe is my
license checker.  I hope I won't confuse you.  For now, I have to use
_spawnl to call sysval.exe, but I always have the splashing dos window
even though the licence is valid.

I appreciate very much for your time and consideration.

Bing Wang



Sun, 07 Nov 1999 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. question about CreateProcess

2. Question About CreateProcess

3. Question about CreateProcess

4. Question on CreateProcess

5. CreateProcess in CreateProcess

6. CreateProcess Questions

7. createprocess question

8. CreateProcess question

9. CreateProcess question

10. CreateProcess() question

11. CreateProcess Question?

12. createProcess question

 

 
Powered by phpBB® Forum Software