首页 > 解决方案 > 将字符串转换为 GUID 不会给出正确的结果

问题描述

在我的程序中,我需要读取存储在 xml 文件中的 guid 值。这是xml文件的样子。

<data>
 <id>3AAAAAAA-BBBB-CCCC-DDDD-2EEEEEEEEEEE</id>
</data>

我的程序需要在 GUID 类型变量中读取此值。以下是我对此的看法。

#include "stdafx.h"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <string>
#include <iostream>
#include <Windows.h>
namespace pt = boost::property_tree;
#pragma comment(lib, "rpcrt4.lib") 
int main()
{
    pt::ptree tree;
    std::string filename = "data.xml";

    pt::read_xml(filename, tree);

    std::string idStr = tree.get<std::string>("data.id");
    std::cout << "id as string = " << idStr << std::endl;
    GUID idAsGuid;

    auto res = UuidFromStringW((RPC_WSTR)idStr.c_str(), &idAsGuid);
    if (FAILED(res))
    {
        std::wcerr << L"Conversion failed with error: 0x" << std::hex << res << std::endl;
    }

   return 0;
}

变量idStr获得正确的值,但idAsGuid变量(即 GUID 类型)获得不正确的值(类似于 CCCCC-CCCC-CCCC-CCCCCCCCCCCCC)。我在这里错了什么?

标签: c++visual-studioboostboost-propertytree

解决方案


std::string::c_str()返回一个const char*指针,您将其类型转换为RPC_WSTR,也就是non-const unsigned short*。那个演员永远不会奏效。至少,您需要先将其转换std::string为 UTF-16 编码std::wstring,例如:

#include <locale>
#include <codecvt>

std::wstring widStr = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>{}.from_bytes(idStr);

auto res = UuidFromStringW(reinterpret_cast<RPC_WSTR>(const_cast<wchar_t*>(widStr.c_str())), &idAsGuid);
// or:
// auto res = UuidFromStringW(reinterpret_cast<RPC_WSTR>(&widStr[0]), &idAsGuid);

否则,UuidFromStringA()请改用,但请注意它RPC_CSTR被定义为非 const unsigned char*,因此您仍然需要类似的强制转换:

auto res = UuidFromStringA(reinterpret_cast<RPC_CSTR>(const_cast<char*>(idStr.c_str())), &idAsGuid);
// or:
// auto res = UuidFromStringA(reinterpret_cast<RPC_CSTR>(&idStr[0]), &idAsGuid);

话虽如此,请考虑使用GUIDFromStringA()而不需要任何转换或强制转换:

auto res = GUIDFromStringA(idStr.c_str(), &idAsGuid);

不过,您可能必须在 guid 字符串中添加大括号:

auto res = GUIDFromStringA(("{" + idStr + "}").c_str(), &idAsGuid);

否则,只需手动解析 guid 字符串,例如 with std::istringstream, std::regex,std::sscanf()等。


推荐阅读