Initialise a GUID variable: How?

2020-07-10 05:30发布

I am attempting to initialise a GUID variable but I not sure this is how you are meant to do it. What I am especially confused about is how to store the last 12 hexadecimal digits in the char array(do I include the "-" character?)

How do I define/initialise a GUID variable?

bool TVManager::isMonitorDevice(GUID id)
{
    // Class GUID for a Monitor is: {4d36e96e-e325-11ce-bfc1-08002be10318}

    GUID monitorClassGuid;
    char* a                = "bfc1-08002be10318"; // do I store the "-" character?
    monitorClassGuid.Data1 = 0x4d36e96e;
    monitorClassGuid.Data2 = 0xe325;
    monitorClassGuid.Data3 = 0x11ce;
    monitorClassGuid.Data4 = a;

    return (bool(id == monitorClassGuid));
}

标签: c++ winapi
2条回答
Rolldiameter
2楼-- · 2020-07-10 06:17

This question was asked long time ago, but maybe it helps somebody else.

You can use this code to initialize a GUID:

#include <combaseapi.h>;

GUID guid;
CLSIDFromString(L"{4d36e96e-e325-11ce-bfc1-08002be10318}", &guid);
查看更多
ら.Afraid
3楼-- · 2020-07-10 06:30

The Data4 member is not a pointer, it's an array. You'd want:

monitorClassGuid.Data4 = { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 };

To make your example work. You might find it easier to do all of the initialization along with the definition of your monitorClassGuid variable:

GUID monitorClassGuid = { 0x4d36e96e, 0xe325, 0x11c3, { 0xbf, 0xc1, 0x08, 0x00, 0x2b, 0xe1, 0x03, 0x18 } };
查看更多
登录 后发表回答