将字符串强制转换为GUID不会给出正确的结果

时间:2018-07-26 19:52:12

标签: c++ visual-studio boost boost-propertytree

在我的程序中,我需要读取存储在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-CCCCCCCCCCCCCCC)。我在这里错什么?

1 个答案:

答案 0 :(得分:2)

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被定义为非常量 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);

但是,您可能必须在大括号中添加大括号:

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

否则,只需手动解析Guid字符串,例如使用std::istringstreamstd::regexstd::sscanf()