from my research, i could use Windows Service or CreateProcess function.
today i spent the whole day at work (literally) trying to find any complete example code in c for both.
it really amazed me how little examples are available on the net!
even the CreateProcess function is very,very tricky and difficult to understand,which i assume at first could be easier than making it a service.
after trying a few CreateProcess examples,none of them compilable..I'll end up with linking error and then when i googled,i received many confusing answers...such as,"change to Win32 API not console,c++,Unicode,add #include <tchar.h>,error LNK2019,WinMain,_main...bla.bla..change this,that!"
msdn documentation is very poor.
the only compilable and working example is this from msdn but i simply don't know how to connect/make my console program a process:
Code:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>/*i have to add this,otherwise error LNK2019...very weird*/
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // 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;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
the ouput is:
Usage: c [cmdline]
press any key to continue...
and then what?! i simply angry with this bcos i have the most unproductive day!