如何在程序中获取驱动程序信息?

phoebe_yanink 2006-04-28 03:15:02
如:就是在设备管理器里面点击板卡属性中的位置信息,
如位置: PCI slot5(PCI 总线0、设备19、功能0)

具体操作:
我的电脑-》属性-》硬件-》设备管理器-》声音,视频和游戏控制器-》某某驱动-》属性-》常规

可以看见如下信息:
设备类型:声音,视频和游戏控制器
制造商: DVB-T
位置: PCI slot5(PCI 总线0、设备19、功能0)

我现在就是想要获取该驱动的这个“位置: PCI slot5(PCI 总线0、设备19、功能0)”信息,好在我的应用程序里面显示出来,这应该有API函数可以获取吧,但怎么样具体操作,我不知道。可以给点例子,或者指点我一下吗,谢谢了!
...全文
727 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
phoebe_yanink 2006-05-09
  • 打赏
  • 举报
回复
多谢几位大侠慷慨相助!
现在我还有一个问题,就是我想获取我的设备的serial number,例如:就是在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\enum\pci\ven_1022&dev_1100&subsys\3&13c0b05&0&c0 的“3&13c0b05&0&c0”这个名字,他也被叫做serial number,根据我们的卡在不同的查槽他的名字是会改变的,我原来想得到的 “PCI slot5(PCI 总线0、设备19、功能0)”信息正是在他里面的键:location information 里,上面大家给的方法其实也就是从这里获取信息的,对吧!但我想获取serial number“3&13c0b05&0&c0”,(可能在同一个设备名字下有多个,原因我也说了),我原来是用“setupDiGetDeviceInterfaceDetail”来获取设备的完整路径,那么在路径的里面,就有我要的serial number,但是这个方法好臃肿,我想,我都可以在serial number这个目录里面获取location information ,应该有很快的方法获取这个serial number名字,我也想了些办法,效果不好,各位高手,拜托你们啦!
help!!!!!!!
会思考的草 2006-05-09
  • 打赏
  • 举报
回复
注册表可以直接读出来的,但是2000下必须要有相应权限。
会思考的草 2006-05-08
  • 打赏
  • 举报
回复
给你一个完整的模拟设备管理器的控件:
/////////////////////////////////////////////////////////////////////////////
// CDeviceTreeCtrl message handlers
int CDeviceTreeCtrl::EnumDeviceClasses(int index, TCHAR* DeviceClassName, TCHAR* DeviceClassDesc, BOOL* DevicePresent, int* ClassImage)
{
int nResult = -1;
GUID ClassGuid = {0};
BOOL resNam = FALSE;
ULONG RequiredSize = MAX_DEV_LEN;
TCHAR* name = new TCHAR[MAX_DEV_LEN];
HDEVINFO NewDeviceInfoSet = NULL;

nResult = CM_Enumerate_Classes(index, &ClassGuid, 0);
*DevicePresent = FALSE;

//incorrect device class:
if(nResult == CR_INVALID_DATA)
{
return -2;
}

//device class is absent
if(nResult == CR_NO_SUCH_VALUE)
{
return -1;
}

//bad param. - fatal error
if(nResult != CR_SUCCESS)
{
return -3;
}

resNam = SetupDiClassNameFromGuid(&ClassGuid, name, RequiredSize, &RequiredSize);
if(RequiredSize > 0)
{
delete[] name;
name = new TCHAR[RequiredSize];
resNam = SetupDiClassNameFromGuid(&ClassGuid, name, RequiredSize, &RequiredSize);
}

SetupDiGetClassImageIndex(&m_ImageListData, &ClassGuid, ClassImage);
NewDeviceInfoSet = SetupDiGetClassDevs(&ClassGuid, 0, NULL, DIGCF_PRESENT);

if(NewDeviceInfoSet == INVALID_HANDLE_VALUE)
{
*DevicePresent = FALSE;
_tcscpy(DeviceClassName, name);

delete[] name;
name = NULL;
return 0;
}

HKEY KeyClass = SetupDiOpenClassRegKeyEx(&ClassGuid,
MAXIMUM_ALLOWED,
DIOCR_INSTALLER,
NULL,
0);

if(KeyClass == INVALID_HANDLE_VALUE)
{
*DevicePresent = FALSE;
_tcscpy(DeviceClassName, name);

delete[] name;
name = NULL;

return 0;
}
else
{
long dwSize = MAX_DEV_LEN;
int res = RegQueryValue(KeyClass, NULL, DeviceClassDesc, &dwSize);

if (res != ERROR_SUCCESS)
_tcscpy(DeviceClassDesc, _T(""));
}

SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);

_tcscpy(DeviceClassName, name);
*DevicePresent = TRUE;

RegCloseKey(KeyClass);

delete [] name;
name = NULL;
return 0;
}


int CDeviceTreeCtrl::EnumDevices(int index, TCHAR* DeviceClassName, TCHAR* DeviceName)
{
GUID* guids = new GUID[1];
ULONG RequiredSize = 0;
HDEVINFO NewDeviceInfoSet = NULL;
SP_DEVINFO_DATA DeviceInfoData = {0};

BOOL res = SetupDiClassGuidsFromName(DeviceClassName, &guids[0], RequiredSize, &RequiredSize);

if (RequiredSize==0)
{
//incorrect class name:
_tcscpy(DeviceName, _T(""));
return -2;
}

if(!res)
{
delete [] guids;
guids = new GUID[RequiredSize];
res = SetupDiClassGuidsFromName(DeviceClassName, &guids[0],
RequiredSize, &RequiredSize);

if (!res || RequiredSize==0)
{
//incorrect class name:
_tcscpy(DeviceName, _T(""));
return -2;
}
}

//get device info set for our device class
NewDeviceInfoSet = SetupDiGetClassDevs(&guids[0], 0, NULL, DIGCF_PRESENT);

if(NewDeviceInfoSet == INVALID_HANDLE_VALUE)
if(!res)
{
//device information is unavailable:
_tcscpy(DeviceName, _T(""));
return -3;
}

DeviceInfoData.cbSize = 28;
//is devices exist for class
DeviceInfoData.DevInst = 0;
ZeroMemory(&DeviceInfoData.ClassGuid, sizeof(GUID));
DeviceInfoData.Reserved = 0;

res = SetupDiEnumDeviceInfo(NewDeviceInfoSet, index, &DeviceInfoData);

if (!res)
{
//no such device:
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
_tcscpy(DeviceName, _T(""));
return -1;
}


if (!SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, &DeviceInfoData,
SPDRP_FRIENDLYNAME, 0, (BYTE*) DeviceName,
MAX_DEV_LEN, NULL))
{
res = SetupDiGetDeviceRegistryProperty(NewDeviceInfoSet, &DeviceInfoData,
SPDRP_DEVICEDESC, 0, (BYTE*) DeviceName,
MAX_DEV_LEN, NULL);
if (!res)
{
//incorrect device name:
SetupDiDestroyDeviceInfoList(NewDeviceInfoSet);
_tcscpy(DeviceName, _T(""));
return -4;
}
}

return 0;
}


