Как получить из COM сервера структуру по двойной ссылке
Код:
virtual HRESULT STDMETHODCALLTYPE GetInfo(/* [out] */ INFO **ppInfo) = 0;
Код:
typedef /* [public][public][public] */
enum __MIDL___MIDL_itf_hae_0000_0001
{ UP = 1,
DOWN = 2
} STATUS;
typedef /* [public][public] */ struct __MIDL___MIDL_itf_hae_0000_0003
{
FILETIME ftTime_1;
FILETIME ftTime_2;
STATUS Status;
DWORD dwReturnRecords;
WORD wCount;
WORD wCount2;
WORD wCount3;
/* [string] */ LPWSTR szStatusString;
/* [string] */ LPWSTR szInfo;
} INFO;
enum __MIDL___MIDL_itf_hae_0000_0001
{ UP = 1,
DOWN = 2
} STATUS;
typedef /* [public][public] */ struct __MIDL___MIDL_itf_hae_0000_0003
{
FILETIME ftTime_1;
FILETIME ftTime_2;
STATUS Status;
DWORD dwReturnRecords;
WORD wCount;
WORD wCount2;
WORD wCount3;
/* [string] */ LPWSTR szStatusString;
/* [string] */ LPWSTR szInfo;
} INFO;
Код:
void GetInfo(out IntPtr ppInfo);
Код:
internal enum HAE_SERVER_STATUS
{
HAE_UP = 1,
HAE_DOWN
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct INFO
{
public System.Runtime.InteropServices.ComTypes.FILETIME ftTime_1;
public System.Runtime.InteropServices.ComTypes.FILETIME ftTime_2;
STATUS Status;
[MarshalAs(UnmanagedType.I4)]
public int dwReturnRecords;
[MarshalAs(UnmanagedType.I2)]
public short wCount;
[MarshalAs(UnmanagedType.I2)]
public short wCount2;
[MarshalAs(UnmanagedType.I2)]
public short wCount3;
[MarshalAs(UnmanagedType.LPWStr)]
public string szStatusString;
[MarshalAs(UnmanagedType.LPWStr)]
public string szVendor;
}
{
HAE_UP = 1,
HAE_DOWN
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct INFO
{
public System.Runtime.InteropServices.ComTypes.FILETIME ftTime_1;
public System.Runtime.InteropServices.ComTypes.FILETIME ftTime_2;
STATUS Status;
[MarshalAs(UnmanagedType.I4)]
public int dwReturnRecords;
[MarshalAs(UnmanagedType.I2)]
public short wCount;
[MarshalAs(UnmanagedType.I2)]
public short wCount2;
[MarshalAs(UnmanagedType.I2)]
public short wCount3;
[MarshalAs(UnmanagedType.LPWStr)]
public string szStatusString;
[MarshalAs(UnmanagedType.LPWStr)]
public string szVendor;
}
Код:
INFO pftStatus = new INFO();
IntPtr lpstruct = MarshalToPointer(pftStatus);
IntPtr lppstruct = MarshalToPointer(lpstruct);
server.GetInfo(out lppstruct);
pftStatus = (INFO)MarshalToStruct(lpstruct, typeof(INFO));
IntPtr lpstruct = MarshalToPointer(pftStatus);
IntPtr lppstruct = MarshalToPointer(lpstruct);
server.GetInfo(out lppstruct);
pftStatus = (INFO)MarshalToStruct(lpstruct, typeof(INFO));
Код:
private IntPtr MarshalToPointer(object data)
{
IntPtr buf = Marshal.AllocHGlobal(Marshal.SizeOf(data));
Marshal.StructureToPtr(data, buf, false);
return buf;
}
private object MarshalToStruct(IntPtr buf, Type t)
{
return Marshal.PtrToStructure(buf, t);
}
{
IntPtr buf = Marshal.AllocHGlobal(Marshal.SizeOf(data));
Marshal.StructureToPtr(data, buf, false);
return buf;
}
private object MarshalToStruct(IntPtr buf, Type t)
{
return Marshal.PtrToStructure(buf, t);
}
Код:
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Unicode)]
internal struct INFO
{
public System.Runtime.InteropServices.ComTypes.FILETIME ftTime_1;
public System.Runtime.InteropServices.ComTypes.FILETIME ftTime_2;
public STATUS Status;
[MarshalAs(UnmanagedType.I4)]
public int dwReturnRecords;
[MarshalAs(UnmanagedType.I2)]
public short wCount;
[MarshalAs(UnmanagedType.I2)]
public short wCount2;
[MarshalAs(UnmanagedType.I2)]
public short wCount3;
[MarshalAs(UnmanagedType.LPWStr)]
public string szStatusString;
[MarshalAs(UnmanagedType.LPWStr)]
public string szVendor;
}
internal struct INFO
{
public System.Runtime.InteropServices.ComTypes.FILETIME ftTime_1;
public System.Runtime.InteropServices.ComTypes.FILETIME ftTime_2;
public STATUS Status;
[MarshalAs(UnmanagedType.I4)]
public int dwReturnRecords;
[MarshalAs(UnmanagedType.I2)]
public short wCount;
[MarshalAs(UnmanagedType.I2)]
public short wCount2;
[MarshalAs(UnmanagedType.I2)]
public short wCount3;
[MarshalAs(UnmanagedType.LPWStr)]
public string szStatusString;
[MarshalAs(UnmanagedType.LPWStr)]
public string szVendor;
}
Код:
IntPtr lppStatus;
server.GetInfo(out lppStatus);
INFO pStatus = new INFO();
pStatus = (INFO)Marshal.PtrToStructure(lppStatus, typeof(INFO));
server.GetInfo(out lppStatus);
INFO pStatus = new INFO();
pStatus = (INFO)Marshal.PtrToStructure(lppStatus, typeof(INFO));