在c#中使用dllImport未更新输入字符串

时间:2014-11-03 00:47:55

标签: c# vb6 dllimport

INTRO

原始的工作代码是在VB6中......但是,当我尝试转换并在c#中使用时,它不起作用

问题

问题出现在c#

的这一行
  

结果= Iso14443Anticoll(HANDLE,147,Uid,MultiTag,0)

变量Uid不包含(更新)任何值,不像VB6那样

在VB6中使用DLL

Declare Function OpenCommPort Lib "MR705API.dll" Alias "?OpenCommPort@@YGHPADPAPAX@Z" (ByVal PortName As String, ByRef hCom As Long) As Long
Declare Function CloseCommPort Lib "MR705API.dll" Alias "?CloseCommPort@@YGHPAX@Z" (ByVal hCom As Long) As Long
Declare Function SetLED Lib "MR705API.dll" Alias "?SetLED@@YGHPAXEE@Z" (ByVal hCom As Long, ByVal Led As Byte, ByVal Addr As Byte) As Long
Declare Function ActiveBuzzer Lib "MR705API.dll" Alias "?ActiveBuzzer@@YGHPAXEE@Z" (ByVal hCom As Long, ByVal DelayTime As Byte, ByVal Addr As Byte) As Long
Declare Function Iso14443Reqa Lib "MR705API.dll" Alias "?Iso14443Reqa@@YGHPAXEPAEE@Z" (ByVal hCom As Long, ByVal ReqAMode As Byte, ByVal ATQ As String, ByVal Addr As Byte) As Long
Declare Function Iso14443Anticoll Lib "MR705API.dll" Alias "?Iso14443Anticoll@@YGHPAXEPAE1E@Z" (ByVal hCom As Long, ByVal AnticollMode As Byte, ByVal Uid As String, ByVal MultiTag As String, ByVal Addr As Byte) As Long

VB6中的功能调用

    Dim HANDLE As Long
    Dim Result As Long
    Dim ATQ As String * 10
    Dim Uid As String * 10
    Dim MultiTag As String * 10
    Dim ID As String
    Dim Temp As String

    Result = OpenCommPort(COMPORT, HANDLE)
    If Result = 0 Then
        Result = Iso14443Reqa(HANDLE, 82, ATQ, 0)
        If Result = 0 Then
            Result = SetLED(HANDLE, 1, 0)
            Result = Iso14443Anticoll(HANDLE, 147, Uid, MultiTag, 0)
            MsgBox Uid //there is value contained after input in Iso14443Anticoll()
            ...
            ...
    End Sub

在C#中使用DLL

[DllImport ("MR705API.dll",
EntryPoint="?OpenCommPort@@YGHPADPAPAX@Z")]
public static extern int OpenCommPort(string portName, ref int hCom); 

[DllImport ("MR705API.dll",
EntryPoint="?CloseCommPort@@YGHPAX@Z")]
public static extern int CloseCommPort(int hCom);

[DllImport ("MR705API.dll",
EntryPoint="?SetLED@@YGHPAXEE@Z")]
public static extern int SetLED(int hCom, Byte Led , Byte Addr);

[DllImport ("MR705API.dll",
EntryPoint="?ActiveBuzzer@@YGHPAXEE@Z")]
public static extern int ActiveBuzzer (int hcom, Byte DelayTime, Byte Addr);

[DllImport ("MR705API.dll",
EntryPoint="?Iso14443Reqa@@YGHPAXEPAEE@Z")]
public static extern int Iso14443Reqa (int hcom, Byte ReqAMode, Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString ATQ, Byte Addr);

[DllImport ("MR705API.dll",
EntryPoint="?Iso14443Anticoll@@YGHPAXEPAE1E@Z"]
public static extern int Iso14443Anticoll (int hcom, Byte AnticollMode, Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString Uid, Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString MultiTag, Byte Addr);

C#中的功能调用

    int HANDLE = 0;
    int Result = 0;
    Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString Uid = new FixedLengthString(10);
    Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString ATQ = new FixedLengthString(10);
    Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString MultiTag = new FixedLengthString(10);
    string ID;
    string Temp;
    string IDNow;

    Result = OpenCommPort("COM9", ref HANDLE);              
    if (Result == 0) {
        Result = Iso14443Reqa(HANDLE, 82, ATQ, 0);
        if (Result == 0) {
            Result = SetLED(HANDLE, 1, 0);
            Result = Iso14443Anticoll(HANDLE, 147, Uid, MultiTag, 0);
            Debug.WriteLine("Uid :: " + Uid);
            //There is no any update value just contained length = 10
            ....
            ....

其他

我已经搜索了一些信息,从VB6转换为C# ,这些问题已经解决

  1. long in vb6 equivalent int int c#
  2. 需要将入口点设置为阻止System.EntryPointNotFoundException
  3. dll函数的错误输入签名可能会导致System.AccessViolationException

1 个答案:

答案 0 :(得分:1)

特别感谢..

好的......首先,我要感谢@DavidG指导我

回答我自己的问题......

由于DllImport函数签名不正确,输入字符串不会更新。

我在这个LINK

中使用了Dependencies Walker来识别我的DLL
  

该工具提供了无法管理的.dll

中每个函数的名称和签名

enter image description here

所以,我改变了一些函数的签名。

例如

  

Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString to byte []

     

字符串 byte []

     

字节字节

     

int

这里有更新答案

        [DllImport ("MR705API.dll",
        EntryPoint="?OpenCommPort@@YGHPADPAPAX@Z")]
        public static extern int OpenCommPort(string portName, ref IntPtr hCom); 

        [DllImport ("MR705API.dll",
        EntryPoint="?CloseCommPort@@YGHPAX@Z")]
        public static extern int CloseCommPort(IntPtr hCom);

        [DllImport ("MR705API.dll",
        EntryPoint="?SetLED@@YGHPAXEE@Z")]
        public static extern int SetLED(IntPtr hCom, byte Led , byte Addr);

        [DllImport ("MR705API.dll",
        EntryPoint="?ActiveBuzzer@@YGHPAXEE@Z")]
        public static extern int ActiveBuzzer (IntPtr hcom, byte DelayTime, byte Addr);

        [DllImport ("MR705API.dll",
        EntryPoint="?Iso14443Reqa@@YGHPAXEPAEE@Z")]
        public static extern int Iso14443Reqa (IntPtr hcom, byte ReqAMode, byte[] ATQ, byte Addr);

        [DllImport ("MR705API.dll",
        EntryPoint="?Iso14443Anticoll@@YGHPAXEPAE1E@Z")]
        public static extern int Iso14443Anticoll (IntPtr hcom, byte AnticollMode,[InAttribute] byte[] Uid, byte[] MultiTag, byte Addr);