?
Solved

Using CreateProcess() and spawn functions

Posted on 1997-10-27
5
Medium Priority
?
1,049 Views
Last Modified: 2012-06-27
I haven't used these before, so I'm not sure what I might be doing wrong.

Spawn functions:
I'm trying to use the spawn functions so I can start an application (child process) and put the calling application (parent) in suspension while the child process runs.  
I'm able to get the child process to start ok, but not with any command line parameters.  And the only way I can get them to work is to give the name of the application in both the command name and argument strings.  If I add the command line parameters, the child process doesn't start up.

CreateProcess():
I need to use this so I can get the handle of the process back so I can terminate it from parent process.  


Any suggestions as to things that are frequently done incorrectly or things that MUST be done in a certain way for these functions would be greatly appreciated.


Jody
0
Comment
Question by:jodyl
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:jodyl
ID: 1172121
I forgot to mention, I'm doing this in MSVC++ 5.0

Jody

0
 
LVL 2

Expert Comment

by:wpinto
ID: 1172122
Hi jody,

I tried the following & it worked fine for me

spawnl( _P_WAIT, "c:\\winnt\\system32\\notepad.exe", "notepad.exe", "c:\\temp\\profiled.txt", NULL );

ie. My app launched Notepad giving it the file profiled.txt

Notes:
The last parameter to spanwl() should be NULL.
The third parameter should be the name of the app (not the first argument to the app)

I remember while programming under DOS, argv[0] contained the name of the app & the arguments actually started from argv[1]... I guess thats the reason the first argument (ie. spawnl() param 3) should be the name of the app!?

btw, which platform are you working under? I tried the above under Windows NT 4.0.

Hope this helps, I will give you some info regarding CreateProcess() as soon as I get some time

Wilfred
0
 

Author Comment

by:jodyl
ID: 1172123
I'm using Win95.

I had not tried it exactly like yours,
spawnl( _P_WAIT, "c:\\winnt\\system32\\notepad.exe", "notepad.exe", "c:\\temp\\profiled.txt", NULL
                         );
I'd tried this:
spawnl( _P_WAIT, "c:\\windows\\notepad.exe", "c:\\temp\\temp.txt", NULL );
and this:
spawnl( _P_WAIT, "c:\\windows\\notepad.exe", "c:\\windows\\notepad.exe", NULL );


I tried what you suggested and it worked fine.  I didn't think about argv[0] being the name of the app in DOS, that makes what the help on _spawn*() says about the arguments make more sense to me now.

Thanks,
Jody


0
 
LVL 2

Accepted Solution

by:
wpinto earned 800 total points
ID: 1172124
Hi jody,

Here's the code that you could use for CreateProcess()

-----------------------------------
PROCESS_INFORMATION processInfo;
STARTUPINFO startupInfo;

memset(&startupInfo, 0, sizeof(STARTUPINFO));
startupInfo.cb  = sizeof(STARTUPINFO);
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_SHOW;

BOOL bReturn = ::CreateProcess("c:\\winnt\\system32\\notepad.exe", "notepad c:\\temp\\profiled.txt", NULL, NULL, TRUE, CREATE_SUSPENDED | NORMAL_PRIORITY_CLASS, NULL, NULL, &startupInfo, &processInfo);

::ResumeThread(processInfo.hThread);
return;
---------------------------------------------------

Please note that all the information that you would need after a call to CreateProcess is contained in processInfo.
Also you should modify the startupInfo structure if you want to modify the display characteristics of the new app (maximized...)

Hope this helps

Wilfred

Note: You could even have a look at the ShellExecute() function.
0
 

Author Comment

by:jodyl
ID: 1172125
Worked great, was able to open and close the app from the parent process.  My problem in both cases seems to be a misunderstanding  of exactly what should be put into the application, command and argument strings.

Thanks very much for your help Wilfred.

Jody

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 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