03/08/2021
Different order of fields in the OPCSERVERSTATUS structure.
in opcda.h
typedef struct tagOPCSERVERSTATUS
{
FILETIME ftStartTime;
FILETIME ftCurrentTime;
FILETIME ftLastUpdateTime;
OPCSERVERSTATE dwServerState;
DWORD dwGroupCount;
DWORD dwBandWidth;
WORD wMajorVersion;
WORD wMinorVersion;
WORD wBuildNumber;
WORD wReserved;
/* [string] */ LPWSTR szVendorInfo;
} OPCSERVERSTATUS;
in opcRcw.da.dll and OPCComRCW.dll
public struct OPCSERVERSTATUS
{
public int dwBandWidth;
public int dwGroupCount;
public OPCSERVERSTATE dwServerState;
public FILETIME ftCurrentTime;
public FILETIME ftLastUpdateTime;
public FILETIME ftStartTime;
public string szVendorInfo;
public short wBuildNumber;
public short wMajorVersion;
public short wMinorVersion;
public short wReserved;
}
these differences lead to an error when marshaling
OpcRcw.Da.OPCSERVERSTATUS pStatus = (OpcRcw.Da.OPCSERVERSTATUS)Marshal.PtrToStructure(ppServerStatus, typeof(OpcRcw.Da.OPCSERVERSTATUS));
perhaps this is due to the build for x64 architectures.
This is magic. This work in x86 build.
05/30/2017
OK – this seems like a major bug that should have been discovered years ago.
When I look at the source for OpcComRcw I see:
/// <exclude /> [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct OPCSERVERSTATUS { public FILETIME ftStartTime; public FILETIME ftCurrentTime; public FILETIME ftLastUpdateTime; public OPCSERVERSTATE dwServerState; [MarshalAs(UnmanagedType.I4)] public int dwGroupCount; [MarshalAs(UnmanagedType.I4)] public int dwBandWidth; [MarshalAs(UnmanagedType.I2)] public short wMajorVersion; [MarshalAs(UnmanagedType.I2)] public short wMinorVersion; [MarshalAs(UnmanagedType.I2)] public short wBuildNumber; [MarshalAs(UnmanagedType.I2)] public short wReserved; [MarshalAs(UnmanagedType.LPWStr)] public string szVendorInfo; }
The structure you posted appears to have its fields sorted in alphabetical order which is how ildasm presents it. It is not clear why ildasm changes the order of the fields but COM interop would be non-functional if the underlying code did not preserve the correct order.
Where does ppServerStatus come from?
03/08/2021
The problem was in using opcrcw.da.dll . Usage opcComRcw.dll solves the problem. Although Visual Studio shows that the structures are identically described. ILSpy shows the cause of the alignment problem in a 4-byte software structure. In opcComRcw.dll the correct alignment is 8 bytes.
1 Guest(s)