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

📄 dynamic.cpp

📁 一个虚拟光驱程序(驱动程序和exe)-VaporCDSource141
💻 CPP
字号:
/*
    VaporCD CD-ROM Volume Emulation for Windows NT/2000
    Copyright (C) 2000  Brad Johnson

    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.

    http://vaporcd.sourceforge.net/

    Brad Johnson
    3305 2nd PL #222
    Lubbock, TX 79415
*/


/*
    Dynamic.c - 1998 James M. Finnegan - Microsoft Systems Journal

    This module implements the functionality to dynamically load and
    unload via the Service Control Manager.
*/
#include <windows.h>

BOOL InstallAndStartDriver(SC_HANDLE hSCManager, LPCTSTR DriverName,
                           LPCTSTR ServiceExe)
{
    SC_HANDLE  hService;
    BOOL       bReturn;

	hService = OpenService(
	  hSCManager,  // handle to service control manager 
							 //  database
	  DriverName, // pointer to name of service to start
	  SERVICE_ALL_ACCESS  // type of access to service
	);

	if (!hService)
	{
		// Create the driver entry in the SC Manager.
		hService = CreateService(hSCManager,            // SCManager database
								 DriverName,            // name of service
								 DriverName,            // name to display
								 SERVICE_ALL_ACCESS,    // desired access
								 SERVICE_KERNEL_DRIVER, // service type
								 SERVICE_AUTO_START,    // start type
								 SERVICE_ERROR_NORMAL,  // error control type
								 ServiceExe,            // service's binary
								 NULL,                  // no load ordering group
								 NULL,                  // no tag identifier
								 NULL,                  // no dependencies
								 NULL,                  // LocalSystem account
								 NULL                   // no password
								 );

	}

    // This may fail because the service entry already exists.  However,
    // we've tried to load it earlier, so something clearly is wrong if
    // we can't create it!

    if(!hService)
        return FALSE;

    // Start the driver!
    bReturn = StartService(hService, 0, NULL);

    // Dispose of the handle...
    CloseServiceHandle(hService);

    // Return whatever the driver start returned...
    return bReturn;
}


BOOL UninstallDriver(SC_HANDLE hSCManager, LPCTSTR DriverName)
{
    SC_HANDLE       hService;
//    SERVICE_STATUS  serviceStatus;
    BOOL            bReturn;

    // Open the Service Control Manager for our driver service...
    hService = OpenService(hSCManager, DriverName, SERVICE_ALL_ACCESS);

    // Stop the driver.  Will return TRUE on success...
//    bReturn = ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus);

    // Delete the driver from the registry...
//    if(bReturn == TRUE)
        bReturn = DeleteService(hService);

    // Close the SC Manager
    CloseServiceHandle(hService);

    return bReturn;
}

BOOL LoadDynamicNTDriver()
{
    SC_HANDLE   hSCManager;
    char        currentDirectory[128];
    BOOL        bReturn;


    // Open Service Control Manager on the local machine...
    hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

    // Assume driver is in the same directory as the DLL.  Hence, create
    // a fully qualified pathname for the SCM to add to the registry.
    GetCurrentDirectory(128, currentDirectory);
    lstrcat(currentDirectory,"\\MSJDrvr.sys");
   
    // Install drive in the registry and start it...
    bReturn = InstallAndStartDriver(hSCManager, "MSJDrvr", currentDirectory);

    // Close the Service Control Manager...
    CloseServiceHandle(hSCManager);

    return bReturn;
}

////////////////////////////////////////////////////////////////////////////
//  Unload routines
////////////////////////////////////////////////////////////////////////////

BOOL UnloadDynamicNTDriver()
{
    SC_HANDLE       hSCManager;
    SC_HANDLE       hService;
    SERVICE_STATUS  serviceStatus;
    BOOL            bReturn;


   // Open Service Control Manager on the local machine...
    hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

    // Open the Service Control Manager for our driver service...
    hService = OpenService(hSCManager, "MSJDrvr", SERVICE_ALL_ACCESS);

    // Stop the driver.  Will return TRUE on success...
    bReturn = ControlService(hService, SERVICE_CONTROL_STOP, &serviceStatus);

    // Delete the driver from the registry...
    if(bReturn == TRUE)
        bReturn = DeleteService(hService);

    // Close the SC Manager
    CloseServiceHandle(hService);

    // Close Service Control Manager
    CloseServiceHandle(hSCManager);

    return bReturn;
}


HANDLE LoadDriver(OSVERSIONINFO *os, BOOL *fNTDynaLoaded)
{
    HANDLE hDevice;

   
    // Default to FALSE on informing the caller we've dynamically
    // loaded the Windows NT driver via the SCM.
    *fNTDynaLoaded = FALSE;

    // If we're under Windows NT...
    if(os->dwPlatformId == VER_PLATFORM_WIN32_NT)
    {
        // Try opening the device driver (in case it's static).  We'll
        // Deal with a failure in a moment...
        hDevice = CreateFile("\\\\.\\MSJDrvr",
                             GENERIC_READ | GENERIC_WRITE,
                             FILE_SHARE_READ | FILE_SHARE_WRITE,
                             0,                     // Default security
                             OPEN_EXISTING,
                             FILE_FLAG_OVERLAPPED,  // Perform asynchronous I/O
                             0);                    // No template
       
        // If the device driver wasn't started, let's dynamically load it
        // here...
        if(hDevice == INVALID_HANDLE_VALUE)
        {
            // Load and start the driver...
            LoadDynamicNTDriver();

            // Open the driver...
            hDevice = CreateFile("\\\\.\\MSJDrvr",
                             GENERIC_READ | GENERIC_WRITE,
                             FILE_SHARE_READ | FILE_SHARE_WRITE,
                             0,                     // Default security
                             OPEN_EXISTING,
                             FILE_FLAG_OVERLAPPED,  // Perform asynchronous I/O
                             0);                    // No template
           
            // Set the global to indicate that we've Dynaloaded.  This will
            // Cause DllMain to stop and unload on a process detach.
            *fNTDynaLoaded = TRUE;
        }

        return hDevice;
    }
    // Otherwise, assume Win95!
    else
    {
        // Dynaload the VxD...
        return CreateFile("\\\\.\\MSJDrvr.vxd", 0,0,0, CREATE_NEW,
                          FILE_FLAG_DELETE_ON_CLOSE | FILE_FLAG_OVERLAPPED, 0);
    }
}

⌨️ 快捷键说明

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