FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Correct way to call an external DLL?
Posts: 883
Joined: Tue Oct 11, 2005 11:57 AM
Correct way to call an external DLL?
Posted: Wed Aug 27, 2014 01:02 PM
Taken from the dll manual.

VARIANT_BOOL Conectar( [in] BSTR IPAdd, [in] long Portl)

Should I DO it like this ?

DLL FUNCTION Conectar( IPAdd AS LPSTR, Portl AS LONG ) AS BOOL PASCAL LIB "xxxxxx.dll"

Thisis my code...

PUBLIC hDLL := LoadLibrary("xxxxxx.dll")
lReturn:=Conectar("192.168.1.1",9999)

MsgInfo( lReturn )

FreeLibrary( hDLL )


But I get a nil value, instead of .T. or .F.

What am I doing wrong... any help will be appreciated

Thanks in advance.

From Chile
Adolfo
;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650
Posts: 883
Joined: Tue Oct 11, 2005 11:57 AM
Re: Correct way to call an external DLL?
Posted: Wed Aug 27, 2014 06:29 PM

UP...
Any idea?

;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650
Posts: 729
Joined: Tue Oct 18, 2005 06:49 PM
Re: Correct way to call an external DLL?
Posted: Wed Aug 27, 2014 08:29 PM
Hi Adolfo,
Code (fw): Select all Collapse
DLL32 FUNCTION FTPGETFILE( hConnect AS LONG, cRemoteFile AS LPSTR, cNewFile AS LPSTR, nFailIfExists AS LONG, nFlagsAndAttribs AS DWORD, nFlags AS DWORD, @nContext AS PTR ) AS BOOL;
聽 聽 PASCAL FROM "FtpGetFileA" LIB "wininet.dll"

DLL32 FUNCTION FTPPUTFILE( hConnect AS LONG, cLocalFile AS LPSTR, cNewRemoteFile AS LPSTR, nFlags AS DWORD, @nContext AS PTR ) AS BOOL;
聽 聽 PASCAL FROM "FtpPutFileA" LIB "wininet.dll"


Regards,

George
Posts: 883
Joined: Tue Oct 11, 2005 11:57 AM
Re: Correct way to call an external DLL?
Posted: Wed Aug 27, 2014 08:54 PM
George.. thanks for answering.

I can load the DLL with no problem, no error, I can see the number of the Handle with no problem.

I tried to call the function in this way.

DLL32 FUNCTION Connect_NET( IPAdd AS STRING, Portl AS LONG ) AS BOOL PASCAL FROM "Connect_NET" LIB "zkemsdk.dll"

DLL32 FUNCTION Connect_NET( IPAdd AS STRING, Portl AS LONG ) AS BOOL FROM "Connect_NET" LIB "zkemsdk.dll"

DLL32 FUNCTION Connect_NET( IPAdd AS LPSTR, Portl AS LONG ) AS BOOL PASCAL LIB "zkemsdk.dll"

DLL32 FUNCTION Connect_NET( IPAdd AS LPSTR, Portl AS LONG ) AS BOOL LIB "zkemsdk.dll"



All of them with the same result NIL, no .T. or .F. as supposed
DLL manual says the function name and paramteres are:

VARIANT_BOOL Connect_NET( [in] BSTR IPAdd, [in] long Portl)
Return True if connected, otherwise False.... but I get a NIL

I'm out of ideas.
;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Correct way to call an external DLL?
Posted: Wed Aug 27, 2014 11:42 PM

Alfonso,

it is not the same a VARIANT_BOOL than a BOOL. There is no an automatic conversion from a VARIANT from the DLL call command, thats probably why you are geting nil as the return value

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 883
Joined: Tue Oct 11, 2005 11:57 AM
Re: Correct way to call an external DLL?
Posted: Wed Aug 27, 2014 11:58 PM

Antonio... thanks...

So how am I supposed to retrieve it.. as what ?

Thanks

;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Correct way to call an external DLL?
Posted: Thu Aug 28, 2014 12:12 AM

Probably as a pointer to the variant (just a generic pointer)

But once you get such pointer, you need to properly inspect it. Harbour has some functions for OLE management that you may try to adapt.

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 883
Joined: Tue Oct 11, 2005 11:57 AM
Re: Correct way to call an external DLL?
Posted: Thu Aug 28, 2014 12:29 AM

Ok
reading some info about it.. I discovered that

