How to call function from .dll library?

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
mol
Posts: 2773
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 104 times
Been thanked: 41 times
Contact:

How to call function from .dll library?

Post by mol » Wed Jun 14, 2017 10:02 pm

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?

ASESORMIX
Posts: 58
Joined: Thu Oct 25, 2012 8:08 pm
Location: Bqto, Venezuela
Been thanked: 4 times

Post by ASESORMIX » Thu Jun 15, 2017 1:08 am

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


edk
Posts: 122
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 35 times
Been thanked: 62 times

Post by edk » Thu Jun 15, 2017 8:25 am

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

User avatar
mol
Posts: 2773
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 104 times
Been thanked: 41 times
Contact:

Post by mol » Thu Jun 15, 2017 1:41 pm

What is nRetType parameter in HMG_CallDLL function?

ASESORMIX
Posts: 58
Joined: Thu Oct 25, 2012 8:08 pm
Location: Bqto, Venezuela
Been thanked: 4 times

Post by ASESORMIX » Thu Jun 15, 2017 2:23 pm

Busca "nRetType" en el Search...
Encontrarás referencias importantes.

edk
Posts: 122
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 35 times
Been thanked: 62 times

Post by edk » Thu Jun 15, 2017 2:25 pm

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)

User avatar
mol
Posts: 2773
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Has thanked: 104 times
Been thanked: 41 times
Contact:

Post by mol » Fri Jun 16, 2017 3:29 pm

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?

User avatar
apais
Posts: 244
Joined: Fri Aug 01, 2008 6:03 pm
Location: uruguay
Has thanked: 18 times
Been thanked: 11 times
Contact:

Post by apais » Fri Jun 16, 2017 4:46 pm

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


Post Reply