kevin_hou
驱动牛犊
驱动牛犊
  • 注册日期2004-07-27
  • 最后登录2005-01-20
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:2171回复:10

如何制作一个usb设备的驱动安装程序?

楼主#
更多 发布于:2004-07-27 10:19
小弟刚接触驱动,请各位大虾指教!最好有源码

最新喜欢:

bearsammibearsa... shahlynnshahly...
bingjie
驱动小牛
驱动小牛
  • 注册日期2001-08-15
  • 最后登录2007-11-29
  • 粉丝0
  • 关注0
  • 积分36分
  • 威望5点
  • 贡献值0点
  • 好评度3点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-07-27 10:26
/*************************************************************************************
*
* Name: FindWdmDevice
* Desc: Find the device info
* Para: HardwareId: the device's hardware id
* pDeviceINfoData: Save the device info
* Retn: if find the device info, return DeviceInfoSet that contain the device info,
* Otherwise return INVALID_HANDLE_VALUE
*
***************************************************************************************/
HDEVINFO
FindWdmDevice(
IN LPCTSTR HardwareId,
IN PSP_DEVINFO_DATA pDeviceInfoData
)
{
    HDEVINFO DeviceInfoSet;
    DWORD i;
    DWORD DataT;
    LPTSTR p;
CCHAR Buffer[ 1024 ];
    DWORD BufferSize = 0;

if( HardwareId == NULL  || pDeviceInfoData == NULL )
return INVALID_HANDLE_VALUE;

    //
    // Create a Device Information Set with all present devices.
    //
    DeviceInfoSet = SetupDiGetClassDevs(NULL, // All Classes
        0,
        0,
        DIGCF_ALLCLASSES  );
    if( DeviceInfoSet == INVALID_HANDLE_VALUE )
    {
        TRACE( "GetClassDevs(All Present Devices) failed\n" );
        return INVALID_HANDLE_VALUE;
    }
    
    TRACE( "Search for Device ID: [%s]\n", HardwareId );
    
    pDeviceInfoData->cbSize = sizeof(SP_DEVINFO_DATA);
    for ( i=0; SetupDiEnumDeviceInfo( DeviceInfoSet, i, pDeviceInfoData ); i++ )
    {

        // 获取DEVICE_ID
BufferSize = sizeof( Buffer );
        if( !SetupDiGetDeviceRegistryProperty(
DeviceInfoSet,
pDeviceInfoData,
SPDRP_HARDWAREID,
&DataT,
(PBYTE)Buffer,
BufferSize,
&BufferSize) )
        {
// 未知错误
// TRACE1( "GetDeviceRegistryProperty failed: 0x%x", GetLastError() );
            continue;
        }

// 按字符串比较(一个设备可能有多个ID )
        for( p = Buffer; *p && ( p < &Buffer[BufferSize] ); p += lstrlen( p ) + sizeof( TCHAR ) )
        {
// TRACE1( "Device: %s", p );
            if (!_tcsicmp( HardwareId, p ) )
            {
// 找到了指定的设备
                return DeviceInfoSet;
            }
        }
        
    }// for
    
// 没找到
SetupDiDestroyDeviceInfoList(DeviceInfoSet);
    return INVALID_HANDLE_VALUE;
}

typedef BOOL ( WINAPI *FPUpdatePNPDevice )( HWND  hwndParent, LPCTSTR  HardwareId, LPCTSTR  FullInfPath, DWORD  InstallFlags,  PBOOL  bRebootRequired OPTIONAL );
typedef BOOL ( WINAPI *FPSetupCopyOEMInf )( LPCTSTR, LPCTSTR, DWORD, DWORD, LPTSTR, DWORD, PDWORD, LPTSTR* );

