在 C++ 中填充 GUID 时出错

Error while filling a GUID in C++

我正在尝试从 std::string.

中用 C++ 填充 GUID 结构 (https://msdn.microsoft.com/fr-fr/library/windows/desktop/aa373931(v=vs.85).aspx)

我正在尝试使用以下字符串 作为示例(该字符串将在实际函数中作为参数传递) : "eec5ad98-8080-425f-922a-dabf3de3f69a" 但我仍然得到一个空的 GUID。

我在 MSDN 论坛上看到函数 CLSIDFromString 可用于此目的:https://social.msdn.microsoft.com/Forums/en-US/58fbd857-edef-4e77-8355-c426523cf06f/how-to-convert-cstring-to-guid-?forum=vcmfcatl

代码:

int main()
{
    std::string sGuid = "eec5ad98-8080-425f-922a-dabf3de3f69a";
    fromStringToGuid(sGuid);
}

GUID fromStringToGuid(std::string sGuid)
{
    GUID guid;
    LPCOLESTR lpcGuid = (LPWSTR) sGuid.c_str();

    std::cout << "LpcGuid : " << lpcGuid << std::endl;
    std::cout << "String GUID : " << sGuid << std::endl;

    CLSIDFromString(lpcGuid, &guid);

    std::cout << "GUID : " << std::hex << guid.Data1 << " " << guid.Data2 << " " << guid.Data3 << " " << guid.Data4;

    return guid;
}

结果:

LpcGuid : 0428ABC0
String GUID : eec5ad98-8080-425f-922a-dabf3de3f69a
GUID : 0 0 0 i = 0

GUID 的十进制值应为 eec5ad98 而不是 0 例如。

编辑:

试过@JeffDavies 方法:

GUID guid;
const OLECHAR aoleGuid[] = OLESTR("eec5ad98-8080-425f-922a-dabf3de3f69a");

std::cout << "GUID before : " << guid.Data1 << " " << guid.Data2 << " " << guid.Data3 << " " << guid.Data4 << std::endl;
CLSIDFromProgID(aoleGuid, &guid);
std::cout << "GUID after : " << guid.Data1 << " " << guid.Data2 << " " << guid.Data3 << " " << guid.Data4 << std::endl;

我在函数之前添加了 GUID 的打印,但结果相同:

GUID before : 1505448 30982 184 `3À
GUID after : 0 0 0 i = 0

根据此页面,这应该是一种方法:(转换似乎不起作用,所以可能不是正确格式的 Unicode - 我认为它正在寻找 UTF16,但可能是 UTF8)

// substitute in std::string for the literal
const OLECHAR wszGuid[] = OLESTR("eec5ad98-8080-425f-922a-dabf3de3f69a");
CLSIDFromString(wszGuid, &guid);

http://forums.codeguru.com/showthread.php?224120-What-s-the-type-quot-LPCOLESTR-quot

这意味着 char/w_char 是自动切换的,但内存必须以特殊方式分配(因此包装在 OLESTR 中),大概 c_str 没有以特殊方式分配。

What's the meaning of BSTR, LPCOLESTR, and others?

https://msdn.microsoft.com/en-us/library/windows/hardware/ff542998(v=vs.85).aspx

//#include <initguid.h> //application call header
//#include <guiddef.h>  //macro definition
DEFINE_GUID( GUID_BUS_TYPE_PCMCIA, 0x09343630L, 0xaf9f, 0x11d0, 
    0x92,0x9f, 0x00, 0xc0, 0x4f, 0xc3, 0x40, 0xb1 );

因为我找不到任何东西,所以我研究了所有的 GUID 格式,所以我会 post 为下一个和我有同样问题的人做的。

GUID结构由4个变量组成:

  • Data1(无符号长整型)
  • Data2(无符号长整型)
  • Data3(无符号长整型)
  • Data4[8](无符号长数组)

所以要分解格式为“00000000-0000-0000-0000-000000000000”的字符串,您必须将第一个00000000放在Data1中,接下来的0000放在Data2中,接下来的0000放在Data3中,全部放在一个十六进制转十进制。

对于data4,前2个字节对​​应下一个0000,其他都是 000000000000 部分的二乘二数字。

C++代码:

GUID            fromStringToGUID(std::string strguid)
{
    GUID guid;

    int i = 2;
    std::vector<std::string> bytes = split(strguid, "-");
    std::string last = bytes[bytes.size() - 1];

    guid.Data1 = fromStringToHex(bytes[0]);
    guid.Data2 = fromStringToHex(bytes[1]);
    guid.Data3 = fromStringToHex(bytes[2]);

    guid.Data4[0] = fromStringToHex(bytes[3].substr(0, 2));
    guid.Data4[1] = fromStringToHex(bytes[3].substr(2, 2));

    for (int j = 0; j < last.size(); j+=2)
    {
        guid.Data4[i] = fromStringToHex(last.substr(j, 2));
        i++;
    }
    return guid;
}

std::vector<std::string> split(std::string str, std::string sep)
{
    std::vector<std::string> result;
    int from;
    int i;
    int to;

    from = 0;
    to = 0;
    for (i = 0; i < str.size(); i++)
    {
        if (str[i] == sep[0])
        {
            to = i;
            result.push_back(str.substr(from, to - from));
            from = i + 1;
        }
    }
    result.push_back(str.substr(from, to - from));
    return result;
}

unsigned long fromStringToHex(std::string number)
{
    std::stringstream sts;
    unsigned long value;

    sts << number;
    sts >> std::hex >> value;

    return value;
}