开发者

Converting an unmanaged GUID to a managed Guid^

I'm brand new to C++/CLI and I am attempting to convert a native C++ GUID to my C++/CLI Guid^. When attempting my conversion:

BlockInfo^ blockInfo = gcnew BlockInfo();
blockInfo->BlockFilterGuid = ba.BlockAllFilter.subLayerKey;

...I recieve the following error:

error C2440: '=' : cannot convert from 'GUID' to 'System::Guid ^'

I understand that the root source of my problem is that am trying to convert from an unmanaged to a managed type, but I'm开发者_Python百科 not proficent enough in either C++ or C++/CLI to know how to solve the issue.


A native GUID is defined:

typedef struct _GUID {
    DWORD Data1;
    WORD  Data2;
    WORD  Data3;
    BYTE  Data4[8];
} GUID;

You need to allocate a System::Guid and construct it properly using the data in the native GUID.

System::Guid ^FromNativeGUID(const GUID &g)
{
    return gcnew System::Guid(g.Data1, g.Data2, g.Data3, g.Data4[0], g.Data4[1], g.Data4[2],
                        g.Data4[3], g.Data4[4], g.Data4[5], g.Data4[6], g.Data4[7]);
}


The previous answer is fine, but this really should be enough:

Guid FromNativeGUID(const GUID &guid)
{
    return *reinterpret_cast<Guid *>(const_cast<GUID *>(&guid));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