我有以下枚舉設備和寫描述安慰代碼:爲什麼PSTR類型在不同的visual studio項目類型上有不同的行爲?
#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#include <tchar.h>
#ifdef DEBUG
#undef DBG
#define DBG 1
#endif
#if DBG
#define OOPS() Oops(__FILE__, __LINE__)
#else
#define OOPS()
#endif
#define ALLOC(dwBytes) GlobalAlloc(GPTR,(dwBytes))
#define REALLOC(hMem, dwBytes) GlobalReAlloc((hMem), (dwBytes), (GMEM_MOVEABLE|GMEM_ZEROINIT))
#define FREE(hMem) GlobalFree((hMem))
#define CHECKFORLEAKS()
_Success_(return == TRUE)
BOOL
GetDeviceProperty(
_In_ HDEVINFO DeviceInfoSet,
_In_ PSP_DEVINFO_DATA DeviceInfoData,
_In_ DWORD Property,
_Outptr_ LPTSTR *ppBuffer
)
{
BOOL bResult;
DWORD requiredLength = 0;
DWORD lastError;
if(ppBuffer == NULL)
{
return FALSE;
}
*ppBuffer = NULL;
bResult = SetupDiGetDeviceRegistryProperty(DeviceInfoSet,
DeviceInfoData,
Property,
NULL,
NULL,
0,
&requiredLength);
lastError = GetLastError();
if((requiredLength == 0) || (bResult != FALSE && lastError != ERROR_INSUFFICIENT_BUFFER))
{
return FALSE;
}
*ppBuffer = ALLOC(requiredLength);
if(*ppBuffer == NULL)
{
return FALSE;
}
bResult = SetupDiGetDeviceRegistryProperty(DeviceInfoSet,
DeviceInfoData,
Property,
NULL,
(PBYTE)*ppBuffer,
requiredLength,
&requiredLength);
if(bResult == FALSE)
{
FREE(*ppBuffer);
*ppBuffer = NULL;
return FALSE;
}
return TRUE;
}
void
EnumerateAllDevices(
)
{
HDEVINFO deviceInfo = NULL;
// Getting all present devices
deviceInfo = SetupDiGetClassDevs(NULL,
NULL,
NULL,
(DIGCF_ALLCLASSES | DIGCF_PRESENT));
if(deviceInfo != INVALID_HANDLE_VALUE)
{
ULONG index;
DWORD error;
error = 0;
index = 0;
while(error != ERROR_NO_MORE_ITEMS)
{
BOOL success;
SP_DEVINFO_DATA infoData;
ZeroMemory(&infoData, sizeof(SP_DEVINFO_DATA));
PSTR DeviceDescName = NULL;
infoData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
success = SetupDiEnumDeviceInfo(deviceInfo,
index,
&infoData);
index++;
if(success == FALSE)
{
error = GetLastError();
if(error != ERROR_NO_MORE_ITEMS)
{
//OOPS();
}
}
else
{
BOOL bResult;
bResult = GetDeviceProperty(deviceInfo,
&infoData,
SPDRP_DEVICEDESC,
&DeviceDescName);
if(bResult == FALSE)
{
//OOPS();
break;
}
printf("DeviceDescName = %s\n", DeviceDescName);
}
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
EnumerateAllDevices();
return 0;
}
DeviceDescName是PSTR你可以從代碼中看到。出於某種原因,這個工程完美一個項目類型的Windows驅動程序的應用程序。在這裏,我看到完整的設備名稱DeviceDescName就像「高清音頻控制器」。
相同的代碼是不工作的其他控制檯應用程序。工具集v120。在DeviceDescName中,我只看到第一個後面的「H」(來自「High Definition Audio Controller」)。可能是什麼原因? (你可以試試這個)
我認爲這是關於不同的字符編碼(ascii,ansi,unicode,...) –