Solved

Problem with CreateProcess() (okay on some PCs)

Posted on 2002-03-13
18
641 Views
Last Modified: 2012-05-04

I've used the sample code in KB article:

HOWTO: Spawn Console Processes with Redirected Standard Handles (Q190351)
http://support.microsoft.com/support/kb/articles/Q190/3/51.ASP

...to launch a console program (a batch file calling Opus make).  

The program works fine on my PC - the console program is launched, with stdout and stderr redirected to C:\stdout.txt and C:\stderr.txt, builds the target app, and the process is closed.

On other PCs, the process is launched, the redirection files are created, but the console application never seems to take enough time to do what it needs to do, and the redirection files are empty.

If I map a shared drive on my PC, and call my program under exactly circumstances (same working directory, same drive letter, same batch file being called), everything goes smoothly.  I can't do anything to replicate the problem on my own PC, and all function calls return positively.

All PCs that I'm trying to get it to run on (including my own) are Win98 SE.

Has anybody else had problems with CreateProcess()?

Here is an snippet of my code:

eShellResult_t CShellTool::PrepAndLaunchRedirectedChild(CString          strBuildCommand,
                                                                 HANDLE          hChildStdOut,
                                                                 HANDLE          hChildStdIn,
                                                                 HANDLE          hChildStdErr)
{
     PROCESS_INFORMATION piProcInfo;
     STARTUPINFO siStartInfo;

     // Set up the start up info struct.
     ZeroMemory(&siStartInfo,
                    sizeof(STARTUPINFO));
     siStartInfo.cb = sizeof(STARTUPINFO);
     siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
     siStartInfo.hStdOutput = hChildStdOut;
     siStartInfo.hStdInput  = hChildStdIn;
     siStartInfo.hStdError  = hChildStdErr;
     siStartInfo.wShowWindow = SW_HIDE;

     // Launch the process to be redirected
     if (CreateProcess(NULL,
                              (LPSTR)(LPCTSTR)strBuildCommand,
                              NULL,                         // process security attributes
                              NULL,                         // primary thread security attributes
                              TRUE,                         // handles are inherited
                              CREATE_NEW_CONSOLE,
                              NULL,                         // use parent's environment
                              NULL,                         // use parent's current directory
                              &siStartInfo,
                              &piProcInfo) == 0)
     {
          DisplayError("CreateProcess");
          return eSHELL_ERROR;
     }

     // Set global child process handle to cause threads to exit.
     hChildProcess = piProcInfo.hProcess;

     // Wait for the process to finish
     WaitForSingleObject(hChildProcess, INFINITE);

     // Close any unnecessary handles.
     if (!CloseHandle(piProcInfo.hThread))
     {
          DisplayError("CloseHandle");
          return eSHELL_ERROR;
     }

     return eSHELL_SUCCESS;
}



Thanks!

Nick
0
Comment
Question by:Nick75
  • 7
  • 5
  • 3
  • +2
18 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Try using 'no redirection' on the PCs it doesn't work with, so you can see the output, e.g.

#ifndef NO_PROBLEM
    // Set up the start up info struct.
    ZeroMemory(&siStartInfo,
                   sizeof(STARTUPINFO));
    siStartInfo.cb = sizeof(STARTUPINFO);
    siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    siStartInfo.hStdOutput = hChildStdOut;
    siStartInfo.hStdInput  = hChildStdIn;
    siStartInfo.hStdError  = hChildStdErr;
    siStartInfo.wShowWindow = SW_HIDE;

#else
    // Set up the start up info struct.
    ZeroMemory(&siStartInfo,
                   sizeof(STARTUPINFO));
    siStartInfo.cb = sizeof(STARTUPINFO);
#endif

BTW, what does "strBuildCommand" look like?
0
 
LVL 1

Author Comment

by:Nick75
Comment Utility
strBuildCommand = "T:\\WorkingDir\\ProjectDir\\Apps\\build.bat"
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Does "build.bat" (or the app called from it) use environment varables (like e.g. PATH, INCLUDE, LIB) that are not present or different on the other machines?
0
 
LVL 1

Author Comment

by:Nick75
Comment Utility

environment variables may be used, but directly calling build.bat from any PC (mine or the others with problems) works okay.

...that's the wierd thing - I can use my program, or call the batch file directly on or from my PC, but the others can only call the batch file directly.
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
I'll bet the problem is something in the batch file itself.  Edit that and put some PAUSE commands in it (including one right at the top.  

I think the problem will be immediately obvious -- e.g., you are not in the right directory or you have not set the correct drive to be the default or you are trying to run a program that is in a different diectory on other computers...

-- Dan
0
 
LVL 1

Author Comment

by:Nick75
Comment Utility
because of the redirection, it's hard to say what's going on.

When the first few lines of the batch file are:

echo Hello world
pause

...there is still nothing in the redirection files.

I'll have to try disabling the redirection again.
0
 
LVL 86

Accepted Solution

by:
jkr earned 0 total points
Comment Utility
>>because of the redirection, it's hard to say what's going on

Did you see

Comment
From: jkr  Date: 03/13/2002 09:19AM PST  
Try using 'no redirection' on the PCs it doesn't work with, so you can see the output, e.g.