void CDeviceTreeCtrl::EnumDevices()
{
DeleteAllItems();

//Set Image List
m_ImageListData.cbSize = sizeof(m_ImageListData);
SetupDiGetClassImageList(&m_ImageListData);

m_ImageList.Attach(m_ImageListData.ImageList);
CBitmap myComputer;
myComputer.LoadBitmap(IDB_BITMAP_COMPUTER);
COLORREF colormask = RGB(255, 0, 255);
m_ImageList.Add(&myComputer, colormask);

SetImageList(&m_ImageList, TVSIL_NORMAL);
HTREEITEM hRoot, hDeviceClasses;

TCHAR ComputerName[MAX_PATH];
DWORD dwSize = MAX_PATH;

GetComputerName(ComputerName, &dwSize);

int ComputerImage = m_ImageList.GetImageCount() - 1;
hRoot = InsertItem(ComputerName, ComputerImage, ComputerImage);

TCHAR classes[MAX_DEV_LEN] = {0};
TCHAR classesDesc[MAX_DEV_LEN] = {0};

BOOL DevExist = FALSE;
int index = 0;
int DeviceImage;
int res = EnumDeviceClasses(index , classes, classesDesc, &DevExist, &DeviceImage);

while (res != -1)
{
if (res >= -1 && DevExist)
{
//Add New Device Entry to Tree
if (_tcsicmp(classesDesc, _T(""))==0)
hDeviceClasses = InsertItem((LPCTSTR) classes,
DeviceImage, DeviceImage, hRoot);
else
hDeviceClasses = InsertItem((LPCTSTR) classesDesc,
DeviceImage, DeviceImage, hRoot);

//loop for enumerating devices of specefic class!
int result, DevIndex = 0;
TCHAR DeviceName[MAX_DEV_LEN] = _T("");

result = EnumDevices(DevIndex, classes, DeviceName);

while (result != -1)
{
if (result == 0)
InsertItem(DeviceName, DeviceImage, DeviceImage, hDeviceClasses);

DevIndex++;
result = EnumDevices(DevIndex, classes, DeviceName);
}

if (GetChildItem(hDeviceClasses)==NULL)
DeleteItem(hDeviceClasses);
else
SortChildren(hDeviceClasses);

}

index++;
res = EnumDeviceClasses(index, classes, classesDesc, &DevExist, &DeviceImage);
}

Expand(hRoot, TVE_EXPAND);
SortChildren(hRoot);
}
seaquester 2006-05-08
  • 打赏
  • 举报
回复
用 SetupDiGetDeviceRegistryProperty 可以得到:

#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>


VOID PrintWin32Error(DWORD dwError)
{
LPVOID lpMsgBuf;

FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
dwError,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,
0,
NULL
);

if (lpMsgBuf)
{
::MessageBox(NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK);
LocalFree(lpMsgBuf);
}
}


int main(int argc, char *argv[], char *envp[])
{
HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
SP_DEVINFO_DATA DeviceInfoData;
DWORD dwErrCode = 0;

//
// Create a HDEVINFO with all present devices.
//
hDevInfo = SetupDiGetClassDevs(
(LPGUID)&GUID_DEVCLASS_DISPLAY,
0,
0,
DIGCF_PRESENT
);
if (hDevInfo == INVALID_HANDLE_VALUE)
{
dwErrCode = GetLastError();
PrintWin32Error(dwErrCode);
return 1;
}

//
// Enumerate through all devices.
//
printf("All Port Device List:\n");

LPTSTR pBuffer = NULL;
DWORD dwBufferSize = 0x40;

pBuffer = (char*)LocalAlloc(LPTR, dwBufferSize);
if (!pBuffer)
{
dwErrCode = GetLastError();
PrintWin32Error(dwErrCode);
return 2;
}

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (DWORD i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); ++i)
{
DWORD PropertyRegDataType;

//
// Call function with NULL to begin with, then use the returned buffer
// size to allocate the buffer. Keep calling until success or an unknown
// failure.
//
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
SPDRP_LOCATION_INFORMATION,
&PropertyRegDataType,
(PBYTE)pBuffer,
dwBufferSize,
NULL))
{
dwErrCode = GetLastError();
if (dwErrCode == ERROR_INSUFFICIENT_BUFFER)
{
//
// Increase the buffer size.
//
dwBufferSize += 0x20;
pBuffer = (char*)LocalReAlloc(pBuffer, dwBufferSize, LMEM_MOVEABLE);
}
else if (dwErrCode == ERROR_NO_MORE_ITEMS)
{ // No more device
break;
}
else
{ // Some error occur!
PrintWin32Error(dwErrCode);

if (pBuffer)
{
LocalFree(pBuffer);
}

// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 3;
}
}

printf("%s\n", pBuffer);
}

if (pBuffer)
{
LocalFree(pBuffer);
}

// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
return 0;
}
phoebe_yanink 2006-04-28
  • 打赏
  • 举报
回复
你们说的方法感觉不行啊!
pomelowu 2006-04-28
  • 打赏
  • 举报
回复
http://www.codeguru.com/cpp/w-p/system/hardwareinformation/article.php/c2815/
lixiaosan 2006-04-28
  • 打赏
  • 举报
回复
这些东西估计注册表里面有吧

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\

2,641

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