Skip to content

Instantly share code, notes, and snippets.

@yoggy
Created March 6, 2012 09:36
Show Gist options
  • Save yoggy/1985321 to your computer and use it in GitHub Desktop.
Save yoggy/1985321 to your computer and use it in GitHub Desktop.
How to use CreateProcess() function (WIN32)
//
// see also...
//
// CreateProcess (MSDN)
// http://msdn.microsoft.com/ja-jp/library/cc429066.aspx
//
#include <SDKDDKVer.h>
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
#ifdef _DEBUG
const char *target_cmd = "TestConsoleAppD.exe";
#else
const char *target_cmd = "TestConsoleApp.exe";
#endif
STARTUPINFO startup_info;
PROCESS_INFORMATION process_info;
memset(&startup_info, 0, sizeof(STARTUPINFO));
startup_info.cb = sizeof(STARTUPINFO);
memset(&process_info, 0, sizeof(PROCESS_INFORMATION));
BOOL rv = ::CreateProcess(
target_cmd, // LPCTSTR lpApplicationName
NULL, // LPTSTR lpCommandLine
NULL, // LPSECURITY_ATTRIBUTES lpProcessAttributes
NULL, // LPSECURITY_ATTRIBUTES lpThreadAttributes
FALSE, // BOOL bInheritHandles
CREATE_NO_WINDOW, // DWORD dwCreationFlags
NULL, // LPVOID lpEnvironment
NULL, // LPCTSTR lpCurrentDirectory
&startup_info, // LPSTARTUPINFO lpStartupInfo
&process_info // LPPROCESS_INFORMATION lpProcessInformation
);
if (rv == FALSE) {
::MessageBox(NULL, "CreateProcess() failed...", "error", MB_OK);
return 0;
}
::CloseHandle(process_info.hThread);
::WaitForSingleObject(process_info.hProcess, INFINITE);
::CloseHandle(process_info.hProcess);
::MessageBox(NULL, "CreateProcess() finish", "error", MB_OK);
return 0;
}
#include <SDKDDKVer.h>
#include <stdio.h>
#include <time.h>
int main(int argc, char* argv[])
{
time_t t;
struct tm d;
char buf[128];
FILE *fp = NULL;
time(&t);
localtime_s(&d, &t);
strftime(buf, 123, "%Y/%m/%d-%H:%M:%S", &d);
printf("TestConsoleApp.exe : touch _hoge.txt %s\n", buf);
fopen_s(&fp, "_hoge.txt", "w");
fprintf(fp, "TestConsoleApp.exe : touch _hoge.txt %s\n", buf);
fclose(fp);
return 0;
}
@yoggy
Copy link
Author

yoggy commented Mar 6, 2012

@Mouradouchane
Copy link

thanks a lot 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment