📄 driverlib.cpp
字号:
/*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Rick Haubenstricker
* Perceptron, Inc
* 47827 Halyard Dr
* Plymouth, Michigan 48170, USA
*
* rickh@perceptron.com
*
*/
#include "stdafx.h"
#include "DriverLib.h"
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// GetDriverStatus (function entry point)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
BOOL GetDriverStatus(
const char *pstrServiceName,
const char *pstrFileName,
stDriverStatus *pstDriverStatus,
DWORD dwDebugFlag)
{
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL;
SERVICE_STATUS ssStatus;
// initialize return structure
pstDriverStatus->bServiceInstalled = FALSE;
pstDriverStatus->bServiceRunning = FALSE;
pstDriverStatus->bFileInstalled = FALSE;
// open service control manager
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
// unable to open service control manager
return FALSE;
}
// open service
schService = OpenService(schSCManager, pstrServiceName, SERVICE_ALL_ACCESS);
if (schService == NULL)
{
// either service does not exist or opening of service failed;
// if opening of service failure, exit; otherwise go on to check
// for existence of file
if (GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST)
{
CloseServiceHandle(schSCManager);
return FALSE;
}
}
else
{
// service installed
pstDriverStatus->bServiceInstalled = TRUE;
// see if service is running
if (!QueryServiceStatus(schService, &ssStatus))
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return FALSE;
}
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
{
// service is running
pstDriverStatus->bServiceRunning = TRUE;
}
// close service
CloseServiceHandle(schService);
}
// done with service control manager
CloseServiceHandle(schSCManager);
// look for file
if (FileFound(pstrFileName, dwDebugFlag))
pstDriverStatus->bFileInstalled = TRUE;
return TRUE;
} // GetDriverStatus
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// FileFound (function entry point)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
BOOL FileFound(
const char *pstrFileName,
DWORD dwDebugFlag)
{
char szFileName[MAX_PATH];
WIN32_FIND_DATA FindFileData;
GetSystemDirectory(szFileName, sizeof(szFileName));
strcat(szFileName, "\\"); // add directory separator
strcat(szFileName, pstrFileName); // add file name
if (FindFirstFile(szFileName, &FindFileData) != INVALID_HANDLE_VALUE)
// file found
return TRUE;
else
// file not found
return FALSE;
} // FileFound
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// InstallFile (function entry point)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
BOOL InstallFile(
WORD wFileID,
const char *pstrFileName,
DWORD dwDebugFlag)
{
HRSRC hResource = FindResource(NULL, MAKEINTRESOURCE(wFileID), "BINARYFILE");
if (hResource == NULL)
{
return FALSE;
}
HGLOBAL hGlobal = LoadResource(NULL, hResource);
if (hGlobal == NULL)
{
return FALSE;
}
LPVOID pvFile = (LPVOID)hGlobal;
char szFileName[MAX_PATH];
GetSystemDirectory(szFileName, sizeof(szFileName));
strcat(szFileName, "\\"); // add directory separator
strcat(szFileName, pstrFileName); // add file name
HANDLE hFile = CreateFile(szFileName, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
FreeResource(hResource);
return FALSE;
}
DWORD dwBytes = SizeofResource(NULL, hResource);
if (!WriteFile(hFile, pvFile, dwBytes, &dwBytes, NULL))
{
CloseHandle(hFile);
FreeResource(hResource);
return FALSE;
}
CloseHandle(hFile);
FreeResource(hResource);
return TRUE;
} // InstallFile
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// RemoveFile (function entry point)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
BOOL RemoveFile(
const char *pstrFileName,
DWORD dwDebugFlag)
{
char szFileName[MAX_PATH];
GetSystemDirectory(szFileName, sizeof(szFileName));
strcat(szFileName, "\\"); // add directory separator
strcat(szFileName, pstrFileName); // add file name
if (!DeleteFile(szFileName))
{
return FALSE;
}
return TRUE;
} // RemoveFile
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ServiceInstall (function entry point)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
BOOL ServiceInstall(
SC_HANDLE schSCManager,
const char *pstrServiceName,
const char *pstrFileName,
DWORD dwDebugFlag)
{
SC_HANDLE schService = NULL;
char szFileName[MAX_PATH];
GetSystemDirectory(szFileName, sizeof(szFileName));
strcat(szFileName, "\\"); // add directory separator
strcat(szFileName, pstrFileName); // add file name
schService = CreateService(schSCManager, pstrServiceName, pstrServiceName,
SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL, szFileName, NULL, NULL, NULL, NULL, NULL);
if (schService == NULL)
{
return FALSE;
}
CloseServiceHandle(schService);
return TRUE;
} //ServiceInstall
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ServiceRemove (function entry point)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
BOOL ServiceRemove(
SC_HANDLE schSCManager,
const char *pstrServiceName,
DWORD dwDebugFlag)
{
SC_HANDLE schService = NULL;
int nWaitAttempts;
// open service
schService = OpenService(schSCManager, pstrServiceName, SERVICE_ALL_ACCESS);
if (schService == NULL)
{
// either service does not exist or opening of service failed;
// if opening of service failure, exit; otherwise go on to check
// for existence of file
if (GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST)
{
return FALSE;
}
}
if (!DeleteService(schService))
{
CloseServiceHandle(schService);
return FALSE;
}
CloseServiceHandle(schService);
// enter loop to test for service deletion
for (nWaitAttempts = 0; nWaitAttempts < 25; nWaitAttempts++)
{
// wait for service to delete
Sleep(200);
// try to re-open service
schService = OpenService(schSCManager, pstrServiceName, SERVICE_ALL_ACCESS);
if (schService == NULL)
return TRUE;
else
// close service and wait some more
CloseServiceHandle(schService);
}
// service must still exist in some form
return FALSE;
} // ServiceRemove
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ServiceStart (function entry point)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
BOOL ServiceStart(
SC_HANDLE schSCManager,
const char *pstrServiceName,
DWORD dwDebugFlag)
{
SC_HANDLE schService = NULL;
SERVICE_STATUS ssStatus;
// open service
schService = OpenService(schSCManager, pstrServiceName, SERVICE_ALL_ACCESS);
if (schService == NULL)
{
// either opening of service failed, or service does not exist;
if (GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST)
{
return FALSE;
}
else
{
return FALSE;
}
}
// send message to start the service
if (!StartService(schService, NULL, NULL))
{
CloseServiceHandle(schService);
return FALSE;
}
DWORD dwPreviousCheckPoint;
// see if service started
if (!QueryServiceStatus(schService, &ssStatus))
{
CloseServiceHandle(schService);
return FALSE;
}
if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
BOOL bRunning = FALSE;
dwPreviousCheckPoint = ssStatus.dwCheckPoint;
// wait for service to start
while(!bRunning)
{
if (!QueryServiceStatus(schService, &ssStatus))
{
CloseServiceHandle(schService);
return FALSE;
}
if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
// service start is not progressing, give up
if (dwPreviousCheckPoint == ssStatus.dwCheckPoint)
{
CloseServiceHandle(schService);
return FALSE;
}
dwPreviousCheckPoint = ssStatus.dwCheckPoint;
// wait for service to start using suggested wait
Sleep(ssStatus.dwWaitHint);
}
else
bRunning = TRUE;
}
if (ssStatus.dwCurrentState != SERVICE_RUNNING)
{
CloseServiceHandle(schService);
return FALSE;
}
}
CloseServiceHandle(schService);
return TRUE;
} // ServiceStart
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// ServiceStop (function entry point)
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
BOOL ServiceStop(
SC_HANDLE schSCManager,
const char *pstrServiceName,
DWORD dwDebugFlag)
{
SC_HANDLE schService = NULL;
SERVICE_STATUS ssStatus;
// open service
schService = OpenService(schSCManager, pstrServiceName, SERVICE_ALL_ACCESS);
if (schService == NULL)
{
// either opening of service failed, or service does not exist;
if (GetLastError() != ERROR_SERVICE_DOES_NOT_EXIST)
{
return FALSE;
}
else
{
return FALSE;
}
}
// send message to stop the service
if (!ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus))
{
CloseServiceHandle(schService);
return FALSE;
}
DWORD dwPreviousCheckPoint;
// see if service stopped
if (!QueryServiceStatus(schService, &ssStatus))
{
CloseServiceHandle(schService);
return FALSE;
}
if (ssStatus.dwCurrentState != SERVICE_STOPPED)
{
BOOL bStopped = FALSE;
dwPreviousCheckPoint = ssStatus.dwCheckPoint;
// wait for service to stop
while(!bStopped)
{
if (!QueryServiceStatus(schService, &ssStatus))
{
CloseServiceHandle(schService);
return FALSE;
}
if (ssStatus.dwCurrentState != SERVICE_STOPPED)
{
// service stop is not progressing, give up
if (dwPreviousCheckPoint == ssStatus.dwCheckPoint)
{
CloseServiceHandle(schService);
return FALSE;
}
dwPreviousCheckPoint = ssStatus.dwCheckPoint;
// wait for service to start using suggested wait
Sleep(ssStatus.dwWaitHint);
}
else
bStopped = TRUE;
}
if (ssStatus.dwCurrentState != SERVICE_STOPPED)
{
CloseServiceHandle(schService);
return FALSE;
}
}
CloseServiceHandle(schService);
return TRUE;
} //ServiceStop
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -