Skip to content

Instantly share code, notes, and snippets.

@livz
Created August 29, 2017 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save livz/7be971ca570434ed9e0700fa0bd18a21 to your computer and use it in GitHub Desktop.
Save livz/7be971ca570434ed9e0700fa0bd18a21 to your computer and use it in GitHub Desktop.
CreateProcess run-time loading of libraries
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
/*
* Compile with:
* cl /nologo /EHsc ExeCreateProcDynamic.c
*
*/
typedef BOOL (WINAPI * ptrCreateProcess)(
LPCTSTR ,
LPTSTR,
LPSECURITY_ATTRIBUTES,
LPSECURITY_ATTRIBUTES,
BOOL,
DWORD,
LPVOID,
LPCTSTR,
LPSTARTUPINFO,
LPPROCESS_INFORMATION);
ptrCreateProcess fCreateProcess;
void _tmain( int argc, TCHAR *argv[] )
{
HMODULE hKernel32;
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
hKernel32 = LoadLibraryA("kernel32.dll");
if (!hKernel32) {
printf( "LoadLibrary failed (%d).\n", GetLastError() );
return;
}
fCreateProcess = (ptrCreateProcess) GetProcAddress(hKernel32, "CreateProcessA");
// Start the child process.
if( !fCreateProcess( NULL, // No module name (use command line)
"hello.exe",
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;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment