Created
June 5, 2014 03:25
-
-
Save ihnorton/fb6985e5895dd7eef986 to your computer and use it in GitHub Desktop.
testcp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// envtest.cpp : Defines the entry point for the console application. | |
// | |
#include <windows.h> | |
#include <tchar.h> | |
#include <stdio.h> | |
int _tmain() | |
{ | |
LPTSTR lpszVariable; | |
LPTCH lpvEnv; | |
// Get a pointer to the environment block. | |
lpvEnv = GetEnvironmentStrings(); | |
// If the returned pointer is NULL, exit. | |
if (lpvEnv == NULL) | |
{ | |
printf("GetEnvironmentStrings failed (%d)\n", GetLastError()); | |
return 0; | |
} | |
// Variable strings are separated by NULL byte, and the block is | |
// terminated by a NULL byte. | |
lpszVariable = (LPTSTR) lpvEnv; | |
while (*lpszVariable) | |
{ | |
_tprintf(TEXT("%s\n"), lpszVariable); | |
lpszVariable += lstrlen(lpszVariable) + 1; | |
} | |
FreeEnvironmentStrings(lpvEnv); | |
_tprintf(TEXT("\n\n\n")); | |
///////////////////////////////////////////////// | |
STARTUPINFO si; | |
PROCESS_INFORMATION pi; | |
ZeroMemory( &si, sizeof(si) ); | |
si.cb = sizeof(si); | |
ZeroMemory( &pi, sizeof(pi) ); | |
DWORD dwFlags = CREATE_UNICODE_ENVIRONMENT; | |
WCHAR cmd[] = L"ping www.github.com"; | |
// Start the child process. | |
if( !CreateProcessW( NULL, // No module name (use command line) | |
cmd, // Command line | |
NULL, // Process handle not inheritable | |
NULL, // Thread handle not inheritable | |
FALSE, // Set handle inheritance to FALSE | |
dwFlags, // 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; | |
} | |
// Wait until child process exits. | |
WaitForSingleObject( pi.hProcess, INFINITE ); | |
// Close process and thread handles. | |
CloseHandle( pi.hProcess ); | |
CloseHandle( pi.hThread ); | |
return 1; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment