delphi通过 dll的头文件动态调用dll。函数类型不知道怎么转换
delphi通过 dll的头文件动态调用dll。函数类型不知道如何转换
dll的头文件是用C/C++写的,
下面是我为了动态调用dll而写的代码:
但是转变的格式不正确,比如delphi中不知道哪里有SOCEKT类型、const的参数不知道该在delphi中如何显示?
------解决思路----------------------
GetProcAddress,少打了个C
------解决思路----------------------
DLL的输出方式加上#define DECL2 extern "C" stdcall, 以C的方式输出,这样的函数_daveFreedc@4怪怪的。
dll的头文件是用C/C++写的,
#ifndef _MPI_INTERFACE_H
#define _MPI_INTERFACE_H
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define DECL2 __stdcall
typedef struct dost {
int rfd;
int wfd;
// int connectionType;
} _daveOSserialType;
typedef void* daveConnection;
typedef void* daveInterface;
#ifdef __cplusplus
extern "C" {
#endif
SOCKET DECL2 openSocket(const int port, const char * peer);
int DECL2 closeSocket(SOCKET h);
void DECL2 daveSetDebug(int nDebug);
int DECL2 daveGetDebug(void);
daveInterface * DECL2 daveNewInterface(_daveOSserialType nfd, char * nname, int localMPI, int protocol, int speed);
daveConnection * DECL2 daveNewConnection(daveInterface * di, int MPI,int rack, int slot);
/*
connect to a PLC. returns 0 on success.
*/
int DECL2 daveConnectPLC(daveConnection * dc);
void DECL2 daveSetTimeout(daveInterface * di, int tmo);
int DECL2 daveReadBytes(daveConnection * dc, int area, int DB, int start, int len, void * buffer);
int DECL2 daveWriteBytes(daveConnection * dc,int area, int DB, int start, int len, void * buffer);
int DECL2 daveReadBits(daveConnection * dc, int area, int DB, int start, int len, void * buffer);
int DECL2 daveWriteBits(daveConnection * dc,int area, int DB, int start, int len, void * buffer);
int DECL2 daveSetBit(daveConnection * dc,int area, int DB, int byteAdr, int bitAdr);
int DECL2 daveClrBit(daveConnection * dc,int area, int DB, int byteAdr, int bitAdr);
void DECL2 daveFreedc(void * dc);
void DECL2 daveFreedi(void * di);
#ifdef __cplusplus
//#ifdef CPLUSPLUS
#endif
#endif
下面是我为了动态调用dll而写的代码:
unit davedll;
interface
type
// PdaveOSType = ^TdaveOSType;
TdaveOSType =record
rfd :Integer;
wfd :Integer;
end;
OpenSk=function( const prot: Integer; const peer: PChar): SOCKET;stdcall;
CloseSk=function(h : SOCKET): Integer;stdcall;
NewInterface=function( var nfd :TdaveOSType; name :PChar; localMPT,protocol,speed :Integer):Pointer;stdcall;
NewConnect=function(var di :Pointer; MPI,rack,slot :Integer):Pointer;stdcall;
ConnectPLC=function(var dc:Pointer):Integer;stdcall;
ReadBytes=function(dc:Pointer; area,DB,start,len:Integer; buffer :Pointer):Integer;stdcall;
WriteBytes=function(dc:Pointer; area,DB,start,len:Integer; buffer :Pointer):Integer;stdcall;
ReadBits=function(dc:Pointer; area,DB,start,len:Integer; buffer :Pointer):Integer;stdcall;
WriteBits=function(dc:Pointer; area,DB,start,len:Integer; buffer :Pointer):Integer;stdcall;
SetBits=function(dc:Pointer;area,DB,byteadr,bitadr:Integer):Integer;stdcall;
ClrBits=function(dc:Pointer;area,DB,byteadr,bitadr:Integer):Integer;stdcall;
Freedc=procedure(dc:Pointer);stdcall;
Freedi=procedure(di:Pointer);stdcall;
THandle = Integer;
{var
PdaveOSType : TdaveOSType;
Handle: THandle;
nif:NewInterface;
nc:NewConnect;
cp:ConnectPLC;
rby:ReadBytes;
wby:WriteBytes;
rb:ReadBits;
wb:WriteBits;
sb:SetBits;
cb:ClrBits;
fdc:Freedc;
fdi:Freedi;
begin
Handle := LoadLibrary('dave.dll');
if Handle<> 0 then
begin
@nif:=GetProAddress(Handle,'daveNewInterface');
@nc:=GetProAddress(Handle,'NewConnect');
......
end;
FreeLibrary(Handle);
end;}
implementation;
end.
但是转变的格式不正确,比如delphi中不知道哪里有SOCEKT类型、const的参数不知道该在delphi中如何显示?
------解决思路----------------------
GetProcAddress,少打了个C
------解决思路----------------------
DLL的输出方式加上#define DECL2 extern "C" stdcall, 以C的方式输出,这样的函数_daveFreedc@4怪怪的。