1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
// run_java.cpp
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>
#include <iostream>
using namespace std;
#ifdef _UNICODE
#define TCOUT wcout
#else
#define TCOUT cout
#endif
DWORD RunJava(bool bWait)
{
DWORD dwRet = NOERROR;
STARTUPINFO startupInfo = {0};
startupInfo.cb = sizeof(startupInfo);
startupInfo.dwFlags = STARTF_USESHOWWINDOW;
startupInfo.wShowWindow = SW_HIDE;
PROCESS_INFORMATION processInfo = {0};
// the command line variable must be writable. So if you pass a const string
// into this function, copy it to a buffer and then pass that to CreateProcess
//
// Note that it is safer to provide a full, quoted path to files
TCHAR cmdLine[1024] = _T("java -jar myprogram.jar");
BOOL bCreate = CreateProcess( NULL, // no app name - provided as part of command line
cmdLine,
NULL, // default process security
NULL, // default thread security
FALSE, // don't inherit handles
0, // creation flags (could pass CREATE_NEW_CONSOLE here
// to force creation of new console?)
NULL, // inherit envrionment
NULL, // inherit current directory
&startupInfo,
&processInfo );
if(FALSE != bCreate)
{
if(bWait)
{
// Wait for process to signal that it's exited
WaitForSingleObject(processInfo.hProcess, INFINITE);
// If your applet could jam up, change INFINITE to a timeout value (in millisecs
// and then check return of WaitForSingleObject
// WAIT_OBJECT_0 = handle signalled :-)
// WAIT_TIMEOUT = app has jammed
// anything else is a prob! (error) => see MSDN
// If you want, you could use the following to get the exit code
// returned by java.exe. If called while it#s still running, you
// get STILL_ACTIVE.
//
// DWORD dwExitCode = NOERROR;
// GetExitCodeProcess(processInfo.hProcess, &dwExitCode);
//
// ...
}
// Close handles to process and main thread
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
}
else
{
dwRet = GetLastError();
}
return dwRet;
}
int _tmain(int argc, TCHAR* argv[])
{
bool bWait = true;
// Handle command line...
DWORD dwRet = RunJava(bWait);
if(NOERROR == dwRet)
TCOUT << _T("Succeeded!") << endl;
else
TCOUT << _T("Failed : Error code = ") << dwRet << endl;
return (int)dwRet;
}
|