Search code examples
c#pinvokeguidunsafe

pInvoke method for GUID in C#


I am new to C# and USB programming. I am having several issues with it. I am not able to communicate with it.Here is how I am declaring GUID to get GUID. I am creating a new instance of GUID and then calling HidGuid to get Guid.Below are pInvoke methods for it.But how to verify GUID I am getting is valid/correct.Is there any way to convert this structure in to a string. If so, how. Let me know. Thanks.

private GUID MYguid = new GUID();
HidGuid();

public unsafe void CT_HidGuid()
// 1.Data1 = 1293833650
//Data2 = 61807
//Data3 = 4559
//data4 = 136 203 0 17 17 0 0 48
// hex values  4d1e55b2 f16f 11cf 7be03c74ac80
{                              
    HidD_GetHidGuid(ref MYguid);  
}
[DllImport("hid.dll", SetLastError = true)]
static extern unsafe void HidD_GetHidGuid(ref GUID lpHidGuid);

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

Solution

  • Do you really need to use PInvoke to generate a GUID? Why no using Guid.NewGuid() method from System namespace?

    http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx