I am wondering if there is a cleaner way to write the (working) code below:
uint uEnum = 0;
PStore.EnumTypes(0, 0, ref uEnum);
System.Reflection.MemberInfo inf = typeof(PSTORECLib.CEnumTypes);
GuidAttribute CEnumGuid =
(GuidAttribute)inf.GetCustomAttributes(typeof(GuidAttribute), false)[0];
Guid tmp = new Guid(CEnumGuid.Value);
IntPtr ppv;
Marshal.QueryInterface((IntPtr)uEnum, ref tmp, out ppv);
PSTORECLib.CEnumTypes EnumPStoreTypes =
(PSTORECLib.CEnumTypes)Marshal.GetObjectForIUnknown(ppv);
//Later
Marshal.Release(ppv);
When I tried stuffing IEnumPStoreTypes**
in the idl file of the PSTORECLib (i.e. when I used the initial IDL output by oleview) into the call to PStore.EnumTypes
, the dll output by tlbimp told me to pass in a reference to a CEnumTypes
. The function was happy with this (it returned S_OK), but it didn't populate the reference. This mess of ugly code is what happened when I changed it to accept a pointer to a long instead, and is what I did to get an instance of CEnumTypes to refer to the pointer. This whole thing strikes me as being a bit messy, though it does work. Is there a cleaner way to do this?
Note that the line PSTORECLib.CEnumTypes cen = new PSTORECLib.CEnumTypes();
will throw a "Class Not Registered" COMException.