早上好/白天/晚上!
我收到了必须从 SCADA 系统(Indusoft Web Studio)检索标签的 dll。它与实际上工作得很好的 VC++ 和 VB 示例一起出现。目前我需要获取这些值并在网络上显示它们(使用 ASP.NET)。我决定使用 C# 来处理从 SCADA 到 HTML 的值(嗯,实际上 Microsoft ASP.NET 指南有点建议这样做)。这就是我卡住的地方,我无法使该功能正常工作。
我为导入的 DLL 创建了类,如下所示:
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);
}
}
不幸的是,当我尝试调用这个函数时,它给了我:
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.
在 C 中完美运行的功能如下所示:
CString CISRWExt::UNReadString(LPCTSTR szTagName)
{
CString result;
static BYTE parms[] =
VTS_BSTR;
InvokeHelper(0x1, DISPATCH_METHOD, VT_BSTR, (void*)&result, parms,
szTagName);
return result;
}
有什么建议么?提前致谢。
上面的信息有点额外。我从 DLL 中导入了另一个函数,现在该类如下所示:
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);
}
}
写入函数(UNWriteString)实际上将 C# 中的值写入 SCADA(我可以在 SCADA 查看器中看到正在更改的值),但是在它正常工作之后我得到另一个错误:
An unhandled exception of type 'System.NullReferenceException' occurred in mscorlib.dll
Additional information: Object reference not set to an instance of an object.