windows获得服务配置(驱动路径)

#include <windows.h>
#include <stdio.h>
#include <ctype.h>
#define ____DEVICE_BASENAME			("myfirst")

//
//	Get system error message string
//
PCSTR SystemMessage(
	DWORD			nError)
{
	static CHAR		msg[256];

	if (!FormatMessage(
		FORMAT_MESSAGE_FROM_SYSTEM |
		FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL, nError, 0, msg, sizeof(msg), NULL)) {

		_snprintf(msg, sizeof(msg),
			"Unknown system error %lu (0x%08x)\n", nError, nError);
	}
	return msg;
}

DWORD WINAPI GetDriverConfig(
	PSTR			sFileName,  // driver file name
	PDWORD			pStart)  // driver state
{
	SC_HANDLE		hScManager;				// Service Control Manager
	SC_HANDLE		hService;				// Service (= Driver)
	LPQUERY_SERVICE_CONFIG config = NULL;
	DWORD			result;
	DWORD			ret = ERROR_SUCCESS;

	if (sFileName)
		ZeroMemory(sFileName, MAX_PATH);
	if (pStart)
		*pStart = 0;

	//	Connect to the Service Control Manager
	hScManager = OpenSCManager(NULL, NULL, 0);

	if (hScManager == NULL) {
		ret = GetLastError();
		fprintf(stderr, "fail to connect scm: %s\n", SystemMessage(ret));
		return ret;
	}

	//	Open the driver entry in the service database
	hService = OpenService(
		hScManager,						// Service control manager
		____DEVICE_BASENAME,			// service name
		SERVICE_QUERY_CONFIG);			// service access mode
	if (hService == NULL) {
		ret = GetLastError();
		fprintf(stderr, "fail to open service: %s\n", SystemMessage(ret));
		goto cleanup;
	}

	//	Get the length of config information
	if (!QueryServiceConfig(hService, NULL, 0, &result))
	{
		ret = GetLastError();
		if (ret == ERROR_INSUFFICIENT_BUFFER)
			ret = ERROR_SUCCESS;
		else
		{
			fprintf(stderr, "fail to get config info: %s\n", SystemMessage(ret));
			goto cleanup;
		}
	}

	//	allocate a required buffer
	config = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR, result);
	if (config == NULL)
	{
		ret = GetLastError();
		fprintf(stderr, "fail to alloc: %s\n", SystemMessage(ret));
		goto cleanup;
	}

	//	get the config information
	if (!QueryServiceConfig(hService, config, result, &result))
	{
		ret = GetLastError();
		fprintf(stderr, "fail to query config: %s\n", SystemMessage(ret));
		goto cleanup;
	}

	//	copy information to output buffer
	if (sFileName)
	{
		if (strncmp(config->lpBinaryPathName, "\\??\\", 4) == 0)
		{
			//	driver path is an absolute UNC path
			strncpy(
				sFileName,
				config->lpBinaryPathName + 4,
				MAX_PATH);
		}
		else if (config->lpBinaryPathName[0] == '\\' ||
			(isalpha(config->lpBinaryPathName[0]) &&
			config->lpBinaryPathName[1] == ':'))
		{
			//	driver path is an absolute path
			strncpy(sFileName,
				config->lpBinaryPathName,
				MAX_PATH);
		}
		else
		{
			//	driver path is relative to the SystemRoot
			DWORD len = GetWindowsDirectory(sFileName, MAX_PATH);
			if (len == 0 || len > MAX_PATH)
			{
				fprintf(stderr, ": %%SystemRoot%% is empty or too long.\n");
				ret = ERROR_BAD_ENVIRONMENT;
				goto cleanup;
			}
			sprintf((sFileName + len), "\\%s",
				config->lpBinaryPathName);
		}
	}
	
	if (pStart)
		*pStart = config->dwStartType;
cleanup:
	//	Free service config buffer
	if (config) {
		LocalFree(config);
	}
	//	Close the service object handle
	if (hService) {
		CloseServiceHandle(hService);
	}
	//	Close handle to the service control manager.
	if (hScManager) {
		CloseServiceHandle(hScManager);
	}

	return ret;
}

int main(int argc, const char* argv[])
{
	CHAR		sFileName[MAX_PATH];
	DWORD       Start;
	GetDriverConfig(sFileName, &Start);
	printf("%s\n", sFileName);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值