1

Im trying to read the data from a magnetic card reader its model is MT188. The Card reader comes with a DLL and a doc for the functions in the DLL. the process to read a card is as follows.

HANDLE APIENTRY CommOpenWithBaut(UINT nPort, UINT _data) 

open serial Communications example : CommOpenWithBaut(1,9600);.

int APIENTRY CarderCheckSt(HANDLE hComHandle, unsigned char &PewSt, unsigned char &CardType, unsigned char &CardSt, unsigned char &PowerSt) 

check Device status.

int APIENTRY MagRead(HANDLE hComHandle,BYTE *MagData,int &lenth) 

read Magnetic card data

int APIENTRY CommClose(HANDLE hComHandle); 

Close port after the card is read.

Each function has a return value. This is an example of the cout<<Magread<<endl; is gives a value of 00007FF792E41258. What am I doing wrong? How can I use these functions the right way?

here is my main.

int main(int arg, char* argc[])
{
    CommOpenWithBaut(1,9600);
    cout<< "This is the cards data:  " << MagRead << endl;
    cout << "This is the cards status:  " << CarderCheckSt << endl;
    CommClose;
}

updates:

I receive this error when I try to run the program:

LNK2019 unresolved external symbol "__declspec(dllimport) void * __cdecl CommOpenWithBaut(unsigned int,unsigned int)" (__imp_?CommOpenWithBaut@@YAPEAXII@Z) referenced in function main 

This is my header file:

#include <iostream>
#include <windows.h>

#ifdef Modulev188_EXPORTS
#define Modulev188_API __declspec(dllexport)
#else
#define Modulev188_API __declspec(dllimport)
#endif

Modulev188_API HANDLE  CommOpenWithBaut(UINT nPort, UINT _data);
Modulev188_API int  CommClose(HANDLE hComHandle);
Modulev188_API int  CarderCheckSt(HANDLE hComHandle, unsigned char& PewSt, unsigned char& CardType, unsigned char& CardSt, unsigned char& PowerSt);
Modulev188_API int  CarderVersion(HANDLE hComHandle, unsigned char* VersionNo, int& lenth);
//DLLEXPORT int WINAPI ContactCPU_ColdReset(HANDLE hComHandle, BYTE* _CPUTYPE, BYTE* _exData, int* _exdataLen);
//DLLEXPORT int WINAPI ContactCPU_Dormancy(HANDLE hComHandle);
Modulev188_API int  ContactCPU_CAPDU(HANDLE hComHandle, int _dataLen, BYTE* _APDUData, BYTE* _exData, int* _exdataLen);
Modulev188_API int  SIM_Reset(HANDLE hComHandle, BYTE _VOLTAGE, BYTE _SIMNo, BYTE& _SIMTYPE, BYTE* _exData, int& _exdataLen);
Modulev188_API int  SIM_C_APDU(HANDLE hComHandle, BYTE SIMNo, int _dataLen, BYTE* _APDUData, BYTE* _exData, int& _exdataLen);
Modulev188_API int  SIM_CardClose(HANDLE hComHandle);
Modulev188_API int  MagRead(HANDLE hComHandle, BYTE* MagData, int& lenth);
Modulev188_API int  MagCleaner(HANDLE hComHandle);
ProgZ_
  • 15
  • 7

1 Answers1

2

You're not actually calling MagRead, CarderCheckSt or CommClose because you aren't invoking them as functions with parentheses and parameters. You're just printing them out as function pointers, so the values you see represent the address of the function entry point.

Instead, you need to provide suitable parameters to each function. From the 'open' function you will get a handle that you pass to all subsequent functions.

#include <iostream>

extern "C" {    
    #include <mt188.h> // The header for your C library
}

using namespace std;

