Acceso a funciones DLL

Acceso a funciones DLL

Postby Mike Serra » Thu May 15, 2008 8:00 pm

Buenas Noches Foro, tengo un problema, he adquirido un dispositivo y me gustaria comunicarmen con el. Entre la documentación he encontrado un ejemplo de Visual Basic con el siguiente código:

Code: Select all  Expand view  RUN
Attribute VB_Name = "CSP2"
'*******************************************************************************
' *
' *  Filename:        csp2.bas
' *
' *  Description:     Defines constants and Declares for use with the csp2.dll
' *
' *  Derived From:    New File
' *
' *  Edit History:
' *   $Log:   U:/keyfob/archives/winfob/examples/vb/csp2.baV  $
rem
rem    Rev 1.2   Feb 01 2002 09:30:00   pangr
rem Removed commented out code
Rem
Rem    Rev 1.1   Jan 29 2002 16:09:08   pangr
Rem Changed Declare statements of functions that accept/return
Rem non-ASCII strings from type String to Byte arrays.
'*******************************************************************************/

Public Enum CommPorts
    COM1 = 0
    COM2 = 1
    COM3 = 2
    COM4 = 3
    COM5 = 4
    COM6 = 5
    COM7 = 6
    COM8 = 7
    COM9 = 8
    COM10 = 9
    COM11 = 10
    COM12 = 11
    COM13 = 12
    COM14 = 13
    COM15 = 14
    COM16 = 15
End Enum


'// Communications
Declare Function csp2Init Lib "csp2.dll" (ByVal nComPort As Long) As Long
Declare Function csp2Restore Lib "csp2.dll" () As Long
Declare Function csp2WakeUp Lib "csp2.dll" () As Long
Declare Function csp2DataAvailable Lib "csp2.dll" () As Long

'// Basic Functions
Declare Function csp2ReadData Lib "csp2.dll" () As Long
Declare Function csp2ClearData Lib "csp2.dll" () As Long
Declare Function csp2PowerDown Lib "csp2.dll" () As Long
Declare Function csp2GetTime Lib "csp2.dll" (aTimeBuf As Byte) As Long
Declare Function csp2SetTime Lib "csp2.dll" (aTimeBuf As Byte) As Long
Declare Function csp2SetDefaults Lib "csp2.dll" () As Long

'// CSP Data Get
Declare Function csp2GetPacket Lib "csp2.dll" (stPacketData As Byte, ByVal lgBarcodeNumber As Long, ByVal maxLength As Long) As Long
Declare Function csp2GetDeviceId Lib "csp2.dll" (szDeviceId As Byte, ByVal nMaxLength As Long) As Long
Declare Function csp2GetProtocol Lib "csp2.dll" () As Long
Declare Function csp2GetSystemStatus Lib "csp2.dll" () As Long
Declare Function csp2GetSwVersion Lib "csp2.dll" (ByVal szSwVersion As String, ByVal nMaxLength As Long) As Long
Declare Function csp2GetASCIIMode Lib "csp2.dll" () As Long
Declare Function csp2GetRTCMode Lib "csp2.dll" () As Long

'// DLL Configuration
Declare Function csp2SetRetryCount Lib "csp2.dll" (ByVal nRetryCount As Long) As Long
Declare Function csp2GetRetryCount Lib "csp2.dll" () As Long

'// Miscellaneous
Declare Function csp2GetDllVersion Lib "csp2.dll" (ByVal szDllVersion As String, ByVal nMaxLength As Long) As Long
Declare Function csp2TimeStamp2Str Lib "csp2.dll" (Stamp As Byte, ByVal value As String, ByVal nMaxLength As Long) As Long
Declare Function csp2GetCodeType Lib "csp2.dll" (ByVal CodeID As Long, ByVal CodeType As String, ByVal nMaxLength As Long) As Long

'// Advanced functions
Declare Function csp2ReadRawData Lib "csp2.dll" (aBuffer As Byte, ByVal nMaxLength As Long) As Long
Declare Function csp2SetParam Lib "csp2.dll" (ByVal nParam As Long, szString As Byte, ByVal nMaxLength As Long) As Long
Declare Function csp2GetParam Lib "csp2.dll" (ByVal nParam As Long, szString As Byte, ByVal nMaxLength As Long) As Long
Declare Function csp2Interrogate Lib "csp2.dll" () As Long
Declare Function csp2GetCTS Lib "csp2.dll" () As Long
Declare Function csp2SetDTR Lib "csp2.dll" (ByVal nOnOff As Long) As Long
Declare Function csp2SetDebugMode Lib "csp2.dll" (ByVal nOnOff As Long) As Long

'// Returned status values...
Global Const STATUS_OK                 As Long = 0
Global Const COMMUNICATIONS_ERROR      As Long = -1
Global Const BAD_PARAM                 As Long = -2
Global Const SETUP_ERROR               As Long = -3
Global Const INVALID_COMMAND_NUMBER    As Long = -4
Global Const COMMAND_LRC_ERROR         As Long = -7
Global Const RECEIVED_CHARACTER_ERROR  As Long = -8
Global Const GENERAL_ERROR             As Long = -9
Global Const FILE_NOT_FOUND            As Long = 2
Global Const ACCESS_DENIED             As Long = 5

'// Parameter values...
Global Const PARAM_OFF                 As Long = 0
Global Const PARAM_ON                  As Long = 1

Global Const DATA_AVAILABLE            As Long = 1
Global Const DATA_NOT_AVAILABLE        As Long = 0

Global Const DETERMINE_SIZE            As Long = 0
                                                   


Las funciones se lo que hacen, pero no se trascribirlas. ¿Podríais ayudarme a descifralo para FWH + xHarbour? Fwh 7.12
Mike Serra
 
Posts: 297
Joined: Fri Apr 14, 2006 5:52 pm
Location: Córdoba (España)

Postby Antonio Linares » Thu May 15, 2008 8:49 pm

Mike,

Aqui te pongo unos ejemplos para que te sirvan de ayuda:

Declare Function csp2Init Lib "csp2.dll" (ByVal nComPort As Long) As Long

En FWH:
DLL FUNCTION csp2Init( nComPort AS LONG ) AS LONG LIB "csp2.dll"

Declare Function csp2Restore Lib "csp2.dll" () As Long

En FWH:
DLL FUNCTION csp2Restore() AS LONG LIB "csp2.dll"

Si una función de esas te generase un GPF entonces prueba a definirla añadiendo la claúsula PASCAL:

DLL FUNCTION csp2Restore() AS LONG PASCAL LIB "csp2.dll"

Tambien comentarte que algunas DLLs requieren que se las mantengan abiertas mientras se usan, por lo que tendrías que hacer una llamada inicial:

local hDLL := LoadLibrary( "csp2.dll" )

y en las declaraciones anteriores cambiar LIB "csp2.dll" por LIB hDLL

y cuando termines de usar las funciones, llamarías a FreeLibrary( hDLL )
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42160
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Mike Serra » Fri May 16, 2008 9:42 am

Buenos Dias a todos.

Gracias Antonio por tus comentarios. Gracias a ellos he conseguido ya comunicarme con el dispositivo, ahora creo que el error (o mejor dicho warning) es pequeño. Lo que me pasa es lo siguiente, tengo el siguiente código:

Code: Select all  Expand view  RUN
#include "fivewin.ch"
#include "dll.ch"
#include "csp2.h"
#include "Constant.ch"
#include "Inkey.ch"

static nHandle

Function Lector
   Public Hora := 0,cadena1:=""
   nHandle := Loadlibrary("CSP2.DLL")
   msginfO(nHandle)
   csp2init(0)
   csp2GetSwVersion(@cadena1,hora)
   msginfo(csp2getprotocol())
   FreeLibrary(nHandle)
