2014-11-03 41 views
1

INTRO輸入字符串不會更新與使用C#中的DllImport

原工作代碼在VB6 ...但是,當我嘗試轉換,並在C#中使用它不起作用

問題

的問題是在這條線在c#

結果= Iso14443Anticoll(HANDLE,147,UID,多標籤,0)

可變Uid不包含(更新)不像VB6它

使用DLL IN VB6

Declare Function OpenCommPort Lib "MR705API.dll" Alias "[email protected]@[email protected]" (ByVal PortName As String, ByRef hCom As Long) As Long 
Declare Function CloseCommPort Lib "MR705API.dll" Alias "[email protected]@[email protected]" (ByVal hCom As Long) As Long 
Declare Function SetLED Lib "MR705API.dll" Alias "[email protected]@[email protected]" (ByVal hCom As Long, ByVal Led As Byte, ByVal Addr As Byte) As Long 
Declare Function ActiveBuzzer Lib "MR705API.dll" Alias "[email protected]@[email protected]" (ByVal hCom As Long, ByVal DelayTime As Byte, ByVal Addr As Byte) As Long 
Declare Function Iso14443Reqa Lib "MR705API.dll" Alias "[email protected]@[email protected]" (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 "[email protected]@[email protected]" (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 
任何值

使用DLL在C#

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

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

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

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

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

[DllImport ("MR705API.dll", 
EntryPoint="[email protected]@[email protected]"] 
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. 在VB6相當於INT INT C#長
  2. 入口點需要設置防止System.EntryPointNotFoundException
  3. 的DLL函數
  4. 輸入錯誤簽名可能會導致System.AccessViolationException
+0

爲什麼是'FixedLengthString'並不僅僅是'string'? – DavidG 2014-11-03 01:11:56

+0

@DavidG實際上,我不知道現在我的問題是什麼原因,所以我嘗試使我的代碼幾乎從vb6的舊版本開始,因此我使用它的原因 – 2014-11-03 01:14:47

+0

但是您不調用VB6編寫的DLL,所以爲什麼參數類型使用它? – DavidG 2014-11-03 01:16:02

回答

1

特別感謝到...

OKAY ...首先我要感謝@DavidG爲我提供指導

回答我自己的問題...

由於DllImport函數簽名不正確,輸入字符串未更新。

我已經在這個LINK

該工具提供的名稱和在不可控制的各功能使用簽名依賴沃克提起鑑定我的DLL。dll的

enter image description here

所以,我必須改變一些功能的簽名。

FOR EXAMPLE

Microsoft.VisualBasic.Compatibility.VB6.FixedLengthString 字節[]

字節[]

字節字節

INT

這裏是更新應答..

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

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

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

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

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

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