VARIANT_BOOL has 2 possible values

    VARIANT_FALSE =  0
    VARIANT_TRUE  = -1

So maybe the function returns 0 or -1
DLL32 has no "Unknown" return value to declare, Because that way I could inspect it, or not.

;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Correct way to call an external DLL?
Posted: Thu Aug 28, 2014 07:39 AM
Adolfo,

Te hablo en espa帽ol y abreviamos :-)

Estoy revisando tu c贸digo y veo que se usa BSTR y eso no es un LPSTR. He buscado en el c贸digo de Harbour y efectivamente es una cadena "wide".

En Harbour\contrib\hbwin\olecore.c encuentras este c贸digo:

Code (fw): Select all Collapse
static BSTR hb_oleItemToString( PHB_ITEM pItem )
{
聽 聽UINT uiStrLen = ( UINT ) hb_itemCopyStrU16( pItem, HB_CDP_ENDIAN_NATIVE,
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽NULL, UINT_MAX );
聽 聽BSTR strVal = SysAllocStringLen( NULL, uiStrLen );

聽 聽hb_itemCopyStrU16( pItem, HB_CDP_ENDIAN_NATIVE, strVal, uiStrLen + 1 );

聽 聽return strVal;
}


que basicamente convierte un item de Harbour (tipo cadena) a una cadena wide BSTR (que son las usadas en OLE).

Asi que lo primero de todo es convertir tu cadena en BSTR y luego proporcionarla a tu funci贸n.
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Correct way to call an external DLL?
Posted: Thu Aug 28, 2014 07:46 AM
Despues de revisarlo no estoy tan seguro de que sea eso lo que necesitas, pues esa funci贸n convierte de un item de OLE a cadena y no de un item de Harbour.

Aqui esta todo el c贸digo de olecore.c para revisarlo pues se usan BSTR en otras partes m谩s y asi ver cual es la forma correcta de hacer la conversi贸n:

https://github.com/harbour/core/blob/master/contrib/hbwin/olecore.c
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 883
Joined: Tue Oct 11, 2005 11:57 AM
Re: Correct way to call an external DLL?
Posted: Thu Aug 28, 2014 11:01 PM

Antonio...

Mirando algunas soluciones para mi problema, encontre esto

std::string ConvertWCSToMBS(const wchar_t* pstr, long wslen)
{
int len = ::WideCharToMultiByte(CP_ACP, 0, pstr, wslen, NULL, 0, NULL, NULL);

std::string dblstr(len, '\0');
len = ::WideCharToMultiByte(CP_ACP, 0 /* no flags */,
                            pstr, wslen /* not necessary NULL-terminated */,
                            &dblstr[0], len,
                            NULL, NULL /* no default char */);

return dblstr;

}

Y aqui de BSTR a STR, posibles 3
//----------------------

include <comdef.h>

std::string myString = _bstr_t (myBSTR);

//----------------
void f(BSTR ms_str)
{
std::wstring ws(ms_str);
}

//*---------------

BSTR ConvertMBSToBSTR(const std::string& str)
{
int wslen = ::MultiByteToWideChar(CP_ACP, 0 / no flags /,
str.data(), str.length(),
NULL, 0);

BSTR wsdata = ::SysAllocStringLen(NULL, wslen);
::MultiByteToWideChar(CP_ACP, 0 /* no flags */,
                      str.data(), str.length(),
                      wsdata, wslen);
return wsdata;

}

Que me dices.. no soy experto en C, pero este problema me tiene acogotado.

Saludos..

De ahi busco las opciones de recibir bien el VARIANT_BOOL

;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Correct way to call an external DLL?
Posted: Fri Aug 29, 2014 04:13 PM

Adolfo,

Si la IP fuese 192.168.1.1 proporcionasela asi: Chr( 0 ) + "1" + Chr( 0 ) + "9" + Chr( 0 ) + "2" + Chr( 0 ) + "." + ...

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 883
Joined: Tue Oct 11, 2005 11:57 AM
Re: Correct way to call an external DLL?
Posted: Fri Aug 29, 2014 06:40 PM

Gracias Master...
Pruebo y comento...

;-) Ji,ji,ji... buena la cosa... "all you need is code"

http://www.xdata.cl - Desarrollo Inteligente
----------
Asus TUF F15, 32GB Ram, 2 * 1 TB NVME M.2, GTX 1650

Continue the discussion