Creating and running the Windows process

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows Xp Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add netapi32.lib (netapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Creating and running the Windows process

To show: The threads and processes operation in Windows system

 

 

 

// For WinXp as a target, change accordingly

#define _WIN32_WINNT 0x0501

 

#include <windows.h>

#include <stdio.h>

 

void main(void)

{

STARTUPINFO si;

PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));

si.cb = sizeof(si);

ZeroMemory(&pi, sizeof(pi));

 

// Start the child process.

if (!CreateProcess(L"C:\\WINDOWS\\system32\\cmd.exe", // module name which is the Windows cmd command

NULL, // Command line.

NULL, // Process handle not inheritable.

NULL, // Thread handle not inheritable.

FALSE, // Set handle inheritance to FALSE.

0, // No creation flags.

NULL, // Use parents environment block.

NULL, // Use parents starting directory.

&si, // Pointer to STARTUPINFO structure.

&pi) // Pointer to PROCESS_INFORMATION structure.

)

printf("\nSorry! CreateProcess() failed.\n\n");

else

printf("\nWell, CreateProcess() looks OK.\n\n");

 

// Wait until child process exits (in milliseconds). If INFINITE, the functions time-out interval never elapses except with user or other intervention.

WaitForSingleObject(pi.hProcess, INFINITE);

printf("\n");

 

// Close process and thread handles.

CloseHandle(pi.hProcess);

CloseHandle(pi.hThread);

}

 

Output example:

 

Well, CreateProcess() looks OK.

Microsoft Windows XP [Version 5.1.2600]

(C) Copyright 1985-2001 Microsoft Corp.

f:\vc2005project\cplus\cplus>dir

Volume in drive F is hd0c

Volume Serial Number is 98DB-52B1

Directory of f:\vc2005project\cplus\cplus

12/24/2006 03:23 PM <DIR> .

12/24/2006 03:23 PM <DIR> ..

12/23/2006 05:56 PM 4,103 cplus.vcproj

12/24/2006 03:23 PM 1,423 cplus.vcproj.MYPERSONAL.Johnny.user

12/24/2006 03:23 PM 1,333 cplusrc.cpp

12/24/2006 03:23 PM <DIR> Debug

12/02/2006 10:11 PM 812 test.h

12/14/2006 10:20 PM 0 ????

5 File(s) 7,671 bytes

3 Dir(s) 8,619,315,200 bytes free

f:\vc2005project\cplus\cplus>exit

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index