任何人都可以帮我解决这个错误,我试图打开一个带有DLL的端口

时间:2014-11-08 17:20:02

标签: delphi dll tcp

任何人都可以帮助我解决我在尝试使用dll打开端口时遇到的此错误。我得到了一个错误:"访问地址为0003D078"当我调用SAAT_Open(myCharPtrOpen)时。不确定我是否在单位UntRFIDAPI上正确定义了句柄。变量PHandle是否正确定义? 这是我的代码:

procedure TForm5.Button1Click(Sender: TObject);
 var
   myString, myString2  : string;
   myCharPtrInit: PChar;
   myCharPtrOpen: PChar;
   i : Integer;
   Open, Init: Boolean;
 begin
  // Create a string of Char's
  myString  := '&hp';
  // Point to the first character in the string
  i := 1;
  myCharPtrInit := Addr(myString[i]);
  if SAAT_TCPInit(myCharPtrInit,'192.168.3.238',7086) = True then
  begin
    StatusBar1.Panels[0].Text := 'Initiated'; //ShowMessage('Initiated');
    myString2 := 'hp';
    myCharPtrOpen := Addr(myString2[i]);
    if SAAT_Open(myCharPtrOpen) = True then StatusBar1.Panels[1].Text := 'Opened';   
  end;
end;


unit UntRFIDAPI;

INTERFACE
uses
  SysUtils, Windows, Messages, Classes, Graphics, Controls, Dialogs,
  StdCtrls, Forms, DBCtrls, DB, Grids, DBGrids,Mask, ExtCtrls,
  Buttons, WinTypes;

var
  A: String;

function SAAT_TCPInit(pHandle: Pointer; pHostName: String; nsocketPort: Integer):Boolean; stdcall;
function SAAT_Open(pHandle: Pointer):Boolean; stdcall;
function SAAT_Close(pHandle: Pointer):Boolean; stdcall;

implementation

function SAAT_TCPInit;  external 'RFIDAPI.dll';
function SAAT_Open;  external 'RFIDAPI.dll';
function SAAT_Close;  external 'RFIDAPI.dll';

end.

以下是API调用:

1.1初始化以太网端口(TCP)连接

//TCP parameters initialization 
//Functionality:import by parameter, initialize TCP to prepare for opening connection
//Parameters: 
//  pHandle for preserving the opening ports handl 
//  pHostName reader IP address, only effective under the ethernet communication 
//  nsocketPort network SOCKET port 
//Returned value:true: Operation Succeeded, false Operation Failed 

bool SAAT_TCPInit (void** pHandle,char *pHostName,int nsocketPort)

如果连接到阅读器的IP是192.168.0.238,则端口是7086,然后调用如下:

HANDLE hp; 
if (!SAAT_TCPInit(&hp, ”192.168.0.238”, 7086)) 
{ 
    printf("reader initialization failed!\n"); 
    return false; 
}

3 个答案:

答案 0 :(得分:3)

正如Rudy和David指出的那样,你对SAAT_TCPInit()函数签名的翻译是错误的,并且你使用这个API的指针句柄是错误的。他们已经解释了为什么你的代码是错误的,所以我不会重复这些原因。不过,他们的答案有一些小问题。

你的Delphi代码看起来应该更像这样:

unit UntRFIDAPI;

interface

function SAAT_TCPInit(out pHandle: Pointer; pHostName: PAnsiChar; nsocketPort: Integer): Boolean; stdcall;
function SAAT_Open(pHandle: Pointer): Boolean; stdcall;
function SAAT_Close(pHandle: Pointer): Boolean; stdcall;

implementation

const
  RFIDAPIDLL = 'RFIDAPI.dll';

function SAAT_TCPInit; external RFIDAPIDLL;
function SAAT_Open; external RFIDAPIDLL;
function SAAT_Close; external RFIDAPIDLL;

end.

