我试图为C#创建一个包装器modbusm.dll文件(win. for /html/mbusocx.htm),为此我使用了dumpbin输出。
转储文件modbusm.dll
文件类型: DLL 节包含用于modbusm.dll的下列导出 00000000特征41128817时间日期邮票,星期五2006年8月00:48:47 2004 0.00版本1序号27函数号27名称序号暗示RVA名称1 000085BA _AbortTheCall@4 2 00003441 _CloseConnection@4 3 000033A7 _ConnectASCII@12 4 000033E1 _ConnectDanielsASCII@12 5 000033C4 _ConnectDanielsRTU@12 6 0000338A _ConnectRTU@12 7 0000001019_ConnectTCP2@12 8 7 00001000 _ConnectTCP@8 9 0000829A _DialCall@8 10 9 00003376 _EnableConnectionCallback@4 11 A 00003342 _EnableModbusCallback@8 12 B 00008123 _GetCallState@8 13 C 00007FD2 _GetLineDeviceName@12 14 D 003320 _GetPollDelay@0 15 E 003339 _Get_Modbus_DLL_Revision@0 16 F 000033 F _HookRspNotification@16 17 000032 E _InitializeWinSock@0 18 11 0000277C _MBAPWndProc@16 19 12 00393F _MODBUSResponse@16 20 13 00007EAA _NumberOfLineDevices@0 21 14 00003521 _PollMODBUS@8 22 15 0039F2 _ReadDebugData@16 23 16 003BA4 _ReadTransparentResponse@16 24 17 00332A _SetPollDelay@4 25 18 00003313 _UnInitializeWinSock@0 26 19 003712 _WriteMODBUS@12 27 1A 00003AB3 _WriteTransparentString@12 摘要 5000 .data 2000 .rdata 2000 .reloc 1000 .rsrc C000 .text
我的C#包装是
class MbMasterV7
{
[DllImport("modbusm.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode, EntryPoint = "_ConnectTCP2@12")]
public static extern int ConnectModbusTCP(short Port);
public static string TCPDevice { set; get; }
}当我运行代码时
MbMasterV7.TCPDevice = "127.0.0.1"; // from demo version of .ocx file converted using tlbimp.exe
MbMasterV7.ConnectModbusTCP(502);在视频演播室里,我遇到了异常
对'TestApp!TestApp.MbMasterV7::ConnectModbusTCP‘函数PInvoke的调用使堆栈不平衡。这可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标非托管签名匹配。
我已经尝试了所有的调用约定,并得到了相同的错误。.Net库是可用的,因为modbus协议对于我正在使用的新plc类型来说还不够好。
FILE HEADER VALUES
14C machine (x86)
5 number of sections
41128817 time date stamp Fri Aug 06 00:48:47 2004
0 file pointer to symbol table
0 number of symbols
E0 size of optional header
210E characteristics
Executable
Line numbers stripped
Symbols stripped
32 bit word machine
DLL
OPTIONAL HEADER VALUES
10B magic # (PE32)
6.00 linker version
C000 size of code
A000 size of initialized data
0 size of uninitialized data
94E4 entry point (100094E4)
1000 base of code
D000 base of data
10000000 image base (10000000 to 10016FFF)
1000 section alignment
1000 file alignment
4.00 operating system version
0.00 image version
4.00 subsystem version
0 Win32 version
17000 size of image
1000 size of headers
0 checksum
2 subsystem (Windows GUI)
0 DLL characteristics
100000 size of stack reserve
1000 size of stack commit
100000 size of heap reserve
1000 size of heap commit
0 loader flags
10 number of directories
DF90 [ 357] RVA [size] of Export Directory
D7C8 [ 50] RVA [size] of Import Directory
14000 [ 3E8] RVA [size] of Resource Directory
0 [ 0] RVA [size] of Exception Directory
0 [ 0] RVA [size] of Certificates Directory
15000 [ BC0] RVA [size] of Base Relocation Directory
0 [ 0] RVA [size] of Debug Directory
0 [ 0] RVA [size] of Architecture Directory
0 [ 0] RVA [size] of Global Pointer Directory
0 [ 0] RVA [size] of Thread Storage Directory
0 [ 0] RVA [size] of Load Configuration Directory
0 [ 0] RVA [size] of Bound Import Directory
D000 [ 198] RVA [size] of Import Address Table Directory
0 [ 0] RVA [size] of Delay Import Directory
0 [ 0] RVA [size] of COM Descriptor Directory
0 [ 0] RVA [size] of Reserved Directory发布于 2015-05-08 19:30:44
首先,_ConnectTCP2@12意味着将12个字节作为参数传递给函数,这意味着short (2字节长)显然是不兼容的。您需要将12个字节作为参数传递,大概以3个DWORD的形式传递。
为了冒险,让我们拆解二进制,看看那里发生了什么。

因此:ConnectTCP@8接收两个DWORD作为参数,调用以0x1F6作为第二个参数的ConnectTCP2 (实际上是一个短参数)。另外,调用约定是stdcall。
这些信息足以让我们弄清楚如何调用该函数:
[DllImport("modbusm.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "_ConnectTCP2@12")]
public static extern int ConnectModbusTCP(int a, short Port, int b);会起作用,但会扔
"Unhandled Exception: System.AccessViolationException:
Attempted to read or write protected memory.
This is often an indication that other memory is corrupt."这是因为第二个整数(我称之为b)实际上是一个指向结构的指针(它的值我只能根据代码猜测)。所以让我们重建这个结构。根据该代码,该结构可按以下方式访问五次:

Offset Type
0x00 -> INT32
0x04 -> INT32
0x08 -> INT32
0x0C -> INT16
0x10 -> INT32因此,通过创建以下结构:
struct MbMasterStruct
{
int a;
int b;
int c;
short d;
int e;
}并重新定义职能如下:
unsafe class MbMasterV7
{
[DllImport("modbusm.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "_ConnectTCP2@12")]
public static extern int ConnectModbusTCP(int a, short Port, MbMasterStruct * b);
}并称其为:
static void Main(string[] args)
{
var structure = new MbMasterStruct();
unsafe
{
MbMasterV7.ConnectModbusTCP(1, 2, &structure);
}
}它实际上很有效,而且它不会扔。在我的计算机上,它返回51 (当结构和参数都为零时)。
现在由您来理解每个参数并查看如何正确调用该函数。
https://stackoverflow.com/questions/30128753
复制相似问题