⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driver.c

📁 PNX系列设备驱动 PNX系列设备驱动
💻 C
字号:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


BOOL 
LoadDeviceDriver( 
	const TCHAR * Name, 
	const TCHAR * Path, 
	DWORD StartType );

BOOL 
UnloadDeviceDriver( 
	const TCHAR * Name
	);

VOID 
	PrintError ( 
	TCHAR *FunctionName
	);

VOID PrintError ( TCHAR *FunctionName )
{
	PVOID MsgBuf;
	DWORD	ErrorCode = GetLastError();

	FormatMessage (
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
		NULL,
		ErrorCode,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
		(LPTSTR) &MsgBuf,
		0,
		NULL );// Process any inserts in lpMsgBuf.

	fprintf ( stderr, "\ndriver:%s:ERROR[0x%x]:%s", FunctionName, ErrorCode, MsgBuf );

	LocalFree( MsgBuf );
}

BOOL LoadDeviceDriver( const TCHAR * Name, const TCHAR * Path, DWORD StartType )
{
	SC_HANDLE	schSCManager;
    SC_HANDLE	schService;
	DWORD		Status = FALSE;
	TCHAR		completeServiceName[128];

	schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );

	if ( schSCManager == NULL )
	{
		PrintError("OpenSCManager");
		goto LoadDeviceDriverExit1;
	}

    wsprintf( completeServiceName, 
		TEXT("\\SystemRoot\\System32\\drivers\\%s"), 
		Path );

    schService = CreateService( 
		schSCManager,          // SCManager database
        Name,           // name of service
        Name,           // name to display
        SERVICE_ALL_ACCESS,    // desired access
        SERVICE_KERNEL_DRIVER, // service type
        StartType,				// start type
        SERVICE_ERROR_NORMAL,  // error control type
        completeServiceName,   // service's binary
        NULL,                  // no load ordering group
        NULL,                  // no tag identifier
        NULL,                  // no dependencies
        NULL,                  // LocalSystem account
        NULL                   // no password
        );


    if ( schService == NULL )
    {
        switch ( GetLastError() )
        {
            case ERROR_SERVICE_EXISTS:
            if ( ( schService = OpenService (
                schSCManager,
                Name,
                SERVICE_ALL_ACCESS ) )  == NULL )
            {
                PrintError("CreateService");
                goto LoadDeviceDriverExit2;
            }
            break;

            default:
            PrintError("CreateService");
            goto LoadDeviceDriverExit2;
        }
    }


    if ( StartService( schService, 0, NULL ) == 0 )
	{
		PrintError("StartService");
		goto LoadDeviceDriverExit3;
	}

	Status = TRUE;

     
LoadDeviceDriverExit3:
    CloseServiceHandle( schService );

LoadDeviceDriverExit2:
	CloseServiceHandle( schSCManager );

LoadDeviceDriverExit1:
	return Status;
}


BOOL UnloadDeviceDriver( const TCHAR * Name )
{
	SC_HANDLE		schSCManager;
	SERVICE_STATUS  serviceStatus;
    BOOL            ret;
	DWORD			Status = FALSE;
	SC_HANDLE		schService;

	schSCManager = OpenSCManager(
	NULL,                 // machine (NULL == local)
	NULL,                 // database (NULL == default)
	SC_MANAGER_ALL_ACCESS );// access required

	if ( schSCManager == NULL )
	{
		PrintError("OpenSCManager");
		goto UnloadDeviceDriverExit1;
	}

    schService = OpenService( schSCManager, Name, SERVICE_ALL_ACCESS );

    if ( schService == NULL )
	{
		PrintError("OpenService");
        goto UnloadDeviceDriverExit2;
	}

	if ( ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus ) == 0 )
	{
		PrintError("ControlService");
        goto UnloadDeviceDriverExit3;
	}

	if ( DeleteService( schService ) == 0  )
	{
		PrintError("DeleteService");
        goto UnloadDeviceDriverExit3;
	}

	Status =  TRUE;

