OPCSERVERSTATUS in OpcRCW.da.dll|Classic OPC: DA, A&E, HDA, XML-DA, etc.|Forum|OPC Foundation

Avatar
Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
Lost password?
sp_Feed sp_PrintTopic sp_TopicIcon
OPCSERVERSTATUS in OpcRCW.da.dll
Avatar
Nikolay Starovoitov
New Member
Members
Forum Posts: 2
Member Since:
03/08/2021
sp_UserOfflineSmall Offline
1
10/05/2021 - 20:52
sp_Permalink sp_Print sp_EditHistory

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. 

Avatar
Randy Armstrong
Admin
Forum Posts: 1445
Member Since:
05/30/2017
sp_UserOfflineSmall Offline
2
10/06/2021 - 16:42
sp_Permalink sp_Print

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?

Avatar
Nikolay Starovoitov
New Member
Members
Forum Posts: 2
Member Since:
03/08/2021
sp_UserOfflineSmall Offline
3
10/06/2021 - 21:10
sp_Permalink sp_Print

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.

Avatar
Randy Armstrong
Admin
Forum Posts: 1445
Member Since:
05/30/2017
sp_UserOfflineSmall Offline
4
10/07/2021 - 08:52
sp_Permalink sp_Print sp_EditHistory

opcRcw.da.dll is .NET 2.0 x86 ONLY as a consequence of how it was created.

OPCComRCW.dll was introduced as a MSIL compatible replacement that supports x64

You should not use opcRcw.da.dll

Forum Timezone: America/Phoenix
Most Users Ever Online: 510
Currently Online:
Guest(s) 17
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Forum Stats:
Groups: 2
Forums: 10
Topics: 1347
Posts: 4567