procedure TForm5.Button1Click(Sender: TObject);
var
  hp: Pointer;
begin
  if SAAT_TCPInit(hp, '192.168.3.238', 7086) then
  begin
    StatusBar1.Panels[0].Text := 'Initiated';
    if SAAT_Open(hp) then
      StatusBar1.Panels[1].Text := 'Opened';   
    ...
    SAAT_Close(hp);
  end;
end;

供参考,以下是C ++的DLL RFIDAPIEXPORT.h头文件的内容:

/***********************************************************************
* Module:  RFIDEXPORT.h
* Author:  
* Modified: 
* Purpose: 
***********************************************************************/
#pragma once

#ifndef  _RFIDEXPORT_H
#define  _RFIDEXPORT_H

#ifdef RFIDAPI_EXPORTS
#define RFID_API __declspec( dllexport )
#else
#define RFID_API __declspec( dllimport )
#endif


extern "C"
{
    bool RFID_API __stdcall SAAT_TCPInit(void** pHandle,char *pHostName,int nsocketPort);

    bool RFID_API __stdcall SAAT_COMInit(void** pHandle,unsigned char nBusAddr,char *pComNum,int nBaud );

    bool RFID_API __stdcall SAAT_USBInit(void** pHandle,unsigned char nBusAddr,char * pUSBNum,int nBaud );

    bool RFID_API __stdcall SAAT_UDPInit(void** pHandle,char *pHostName,int nsocketPort);

    bool RFID_API  __stdcall SAAT_Open(void* pHandle);

    bool RFID_API  __stdcall SAAT_Close(void *pHandle);

    bool RFID_API  __stdcall SAAT_Reconnect(void *pHandle);

    bool RFID_API  __stdcall SAAT_HeartSend (void* pHandle);

    bool RFID_API  __stdcall SAAT_SysInfSet (void* pHandle ,unsigned char nType,unsigned char* pParm,int nLen);

    bool RFID_API  __stdcall SAAT_SysInfQuery (void* pHandle ,unsigned char nType, unsigned char *pPara, unsigned char *pLen);

    bool RFID_API  __stdcall SAAT_WorkModeSet (void* pHandle ,unsigned char nType);

    bool RFID_API  __stdcall SAAT_ParmOp (void* pHandle ,unsigned char nType, unsigned char nStartAddrr, unsigned char nLen,  unsigned char *pData, unsigned char *pDataLen);

    bool RFID_API  __stdcall SAAT_RFParaSet (void* pHandle ,unsigned char nType, unsigned char nParaLen,unsigned char* pPara);

    bool RFID_API  __stdcall SAAT_RFParaQuery (void* pHandle ,unsigned char nType,unsigned char* pPara, unsigned char *pLen);

    bool RFID_API  __stdcall SAAT_CommunicatParaSet (void* pHandle ,unsigned char nType, unsigned char* pPara, unsigned char nLen);

    bool RFID_API  __stdcall SAAT_CommunicatParaQuery (void* pHandle ,unsigned char nType, unsigned char* pPara,unsigned char *pLen);

    bool RFID_API  __stdcall SAAT_NetParaSet (void* pHandle ,unsigned char nType, unsigned char* pPara, unsigned char nLen);

    bool RFID_API  __stdcall SAAT_NetParaQuery (void* pHandle ,int nType, unsigned char* pPara,unsigned char *pLen);

    bool RFID_API  __stdcall SAAT_TagOpParaSet(void* pHandle ,unsigned char nType, unsigned char *pPara,unsigned char nLen);

    bool RFID_API  __stdcall SAAT_TagOpParaQuery (void* pHandle ,unsigned char nType, unsigned char* pPara, unsigned char *pLen);

    bool RFID_API  __stdcall SAAT_ExtendBroadParaSet (void* pHandle ,unsigned char nType, unsigned char pSendChunnel);

    bool RFID_API  __stdcall SAAT_ExtendBroadParaQuery (void* pHandle ,unsigned char nType, char* pPara, unsigned char* pLen);

    bool RFID_API  __stdcall SAAT_TotalAntennaParmQuery (void* pHandle,unsigned char *szAntennaPara,unsigned char *pLen);

    bool RFID_API  __stdcall SAAT_AntennaParmQuery (void* pHandle,unsigned char nAntenna,unsigned char * pAntennaEnable,unsigned char *pAntennaPower,unsigned char *pAntennaQueryTime );

    bool  RFID_API  __stdcall SAAT_AntennaParmSet(void* pHandle ,unsigned char *pPara,unsigned char nLen );

    bool RFID_API  __stdcall SAAT_SetAntennaPortEnable (void* pHandle,unsigned char nAntenna,unsigned char nEnable );

    bool RFID_API  __stdcall SAAT_SetAntennaPower (void* pHandle,unsigned char nAntenna,unsigned char nPower );

    bool RFID_API  __stdcall SAAT_SetAntennaTime (void* pHandle,unsigned char nAntenna,unsigned char nTime );

    bool RFID_API  __stdcall SAAT_PowerOff(void *pHandle);

    bool RFID_API  __stdcall SAAT_CarrierWaveOp(void* pHandle ,unsigned char nType, unsigned char  nPort);

    bool RFID_API  __stdcall SAAT_IOOperate(void* pHandle,unsigned char nPort,unsigned char nState);

    bool RFID_API  __stdcall SAAT_IOStateQuery(void* pHandle,unsigned char *pState);

    bool RFID_API  __stdcall SAAT_Reboot (void* pHandle,unsigned char nMode);

    bool RFID_API  __stdcall SAAT_Reading_IOConfig (void* pHandle,unsigned char nConfigBit);

    bool RFID_API  __stdcall SAAT_Reading_IOQuery (void* pHandle,unsigned char* pConfigBit);

    bool RFID_API  __stdcall SAAT_IOPulseWidthSet (void* pHandle,unsigned char nIOPort,unsigned char nWidth);

    bool RFID_API  __stdcall SAAT_IOPulseWidthQuery (void* pHandle,unsigned char nIOPort,unsigned char* pWidth);

    bool RFID_API  __stdcall  SAAT_6BTagSelect ( void* pHandle, unsigned char nType, unsigned char nStartAddr,
                                            unsigned char nDataBite, unsigned char * Data );

    bool RFID_API  __stdcall  SAAT_6BReadUIDCode (void *pHandle,unsigned char nAntenna,unsigned char nType);

    int  RFID_API  __stdcall SAAT_6BRevUIDMsg (void *pHandle, unsigned char* nAntenna, unsigned char* pUIDData,
        unsigned char* nUIDLen);

    bool RFID_API  __stdcall SAAT_6BReadUserData ( void *pHandle ,unsigned char nAntenna,unsigned char nType,unsigned char * pTagID,unsigned char nStartAddr,unsigned char nReadLen, unsigned char *pdata,unsigned char dataLen);

    bool RFID_API  __stdcall SAAT_6BWriteUserData (void* pHandle,
        unsigned char nAntenna,
        unsigned char nType, 
        unsigned char *pTagID,
        unsigned char nStartAddr,
        unsigned char *pValue, 
        unsigned char *pLen);

    bool RFID_API  __stdcall SAAT_6BTagLock (void* pHandle, unsigned char nAntenna, unsigned char nType,
        unsigned char *pTagID, unsigned char nStartAddrr,  unsigned char nLen);

    bool RFID_API  __stdcall SAAT_6BTagLockQuery (void* pHandle, unsigned char nAntenna, 
        unsigned char *pTagID, unsigned char nStartAddr,  unsigned char nLen,unsigned char *pData,unsigned char nDataLen);

    bool RFID_API  __stdcall SAAT_6CTagSelect (  void *pHandle, unsigned char nBank ,unsigned short nStartAddr,unsigned char MaskBit, 
        unsigned char *Data ,unsigned char Datalength,unsigned char nSessionZone,
        unsigned char nActiveFlag, unsigned char nCutFlag );

    bool RFID_API  __stdcall SAAT_6CReadEPCCode ( void *pHandle,unsigned char nAntenna, unsigned char nType,
        unsigned int nTagCount);

    int  RFID_API  __stdcall SAAT_6CRevEPCMsg (void *pHandle, unsigned char* nAntenna, unsigned char* pEPCData,
        unsigned char* nEPCLen);

    bool RFID_API  __stdcall  SAAT_6CReadTIDCode ( void *pHandle,unsigned char nAntenna, unsigned char nType, unsigned int nTagCount);

    int  RFID_API  __stdcall SAAT_6CRevTIDMsg (void *pHandle, unsigned char* nAntenna, unsigned char* pTIDData, unsigned char* nTIDLen);

    bool RFID_API  __stdcall  SAAT_6CWriteEPCCode ( void* pHandle,unsigned char nAntenna,unsigned char nType,
        unsigned char *pAccessPWD, unsigned char *pWriteData,unsigned char nLen );

    bool RFID_API  __stdcall SAAT_6CReadUserData ( void* pHandle, 
        unsigned char nAntenna, 
        unsigned int StartAddr,
        unsigned int nToReadLen,
        unsigned int nWaitTime,
        unsigned char * UserData,
        unsigned int* pDataLen);

    bool RFID_API  __stdcall SAAT_6CWriteUserData (void* pHandle,  
        unsigned char nAntenna, 
        unsigned char nType,  
        unsigned char *pAccessPWD,
        unsigned int  nStartAddr,
        unsigned int nWaitTime,
        unsigned char *pWriteData, 
        unsigned int *pToWriteLen);

    bool RFID_API  __stdcall SAAT_6CWriteBankData (void* pHandle, unsigned char nAntenna, unsigned char nType, unsigned char *pAccessPWD,
        unsigned char nBank,  unsigned char *pWriteData, unsigned char nLen);

    bool RFID_API  __stdcall SAAT_6CClearBankData (void* pHandle, unsigned char nAntenna, unsigned char nType, unsigned char *pAccessPWD,
        unsigned char nBank, unsigned char nStartAddr, unsigned char nLen);

    bool RFID_API  __stdcall SAAT_6CAccessPWDSet (void *pHandle, unsigned char nAntenna, unsigned char nType, unsigned char *pOrgPWD, 
        unsigned char *pNewPWD);

    bool RFID_API  __stdcall SAAT_6CDestroyPWDSet (void *pHandle, unsigned char nAntenna,unsigned char nType, unsigned char *pAccessPWD, 
        unsigned char *pDestroyPWD );

    bool RFID_API  __stdcall  SAAT_6CTagLock (void *pHandle, unsigned char nAntenna, unsigned char *pAccessPWD, unsigned char nType,
        unsigned char nBank);

    bool RFID_API  __stdcall SAAT_6CTagKill (void *pHandle, unsigned char nAntenna,unsigned char *pDestroyPWD,
        unsigned char *pEPC, int nEPCLen);

    bool RFID_API  __stdcall SAAT_6CEASFlagSet (void *pHandle, unsigned char nAntenna, unsigned char nType,
        unsigned char* pAccessPwd,  int nEASFlag);

    bool  RFID_API  __stdcall SAAT_6CEASMonitorEnable (void *pHandle, unsigned char nAntenna,unsigned char nSetEAS);

    bool RFID_API __stdcall SAAT_Copyright(void** pHandle, char* copyright);

    bool RFID_API  __stdcall SAAT_SetLanguageType (void* pHandle,char* szType);

    bool RFID_API  __stdcall SAAT_GetErrorMessage(void *pHandle,char *szMsg, int nLen);

    bool RFID_API  __stdcall SAAT_GetErrorCode(void *pHandle,int *pCode);

    bool RFID_API  __stdcall SAAT_SysTest(void* pHandle ,unsigned char nType,unsigned char nAntenna, unsigned char *pTestParm, unsigned char nLen);

    bool RFID_API  __stdcall SAAT_RawSendData(void* pHandle , unsigned char *pSendData, unsigned char nLen);

    bool RFID_API  __stdcall SAAT_RawRevData(void* pHandle , unsigned char *pRecvData, unsigned char* pLen,int nWaitTime); 

    bool RFID_API  __stdcall SAAT_RawSendAndRevData(void* pHandle , 
        unsigned char *pSendData,
        unsigned char nLen,
        unsigned char *pRecvData,
        unsigned char *pLen,
        unsigned char nWaitTime); 

    bool RFID_API __stdcall SAAT_EnterTrans(void* pHandle ,unsigned char nType);

    bool RFID_API __stdcall SAAT_ResetWifiBaund(void* pHandle);
};
#endif