#ifndef NO_PROBLEM
   // Set up the start up info struct.
   ZeroMemory(&siStartInfo,
                  sizeof(STARTUPINFO));
   siStartInfo.cb = sizeof(STARTUPINFO);
   siStartInfo.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
   siStartInfo.hStdOutput = hChildStdOut;
   siStartInfo.hStdInput  = hChildStdIn;
   siStartInfo.hStdError  = hChildStdErr;
   siStartInfo.wShowWindow = SW_HIDE;

#else
   // Set up the start up info struct.
   ZeroMemory(&siStartInfo,
                  sizeof(STARTUPINFO));
   siStartInfo.cb = sizeof(STARTUPINFO);
#endif
 
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Another thing to try.  Try to executec Cmd.Exe and set as its command-line parameters:

/cT:\somedir\somebatfile.bat

-- Dan
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
Dear

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to split between

     "jkr and DanRollins"


If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
Thanks, but why a "C"???
0
 
LVL 1

Author Comment

by:Nick75
Comment Utility
Sorry, not your fault I know.

I never did get around to solving the problem - I have no idea why it works on my PC but not on others' - the C probably belongs to MS - maybe me :-)

What I was trying to do was to launch a batch file and have all output redirected to a text file.  It was intended for automated test builds of code - if there are any errors or warnings, the program would generate an email and post it out to the team members.

I've got a work around for the problem PCs, but there's no redirection & no indication of an error (perhaps other than a lack of new files in the output directory).  ...kind of defeatst the purpose though...
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
Hi Nick75,
One way to ensure that you get help is to reply to posts that ask you questions and that suggest possible solutions; that is to provide useful feedback during the diagnosis process.  One way to ensure that you get little expert help is to insult experts by 'awarding' a grade of C.

I just thought that I'd throw a bit of enlightenment your way.

-- Dan
0
 
LVL 1

Author Comment

by:Nick75
Comment Utility
I intended the grade of C to reflect the quality of the responses (including mine) to the problem, not the expertise of either jkr or yourself.  

I think that many people buy previously answered questions.  As you say Dan, I probably should have tried to offer more feedback - but the root of the problem was that I couldn't see any cause for the problem.  I don't think this particular question thread would be of any value to anyone else.
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
I know of no expert here who feels they deliver "C" level guidance, which is the lowest grade we have here.  And, as Dan points out, some experts actually look at grading history before deciding if they will step in and help.

That said, as I read this thread 4 times now, the information posted would have helped resolve the original problem precisely and therefore actually contained a cummulative "A" level result for others who may find themselves in this situation and tap our PAQ to find this.  Few, however, would bother to access a question in our PAQ graded as a "C".  The fact that the actual results for you remain "unresolved" due to variables is a tough one; but you did say you found a workaround.  What was that?

I'd appreciate guidance here in making an adjustment to this question, so will listen further.

Moondancer - EE Moderator
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
I'd say: If all the responses are completely useless, this thread should have been deleted. A grade of "C" would indicate it as "average" useful.

BTW - This might have worked out better if there wasn't "miscommunication" like

"From: jkr  Date: 03/13/2002 09:19AM PST  
Try using 'no redirection' on the PCs it doesn't work with, so you can see the output, e.g."

"From: Nick75  Date: 03/14/2002 05:45AM PST  
because of the redirection, it's hard to say what's going on."
0
 
LVL 1

Author Comment

by:Nick75
Comment Utility
This question was posted in the hope of finding a solution to a problem I was having with an in-house project at work.

We only use this application from time-to-time, and this problem was not seen as a major priority, so I've been allocated little time to resolve it since first posting the question.  

jkr has a point that I probably should have tried using 'no redirection', but to be honest, it was that long ago that I can't remember if I did or not.

I didn't mean to offend anyone - perhaps I should read through the FAQs.  I wasn't aware that a question could be deleted, even if I did, I probably would have decided to award the points to jkr if not only for his effort.

As I said, I intended for the grade of C to be awarded to the thread itself, not to jkr.  Because the problem's still unresolved, I feel that this thread doesn't hold much use to anyone with a similar problem.

The work-around that I've used is to replace the call to CreateProcess() with a simpler function that does not enable the output to be examined.  It doesn't give us everything that we want, but it did let us move onto more important issues.

Perhaps the thread should be deleted - if you do & jkr loses the points, I'll post a dummy question for him.
0
 
LVL 1

Expert Comment

by:Moondancer
Comment Utility
Thanks, here's what I've done:
1) Refunded to you 200 points, which took them away from jkr and leaves this Q in our PAQ at zero points.  This leaves a link to the Points for question you will be posting to tie things together.
2) Changed this grade from C to A
3) If you now wish to award points for contributions made, post a new question in this topic area please entitled
Points for __expertname__ and paste the link to this question in the comment field

Anytime you've asked a question and comments have been added, it requires Moderator intervention to delete it, refund points, etc.  We're always happy to help in all such regards, just post a zero point question with your request and the URL in the Community Support topic area.

If more is needed, let me know here via comment.

Thanks all,
Moondancer - EE Moderator
0
 
LVL 1

Author Comment

by:Nick75
Comment Utility
Sorry, think I missed out on the email notification of your addition Moondancer,

Hope this works

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20305099
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now