Page 4 of 4

Re: How to call function from .dll library?

Posted: Thu Sep 05, 2019 12:43 am
by jparada
Hi Krzysztof,
I am very grateful, thanks to your explanation I have been able to understand some things, but I have just realized that I don't need an array,
but what I need is a hash, do you have some example similar to this but with hash?, I am sorry I can't send code because I don't have nothing, I'm still studying.

I appreciate your help.

Regards,
Javier

Re: How to call function from .dll library?

Posted: Thu Sep 05, 2019 6:58 pm
by KDJ
jparada wrote: ...do you have some example similar to this but with hash?...
No, I don't have any example with hash array passed from Harbour to C function.

To do something, you should give more data, at least:
1. Declaration of dll function (function name, type of return value, types of the parameters) and definitions of the structures if are used in this function.
2. Description of the parameters passed from Harbuor level to C function
3. Description of the value returned from C function to Harbour level.

Re: How to call function from .dll library?

Posted: Fri Sep 06, 2019 11:01 pm
by jparada
Hi Krzysztof,
Thanks for answering.
Krzysztof wrote:1. Declaration of dll function (function name, type of return value, types of the parameters) and definitions of the structures if are used in this function.
function name: altaProducto
parameter1 : integer Product Identifier, passed by reference
parameter2 : data structure

example data structure definition:

Code: Select all

field           type    length
codProducto	string	30	
descProducto	string	60
tipo            integer	
fechaRegistro	string	23
precio		double
unidad		string	30
Krzysztof wrote:2. Description of the parameters passed from Harbour level to C function
I'm not sure here, but I think it must be a hash, so

local idpdto := 0
local hdatos

hdatos := Hash()
hdatos["codProducto"] := "PDTO1"
hdatos["descProducto"] := "Nombre Producto"
hdatos["tipo"] := 1
hdatos["fechaRegistro"] := "06/09/2019"
hdatos["precio"] := 120.65
hdatos["unidad"] := "PIEZA"

altaProducto(@idpdto, hdatos)
Krzysztof wrote:3. Description of the value returned from C function to Harbour level.
return: integer values, 0 without error, different from 0, error code.

I hope that with this information you can give me some guidance.

I appreciate your time and your help.

Regards,
Javier

Re: How to call function from .dll library?

Posted: Sat Sep 07, 2019 4:23 pm
by KDJ
jparada wrote: ...I'm not sure here, but I think it must be a hash...
No, it does not have to be a hash array.

This is sample code (compiled but not tested):

Code: Select all

#include "hmg.ch"

#define APROD_CPROD  1
#define APROD_DPROD  2
#define APROD_TIPO   3
#define APROD_FREG   4
#define APROD_PRECIO 5
#define APROD_UNIDAD 6
#define APROD_ALEN   6


FUNCTION Main()
  LOCAL nProdID := 0
  LOCAL aProd[APROD_ALEN]
  LOCAL nResult

  aProd[APROD_CPROD]  := "PDTO1"
  aProd[APROD_DPROD]  := "Nombre Producto"
  aProd[APROD_TIPO]   := 1
  aProd[APROD_FREG]   := "06/09/2019"
  aProd[APROD_PRECIO] := 120.65
  aProd[APROD_UNIDAD] := "PIEZA"

  nResult := my_AltaProducto(@nProdID, aProd)

  SWITCH nResult
    CASE 0 //no error
      MsgBox("Product ID: " + hb_NtoS(nProdID))
      EXIT
    CASE -1
      MsgBox("Error: can not load my_lib.dll library!")
      EXIT
    CASE -2
      MsgBox("Error: altaProducto function not found!")
      EXIT
    OTHERWISE
      MsgBox("Error code: " + hb_NtoS(nResult))
  ENDSWITCH

RETURN NIL


#pragma BEGINDUMP

#include "SET_COMPILE_HMG_UNICODE.ch"
#include "HMG_UNICODE.h"

#include <windows.h>
#include "hbapi.h"
#include "hbapiitm.h"

typedef struct
{
  LPSTR  codProducto;
  LPSTR  descProducto;
  INT    tipo;
  LPSTR  fechaRegistro;
  DOUBLE precio;
  LPSTR  unidad;
} PROD;


        //my_AltaProducto(@nProdID, aProd)
HB_FUNC ( MY_ALTAPRODUCTO )
{
  HMODULE  hLib = LoadLibrary(_T("my_lib.dll"));
  INT      ProdID;
  PROD     ProdData;
  PHB_ITEM pArray;

  INT WINAPI (*pAltaProducto)(INT * ProdID, PROD * ProdData);

  if (hLib)
  {
    pAltaProducto = (INT WINAPI (*)(INT *, PROD *)) GetProcAddress(hLib, "altaProducto");

    if (pAltaProducto)
    {
      pArray = hb_param(2, HB_IT_ARRAY);

      ProdData.codProducto   = (LPSTR)  hb_arrayGetCPtr(pArray, 1);
      ProdData.descProducto  = (LPSTR)  hb_arrayGetCPtr(pArray, 2);
      ProdData.tipo          = (INT)    hb_arrayGetNI  (pArray, 3);
      ProdData.fechaRegistro = (LPSTR)  hb_arrayGetCPtr(pArray, 4);
      ProdData.precio        = (DOUBLE) hb_arrayGetND  (pArray, 5);
      ProdData.unidad        = (LPSTR)  hb_arrayGetCPtr(pArray, 6);

      hb_retni(pAltaProducto(&ProdID, &ProdData));
      hb_storni(ProdID, 1);
    }
    else
    {
      hb_retni(-2);
    }

    FreeLibrary(hLib);
  }
  else
  {
    hb_retni(-1);
  }
}

#pragma ENDDUMP
I still have questions:
What code page are you using?
What is the range of error codes returned by altaProducto function?
Can you share the dll file for testing?