以下是DLL文档的链接:

API Calling Quick Start SAAT-800 Series Reader

答案 1 :(得分:1)

bool SAAT_TCPInit(void** pHandle,char *pHostName,int nsocketPort)

此函数生成一个类型为void*的新句柄。在Delphi中映射到Pointer,一个无类型指针。因为C只支持传递值,为了让函数将值传递给调用者,该参数被声明为指向void*的指针,即void**

char*是一个输入参数,指向8位字符的空终止数组的指针。这映射到PAnsiChar。绝对不能使用string进行互操作。它是一种私有的Delphi类型,对于互操作而言根本无效。

由于没有指定调用约定,我们假设它是cdecl

因此,正确的翻译是:

function SAAT_TCPInit(out Handle: Pointer; HostName: PAnsiChar; 
  SocketPort: Integer): Boolean; cdecl; external 'RFIDAPI.dll';

我们已将句柄类型void*映射到Pointer。并且out的使用引入了所需的额外间接层。

这样称呼:

var
  Handle: Pointer;
....
if SAAT_TCPInit(Handle, '192.168.3.238', 7086) then
  ....

DLL中的函数实际上是stdcall似乎是合理的,即使您提供的C原型没有指定。如果是这样,并且只有你可以解决这个问题,那么你可以相应地改变调用约定。事实上,Remy挖出了头文件并显示函数是stdcall

