[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

spawnl function fails?

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
riclueje
Asked:
riclueje
  • 3
  • 2
1 Solution
 
hougaardCommented:
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
 
ricluejeAuthor Commented:
I can´t because I need the pid to kill the process.
0
 
hougaardCommented:
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
 
ricluejeAuthor Commented:
Can you send me an example? I can´t do it.
0
 
hougaardCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now