Managed GUID to unsafe win32 GUID struct


P

Pål A.

I need to convert a managed GUID (or GUID in string format) into an
unsafe win32 GUID struct. Any ideas?

How to convert this:

string DEVICECLASS = "{a5dcbf10-6530-11d2-901f-00c04fb951ed}";

into an instance of this:

[StructLayout(LayoutKind.Sequential)]
public unsafe struct GUID
{
public int Data1;
public System.UInt16 Data2;
public System.UInt16 Data3;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] data4;
}

Thanks
 
Ad

Advertisements

W

Willy Denoyette [MVP]

Pål A. said:
I need to convert a managed GUID (or GUID in string format) into an
unsafe win32 GUID struct. Any ideas?

How to convert this:

string DEVICECLASS = "{a5dcbf10-6530-11d2-901f-00c04fb951ed}";

into an instance of this:

[StructLayout(LayoutKind.Sequential)]
public unsafe struct GUID
{
public int Data1;
public System.UInt16 Data2;
public System.UInt16 Data3;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] data4;
}

Thanks


System.Guid *is* a structure, you can pass it *as is* to unmanaged code, no
need to declare your own.

[DllImport(".......")]
public static extern void PassGuidToUnmanaged(Guid g);
...
string DEVICECLASS = "{a5dcbf10-6530-11d2-901f-00c04fb951ed}";
Guid g = new Guid(DEVICECLASS);
PassGuidToUnmanaged(g);

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top