我们看不到其他函数的C声明,但它们看起来很简单。他们似乎都接受了SAAT_TCPInit调用返回的句柄。因此,他们应该接受按值传递的Pointer类型的参数。大概是这样的:

function SAAT_Open(Handle: Pointer): Boolean; cdecl; external 'RFIDAPI.dll';

同样适用于SAAT_Close

为了清晰起见,为此句柄定义新类型可能更好。像这样:

type
  TSAATHandle = type Pointer;

或者如果您更喜欢:

type
  TSAATHandle = type NativeUInt;

显然,您使用此类型而不是Pointer

答案 2 :(得分:0)

你对C标题的翻译几乎没有。我没有其他功能的原型,但SAAT_TCPInit应声明为:

function SAAT_TCPInit(var pHandle: THandle; pHostName: PAnsiChar; nSocketPort: Integer): ByteBool;
    cdecl;

C对Delphi字符串一无所知,因此不要将C字符串翻译为Delphi的类型string,因为它不是二进制兼容的。 char *应翻译为PAnsiCharvoid** pHandle应翻译为var pHandle: THandle。调用约定可能是cdecl

这假设使用了正常的caling约定。如果设置为生成stdcall,那么您必须声明而不是cdecl

我认为这是你遇到的主要问题。纠正翻译并重试。

有关翻译C标头的详细信息,请参阅我的文章Pitfalls of converting(现在就开始使用,因为网站可能会暂停一段时间,我会切换提供商)。