int main(int argc, char* argv[])
{
    // Open the device

    HANDLE port = CommOpenWithBaut(1, 9600);

    if (port == 0)
    {
        cerr << "Cannot open device";
        return -1;
    }

    // Read status

    unsigned char PewSt = 0;
    unsigned char CardType = 0;
    unsigned char CardSt = 0;
    unsigned char PowerSt = 0;

    int rc = CarderCheckSt(port, PewSt, CardType, CardSt, PowerSt);

    // Read buffer of data

    int bufferSize = 128;
    BYTE buffer[bufferSize];

    int bytesRead = MagRead(port, buffer, bufferSize);

    //

    CommClose(port);

    return 0;
}
gavinb
  • 19,278
  • 3
  • 45
  • 60
  • Thanks alot your answer helped but Im still unable to open communications with the reader `Cannot open device`. Any Ideas on how to solve this issue? – ProgZ_ Jan 27 '20 at 05:44
  • Try a different port, I guess. Is it a serial connection? Then open up your device manager and see which COM port the device is connected to. Do you need to install any drivers or VCP? It's hard to do much but guess without an API reference. – gavinb Jan 27 '20 at 08:14
  • I can communicate with the device using the test app on port 1 and bauds 9600. I don't need to install drivers only the DLL. I have an error: `LNK2019 unresolved external symbol "__declspec(dllimport) void * __cdecl CommOpenWithBaut(unsigned int,unsigned int)" (__imp_?CommOpenWithBaut@@YAPEAXII@Z) referenced in function main ` this might be related to the issue ? The API reference doesn't mention much. – ProgZ_ Jan 27 '20 at 08:24
  • also, here is the tags in my header file . `#include #include #ifdef Modulev188_EXPORTS #define Modulev188_API __declspec(dllexport) #else #define Modulev188_API __declspec(dllimport) #endif` – ProgZ_ Jan 27 '20 at 08:25
  • here are the functions : `Modulev188_API HANDLE CommOpenWithBaut(UINT nPort, UINT _data); Modulev188_API int CommClose(HANDLE hComHandle); Modulev188_API int CarderCheckSt(HANDLE hComHandle, unsigned char& PewSt, unsigned char& CardType, unsigned char& CardSt, unsigned char& PowerSt); Modulev188_API int MagRead(HANDLE hComHandle, BYTE* MagData, int& lenth); ` – ProgZ_ Jan 27 '20 at 08:26
  • An "unresolved external" error is a linker error and here means you need to specify the import library that corresponds to the DLL. But how can you run the test program if you can't link it?? Also it would be much easier to read if you amend your question with the above info rather than squish it into comments. – gavinb Jan 27 '20 at 08:29
  • I made a .lib file myself to link it to the dll. the manufacture didn't include a .lib file only The DLL. The .exe test software works fine. and orry I amended my question. – ProgZ_ Jan 27 '20 at 08:38
  • The linker error shows the problem - where it says the symbol `__imp_?CommOpenWithBaut@@YAPEAXII@Z` this is actually a mangled name, which means it's treating it as a C++ function. Your library must be straight C, so you need an `extern "C"` declaration around the header when you include this in a C++ module. This will ensure the function names are not mangled, and it should then find the correct symbols. I updated the code in my answer above to show the `extern` declaration. – gavinb Jan 27 '20 at 13:07
  • I figured that issue out and added it in the header file this error showed : `LNK2019 unresolved external symbol __imp_CommOpenWithBaut referenced in function main` I also tried your code to make sure. the same error occurred. – ProgZ_ Jan 27 '20 at 13:17
  • Sounds like misconfigration of imports - see here for explanation: https://stackoverflow.com/questions/5159353/how-can-i-get-rid-of-the-imp-prefix-in-the-linker-in-vc – gavinb Jan 28 '20 at 03:54
  • I solved The issue. When I recreated the .lib and ran the program at x32 it worked with me. Thanks for your help. – ProgZ_ Jan 28 '20 at 05:22
  • Aha, yes you have to be extra careful to match build flavours on Windows. You can't mix 32/64-bit or Debug/Release. Glad it's all resolved now. – gavinb Jan 28 '20 at 11:47