问题描述
我在Visual Studio中使用setupapi中的SetupDiGetDeviceRegistryProperty。如果我使用多字符集,则字节指针(BYTE * friendlyName)会提供正确的FRIENDLYNAME。但是,当我使用Unicode字符集编译它时,返回的friendlyName在字节数组中的每个值后面都有一个NULL字符。
这是代码片段:
SetupDiGetDeviceRegistryProperty(hDeviceInfo,& devInfoData,SPDRP_FRIENDLYNAME,nullptr,nullptr,0,& reqSize);
BYTE * friendlyName = new BYTE [ 300]; //(reqSize> 1)? reqSize:1];
if(!SetupDiGetDeviceRegistryProperty(hDeviceInfo,& devInfoData,SPDRP_FRIENDLYNAME,nullptr,friendlyName,sizeof(friendlyName)* reqSize,nullptr)) />
{
//设备没有设置此属性
memset(friendlyName,0,reqSize> 1?reqSize:1);
}
我尝试过:
我尝试将BYTE数组(unsigned char)更改为具有相同结果的字节。如果我尝试将其更改为char,则编译器无法正常工作。
我还将数组设置为设置大小,而不是允许函数定义reqSize因为第一个字符后的NULL。
I am using SetupDiGetDeviceRegistryProperty from setupapi in Visual Studio. If I use the Multicharacter set, the byte pointer (BYTE* friendlyName) gives the correct FRIENDLYNAME. However, when I compile it using the Unicode Character set, the returned friendlyName has a NULL character behind every value in the byte array.
Here's the code snippet:
SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, nullptr, nullptr, 0, &reqSize);
BYTE* friendlyName = new BYTE[300]; //(reqSize > 1) ? reqSize : 1];
if (!SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, SPDRP_FRIENDLYNAME, nullptr, friendlyName, sizeof(friendlyName) * reqSize, nullptr))
{
// device does not have this property set
memset(friendlyName, 0, reqSize > 1 ? reqSize : 1);
}
What I have tried:
I have tried changing the BYTE array (unsigned char) to byte with the same results. If I try to change it to char, the compiler isn't working.
I also set the array to a set size instead of allowing the function to define the reqSize because of the NULL after the first character.
推荐答案
const int BufferSize = 299;
TCHAR friendlyName[BufferSize+1] = { 0 };
if( ! SetupDiGetDeviceRegistryProperty( hDeviceInfo, &devInfoData,
SPDRP_FRIENDLYNAME, nullptr,
friendlyName, BufferSize, nullptr ) )
{
TRACE( _T( "device does not have this property set\n" ) );
}
这篇关于C ++中使用unicode字符集的串行端口友好名称中的字节类型问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!