return nil
*======================================================================
DLL FUNCTION csp2Init( nComPort AS LONG ) AS LONG PASCAL LIB nHandle
DLL FUNCTION csp2Restore () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2WakeUp () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2DataAvailable () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2ReadData () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2Cleardata () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2PowerDown () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetTime (aTimeBuf As Byte) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2SetTime (aTimeBuf As Byte) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2SetDefaults () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetPacket (stPacketData As Byte,  lgBarcodeNumber as long ,  maxLength As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetDeviceId (szDeviceId As Byte,  nMaxLength As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetProtocol () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetSystemStatus () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetSwVersion (szSwVersion As String,  nMaxLength As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetASCIIMode () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetRTCMode () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2SetRetryCount (nRetryCount As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetRetryCount () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetDLL32Version (szDLL32Version As String,  nMaxLength As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2TimeStamp2Str (Stamp As Byte,  value As String,  nMaxLength As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetCodeType (CodeID As Long,  CodeType As String,  nMaxLength As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2ReadRawData (aBuffer As Byte,  nMaxLength As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2SetParam (nParam As Long, szString As Byte,  nMaxLength As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetParam (nParam As Long, szString As Byte,  nMaxLength As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2Interrogate () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2GetCTS () as LONG PASCAL LIB nHandle
DLL FUNCTION csp2SetDTR (nOnOff As Long) as LONG PASCAL LIB nHandle
DLL FUNCTION csp2SetDebugMode (nOnOff As Long) as LONG PASCAL LIB nHandle


Este código me da warnigs de estos tipos:

lector.prg(41) Warning W0001 Ambiguous reference: 'STRING'
lector.prg(41) Warning W0001 Ambiguous reference: 'LONG'
lector.prg(42) Warning W0001 Ambiguous reference: 'BYTE'

La linea csp2init(0) sirve para inicializar el dispositivo, y lo hace correctamente, pero cuando uso alguna funcion que hay que pasarle parametros, como por ejemplo csp2GetSwVersion cuyos parámetros son (szSwVersion As String, nMaxLength As Long), me salta un error como el siguiente:

Error description: Error BASE/1003 Variable does not exist: STRING
ó
Error description: Error BASE/1003 Variable does not exist: LONG
...
...

Creo que esto viene por los warnings, que aunque pueda compilar y generar el ejecutable, puede que no funcione bien

MUCHAS GRACIAS DE NUEVO :)
Mike Serra
 
Posts: 297
Joined: Fri Apr 14, 2006 5:52 pm
Location: Córdoba (España)

Postby Antonio Linares » Fri May 16, 2008 4:50 pm

Que código corresponde a la línea 41 ? Genera el fichero PPO preprocesado y revisa la línea 41.

Que contiene "csp2.h" ?

Prueba a hacer un MsgInfo( String ) antes de usar las funciones de la DLL y veamos que muestra.
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42160
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Mike Serra » Mon May 19, 2008 11:24 am

Antonio,buenos días, no se cómo pero ya compiló bien y el problema que tengo ahora es otro (poco a poco :) ).

Si te fijas en el primer post, hay estan todas las funciones de la libreria, bueno pues las funciones que reciben parametros que no sean LONG, es decir, por ejemplo algún parametro BYTE o STRING no funcionan correctamente, ejemplo:

- csp2Init( nComPort AS LONG ) AS LONG PASCAL LIB nHandle
- csp2Restore () as LONG PASCAL LIB nHandle
- csp2WakeUp () as LONG PASCAL LIB nHandle
- csp2DataAvailable () as LONG PASCAL LIB nHandle

estas funcionan OK, pero por ejemplo:

- csp2GetDLL32Version (szDLL32Version As String, nMaxLength As Long) as LONG PASCAL LIB nHandle
- csp2TimeStamp2Str (Stamp As Byte, value As String, nMaxLength As Long) as LONG PASCAL LIB nHandle

estas me da un error que me echa del programa, pero el error no es de xharbour ni fivewin sino directamente el típico mensaje de error de si quiero enviar o no el mensaje a microsoft.

El código de las funciones está extraido integramente de un código en Visual Basic, y supongo que puede que cambie algo las definiciones internas de las llamadas a las funciones.

¿Sabrías que puede ser?

SALUDOS, :)
Mike Serra
 
Posts: 297
Joined: Fri Apr 14, 2006 5:52 pm
Location: Córdoba (España)

Postby Antonio Linares » Mon May 19, 2008 12:15 pm

Mike,

Prueba a declararlas así:

DLL FUNCTION csp2GetDLL32Version (szDLL32Version As LPSTR, nMaxLength As Long) as LONG PASCAL LIB nHandle

y desde el PRG no le pongas @ delante
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42160
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Postby Mike Serra » Mon May 19, 2008 2:32 pm

Bueno Antonio, otro paso dado, aunque no me simpatiza lo que me devuelven las redefinidas con LPSTR, pero por lo menos no me da un error y me echa del programa.

Creo que por último, ¿como me aconsejas que redefina los parametros tipo BYTE y DWORD?

Muchas Gracias.
Mike Serra
 
Posts: 297
Joined: Fri Apr 14, 2006 5:52 pm
Location: Córdoba (España)

Postby Antonio Linares » Mon May 19, 2008 6:13 pm

Mike,

> ¿como me aconsejas que redefina los parametros tipo BYTE y DWORD?

Usa LONG para ambos (ya que estamos en 32 bits y el compilador de C los suele tratar en ese tamaño)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42160
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain


Return to FiveWin para Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 49 guests