#include <windows.h> #include <stdio.h> class CSomeObj { public: CSomeObj(int index) { m_index = index; printf("Constructor\r\n"); } ~CSomeObj() { printf("Destructor %d \r\n",m_index); } public: int m_index; }; CSomeObj g_GlobalObj(100); int _tmain(int argc, _TCHAR* argv[]) { CSomeObj LocalObj(200); //This shouldn't be here ExitProcess(0); return 0; }
#include <windows.h> #include <stdio.h> typedef DWORD(__stdcall *NtTerminateProcess)(HANDLE, UINT); NtTerminateProcess fNtTerminateProcess = NULL; BOOL ExitProc(HANDLE hProc) { HINSTANCE hModule = LoadLibrary(_T("ntdll.dll")); //加载 ntdll.dll if (hModule != 0) { fNtTerminateProcess = (NtTerminateProcess)GetProcAddress(hModule, "NtTerminateProcess"); //加载外部DLL函数 HANDLE hToken = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)hProc); //获得进程的最大权限 if (hToken != 0) { if (fNtTerminateProcess(hToken, 1) == 0) //关闭程序 { printf("End Proc:%d\n", (int)hProc); return TRUE; } else { return FALSE; } } return FALSE; } } int _tmain(int argc, _TCHAR* argv[]) { STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); ZeroMemory(&pi, sizeof(pi)); TCHAR szpath[] = TEXT("WORDPAD README.txt"); TCHAR lpCommandLine[] = _T("C:\\Windows\\notepad.exe"); // Start the child process. if (!CreateProcess(NULL, // No module name (use command line) lpCommandLine, // Command line NULL, // Process handle not inheritable NULL, // Thread handle not inheritable FALSE, // Set handle inheritance to FALSE 0, // No creation flags NULL, // Use parent's environment block NULL, // Use parent's starting directory &si, // Pointer to STARTUPINFO structure &pi) // Pointer to PROCESS_INFORMATION structure ) { printf("CreateProcess failed (%d)\n", GetLastError()); return 0; } // ExitProcess(0); //ExitThread(0); ExitProc(pi.hProcess); //CloseHandle(pi.hProcess); //CloseHandle(pi.hThread); return 0; }
#include <Windows.h> #include <tchar.h> #include <strsafe.h> int _tmain(int argc, _TCHAR* argv[]) { TCHAR lpCommandLine[] = TEXT("NOTEPAD"); STARTUPINFO si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); SECURITY_ATTRIBUTES ps; ps.bInheritHandle = FALSE; ps.nLength = sizeof(ps); ps.lpSecurityDescriptor = NULL; if (!CreateProcess(NULL, lpCommandLine, &ps, NULL, FALSE, NULL, NULL, NULL, &si, &pi )) { printf("CreateProcess failed (%d)\n", GetLastError()); return 0; } else { DWORD dwExitCode = NULL; //Close the thread handle as soon as //it is no longer needed! //Suspend our execution until //the child has terminated. WaitForSingleObject(pi.hProcess, INFINITE); //The child process terminated; //get its exit code. if (!GetExitCodeProcess(pi.hProcess, &dwExitCode)) { _tprintf(_T("GetExitCodeProcess false")); printf("exitcode failed (%d)\n", GetLastError()); } else { printf("exitcode is %s", dwExitCode); } //Close the process handle as soon as //it is no longer needed. CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } return 0; }
该函数查看进程的内核对象(由hProcess参数来标识),取出内核对象的数据结构中用于标识进程的退出代码的成员。该退出代码的值在pdwExitCode参数指向的DWORD中返回。
// EnumProcess.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <windows.h> #include <tchar.h> #include <strsafe.h> #include <Psapi.h> void PrintProcessNameAndID(DWORD processID) { TCHAR szProcessName[MAX_PATH] = _T("<unknown>"); //进程ID 打开进程 HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); if (NULL != hProcess) { HMODULE hMod; DWORD cbNeeded; if (EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) { GetModuleBaseName(hProcess, hMod, szProcessName, sizeof(szProcessName) / sizeof(TCHAR)); } } _tprintf(_T("(PID: %-4u)\t%20s\n"), processID, szProcessName); CloseHandle(hProcess); } int _tmain(int argc, _TCHAR* argv[]) { DWORD aProcesses[1024], cbNeeded, cProcesses; unsigned int i; //枚举进程的ID if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) return 0; cProcesses = cbNeeded / sizeof(DWORD); for (i = 0; i < cProcesses; i++) if (aProcesses[i] != 0) PrintProcessNameAndID(aProcesses[i]); _tsystem(_T("PAUSE")); return 0; }
原文地址:http://blog.csdn.net/u012938203/article/details/43339857