Solved

spawnl function fails?

Posted on 1999-01-29
5
337 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

839 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