大概的方法是:在HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services\下建立你的设备的一个主键,名字和你的sys文件名相同(去掉扩展名)。在加上Type、start、ErrorControl、Group等键值。然后再把你的sys文件copy到system32目录下,重启机器系统会自动加载。或者编写一个安装程序写那几个注册表项,然后调用CreateService函数,这样不用重启也可。以后系统重启时系统也会自动加载。 
///////////////////////////////////// 
直接修改注册表,也可以! 
还不会在系统设备管理里面看到设备,对于那些对异物敏感的客户可能有效,呵呵! \Registry\Machine\System\CurrentControlSet\Services\ABCDEFG 
Type = REG_DWORD 0x00000001 
Start = REG_DWORD 0x00000002 
DisplayName = "myfilename" 
ErrorControl = REG_DWORD 0x0000001 这样就可以,自己写吧,只要把sys拷贝到 system32/drivers/即可 
重新启动,哈哈,不是就ok了吗? 我已经照做了,可还是不行,请高手帮帮我啊!!
这个SYS是人家提供给我的,用他提供的安装程序安装后是可以正确使用的,但是我现在用它进行开发后,想将它和我的应用程序一起做成一个安装包,但就是装不上去。以上注册表项是它的安装程序建立的,另外还有一项,但是我用REGEDIT既不能删除,也不能导入。其值为: 
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\LEGACY_abcd] 
"NextInstance"=dword:00000001 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\LEGACY_DLPORTIO\0000] 
"Service"="abcd" 
"Legacy"=dword:00000001 
"ConfigFlags"=dword:00000000 
"Class"="LegacyDriver" 
"ClassGUID"="{8ECC055D-047F-11D1-A537-0000F8753ED1}" 
"DeviceDesc"="abcd I/O Driver" 
"Capabilities"=dword:00000000 [HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Enum\Root\LEGACY_DLPORTIO\0000\Control] 
"DeviceReference"=dword:81888430 
"ActiveService"="abcd" 不知此项对其加载有无影响,又是如何建立的?

