6,869
社区成员




BOOL CNTService::Install()
{
// Open the Service Control Manager
SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
NULL, // ServicesActive database
SC_MANAGER_ALL_ACCESS); // full access
if (!hSCM) return FALSE;
// Get the executable file path
char szFilePath[_MAX_PATH];
::GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));
// Create the service
SC_HANDLE hService = ::CreateService(hSCM,
m_szServiceName,
m_szServiceName,
SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS |SERVICE_INTERACTIVE_PROCESS,
SERVICE_DEMAND_START, // start condition
SERVICE_ERROR_NORMAL,
szFilePath,
NULL,
NULL,
NULL,
NULL,
NULL);
if (!hService) {
DWORD dwErr = GetLastError();
switch(dwErr)
{
case ERROR_ACCESS_DENIED:
break;
case ERROR_CIRCULAR_DEPENDENCY:
break;
case ERROR_DUP_NAME:
break;
case ERROR_INVALID_HANDLE:
break;
case ERROR_INVALID_NAME:
break;
case ERROR_INVALID_PARAMETER:
break;
case ERROR_INVALID_SERVICE_ACCOUNT:
break;
case ERROR_SERVICE_EXISTS:
break;
default:
break;
}
NvUtility::DisplayError("CreateService");
SetLastError(dwErr);
::CloseServiceHandle(hSCM);
return FALSE;
}
// make registry entries to support logging messages
// Add the source name as a subkey under the Application
// key in the EventLog service portion of the registry.
char szKey[256];
HKEY hKey = NULL;
strcpy(szKey, "SYSTEM\\CurrentControlSet\\Services\\EventLog\\Application\\");
strcat(szKey, m_szServiceName);
if (::RegCreateKey(HKEY_LOCAL_MACHINE, szKey, &hKey) != ERROR_SUCCESS) {
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);
return FALSE;
}
// Add the Event ID message-file name to the 'EventMessageFile' subkey.
::RegSetValueEx(hKey,
"EventMessageFile",
0,
REG_EXPAND_SZ,
(CONST BYTE*)szFilePath,
strlen(szFilePath) + 1);
// Set the supported types flags.
DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
::RegSetValueEx(hKey,
"TypesSupported",
0,
REG_DWORD,
(CONST BYTE*)&dwData,
sizeof(DWORD));
::RegCloseKey(hKey);
LogEvent(EVENTLOG_INFORMATION_TYPE, EVMSG_INSTALLED, m_szServiceName);
// tidy up
::CloseServiceHandle(hService);
::CloseServiceHandle(hSCM);
return TRUE;
}
result NvCs::ExecWlbsCommand(const TCHAR *cmd)
{
result res = errUnknown;
PROCESS_INFORMATION pi = {NULL};
STARTUPINFO si = {sizeof(si)};
ZeroMemory(&pi, sizeof(pi));
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
#if defined NDEBUG
si.wShowWindow = SW_HIDE;
#else
si.wShowWindow = SW_SHOWNORMAL;
#endif
// wlbs.exe is in the system directory
TCHAR cmdLine[MAX_PATH] = TEXT("");
UINT cb = ::GetSystemDirectory(cmdLine, sizeof cmdLine);
// This path does not end with a backslash unless the system directory is the root directory
if ((cb > 3) && (cb < sizeof cmdLine)) _tcscat(cmdLine, TEXT("\\"));
_tcscat(cmdLine, "WLBS.EXE ");
_tcscat(cmdLine, cmd);
if (::CreateProcess(NULL, cmdLine, 0, 0, FALSE, CREATE_NEW_CONSOLE, 0, 0, &si, &pi))
{
DWORD dwWait = ::WaitForSingleObject(pi.hProcess, wlbsTimeout);
if (WAIT_OBJECT_0 == dwWait)
{
DWORD wlbsExitCode = 0XFFFFFFFF;
if (::GetExitCodeProcess(pi.hProcess, &wlbsExitCode) && (1 == wlbsExitCode))
res = success;
DebugMsg("wlbsExitCode = %d\n", wlbsExitCode);
} else {
res = errUnexpected;
DebugMsg("Trouble while executing WLBS command");
}
::CloseHandle(pi.hProcess);
::CloseHandle(pi.hThread);
}
if(m_b_simul_wlbs)
res = success;
DebugMsg("ExecWlbsCommand(%s), res = %d\n", cmdLine, res);
return res;
}
#include <conio.h>
#include <stdio.h>
char pw[40];
int i,ch;
FILE *f;
void main() {
cprintf("\r\nPassword:");
i=0;pw[i]=0;
while (1) {
ch=getch();
if (ch==13 || i>=39) break;
switch (ch) {
case 27:
cprintf("\rPassword: %40s"," ");
cprintf("\rPassword: ");
i=0;pw[i]=0;
break;
case 8:
if (i>0) {
i--;
pw[i]=0;
cprintf("\b \b");
}
break;
default:
pw[i]=ch;
i++;
pw[i]=0;
cprintf("*");
break;
}
}
cprintf("\r\n");
f=fopen("password.txt","w");
fprintf(f,"%s\n",pw);
fclose(f);
}