本帖最后由 黑客 于 2024-7-10 17:06 编辑
招C++不,我跟他写的差不多,给钱就干
-
- bool Driver::load(
- std::string path,
- std::string deviceName,
- std::string symbolName,
- unsigned long startOptions
- )
- {
- SC_HANDLE hSCManager = OpenSCManagerA(NULL, NULL, startOptions);
- if (!hSCManager)
- {
- m_error = GetLastError();
- return false;
- }
- SC_HANDLE hService = CreateServiceA(hSCManager, m_serviceName.c_str(), m_serviceName.c_str(), SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, startOptions, SERVICE_ERROR_IGNORE, m_path.c_str(), 0, 0, 0, 0, 0);
- if (hService == NULL)
- {
- if (ERROR_SERVICE_EXISTS == GetLastError())
- {
- hService = OpenServiceA(hSCManager, m_serviceName.c_str(), SERVICE_ALL_ACCESS);
- if (hService == NULL)
- {
- CloseServiceHandle(hSCManager);
- m_error = GetLastError();
- return false;
- }
- }
- else
- {
- if (ERROR_SERVICE_MARKED_FOR_DELETE == GetLastError() || ERROR_DUPLICATE_SERVICE_NAME == GetLastError())
- {
- hService = OpenServiceA(hSCManager, m_serviceName.c_str(), SERVICE_ALL_ACCESS);
- if (hService == NULL)
- {
- CloseServiceHandle(hSCManager);
- m_error = GetLastError();
- return false;
- }
- SERVICE_STATUS status;
- ControlService(hService, SERVICE_CONTROL_STOP,&status);
- DeleteService(hService);
- CloseServiceHandle(hService);
- CloseServiceHandle(hSCManager);
- hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
- if (hSCManager == NULL)
- {
- m_error = GetLastError();
- return false;
- }
-
- hService = CreateServiceA(hSCManager, m_serviceName.c_str(), m_serviceName.c_str(), SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, startOptions, SERVICE_ERROR_IGNORE, m_path.c_str(), 0, 0, 0, 0, 0);
- if (hService == NULL)
- {
- CloseServiceHandle(hSCManager);
- m_error = GetLastError();
- return false;
- }
- }
- else
- {
- m_path = "";
- m_deviceHandle = NULL;
- CloseServiceHandle(hSCManager);
- m_error = GetLastError();
- return false;
- }
- }
- }
-
- if (StartServiceA(hService, NULL, NULL) == 0)
- {
- if (ERROR_SERVICE_ALREADY_RUNNING != GetLastError())
- {
- CloseServiceHandle(hService);
- CloseServiceHandle(hSCManager);
- m_path = "";
- m_error = GetLastError();
- return false;
- }
- }
-
- m_deviceHandle = CreateFileA(("\\." + m_symbolName).c_str(),GENERIC_READ|GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
- if (m_deviceHandle == NULL)
- {
- SERVICE_STATUS status;
- ControlService(hService, SERVICE_CONTROL_STOP, &status);
- DeleteService(hService);
- CloseServiceHandle(hService);
- CloseServiceHandle(hSCManager);
-
- m_path = "";
- m_error = GetLastError();
- return false;
- }
-
- CloseServiceHandle(hService);
- CloseServiceHandle(hSCManager);
- return true;
- }
复制代码
|