UnloadDeviceDriverExit3:
	CloseServiceHandle( schService );

UnloadDeviceDriverExit2:
	CloseServiceHandle( schSCManager );

UnloadDeviceDriverExit1:
	return Status;

}




int main  ( int argc, char *argv[] )
{

	DWORD	IdxArg;
	BOOL	Install = TRUE;
	DWORD	StartType = SERVICE_AUTO_START;
	DWORD	Status;
	TCHAR	ServiceName[64];
	CHAR    ServicePath[MAX_PATH];

	fprintf ( stderr, "\nWindows NT Driver Installation Utility - Version 1.2\n");

	ServicePath[0] = 0;

	if ( argc == 1 )
		goto mainUsage;

	// process the command line parameters
	for ( IdxArg = 1; IdxArg < argc ; IdxArg++ )
	{
		if( argv[IdxArg][0] != '-' )
		{
			break;
		}

		switch ( toupper ( argv[IdxArg][1]) )
		{
			case '?' :
			fprintf ( stderr, "\ndriver : Installs a Kernel Mode Driver via the WinNT SCM\n");
			fprintf ( stderr, "\tShould be run with Administrator Privileges\n");
			fprintf ( stderr, "\nusage : driver -o[i|r] -s[a|b|s|m|d] -nDriverName [-fDriverFile]\n");

			fprintf ( stderr, "\t-oi : Operation Install\n");
			fprintf ( stderr, "\t-or : Operation Remove\n");
			fprintf ( stderr, "\t-sa : StartType Automatic\n");
			fprintf ( stderr, "\t-sb : StartType Boot\n");
			fprintf ( stderr, "\t-ss : StartType System\n");
			fprintf ( stderr, "\t-sm : StartType Manual\n");
			fprintf ( stderr, "\t-sd : StartType Disable\n");
			fprintf ( stderr, "\t-nDriverName : Servrice Name\n");
			fprintf ( stderr, "\t-fDriverFile : Image File(should be in %%SystemRoot%%\\System32\\Drivers)\n");
			fprintf ( stderr, "\nexample usage : driver -oi -sa -ntmman\n");
			return 0;
			break;

			case 'F' :
			if ( sscanf(&argv[IdxArg][2], "%s", &ServicePath ) != 1 )
			{
				goto mainUsage;
			}
			break;


			case 'N' :
			if ( sscanf(&argv[IdxArg][2], "%s", &ServiceName ) != 1 )
			{
				goto mainUsage;
			}
			break;

			case 'O' :
			switch ( toupper ( argv[IdxArg][2] ) )
			{
				case 'I' :
				Install = TRUE;
				break;

				case 'R' :
				Install = FALSE;
				break;
			}
			break;

			case 'S' :
			switch ( toupper ( argv[IdxArg][2] ) )
			{
				case 'A' :
				StartType = SERVICE_AUTO_START;
				break;

				case 'B' :
				StartType = SERVICE_BOOT_START;
				break;

				case 'S' :
				StartType = SERVICE_SYSTEM_START;
				break;

				case 'M' :
				StartType = SERVICE_DEMAND_START;
				break;

				case 'D' :
				StartType = SERVICE_DISABLED;
				break;
			}
			break;

			default :
			goto mainUsage;
		}

	}

	if  ( Install )
	{
		if ( ServicePath[0] == '\0' )
		{
			// use the service name as the default sys file name
			sprintf ( ServicePath, "%s.sys", ServiceName );

		}

		// attemp to install the driver via SCM and start it.
		Status = LoadDeviceDriver( ServiceName, ServicePath, StartType );
	}
	else
	{
		//attempt to remove the driver from the system.
		Status = UnloadDeviceDriver( ServiceName );
	}

	return Status;

mainUsage:
	fprintf ( stderr, "\nusage : driver -o[i|r] -s[a|b|s|m|d] -nDriverName [-fDriverFileName]\n");
	fprintf ( stderr, "\nhelp : driver -?\n");
	
	return -1;
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -