Good morning/day/evening!
I have recieved dll which has to retrieve Tags from SCADA system (Indusoft Web Studio). It came alongside with VC++ and VB samples that actually work perfectly fine. Currently i need to get those values and show them on web (using ASP.NET). I decided to use C# for values processing from SCADA to HTML (well, actually Microsoft ASP.NET guides kind of adviced to do it). And this is where i got stuck, i cannot make the function work.
I created class for the imported DLL, here what it looks like:
using System.Runtime.InteropServices;
namespace TagAccess
{
public class ISRW
{
[DllImport("C:\\Windows\\SysWOW64\\ISRWExtDLL.dll", CharSet = CharSet.Auto )]
public static extern string UNReadString([MarshalAs(UnmanagedType.BStr)] string szTagName);
}
}
Unfortunatelly, when i try to call this function it gives me:
Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\Users\Denis\Documents\Visual Studio 2013\Projects\TagAccess\TagAccess\bin\Debug\TagAccess.vshost.exe'.
Additional information: A call to PInvoke function 'TagAccess!TagAccess.ISRW::UNReadString' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the
calling convention and parameters of the PInvoke signature match the target unmanaged signature.
What works perfectly fine in C looks like:
CString CISRWExt::UNReadString(LPCTSTR szTagName)
{
CString result;
static BYTE parms[] =
VTS_BSTR;
InvokeHelper(0x1, DISPATCH_METHOD, VT_BSTR, (void*)&result, parms,
szTagName);
return result;
}
Any suggestions? Thanks in advance.
A little extra for the info above. I have imported another function from DLL, now the class looks like this:
namespace TagAccess
{
public class ISRW
{
[DllImport("C:\\Windows\\SysWOW64\\ISRWExtDLL.dll", EntryPoint = "#1", CharSet = CharSet.Unicode, SetLastError = true, CallingConvention = CallingConvention.Winapi, ThrowOnUnmappableChar = true )]
public static extern string UNReadString([MarshalAs(UnmanagedType.BStr)] string szTagName);
[DllImport("C:\\Windows\\SysWOW64\\ISRWExtDLL.dll", CharSet = CharSet.Auto)]
public static extern string UNWriteString([MarshalAs(UnmanagedType.BStr)] string szTagName, [MarshalAs(UnmanagedType.BStr)] string szValue);
}
}
Write Function (UNWriteString) actually writes values from C# into SCADA (i can see values being changed in SCADA viewer), but right after it works fine i get another error:
An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll
Additional information: Object reference not set to an instance of an object.