VC知識庫:StartServiceCtrlDispatcher總返回ERROR

飛來科技  發布時間:2019-11-12 18:05:36

本文關鍵詞:vc知識庫 windows服務 vc

vc知識庫 windows服務 vc_vc windows網絡編程_windows vc運行庫

代碼:

在對話框初始函數中:

//

//搞成服務

//

Init();

dwThreadID = ::GetCurrentThreadId();

SERVICE_TABLE_ENTRY st[] =

{

{ szServiceNamevc知識庫 windows服務 vc, (LPSERVICE_MAIN_FUNCTION)ServiceMain },

{ NULL, NULL }

};

Install();

/*

if (stricmp(lpCmdLine, L"/install") == 0)

{

Install();

}

else if (stricmp(lpCmdLine, "/uninstall") == 0)

{

Uninstall();

}

else

{

if (!::StartServiceCtrlDispatcher(st))

{

LogEvent(_T("Register Service Main Function Error!"));

}

}

*/

if (!StartServiceCtrlDispatcher(st))

{

DWORD dwError = GetLastError();

if(dwerror==error_io_incomplete)continue。error:failed onnect o android.googlesource.com:443。 if(dwerror) { if(formatmessage(format_message_allocate_buffer | format_message_from_system | format_message_ignore_inserts, null, dwerror, makelangid(lang_neutral, sublang_default), // default language (lptstr) &lpmsgbuf, 0, null)) { sret.format(_t("x: %s"),dwerror,lpmsgbuf)。

{

//AfxMessageBox(L"ERROR_FAILED_SERVICE_CONTROLLER_CONNECT");

}

windows vc運行庫_vc知識庫 windows服務 vc_vc windows網絡編程

else if (dwError == ERROR_INVALID_DATA)

{

//AfxMessageBox(L"ERROR_INVALID_DATA");

}

else if (dwError == ERROR_SERVICE_ALREADY_RUNNING)

{

//AfxMessageBox(L"ERROR_SERVICE_ALREADY_RUNNING");

}

LogEvent(_T("Register Service Main Function Error!"));

}

/////////////////////////下面是全局的函數:

void Init()

{

hServiceStatus = NULL;

status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;

status.dwCurrentState = SERVICE_STOPPED;

status.dwControlsAccepted = SERVICE_ACCEPT_STOP;

status.dwWin32ExitCode = 0;

status.dwServiceSpecificExitCode = 0;

status.dwCheckPoint = 0;

status.dwWaitHint = 0;

}

void WINAPI ServiceMain()

{

status.dwCurrentState = SERVICE_START_PENDING;

status.dwControlsAccepted = SERVICE_ACCEPT_STOP;

hServiceStatus = ::RegisterServiceCtrlHandler(szServiceName, ServiceStrl);

if (hServiceStatus == NULL)

{

LogEvent(_T("Handler not installed"));

return;

}

SetServiceStatus(hServiceStatus, &status);

status.dwWin32ExitCode = S_OK;

status.dwCheckPoint = 0;

status.dwWaitHint = 0;

status.dwCurrentState = SERVICE_RUNNING;

windows vc運行庫_vc知識庫 windows服務 vc_vc windows網絡編程

SetServiceStatus(hServiceStatus, &status);

/*

int i = 0;

while (i < 10)

{

Sleep(1000);

i++;

}

status.dwCurrentState = SERVICE_STOPPED;

SetServiceStatus(hServiceStatus, &status);

LogEvent(_T("Service stopped"));

*/

}

void WINAPI ServiceStrl(DWORD dwOpcode)

{

switch (dwOpcode)

{

case SERVICE_CONTROL_STOP:

{

AfxMessageBox(L"SERVICE_CONTROL_STOP");

status.dwCurrentState = SERVICE_STOP_PENDING;

SetServiceStatus(hServiceStatus, &status);

PostThreadMessage(dwThreadID, WM_CLOSE, 0, 0);

break;

}

case SERVICE_CONTROL_PAUSE:

AfxMessageBox(L"SERVICE_CONTROL_PAUSE");

break;

case SERVICE_CONTROL_CONTINUE:

AfxMessageBox(L"SERVICE_CONTROL_CONTINUE");

Install();

break;

case SERVICE_CONTROL_INTERROGATE:

AfxMessageBox(L"SERVICE_CONTROL_INTERROGATE");

break;

case SERVICE_CONTROL_SHUTDOWN:

AfxMessageBox(L"SERVICE_CONTROL_SHUTDOWN");

vc知識庫 windows服務 vc_vc windows網絡編程_windows vc運行庫

Uninstall();

break;

default:

LogEvent(_T("Bad service request"));

}

}

BOOL IsInstalled()

{

BOOL bResult = FALSE;

schscmanager=openscmanager(null,null,sc_manager_all_access)。//schscmanager=openscmanager(null,null,sc_manager_all_access)。sh1 = openscmanager( null, null, sc_manager_all_access )。

if (hSCM != NULL)

{

sc_handle hscm = openscmanager(null, null, sc_manager_all_access)。 sc_handle hservice。 hservice=openservice((struct sc_handle__ *)schscmanager,wsservicename.c_str(),service_all_access)。

if (hService != NULL)

{

bResult = TRUE;

CloseServiceHandle(hService);

}

CloseServiceHandle(hSCM);

}

return bResult;

}

BOOL Install()

{

if (IsInstalled())

return TRUE;

SC_HANDLE hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

if (hSCM == NULL)

{

AfxMessageBox(_T("Couldn't open service manager"), MB_OK);

return FALSE;

}

TCHAR szFilePath[MAX_PATH];

GetModuleFileName(NULL, szFilePath, MAX_PATH);

SC_HANDLE hService = CreateService(

hSCM, szServiceName, szServiceName,

SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,

vc windows網絡編程_vc知識庫 windows服務 vc_windows vc運行庫

SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,

szFilePath, NULL, NULL, _T(""), NULL, NULL);

if (hService == NULL)

{

CloseServiceHandle(hSCM);

AfxMessageBox(_T("Couldn't create service"), MB_OK);

return FALSE;

}

//AfxMessageBox(L"CreateService OK");

CloseServiceHandle(hService);

CloseServiceHandle(hSCM);

return TRUE;

}

BOOL Uninstall()

{

if (!IsInstalled())

return TRUE;

schscmanager=openscmanager(null,null,sc_manager_all_access)。//schscmanager=openscmanager(null,null,sc_manager_all_access)。sh1 = openscmanager( null, null, sc_manager_all_access )。

if (hSCM == NULL)

{

AfxMessageBox(_T("Couldn't open service manager"), MB_OK);

return FALSE;

}

sc_handle hscm = openscmanager(null, null, sc_manager_all_access)。 sc_handle hservice。 hservice=openservice((struct sc_handle__ *)schscmanager,wsservicename.c_str()vc知識庫 windows服務 vc,service_all_access)。

if (hService == NULL)

{

CloseServiceHandle(hSCM);

AfxMessageBox(_T("Couldn't open service"), MB_OK);

return FALSE;

}

SERVICE_STATUS status;

ControlService(hService, SERVICE_CONTROL_STOP, &status);

BOOL bDelete = DeleteService(hService);

CloseServiceHandle(hService);

CloseServiceHandle(hSCM);

if (bDelete)

return TRUE;

LogEvent(_T("Service could not be deleted"));

return FALSE;

}

本文來自互聯網,由機器人自動采編,文章內容不代表本站觀點,請讀者自行辨別信息真偽,如有發現不適內容,請及時聯系站長處理。

    相關閱讀
    波克捕鱼小米版下载