解决方案 »

  1.   

    这是驱动程序的问题,去WWW.DRIVERDEVELOP.COM
    (不过建议你最好先找找驱动开发的入门级的书看看.)
      

  2.   

    直接将.SYS考到SYSTEM32目录下,然后参考用以下几个函数建立注册表项,这样就不会在系统设备管理里面看到设备了.
    OpenSCManager
    CreateService
    OpenService
    StartService
    CloseServiceHandle最好用.SYS附带的.DEF文件来安装这个设备驱动.这样虽然会在系统设备管理里面看到设备,但比较安全.不要直接修改注册表!
      

  3.   

    Q。怎样安装硬件的驱动而不让Windows弹出“指定.sys"的对话框?
    A。在Win98下,将.inf拷到<WINDIR>Inf下,.sys拷到<WINDIR>System32Drivers下,并且删除<WINDIR>Inf下的DRVIDX.BIN和DRVDATA.BIN,再插入硬件。Win2K下,用SetupCopyOEMInf将.inf文件拷到<WINDIR>Inf下,并且.inf中不能有拷贝.inf的句子,否则,当插入第二个硬件时,系统仍然会提示找不到.sys。
    //I have tested this in installshield
      

  4.   

    system.ini
    [368enhanced]
    device=[path]\abc.sys
      

  5.   

    听说Windows的服务函数可以实现:我编了一个,不知对不:
    #include <windows.h> 
    #include <winsvc.h> 
    #include <conio.h>
    #include <stdio.h> int main(int argc, char* argv[]) 

       HANDLE hWdm; 
       printf("Hello World!\n");    SC_HANDLE hServiceMgr, hServiceTwdm; 
       BOOL bRtn; 
       DWORD dwRtn, dwSize = 256; 
       char szDir[256]; 
     
       GetCurrentDirectory( dwSize, szDir );//取当前目录 
       strcat( szDir, "\\NPF.sys" ); //取驱动程序的全路径 
       LPCTSTR lpszBinaryPathName = TEXT(szDir); 
       hServiceMgr = OpenSCManager( NULL, NULL,  SC_MANAGER_ALL_ACCESS ); //打开服务控制管理器
       if( hServiceMgr == NULL ) { 
         printf( "OpenSCManager() Faild %d ! \n", GetLastError() ); 
         return 0; 
       } 
       else 
       { 
         printf( "OpenSCManager() ok ! \n" ); 
       }    hServiceTwdm = CreateService( hServiceMgr, 
       TEXT("NPF"), //SYSTEMCurrentControlSetServices 驱动程序的在注册表中的名字 
       TEXT("NPF"), // 注册表驱动程序的 DisplayName 值 
       SERVICE_ALL_ACCESS, // 加载驱动程序的访问权限 
       SERVICE_KERNEL_DRIVER,// 表示加载的服务是驱动程序 
       2, // 注册表驱动程序的 Start 值 
       1, // 注册表驱动程序的 ErrorControl 值 
       lpszBinaryPathName, // 注册表驱动程序的 ImagePath 值 
       NULL, 
       NULL, 
       NULL, 
       NULL, 
       NULL); 
    if( hServiceTwdm == NULL ) 

     dwRtn = GetLastError(); 
     if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS ) 
     { 
     CloseServiceHandle( hServiceMgr ); 
     printf( "CrateService() Faild %d ! \n", dwRtn ); 
     return 0; 
     } 
     else 
     { 
     printf( "CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \n" ); 
     }  // 驱动程序已经加载,只需要打开 
     hServiceTwdm = OpenService( hServiceMgr, TEXT("NPF"), SERVICE_ALL_ACCESS ); 
     if( hServiceTwdm == NULL ) 
     { 
     dwRtn = GetLastError(); 
     CloseServiceHandle( hServiceMgr ); 
     printf( "OpenService() Faild %d ! \n", dwRtn ); 
     return 0; 
     } 
     else 
     { 
     printf( "OpenService() ok ! \n" ); 
     } 

    else 

    printf( "CrateService() ok ! \n" ); 

    printf( "按任意键退出 !\n" ); 
    getch(); 
    return 0; 
    }
      

  6.   

    基本上框架是对的.
    不过:
    "// 驱动程序已经加载,只需要打开 
     hServiceTwdm = OpenService...."
    此时驱动程序并没有加载,只是打开了操作系统的管理器.
    要在OpenService之后StartService,再用CreateFile打开驱动程序的句柄.再通过这个句柄访问驱动程序.
    OpenSCManager之后,在OpenService打不开的情况下,才用CreateService(通常是在该程序第一次运行的时候通过CreateService修改注册表.).给你一分源码参考.
    int StartPctIODriver()
    {
    SC_HANDLE  SchSCManager;
    SC_HANDLE  schService;
    BOOL       ret;
    DWORD      err;

    if (hPctIODrv != NULL)
    {
    bStopHandle = false;
    return 0;
    }
    else
    {
    bStopHandle = true;
    } /* Open Handle to Service Control Manager */
    SchSCManager = OpenSCManager(NULL,     // machine (NULL == local)
    NULL,     // database (NULL == default)
    SC_MANAGER_ALL_ACCESS      // access required
    );

    if (SchSCManager == NULL)
    {
    if (GetLastError() == ERROR_ACCESS_DENIED)
    {
    /* We do not have enough rights to open the SCM, therefore we must */
    /* be a poor user with only user rights. Check if we can open */
    /* handle to driver */
    if (OpenHandletoPctDriver()) 
    {
    //printf("PCT: The PCT driver is running. [USER RIGHTS]\n");
    return 1; /* All done */
    }
    //printf("\nPCT: You do not have rights to access the Service Control Manager and\n");
    //printf("PCT: the PCT driver is not installed or stated.\n");
    return 2;
    }
    }

    int count = 0;
    do{
    count++;

    /* Open a Handle to the PCT Service Database */
    schService = OpenService(SchSCManager,   // handle to service control manager database
    "PctDrv",       // pointer to name of service to start
    SERVICE_ALL_ACCESS   // type of access to service
    );
    if (schService == NULL)
    {
    switch (GetLastError())
    {
    case ERROR_ACCESS_DENIED:
    //printf("PCT: You do not have rights to the PCT service database\n");
    return 3;
    case ERROR_INVALID_NAME:
    //printf("PCT: The specified service name is invalid.\n");
    return 4;
    case ERROR_SERVICE_DOES_NOT_EXIST:
    InstallPctDriver();
    break;
    }
    }
    } while ((schService == NULL) && (count < 3));

    /* Start the PCT Driver. Errors will occur here if */
    /* .SYS file doesn't exist */
    ret = StartService (schService,    // service identifier
    0,             // number of arguments
    NULL           // pointer to arguments
    );

    if (ret)
    {
    //printf("PCT: The PCT driver has been successfully started.\n");
    }
    else
    {
    err = GetLastError();
    if (err == ERROR_SERVICE_ALREADY_RUNNING)
    {
    //printf("PCT: The PCT driver is already running.\n");
    }
    else
    {
    //printf("PCT: Unknown error while starting PCT driver service.\n");
    //printf("PCT: Does PctDrv.SYS exist in your \\System32\\Drivers Directory?\n");
    CloseServiceHandle (schService);
    return 5;
    }
    }

    /* Now driver is loaded and started, Open handle to PCT Driver so we can talk to it */

    if (!OpenHandletoPctDriver())
    {
    //printf("PCT: Couldn't create handle to PCT driver, Please ensure driver is loaded.\n");
    CloseServiceHandle (schService);
    return 6;
    }

    /* Close handle to Service Control Manager */
    CloseServiceHandle (schService);
    return 0;
    }void StopPctIODriver()
    {
    if (bStopHandle)
    {
    CloseHandle (hPctIODrv);
    }
    bStopHandle = true;
    }BOOL OpenHandletoPctDriver(void)
    {
    char ErrMsg[100];
    char device[20];
    sprintf(device,"\\\\.\\PCT_0"); hPctIODrv = CreateFile( device,
    GENERIC_READ|GENERIC_WRITE,
    FILE_SHARE_READ|FILE_SHARE_WRITE,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL); if(hPctIODrv == INVALID_HANDLE_VALUE)
    {
    DWORD errCode = GetLastError();
    sprintf(ErrMsg,"Open Handle to device %s Fail!\r\nError Code: %d",device,errCode);
    MessageBox(NULL,ErrMsg,NULL,MB_OK|MB_ICONERROR);
    return(FALSE);
    }
    else
    {
    return(TRUE);
    }
    }void InstallPctDriver(void)
    {
    SC_HANDLE  SchSCManager;
    SC_HANDLE  schService;
    DWORD      err;

    /* Open Handle to Service Control Manager */
    SchSCManager = OpenSCManager(NULL,                 // machine (NULL == local)
    NULL,                 // database (NULL == default)
    SC_MANAGER_ALL_ACCESS // access required
    );

    /* Create Service/Driver - This adds the appropriate registry keys in */
    /* HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services - It doesn't  */
    /* care if the driver exists, or if the path is correct. */
    schService = CreateService (SchSCManager,     // SCManager database
    "PctDrv",         // name of service
    "PctDrv",         // name to display
    SERVICE_ALL_ACCESS,    // desired access
    SERVICE_KERNEL_DRIVER, // service type
    SERVICE_DEMAND_START,  // start type
    SERVICE_ERROR_NORMAL,  // error control type
    "System32\\Drivers\\PctDrv.SYS",        // service's binary
    NULL,                  // no load ordering group
    NULL,                  // no tag identifier
    NULL,                  // no dependencies
    NULL,                  // LocalSystem account
    NULL                   // no password
    );

    if (schService == NULL)
        {
    err = GetLastError();
    if (err != ERROR_SERVICE_EXISTS)
    return;
        }

    /* Close Handle to Service Control Manager */
    CloseServiceHandle (schService);
    }
      

  7.   

    基本上框架是对的.
    不过:
    "// 驱动程序已经加载,只需要打开 
     hServiceTwdm = OpenService...."
    此时驱动程序并没有加载,只是打开了操作系统的管理器.
    要在OpenService之后StartService,再用CreateFile打开驱动程序的句柄.再通过这个句柄访问驱动程序.
    OpenSCManager之后,在OpenService打不开的情况下,才用CreateService(通常是在该程序第一次运行的时候通过CreateService修改注册表.).给你一分源码参考.
    int StartPctIODriver()
    {
    SC_HANDLE  SchSCManager;
    SC_HANDLE  schService;
    BOOL       ret;
    DWORD      err;

    if (hPctIODrv != NULL)
    {
    bStopHandle = false;
    return 0;
    }
    else
    {
    bStopHandle = true;
    } /* Open Handle to Service Control Manager */
    SchSCManager = OpenSCManager(NULL,     // machine (NULL == local)
    NULL,     // database (NULL == default)
    SC_MANAGER_ALL_ACCESS      // access required
    );

    if (SchSCManager == NULL)
    {
    if (GetLastError() == ERROR_ACCESS_DENIED)
    {
    /* We do not have enough rights to open the SCM, therefore we must */
    /* be a poor user with only user rights. Check if we can open */
    /* handle to driver */
    if (OpenHandletoPctDriver()) 
    {
    //printf("PCT: The PCT driver is running. [USER RIGHTS]\n");
    return 1; /* All done */
    }
    //printf("\nPCT: You do not have rights to access the Service Control Manager and\n");
    //printf("PCT: the PCT driver is not installed or stated.\n");
    return 2;
    }
    }

    int count = 0;
    do{
    count++;

    /* Open a Handle to the PCT Service Database */
    schService = OpenService(SchSCManager,   // handle to service control manager database
    "PctDrv",       // pointer to name of service to start
    SERVICE_ALL_ACCESS   // type of access to service
    );
    if (schService == NULL)
    {
    switch (GetLastError())
    {
    case ERROR_ACCESS_DENIED:
    //printf("PCT: You do not have rights to the PCT service database\n");
    return 3;
    case ERROR_INVALID_NAME:
    //printf("PCT: The specified service name is invalid.\n");
    return 4;
    case ERROR_SERVICE_DOES_NOT_EXIST:
    InstallPctDriver();
    break;
    }
    }
    } while ((schService == NULL) && (count < 3));

    /* Start the PCT Driver. Errors will occur here if */
    /* .SYS file doesn't exist */
    ret = StartService (schService,    // service identifier
    0,             // number of arguments
    NULL           // pointer to arguments
    );

    if (ret)
    {
    //printf("PCT: The PCT driver has been successfully started.\n");
    }
    else
    {
    err = GetLastError();
    if (err == ERROR_SERVICE_ALREADY_RUNNING)
    {
    //printf("PCT: The PCT driver is already running.\n");
    }
    else
    {
    //printf("PCT: Unknown error while starting PCT driver service.\n");
    //printf("PCT: Does PctDrv.SYS exist in your \\System32\\Drivers Directory?\n");
    CloseServiceHandle (schService);
    return 5;
    }
    }

    /* Now driver is loaded and started, Open handle to PCT Driver so we can talk to it */

    if (!OpenHandletoPctDriver())
    {
    //printf("PCT: Couldn't create handle to PCT driver, Please ensure driver is loaded.\n");
    CloseServiceHandle (schService);
    return 6;
    }

    /* Close handle to Service Control Manager */
    CloseServiceHandle (schService);
    return 0;
    }void StopPctIODriver()
    {
    if (bStopHandle)
    {
    CloseHandle (hPctIODrv);
    }
    bStopHandle = true;
    }BOOL OpenHandletoPctDriver(void)
    {
    char ErrMsg[100];
    char device[20];
    sprintf(device,"\\\\.\\PCT_0"); hPctIODrv = CreateFile( device,
    GENERIC_READ|GENERIC_WRITE,
    FILE_SHARE_READ|FILE_SHARE_WRITE,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL); if(hPctIODrv == INVALID_HANDLE_VALUE)
    {
    DWORD errCode = GetLastError();
    sprintf(ErrMsg,"Open Handle to device %s Fail!\r\nError Code: %d",device,errCode);
    MessageBox(NULL,ErrMsg,NULL,MB_OK|MB_ICONERROR);
    return(FALSE);
    }
    else
    {
    return(TRUE);
    }
    }void InstallPctDriver(void)
    {
    SC_HANDLE  SchSCManager;
    SC_HANDLE  schService;
    DWORD      err;

    /* Open Handle to Service Control Manager */
    SchSCManager = OpenSCManager(NULL,                 // machine (NULL == local)
    NULL,                 // database (NULL == default)
    SC_MANAGER_ALL_ACCESS // access required
    );

    /* Create Service/Driver - This adds the appropriate registry keys in */
    /* HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services - It doesn't  */
    /* care if the driver exists, or if the path is correct. */
    schService = CreateService (SchSCManager,     // SCManager database
    "PctDrv",         // name of service
    "PctDrv",         // name to display
    SERVICE_ALL_ACCESS,    // desired access
    SERVICE_KERNEL_DRIVER, // service type
    SERVICE_DEMAND_START,  // start type
    SERVICE_ERROR_NORMAL,  // error control type
    "System32\\Drivers\\PctDrv.SYS",        // service's binary
    NULL,                  // no load ordering group
    NULL,                  // no tag identifier
    NULL,                  // no dependencies
    NULL,                  // LocalSystem account
    NULL                   // no password
    );

    if (schService == NULL)
        {
    err = GetLastError();
    if (err != ERROR_SERVICE_EXISTS)
    return;
        }

    /* Close Handle to Service Control Manager */
    CloseServiceHandle (schService);
    }
      

  8.   

    请问Windows 2000下可以这样调用,那么98下怎么实现呢?
      

  9.   

    OpenSCManager等不被98支持.(查MSDN,讲得很清楚)
    所以不能在98下用这种方法.
    而且WIN2K的驱动程序不一定被98支持.驱动程序挺复杂的.
    For details,还是建议你去WWW.DRIVERDEVELOP.COM问问.
      

  10.   

    98下怎么实现,还要看你的驱动程序是什么类型的.
    比如是并口的,还是USB的,大致是实现什么功能的,等等.
      

  11.   

    我的驱动程序在98和2000下都是可以启动的,名字是npf.sys。可98下没有服务,
    OpenSCManager函数不被支持,请问,我该从什么地方入手解决这个问题?
    用你的方法我已经实现了2000下的启动,但在98下出错?谢谢!请多指教!
      

  12.   

    请问高手们,Windows 98下,如何启动驱动程序??
    关注。。
      

  13.   

    你可以试试:(不同类型的驱动程序可能打开方式不一样,不保证一定有效)
    SetupDiGetClassDevs,// Get handle to relevant device information set
    SetupDiEnumDeviceInterfaces,// Get interface data for the requested instance
    SetupDiGetDeviceInterfaceDetail,// Get size of symbolic link name
    SetupDiGetDeviceInterfaceDetail,// Get symbolic link name
    CreateFile,// Open file
    SetupDiDestroyDeviceInfoList,源代码供参考:
    // GetDeviceViaInterface: Open a handle via a device interfaceHANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance)
    {
    // Get handle to relevant device information set
    HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE);
    if(info==INVALID_HANDLE_VALUE)
    {
    printf("No HDEVINFO available for this GUID\n");
    return NULL;
    } // Get interface data for the requested instance
    SP_INTERFACE_DEVICE_DATA ifdata;
    ifdata.cbSize = sizeof(ifdata);
    if(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, instance, &ifdata))
    {
    printf("No SP_INTERFACE_DEVICE_DATA available for this GUID instance\n");
    SetupDiDestroyDeviceInfoList(info);
    return NULL;
    } // Get size of symbolic link name
    DWORD ReqLen;
    SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL);
    PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]);
    if( ifDetail==NULL)
    {
    SetupDiDestroyDeviceInfoList(info);
    return NULL;
    } // Get symbolic link name
    ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
    if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL))
    {
    SetupDiDestroyDeviceInfoList(info);
    delete ifDetail;
    return NULL;
    } printf("Symbolic link is %s\n",ifDetail->DevicePath);
    // Open file
    HANDLE rv = CreateFile( ifDetail->DevicePath, 
    GENERIC_READ | GENERIC_WRITE,
    FILE_SHARE_READ | FILE_SHARE_WRITE,
    NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if( rv==INVALID_HANDLE_VALUE) rv = NULL; delete ifDetail;
    SetupDiDestroyDeviceInfoList(info);
    return rv;
    }
      

  14.   

    再次打扰lily311,请问我看到有的帖子说在WINDOWS 2000/NT下可以用UpdateDriverPlugAndPlayDevice()实现驱动程序的加载,这和用CreateServive()加载有什么不同吗?谢谢!必定有分!!