/*************************************************************************************
*
* Name: UpdateWinNTDriver
* Desc: Update the device driver under WindowsNT based os
* Para: InfPath: the device's inf file path, must include full path and file name
* HardwareId: the device's hardware id
* pisNeedReboot: ture means need restart the windows to complete the install
* Retn: return TRUE means install success, otherwise install failed
*
***************************************************************************************/
BOOL
UpdateWinNTDriver(
IN LPCTSTR InfPath,
IN LPCTSTR HardwareId,
IN OUT PBOOL pisNeedReboot
)
{
    HDEVINFO DeviceInfoSet;
    SP_DEVINFO_DATA DeviceInfoData;
FPUpdatePNPDevice fpUpdatePNPDevice;
FPSetupCopyOEMInf fpSetupCopyOEMInf;
HMODULE hModule;
BOOL bRet = FALSE;
DWORD dwErrCode;

DeviceInfoSet = FindWdmDevice( HardwareId, &DeviceInfoData );
if( DeviceInfoSet == INVALID_HANDLE_VALUE )
{
hModule = LoadLibrary( "SetupApi.dll" );
if( hModule != NULL )
{
fpSetupCopyOEMInf = ( FPSetupCopyOEMInf )
GetProcAddress( hModule, "SetupCopyOEMInfA" );

if( fpSetupCopyOEMInf == NULL )
{
TRACE1( "Get SetupCopyOEMInf function failed: 0x%x", GetLastError() );
bRet = FALSE;
}
else
{
bRet = ( *fpSetupCopyOEMInf )( InfPath, NULL, SPOST_PATH, SP_COPY_NOOVERWRITE, NULL, 0, NULL, NULL );
if( !bRet )
{
dwErrCode = GetLastError();
TRACE1( "SetupCopyOEMInf failed: 0x%x", dwErrCode );
if( dwErrCode == ERROR_FILE_EXISTS )
{
TRACE0( "The inf file exists" );
bRet = TRUE;
}
}
}

FreeLibrary( hModule );
}

return bRet;
}

SetupDiDestroyDeviceInfoList( DeviceInfoSet );

hModule = LoadLibrary( "newdev.dll" );
if( hModule != NULL )
{
fpUpdatePNPDevice = ( FPUpdatePNPDevice )
GetProcAddress( hModule, "UpdateDriverForPlugAndPlayDevicesA" );

if( fpUpdatePNPDevice == NULL )
{
TRACE0( "UpdateDriverForPlugAndPlayDevice function not find" );
FreeLibrary( hModule );
return FALSE;
}

bRet = ( *fpUpdatePNPDevice )( GetDesktopWindow(),
HardwareId,
InfPath,
INSTALLFLAG_FORCE,
pisNeedReboot
);
if( !bRet )
{
TRACE1( "UpdateDriverForPlugAndPlayDevice failed: 0x%x", GetLastError() );
}

FreeLibrary( hModule );

}

return bRet;

}
kevin_hou
驱动牛犊
驱动牛犊
  • 注册日期2004-07-27
  • 最后登录2005-01-20
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
板凳#
发布于:2004-07-27 14:37
谢谢楼上的大虾,有没有win98的程序?
kevin_hou
驱动牛犊
驱动牛犊
  • 注册日期2004-07-27
  • 最后登录2005-01-20
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地板#
发布于:2004-07-27 14:42
还有我在硬件管理器中删除了此硬件,然后运行这个程序就无法安装成功!但是如果拔下硬件,然后再插入后,就可以安装成功。这是为什么?请教大虾。谢谢。
bingjie
驱动小牛
驱动小牛
  • 注册日期2001-08-15
  • 最后登录2007-11-29
  • 粉丝0
  • 关注0
  • 积分36分
  • 威望5点
  • 贡献值0点
  • 好评度3点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2004-07-27 14:42
