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

📄 loaddrv.c

📁 这是一个非常有价值的参考代码
💻 C
📖 第 1 页 / 共 2 页
字号:
   SC_HANDLE hService = NULL;
   SERVICE_STATUS serviceStatus;

   // get a handle to the service
   if ((hService = OpenService(hSCMan, lpDriver, 
      SERVICE_ALL_ACCESS)) != NULL) 
   {
      // stop the driver
      if (!ControlService(hService, SERVICE_CONTROL_STOP,
         &serviceStatus))
            dwStatus = GetLastError();
   } else dwStatus = GetLastError();

   if (hService != NULL) CloseServiceHandle(hService);
   return dwStatus;
} // DriverStop

/**-----------------------------------------------------**/
DWORD DriverRemove(LPSTR lpDriver)
{
   BOOL dwStatus = OKAY;
   SC_HANDLE hService = NULL;

   // get a handle to the service
   if ((hService = OpenService(hSCMan, lpDriver, 
      SERVICE_ALL_ACCESS)) != NULL) 
   {  // remove the driver
      if (!DeleteService(hService))
         dwStatus = GetLastError();
   } else dwStatus = GetLastError();

   if (hService != NULL) CloseServiceHandle(hService);
   return dwStatus;
} // DriverRemove

/**-----------------------------------------------------**/
////extensions by Lch
/**-----------------------------------------------------**/
DWORD DriverStatus(LPSTR lpDriver) {
    BOOL dwStatus = OKAY;
    SC_HANDLE hService = NULL;
    DWORD dwBytesNeeded;

    // get a handle to the service
    if ((hService = OpenService(hSCMan, lpDriver, 
                                SERVICE_ALL_ACCESS)) != NULL) 
    {
        LPQUERY_SERVICE_CONFIG lpqscBuf;
        //~ LPSERVICE_DESCRIPTION lpqscBuf2;
        // Allocate a buffer for the configuration information. 
        if ((lpqscBuf = (LPQUERY_SERVICE_CONFIG) LocalAlloc( 
            LPTR, 4096)) != NULL)
        {
            //~ if ((lpqscBuf2 = (LPSERVICE_DESCRIPTION) LocalAlloc( 
                //~ LPTR, 4096)) != NULL)
            {
                // Get the configuration information. 
                if (QueryServiceConfig(
                        hService,
                        lpqscBuf,
                        4096,
                        &dwBytesNeeded) //&&
                    //~ QueryServiceConfig2( 
                        //~ hService,
                        //~ SERVICE_CONFIG_DESCRIPTION,
                        //~ lpqscBuf2,
                        //~ 4096, 
                        //~ &dwBytesNeeded
                )
                {
                    // Print the configuration information. 
                    printf("Type:           [0x%02lx] ", lpqscBuf->dwServiceType);
                    switch (lpqscBuf->dwServiceType) {
                        case SERVICE_WIN32_OWN_PROCESS:
                            printf("The service runs in its own process.");
                            break;
                        case SERVICE_WIN32_SHARE_PROCESS:
                            printf("The service shares a process with other services.");
                            break;
                        case SERVICE_KERNEL_DRIVER:
                            printf("Kernel driver.");
                            break;
                        case SERVICE_FILE_SYSTEM_DRIVER:
                            printf("File system driver.");
                            break;
                        case SERVICE_INTERACTIVE_PROCESS:
                            printf("The service can interact with the desktop.");
                            break;
                        default:
                            printf("Unknown type.");
                    }
                    printf("\nStart Type:     [0x%02lx] ", lpqscBuf->dwStartType); 
                    switch (lpqscBuf->dwStartType) {
                        case SERVICE_BOOT_START:
                            printf("Boot");
                            break;
                        case SERVICE_SYSTEM_START:
                            printf("System");
                            break;
                        case SERVICE_AUTO_START:
                            printf("Automatic");
                            break;
                        case SERVICE_DEMAND_START:
                            printf("Manual");
                            break;
                        case SERVICE_DISABLED:
                            printf("Disabled");
                            break;
                        default:
                            printf("Unknown.");
                    }
                    printf("\nError Control:  [0x%02lx] ", lpqscBuf->dwErrorControl); 
                    switch (lpqscBuf->dwErrorControl) {
                        case SERVICE_ERROR_IGNORE:
                            printf("IGNORE: Ignore.");
                            break;
                        case SERVICE_ERROR_NORMAL:
                            printf("NORMAL: Display a message box.");
                            break;
                        case SERVICE_ERROR_SEVERE:
                            printf("SEVERE: Restart with last-known-good config.");
                            break;
                        case SERVICE_ERROR_CRITICAL:
                            printf("CRITICAL: Restart w/ last-known-good config.");
                            break;
                        default:
                            printf("Unknown.");
                    }
                    printf("\nBinary path:    %s\n", lpqscBuf->lpBinaryPathName); 
                    
                    if (lpqscBuf->lpLoadOrderGroup != NULL) 
                        printf("Load order grp: %s\n", lpqscBuf->lpLoadOrderGroup); 
                    if (lpqscBuf->dwTagId != 0) 
                        printf("Tag ID:         %ld\n", lpqscBuf->dwTagId); 
                    if (lpqscBuf->lpDependencies != NULL) 
                        printf("Dependencies:   %s\n", lpqscBuf->lpDependencies); 
                    if (lpqscBuf->lpServiceStartName != NULL) 
                        printf("Start Name:     %s\n", lpqscBuf->lpServiceStartName); 
                    //~ if (lpqscBuf2->lpDescription != NULL) 
                        //~ printf("Description:    %s\n", lpqscBuf2->lpDescription); 
                }
                //~ LocalFree(lpqscBuf2);
            }
            LocalFree(lpqscBuf);
        } else {
            dwStatus = GetLastError();
        }
    } else {
        dwStatus = GetLastError();
    }

   if (hService != NULL) CloseServiceHandle(hService);
   return dwStatus;
} // DriverStatus

