1

INTRO

The original working code is in VB6... But, when I try to convert and use in c# it doesn't work

PROBLEM

The problem is on this line in c#

Result = Iso14443Anticoll(HANDLE, 147, Uid, MultiTag, 0)

Variable Uid doesn't contain (update) any value unlike VB6 it does

USING DLL IN VB6

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

FUNCTION CALL IN 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

USING DLL IN C#

[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);

FUNCTION CALL IN 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
            ....
            ....

ADDITIONAL

I already search some information to convert from VB6 to C# , These Problem was already resolved

  1. long in vb6 equivalent int int c#
  2. Entry Point need to be set to prevent System.EntryPointNotFoundException
  3. wrong input signature for dll function may cause System.AccessViolationException
Jongz Puangput
  • 5,527
  • 10
  • 58
  • 96
  • Why is it a `FixedLengthString` and not just `string`? – DavidG Nov 03 '14 at 01:11
  • @DavidG actually, I don't know what is the cause of my problem now so I try to make my code nearly the old version from vb6 first that why I use it – Jongz Puangput Nov 03 '14 at 01:14
  • But you are not calling a DLL written in VB6, so why is the parameter type using that? – DavidG Nov 03 '14 at 01:16
  • @DavidG in c# There is no string fixed length declaration like vb6 as you can see in vb6 variable declaration. However, whether it's string or what I using now the problem remain the same – Jongz Puangput Nov 03 '14 at 01:25
  • I know that, but the DLL import is not for a fixed length string right? – DavidG Nov 03 '14 at 01:27
  • @DavidG I have no idea what is the actual parameter for dll since it's very old dll with no any document left – Jongz Puangput Nov 03 '14 at 01:32
  • Only thing that I know it's not .net dll – Jongz Puangput Nov 03 '14 at 01:33
  • 1
    You could perhaps use a tool to figure out the parameters then? Like this maybe? http://stackoverflow.com/questions/1548637/is-there-any-native-dll-export-functions-viewer – DavidG Nov 03 '14 at 01:36
  • @DavidG if you still here, I have extract dll function from dependencies walker. `int Iso14443Anticoll(void *,unsigned char,unsigned char *,unsigned char *,unsigned char)` do you have any idea?? – Jongz Puangput Nov 03 '14 at 12:16

1 Answers1

1

SPECIAL THANK TO..

OKAY... First of all I want to thank @DavidG for guiding me

TO ANSWER MY OWN QUESTION...

The input string is not updated because of DllImport function signature are incorrect.

I have identified my DLL by using Dependencies Walker as mention in this LINK

The tool provides name and signature of each functions in the unmanageable .dll

enter image description here

So, I have change some function's signature.

FOR EXAMPLE

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

string to byte[]

Byte to byte

long to int

HERE IS UPDATE ANSWER..

        [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);
Community
  • 1
  • 1
Jongz Puangput
  • 5,527
  • 10
  • 58
  • 96