读书人

磁盘驱动器的名字怎样取得解决办法

发布时间: 2012-03-27 13:44:24 作者: rapoo

磁盘驱动器的名字怎样取得
如图:


怎样取得插入U盘后的那个驱动器的名字Kingston **那段

[解决办法]
http://topic.csdn.net/u/20090828/14/b5c9db15-4991-4425-9168-f1ef7856f935.html
[解决办法]
1楼不是和你说了么

获取U盘,处理WM_DEVICECHANGE
[解决办法]
getvolumeinformation好像只能获取卷标,要得到设备名称还得靠setupdi接口,codeproject上面有设备管理器的例子你可以去看看
[解决办法]
deviceioctrl的确能取得,就是前后构造那些cwb很麻烦。

[解决办法]
用Setupid..系列函数
#include <stdio.h>
#include <windows.h>
#include <setupapi.h>
#include <devguid.h>
#include <regstr.h>
#include <TCHAR.h>

#pragma comment (lib,"setupapi")

const TCHAR szDevClass[] = "DiskDrive";

int main( int argc, char *argv[ ], char *envp[ ] )
{
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
DWORD PropertyID = SPDRP_CLASS;
bool isdevflag = false;


// Create a HDEVINFO with all present devices.
hDevInfo = SetupDiGetClassDevs(NULL,
0, // Enumerator
0,
DIGCF_PRESENT | DIGCF_ALLCLASSES );

if (hDevInfo == INVALID_HANDLE_VALUE)
{
// Insert error handling here.
return 1;
}

// Enumerate through all devices in Set.

DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0;SetupDiEnumDeviceInfo(hDevInfo,i,
&DeviceInfoData);i++)
{
DWORD DataT;
LPTSTR buffer = NULL;
DWORD buffersize = 0;
DWORD dwErrCode;

//
// Call function with null to begin with,
// then use the returned buffer size
// to Alloc the buffer. Keep calling until
// success or an unknown failure.
//
Nxt:
while (!SetupDiGetDeviceRegistryProperty(
hDevInfo,
&DeviceInfoData,
PropertyID,
&DataT,
(PBYTE)buffer,
buffersize,
&buffersize))
{

if ((dwErrCode = GetLastError() ) ==
ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
buffer = (TCHAR *)LocalAlloc(LPTR,buffersize);
}
else if ( ERROR_INVALID_DATA == dwErrCode )
{
printf("SetupDiGetDeviceRegistryProperty UnSupported\n");
goto Exit;
}
else
{
// Insert error handling here.
printf("SetupDiGetDeviceRegistryProperty Error : [%ld]\n",dwErrCode);
goto Exit;
}
}

//是磁盘驱动器?
if ( _tcscmp( szDevClass, buffer ) == 0 )
{
PropertyID = SPDRP_FRIENDLYNAME;
if (buffer)
{
LocalFree(buffer);
buffer = NULL;
}
buffersize = 0;
isdevflag = true;
goto Nxt;
}
else if ( isdevflag )
{
printf("DevName:%s\n",buffer);
if (buffer)
{
LocalFree(buffer);
buffer = NULL;
}
buffersize = 0;
isdevflag = false;

}

PropertyID = SPDRP_CLASS;



}


if ( GetLastError()!=NO_ERROR &&
GetLastError()!=ERROR_NO_MORE_ITEMS )


{
// Insert error handling here.
return 1;
}

Exit:
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);

return 0;
}

[解决办法]
好像得从驱动一层查找
[解决办法]
经鉴定,1楼发的链接里面有完整的信息,呵呵
祝楼主成功。

读书人网 >VC/MFC

热点推荐