Solved

spawnl function fails?

Posted on 1999-01-29
5
335 Views
Last Modified: 2008-03-06
I am working on NT Server.I execute a program inside another with the spawnl function.
I wrote:
       father_pid = getpid ();
       if( (pid = spawnl (P_NOWAIT, argv[0], argv[0], NULL ) ) == -1 )
          printf( "Spawn failed" );

The pid must to be the pid of de called program and the father_pid the pid of the calling program.  I start the Task Manager and can see that the father_pid is correct, but the pid of the called program is different, so I only can know the pid of the called program if I see the Task Manager.

Has the spawnl function failed?. Thanks.

0
Comment
Question by:riclueje
  • 3
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
hougaard earned 50 total points
ID: 1258499
Use CreateProcess instead ... That is the official function within win32 to do a spawn!

BOOL CreateProcess(

    LPCTSTR lpApplicationName,      // pointer to name of executable module
    LPTSTR lpCommandLine,      // pointer to command line string
    LPSECURITY_ATTRIBUTES lpProcessAttributes,      // pointer to process security attributes
    LPSECURITY_ATTRIBUTES lpThreadAttributes,      // pointer to thread security attributes
    BOOL bInheritHandles,      // handle inheritance flag
    DWORD dwCreationFlags,      // creation flags
    LPVOID lpEnvironment,      // pointer to new environment block
    LPCTSTR lpCurrentDirectory,      // pointer to current directory name
    LPSTARTUPINFO lpStartupInfo,      // pointer to STARTUPINFO
    LPPROCESS_INFORMATION lpProcessInformation       // pointer to PROCESS_INFORMATION  
   );


0
 

Author Comment

by:riclueje
ID: 1258500
I can´t because I need the pid to kill the process.
0
 
LVL 2

Expert Comment

by:hougaard
ID: 1258501
The PROCESS_INFORMATION structure returned will give you the PID in the dwProcessId member!

typedef struct _PROCESS_INFORMATION { // pi
    HANDLE hProcess;
    HANDLE hThread;
    DWORD dwProcessId;
    DWORD dwThreadId;
} PROCESS_INFORMATION;
 

Members

hProcess

Returns a handle to the newly created process. The handle is used to specify the process in all functions that perform operations on the process object.

hThread

Returns a handle to the primary thread of the newly created process. The handle is used to specify the thread in all functions that perform operations on the thread object.

dwProcessId

Returns a global process identifier that can be used to identify a process. The value is valid from the time the process is created until the time the process is terminated.

dwThreadId

Returns a global thread identifiers that can be used to identify a thread. The value is valid from the time the thread is created until the time the thread is terminated.


0
 

Author Comment

by:riclueje
ID: 1258502
Can you send me an example? I can´t do it.
0
 
LVL 2

Expert Comment

by:hougaard
ID: 1258503
STARTUPINFO         si;
PROCESS_INFORMATION pi;

    si.cb=sizeof(STARTUPINFO);
    si.lpReserved=NULL;
    si.lpTitle=NULL;
    si.lpDesktop=NULL;
    si.dwX=si.dwY=si.dwYSize=si.dwXSize=0;
    si.dwFlags=0;
    si.wShowWindow=SW_SHOW;
    si.lpReserved2=NULL;
    si.cbReserved2=0;

if (CreateProcess(NULL,
            "C:\WINNT\SYSTEM32\TELNET.EXE",
            NULL,
            NULL,
            FALSE,
            CREATE_NEW_CONSOLE,
            NORMAL_PRIORITY_CLASS,
           NULL,
           NULL,
           &si,
           &pi))
{
  printf("Process ID = %i\n",pi.dwProcessId);
}
}
           
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

920 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

16 Experts available now in Live!

Get 1:1 Help Now