Wrappers for Payway+Calling DLL with Structure

27 views Asked by At

Good morning everyone. I need help to be able to use a Prisma Payway POS DLL (VpiPc.dll) in C++? text

I was able to program it in C#. I leave the code of the basic procedures in case anyone is interested.

// Open Port - Structure and Entry Parameters
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct ComParams_t
{
 public string com;      // Port name. Ex: "COM1", etc.
 public ushort baudRate; // Transmission speed: Ex: 19200
 public ushort byteSize; // Byte length. Ex: 7, 8
 public char   parity;   // Parity.Ex: 'N' none, 'E' even, 'O' odd
 public ushort stopBits; // Stop bits. Ex: 1, 2
}

// Get Version of Versión of VPI
public struct vpiVersionOut
{ 
   public ushort version; 
};

// Open Port
[DllImport("VpiPc.dll", CallingConvention = CallingConvention.StdCall)]
public static extern ushort vpiOpenPort(ref ComParams_t portParams);
       
// Close Port
[DllImport("VpiPc.dll", CallingConvention = CallingConvention.StdCall)]
public static external ushort vpiClosePort();
        
// Get Version
[DllImport("VpiPc.dll", CallingConvention = CallingConvention.StdCall)]
public static extern ushort vpiGetVersion(ref vpiVersionOut versionParams);

// Procedure Open Connection
static bool OpenConnection()
{
  bool lConnection = false;
            
  ComParams_t paramsStruct = new ComParams_t();

  paramsStruct.com = "COM4";
  paramsStruct.baudRate = 19200;
  paramsStruct.byteSize = 8;
  paramsStruct.parity = 'N';
  paramsStruct.stopBits = 1;
 
  ushort result = vpiOpenPort(ref paramsStruct);
  logger.Info("Port Open: " + result);
  if (result != 0)
     {
       for (int i = 0; i < 3; i++)
       {
        logger.Info("Retrying to open port...");
        result = vpiOpenPort(ref paramsStruct);
        if (result == 0)
           break;
       }
     }
  if (result == VPI_OK)
            {
                logger.Info("Connection Test - Communication with POS OK!!!");
                lConnection = true;
            }
     else
            {
                logger.Error("Error Connection ");
            }

  return lConnection;
}

In C# it works great, but I need to translate it to C++. So far I have done this but I don't know if it is right, nor do I know how to send the connection data.

#pragma BEGINDUMP

#include "Windows.h"
#include "hbapi.h"

typedef struct COM_PARAMS{
   LPSTR com;         // Port name. Ex: "COM1", etc.
   WORD baudRate;     // Transmission speed: Ex: 19200
   WORD byteSize;     // Byte length. Ex: 7, 8
   BYTE parity;       // Parity. Ex: 'N' none, 'E' even, 'O' odd
   WORD stopBits;     // Stop bits. Ex: 1, 2
}comParams_t;

typedef WORD ( __stdcall *__vpiOpenPort)(comParams_t* params);

HB_FUNC( TESTCONNECTION )
{
   static __vpiOpenPort pFunction = NULL;
   static HINSTANCE hLib;
   WORD result;

   if( !hLib )
      hLib = LoadLibrary( "VpiPc.dll" );
   if( hLib )
   {
      if( !pFunction )
         pFunction = (__vpiOpenPort) GetProcAddress( hLib, "vpiOpenPort" );
   }

   if( pFunction )
      result = (pFunction) ((LPSTR)hb_parc(params-->com), (WORD)hb_parnl(params-->baudRate), (WORD)hb_parnl(params-->byteSize), (BYTE)hb_parc(params-->parity), (WORD)hb_parnl(params-->stopBits));
   else
      result = -2000;

   hb_retni(result);
}

#pragma ENDDUMP

// Here I call the procedure

#define HB_DYN_CALLCONV_STDCALL 0x0100000  /* Windows API default */

METHOD test1Click( oSender ) CLASS TMenuPrincipal
local nHandle, aParams:={"COM4", 19200, 8, 'N', 1} 

nHandle:=TESTCONNECTION(aParams)
 
RETURN Nil

// I also tried this, with HB_DynCall but it doesn't work either
METHOD test2Click( oSender ) CLASS TMenuPrincipal
local nHandle, aParams:={"COM4", 19200, 8, 'N', 1} , hLib, n, cVersion
  
nHandle := HB_DynCall({"vpiOpenPort","VpiPc.dll", HB_DYN_CALLCONV_STDCALL}, HB_AParams( aParams ))
   

RETURN Nil

So far no luck. Please, I would greatly appreciate any help. Regards.

If I can program these basic procedures, It is probable that I can program the other procedures, sale, cancellation of sale, return of sale and QR.

0

There are 0 answers