Solved

spawnl function fails?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

708 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

15 Experts available now in Live!

Get 1:1 Help Now