/**-----------------------------------------------------**/
DWORD DriverStartType(LPSTR lpDriver, DWORD dwStartType) {
    BOOL dwStatus = OKAY;
    SC_HANDLE hService = NULL;

    SC_LOCK sclLock;
    LPQUERY_SERVICE_LOCK_STATUS lpqslsBuf;
    DWORD dwBytesNeeded;
    
    // Need to acquire database lock before reconfiguring.
    sclLock = LockServiceDatabase(hSCMan);

    // If the database cannot be locked, report the details.
    if (sclLock == NULL) {
        // Exit if the database is not locked by another process.
        if (GetLastError() == ERROR_SERVICE_DATABASE_LOCKED) {
            
            // Allocate a buffer to get details about the lock.
            lpqslsBuf = (LPQUERY_SERVICE_LOCK_STATUS) LocalAlloc(
                LPTR, sizeof(QUERY_SERVICE_LOCK_STATUS)+256);
            if (lpqslsBuf != NULL) {
                // Get and print the lock status information.
                if (QueryServiceLockStatus(
                    hSCMan,
                    lpqslsBuf,
                    sizeof(QUERY_SERVICE_LOCK_STATUS)+256,
                    &dwBytesNeeded) )
                {
                    if (lpqslsBuf->fIsLocked) {
                        printf("Locked by: %s, duration: %ld seconds\n",
                            lpqslsBuf->lpLockOwner,
                            lpqslsBuf->dwLockDuration
                        );
                    } else {
                        printf("No longer locked\n");
                    }
                }
                LocalFree(lpqslsBuf);
            }
        }
        dwStatus = GetLastError();
    } else {
        // The database is locked, so it is safe to make changes.
        // Open a handle to the service.
        hService = OpenService(
            hSCMan,                 // SCManager database
            lpDriver,               // name of service
            SERVICE_CHANGE_CONFIG
        ); // need CHANGE access
        if (hService != NULL) {
            // Make the changes.
            if (!ChangeServiceConfig(
                hService,          // handle of service
                SERVICE_NO_CHANGE, // service type: no change
                dwStartType,       // change service start type
                SERVICE_NO_CHANGE, // error control: no change
                NULL,              // binary path: no change
                NULL,              // load order group: no change
                NULL,              // tag ID: no change
                NULL,              // dependencies: no change
                NULL,              // account name: no change
                NULL,              // password: no change
                NULL) )            // display name: no change
            {
                dwStatus = GetLastError();
            }
        }
        // Release the database lock.
        UnlockServiceDatabase(sclLock);
    }

    if (hService != NULL) CloseServiceHandle(hService);
    return dwStatus;
} // DriverStartType

⌨️ 快捷键说明

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