/*************************************************************************************
*
* Name: UpdateWin9xDriver
* Desc: Update the device driver under Windows9x based os
* Para: InfPath: the device's inf file path, must include full path and file name
* HardwareId: the device's hardware id
* pisNeedReboot: ture means need restart the windows to complete the install
* Retn: return TRUE means install success, otherwise install failed
*
***************************************************************************************/
BOOL
UpdateWin9xDriver(
IN LPCTSTR InfPath,
IN LPCTSTR HardwareId,
IN OUT PBOOL pisNeedReboot
)
{
TCHAR WinDir[ _MAX_PATH ];
TCHAR DesDir[ _MAX_PATH ];
TCHAR SrcDir[ _MAX_PATH ];
TCHAR DesFile[ _MAX_PATH ];
TCHAR SrcFile[ _MAX_PATH ];
INT i;

// Under win9x,
// 1. remove the previous driver
// 2. copy the inf file to windows inf directory
// 3. copy the sys file to windows system directory
// 4. restart windows
RemoveWdmDriver( HardwareId, NULL );
*pisNeedReboot = TRUE;

WinDir[ 0 ] = '';
GetWindowsDirectory( WinDir, sizeof( WinDir ) );
if( strlen( WinDir ) == 0 )
{
TRACE1( "Get windows root directory failed: 0x%x", GetLastError() );
return FALSE;
}
else if( WinDir[ strlen( WinDir ) - 1 ] != '\' )
{
strcat( WinDir, "\" );
}

for( i = strlen( InfPath ); i > 0; i-- )
{
memset( SrcDir, 0, sizeof( SrcDir ) );
if( InfPath[ i - 1 ] == '\' )
{
strncpy( SrcDir, InfPath, i );
break;
}
}

if( i <= 0 )
{
TRACE1( "Invalid inf file path: %s", InfPath );
return FALSE;
}

TRACE1( "Source path: %s", SrcDir );

strcpy( SrcFile, SrcDir );
strcat( SrcFile, INF_FILE );

strcpy( DesDir, WinDir );
strcat( DesDir, "INF\" );

strcpy( DesFile, DesDir );
strcat( DesFile, INF_FILE );
if( !CopyFile( SrcFile, DesFile, FALSE ) )
{
TRACE1( "Copy inf file failed: 0x%x", GetLastError() );
return FALSE;
}

strcat( DesDir, "Catalog\" );
CreateDirectory( DesDir, NULL );

strcpy( SrcFile, SrcDir );
strcat( SrcFile, CAT_FILE );

strcpy( DesFile, DesDir );
strcat( DesFile, CAT_FILE );
CopyFile( SrcFile, DesFile, FALSE );

DesDir[ 0 ] = '';
GetSystemDirectory( DesDir, sizeof( DesDir ) );
if( strlen( DesDir ) == 0 )
{
TRACE1( "Get system path failed: 0x", GetLastError() );
return FALSE;
}
else if( DesDir[ strlen( DesDir ) - 1 ] != '\' )
{
strcat( DesDir, "\" );
}

strcpy( SrcFile, SrcDir );
strcat( SrcFile, SYS_FILE );

strcpy( DesFile, DesDir );
strcat( DesFile, SYS_FILE );
if( !CopyFile( SrcFile, DesFile, FALSE ) )
{
TRACE1( "Copy sys file failed: 0x%x", GetLastError() );
return FALSE;
}

return TRUE;

}


[编辑 -  7/28/04 by  bingjie]
kevin_hou
驱动牛犊
驱动牛犊
  • 注册日期2004-07-27
  • 最后登录2005-01-20
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
5楼#
发布于:2004-07-27 17:21
没有写完呀。请继续
kevin_hou
驱动牛犊
驱动牛犊
  • 注册日期2004-07-27
  • 最后登录2005-01-20
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
6楼#
发布于:2004-07-28 17:55
谢谢,可是那些常量是什么?如何在win98卸载驱动?
wound
驱动牛犊
驱动牛犊
  • 注册日期2004-06-08
  • 最后登录2004-09-16
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
7楼#
发布于:2004-08-14 15:09
能给点详细的说明,最好能给一个完整的事例,谢谢了
我的mail:wound1979@163.com
jxf301
驱动牛犊
驱动牛犊
  • 注册日期2004-08-03
  • 最后登录2008-07-25
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
8楼#
发布于:2004-08-16 14:03
我也想有完整的例子,jxf16@sina.com,谢谢!
pian_zhou
驱动牛犊
驱动牛犊
  • 注册日期2004-07-25
  • 最后登录2004-08-21
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
9楼#
发布于:2004-08-16 17:35
我刚接触,不好意思。
想问一下,有了这个程序,然后再怎么弄?
有没有哪位解释一下
轻舟技术--嵌入式精品小站。http://qztech.51.net,QQ:9752283 |51/DSP/ARM |嵌入式Linux |μC/OS-II |CAN/USB总线 |VC/C编程 低价代理PCB制板,低价供应CAN-USB-RS232接口卡,2407DSPEVM板 轻舟技术---个人承接51/DSP/ARM项目
usb_anywhere
驱动小牛
驱动小牛
  • 注册日期2003-07-29
  • 最后登录2005-11-22
  • 粉丝0
  • 关注0
  • 积分3分
  • 威望1点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
10楼#
发布于:2004-09-09 16:03
我刚接触,不好意思。
想问一下,有了这个程序,然后再怎么弄?
有没有哪位解释一下


你要做的是驱动安装程序吗?

还是要在应用层与驱动打交道?

上面那个原码是干什么的你先搞清楚。
驱网是你们的,也是我们的,归根结底还是大家的!
游客

返回顶部