Try this instead:
static const LPTSTR NTSERVICE = TEXT("MyService");
static const LPTSTR svcname = TEXT("My Service");
void InstallService(char *exename)
{
SC_HANDLE myService, scm;
HKEY MyKey;
TRACE (_T("Installing service..."));
scm = OpenSCManager(0, 0, SC_MANAGER_CREATE_SERVICE);
if (!scm) {
TRACE (_T("Failed to open Service Control Manager! (Error code = %d)"), GetLastError());
return;
}
TCHAR modname[MAX_PATH+1] = {0};
GetModuleFileName(NULL, modname, MAX_PATH);
myService = CreateService(scm,
NTSERVICE, //Internal service name
svcname, //Show name
SERVICE_ALL_ACCESS, //We want full control
SERVICE_WIN32_OWN_PROCESS, //Let's not mess it up for somebody else..
SERVICE_AUTO_START, //The service requires manual start
SERVICE_ERROR_NORMAL, //Normal handling when error in startup
modname, //Binary file
0, 0, 0, 0, 0); //Misc :)
if (!myService)
{
TRACE (_T("Failed to create the %s service! (Error code =%d)"), NTSERVICE, GetLastError());
CloseServiceHandle(scm);
return;
}
TCHAR keyname[300];
_tcscpy_s(keyname, 300, _T("SYSTEM\\CurrentControlSet\\Services\\"));
_tcscat_s(keyname, 300, NTSERVICE);
DWORD Disposition = 0;
if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,
keyname,
NULL, //Reserved
NULL, //Class
REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS,
NULL, //Security attributes :)
&MyKey,
&Disposition) != ERROR_SUCCESS)
{
TRACE(_T("Failed to open registry key!"));
CloseServiceHandle(myService);
CloseServiceHandle(scm);
return;
}
TCHAR buffer[MAX_PATH+1] = {0};
#ifdef UNICODE
int len = MultiByteToWideChar(CP_ACP, 0, exename, strlen(exename), buffer, MAX_PATH);
#else
strcpy_s(buffer, MAX_PATH, exename);
int len = strlen(buffer);
#endif
buffer[len] = 0;
if (RegSetValueEx(MyKey,
TEXT("Exename"),
NULL,
REG_SZ,
(const BYTE*)buffer,
(len + 1) * sizeof(TCHAR)) != ERROR_SUCCESS)
{
TRACE(_T("Failed to write binary executable name to registry!"));
RegCloseKey(MyKey);
CloseServiceHandle(myService);
CloseServiceHandle(scm);
return;
}
RegCloseKey(MyKey);
CloseServiceHandle(myService);
CloseServiceHandle(scm);
TRACE(_T("Service successfully installed."));
printf("Service successfully installed.");
}