Page 1 of 4

How to call function from .dll library?

Posted: Wed Jun 14, 2017 10:02 pm
by mol
Hi guys!
I need to communicate with Dibal labeling scale.
I've reached SDK from my vendor, samples in C# and VB and two .dll files needed to do transfer of articles to the scale.
I can't start with anything, because I can't find how to call function from this .dll file
My sample code:

Code: Select all

// DataSend is one of the function id Dibalscop.dll
DYNAMIC DataSend

FUNCTION Main
Local hLib

hLib := hb_LibLoad( "Dibalscop.dll" )
// I want to proof if hLib is not empty
msgdebug( hLib )	// returns pointer

//one way to call DataSend function
x :=  &("DataSend")( 1, date(), time() )
msgdebug(x)
IF !Empty( hLib )
	//second way - it should work when DYNAMIC DataSend is declared
	x := DataSend()
ENDIF
hb_LibFree( hLib )
RETURN Nil
I'm getting error:

Code: Select all

Date:06/14/17 Time: 23:49:17
Error BASE/1001 Undefined function: DATASEND
Called from DATASEND(0) 
Called from MAIN(9) 
What am I doing wrong while calling DataSend function?

Re: How to call function from .dll library?

Posted: Thu Jun 15, 2017 1:08 am
by ASESORMIX
Hola.

Te servirá este ejemplo?

Code: Select all

PROCEDURE MAIN
 PARAMETERS ARCH
 LOCAL STATUS:=SPAC(128)
 LOCAL ERROR:=SPAC(128)
 CALLDLL32("OpenFpctrl","TFHKAIF.DLL","COM1")
 CALLDLL32("SendFileCmd","TFHKAIF.DLL",@STATUS,@ERROR,ARCH)
 CALLDLL32("CloseFpctrl","TFHKAIF.DLL","COM1",0)
RETURN

Re: How to call function from .dll library?

Posted: Thu Jun 15, 2017 6:04 am
by mol
I'll try. Thanks!

Re: How to call function from .dll library?

Posted: Thu Jun 15, 2017 8:25 am
by edk
Marek, try this:

Code: Select all

/*
   HB_DYNCALL ({<cFunction>, <cLibrary> | <pLibrary> [, <nFuncFlags> [, <nArgFlags1>, ..., <nArgFlagsn>]]}, ...) -> <xResult>

      nFuncFlags ---> hb_bitOr (HB_DYN_CTYPE_ *, HB_DYN_ENC_ *, HB_DYN_CALLCONV_ *)
      nArgFlags  ---> hb_bitOr (HB_DYN_CTYPE_ *, HB_DYN_ENC_ *)
      pLibrary : = hb_libLoad (cLibrary)
*/
or Claudio's function

Code: Select all

//-----------------------------------------------------------------------------------------------
//   HMG_CallDLL ( cLibName , [ nRetType ] , cFuncName , Arg1 , ... , ArgN ) ---> xRetValue
//-----------------------------------------------------------------------------------------------
see source in h_controlmisc.prg

Re: How to call function from .dll library?

Posted: Thu Jun 15, 2017 1:41 pm
by mol
What is nRetType parameter in HMG_CallDLL function?

Re: How to call function from .dll library?

Posted: Thu Jun 15, 2017 2:23 pm
by ASESORMIX
Busca "nRetType" en el Search...
Encontrarás referencias importantes.

Re: How to call function from .dll library?

Posted: Thu Jun 15, 2017 2:25 pm
by edk
mol wrote: Thu Jun 15, 2017 1:41 pm What is nRetType parameter in HMG_CallDLL function?
I follow the documentation:

HMG_CallDLL()
Calls a ANSI/Unicode DLL Function


HMG_CallDLL() detects the current code page and appropriately called the ANSI or Unicode version of the function.

Syntax:
HMG_CallDLL ( cLibName, [ nRetType ] , cFuncName, FuncArg1, ..., FuncArgN ) ---> xRetValue

nRetType: value type returned by the function (constants defined in hbdyn.ch)
- HB_DYN_CTYPE_DEFAULT
- HB_DYN_CTYPE_CHAR
- HB_DYN_CTYPE_CHAR_UNSIGNED
- HB_DYN_CTYPE_CHAR_PTR
- HB_DYN_CTYPE_CHAR_UNSIGNED_PTR
- HB_DYN_CTYPE_SHORT
- HB_DYN_CTYPE_SHORT_UNSIGNED
- HB_DYN_CTYPE_SHORT_PTR
- HB_DYN_CTYPE_SHORT_UNSIGNED_PTR
- HB_DYN_CTYPE_INT
- HB_DYN_CTYPE_INT_UNSIGNED
- HB_DYN_CTYPE_INT_PTR
- HB_DYN_CTYPE_INT_UNSIGNED_PTR
- HB_DYN_CTYPE_LONG
- HB_DYN_CTYPE_LONG_UNSIGNED
- HB_DYN_CTYPE_LONG_PTR
- HB_DYN_CTYPE_LONG_UNSIGNED_PTR
- HB_DYN_CTYPE_LLONG
- HB_DYN_CTYPE_LLONG_UNSIGNED
- HB_DYN_CTYPE_LLONG_PTR
- HB_DYN_CTYPE_LLONG_UNSIGNED_PTR
- HB_DYN_CTYPE_FLOAT
- HB_DYN_CTYPE_FLOAT_PTR
- HB_DYN_CTYPE_DOUBLE
- HB_DYN_CTYPE_DOUBLE_PTR
- HB_DYN_CTYPE_BOOL
- HB_DYN_CTYPE_BOOL_PTR
- HB_DYN_CTYPE_VOID
- HB_DYN_CTYPE_VOID_PTR
- HB_DYN_CTYPE_STRUCTURE
- HB_DYN_CTYPE_STRUCTURE_PTR

Source example:
cBuffer := SPACE (1024)
nCopyCharacters := HMG_CallDLL ("USER32.DLL", HB_DYN_CTYPE_INT, "GetWindowText", Win_1.HANDLE, @cBuffer, 512)
MsgInfo (cBuffer)

Re: How to call function from .dll library?

Posted: Fri Jun 16, 2017 3:29 pm
by mol
I can't transfer parameters from my HMG Function to .dll function. Program crashes everytime
I have an example where is defined such a type:

Code: Select all

typedef struct _Scale
{
int masterAddress;
LPWSTR ipAddress;
int txPort;
int rxPort;
LPWSTR model;
LPWSTR display;
LPWSTR section;
int group;
LPWSTR logsPath;
}Scale
It must be sent to the scale, so I want to define my array:

Code: Select all

aScale:={ 00,;	// master adres
			"192.168.20.181",;	// IP
			3001,;		//TX port
			3000,;		//RX port
			"500RANGE",;	// model
			"ALPHANUMERIC",;// display
			"Section",;	// section - unused
			0,;		// group
			"";		// unused
		}
Is it OK?

Re: How to call function from .dll library?

Posted: Fri Jun 16, 2017 4:46 pm
by apais
A C structure is a continuous and positional list of characters, while a harbour array is a pointer to a dynamic non-continuous set of chars.
In your case the representation of the structure would be something similar to: 00192.168.20.18130013000500RANGEALPHANUMERICSection0
But I'm sure a C profesional can tell you the exact translation of that. You have to know how many digits an int is and the exact length of the structure.
Also how they represent a null char for the last parameter

HTH
Angel

Re: How to call function from .dll library?

Posted: Fri Jun 16, 2017 5:17 pm
by mol
I'll test. May thanks!