首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将HLOCAL转换为LPTSTR

将HLOCAL转换为LPTSTR
EN

Stack Overflow用户
提问于 2010-10-27 04:52:25
回答 2查看 2.4K关注 0票数 2

如何将HLOCAL数据类型转换为LPTSTR数据类型?我正在尝试从Microsoft working获取代码片段,这是唯一的错误,我不确定如何解决:

代码语言:javascript
复制
// 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 NULL;
}

// 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;

    //
    // Call function with null to begin with, 
    // then use the returned buffer size (doubled)
    // to Alloc the buffer. Keep calling until
    // success or an unknown failure.
    //
    //  Double the returned buffersize to correct
    //  for underlying legacy CM functions that 
    //  return an incorrect buffersize value on 
    //  DBCS/MBCS systems.
    // 
    while (!SetupDiGetDeviceRegistryProperty(
        hDevInfo,
        &DeviceInfoData,
        SPDRP_DEVICEDESC,
        &DataT,
        (PBYTE)buffer,
        buffersize,
        &buffersize))
    {
        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            // Change the buffer size.
            if (buffer) LocalFree(buffer);
            // Double the size to avoid problems on 
            // W2k MBCS systems per KB 888609. 
            buffer = LocalAlloc(LPTR,buffersize * 2); // <- Error Occurs Here
        }
        else
        {
            // Insert error handling here.
            break;
        }
    }

    printf("Result:[%s]\n",buffer);

    if (buffer) LocalFree(buffer);
}


if ( GetLastError()!=NO_ERROR && GetLastError()!=ERROR_NO_MORE_ITEMS )
{
    // Insert error handling here.
    return NULL;
}

//  Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);

欢迎大家提出意见。谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-10-27 05:08:24

LocalLock()返回指针。但这是18岁的愚蠢,只需使用

代码语言:javascript
复制
        // Change the buffer size.
        delete buffer;
        // Double the size to avoid problems on 
        // W2k MBCS systems per KB 888609. 
        buffer = new TCHAR[buffersize * 2];

暂时忽略仍在使用TCHAR的愚蠢之处。您需要修改printf()语句,这取决于您是否使用Unicode进行编译。%ls。我猜这就是您真正的问题所在,使用wprintf()。

票数 5
EN

Stack Overflow用户

发布于 2010-10-27 11:23:58

代码语言:javascript
复制
  buffer = (LPTSTR)LocalAlloc(LPTR, buffersize * 2);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4027917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档