关于网友提出的“ 请教调用VC创建的DLL的相关问题”问题疑问,本网通过在网上对“ 请教调用VC创建的DLL的相关问题”有关的相关答案进行了整理,供用户进行参考,详细问题解答如下:
问题: 请教调用VC创建的DLL的相关问题
描述: 我们现在有一个视频监控的SDK,其中的DLL是用VC++编的,我知道调用DELPHI自己编的DLL,但却对调用VC编的有些不明白---主要是不知道那些函数在DELPHI中该怎样声明以及参数该怎样调用?该SDK中有个socketdll.dll,然后有个socketdll.h,socketdll.h内容如下:
#ifndef SOCKETDLL_H
#define SOCKETDLL_H
#include
#include
//==================macro define=====================
#define OK 0
#define FAIL -1
#define INVALID_CHANNEL_NUM (short)-1
#define MAX_PEER_NUM 64
//====================消息定义========================
#define WM_CONNECT_OK WM_USER+10
#define WM_CONNECT_FAIL WM_USER+11
#define WM_READ_CTRL_MSG WM_USER+12
//===================报文类型定义=====================
#define MT_OPEN_CHANNEL 'A'
#define MT_CLOSE_CHANNEL 'B'
#define MT_CHANNEL_PHOT_DATA 'C'
#define MT_FILE_PHOT_DATA 'D'
#define MT_ALARM_MSG '$'
#define MT_NET_TEST_REQU '>'
#define MT_NET_TEST_RESP '<'
//====================帧类型定义======================
#define FT_HEAD 'H'
#define FT_IFRAME 'I'
#define FT_BFRAME 'B'
#define FT_PFRAME 'P'
#define FT_AUDIO 'A'
//====================结构定义========================
//链表头结点
typedef struct HeadNode{
long nlNodeNum; //链表结点个数
void *lpNext; //指向的链表指针
}HEADNODE;
//报文头结构
typedef struct MsgHead{
long nlMsgBodyLen; //数据包长度(不含包头)
char cMsgType; //报文类型
char cFrameType; //帧类型
struct in_addr stxPhotSourAddr; //图像源服务器IP
u_short usPhotServPort; //图像源服务器端口号
short nsChannel; //图像源服务器通道编号
short nsRouteNum; //路由表节点数
short nsNextPos; //下一节点在路由表中的序号(0,1,2...)
//节点收到报文时,表示本节点在路由表的序号
//发送报文时,该序号递增1
//eg: |源结点|路由结点1|路由结点2|...|路由结点n|目的结点|
time_t lTimeStamp; //报文到达结点时间(秒)
short nsWaitTime; //报文等待发送时间(秒)
}MSGHEAD;
//主机信息结构(唯一标识一台主机)
typedef struct RouteInf{
struct in_addr ulAddr;
u_short usPort;
}ROUTEINF;
//端口通道信息(用于向主线程发送连接信息)
typedef union PortCh{
struct{
short nsChannel;
u_short usPort;
} stxPortChannel;
long lPortChannel;
}PORTCHANINF;
//==================function define=================
typedef int APIENTRY GETROUTEINF(struct in_addr *lpRemoteAddr, u_short usRemotePort, ROUTEINF RouteInf[]);
typedef int APIENTRY LOCALMSGHANDLE(MSGHEAD *lpMsgHead, byte *lpMsgBody);
//====================结构定义========================
//导出全局变量结构
typedef struct GlobVar{
HWND hWnd; //Main window handle
HANDLE hReadEvent; //Read message event
char TransferListFile[MAX_PATH]; //transfer list map file
char ReconnListFile[MAX_PATH]; //reconnect list map file
char LoclReqListFile[MAX_PATH]; //local request list map file
GETROUTEINF *lpGetRouteInf; //
LOCALMSGHANDLE *lpLocalMsgHandle; //
}GLOBVAR;
//==================== Class define ========================
class CSocketDll
{
public:
//Initial SocketDll
virtual void APIENTRY InitSocketDll()=0;
//Transfer the global var address pointer
virtual GlobVar *APIENTRY GetGlobVarAddr()=0;
//Start Server
virtual int APIENTRY StartServ(u_short ServPort)=0;
//Connect Server
virtual int APIENTRY ConnectServ(struct in_addr *lpRemoteAddr, u_short usServPort)=0;
virtual int APIENTRY ConnectServ(char *lpRemoteAddr, u_short usServPort)=0;
//Disconnect from server
virtual int APIENTRY DisConnect(struct in_addr *lpRemoteAddr, u_short usServPort)=0;
virtual int APIENTRY DisConnect(char *lpRemoteAddr, u_short usServPort)=0;
//Server send the photo data message
virtual void APIENTRY ServSendPhotMsg(short nsChannel, char cMsgType, char cFrameType, byte *lpPhotData, int niPhotDataLen)=0;
//Client send the control message
virtual void APIENTRY ClientSendCtrlMsg(struct in_addr *lpPhotServAddr, u_short usPhotServPort, short nsChannel, char cMsgType, byte *lpMsg, int niMsgLen)=0;
virtual void APIENTRY ClientSendCtrlMsg(char *lpPhotServAddr, u_short usPhotServPort, short nsChannel, char cMsgType, byte *lpMsg, int niMsgLen)=0;
//Server send the control message
virtual void APIENTRY ServSendCtrlMsg(ROUTEINF RouteInf[], int niRouteNum, short nsChannel, char cMsgType, char cFrameType, byte *lpMsg, int niMsgLen)=0;
//Server send the alarm message
virtual void APIENTRY ServSendAlarmMsg(short nsChannel, char cMsgType, byte *lpMsg, int niMsgLen)=0;
//Local Photo Request
virtual void APIENTRY OpenChannelReq(struct in_addr *lpPhotSrcAddr, u_short usPhotSrcPort, u_short nsChannel)=0;
virtual void APIENTRY OpenChannelReq(char *lpPhotSrcAddr, u_short usPhotSrcPort, u_short nsChannel)=0;
//Close channel Request
virtual void APIENTRY CloseChannelReq(struct in_addr *lpPhotSrcAddr, u_short usPhotSrcPort, u_short nsChannel)=0;
virtual void APIENTRY CloseChannelReq(char *lpPhotSrcAddr, u_short usPhotSrcPort, u_short nsChannel)=0;
//reverse the route information in the message body
virtual void APIENTRY ReverseRouteInf(short nsRouteNum, ROUTEINF RouteInf[])=0;
//read the control message from list
virtual BOOL APIENTRY ReadCtrlMsg(MSGHEAD *lpMsgHead, byte *lpMsgBody)=0;
};
//====================Export function======================
//Create the SocketDll object
CSocketDll *APIENTRY CreateSocketDll();
//Release the SocketDll resource
void APIENTRY ReleaseSocketDll();
#endif
请问我该如何在DELPHI中调用?恳请详细指教!谢谢!
解决方案1: 怎么发????
解决方案2: 尽管使用目标文件中的C + +类是不可能的,但是有限制地使用包含在D L L中的C + +类是可以实现的,这是指在D e l p h i中使用C + +类中的虚拟方法。因为,Object Pascal和C + +对于虚拟接口同样都遵循C O M标准,需要注意,此D L L中至少要包括两个输出函数:一个用于创建并返回对新对象的引用;另一个函数用于释放给定的引用。这两个函数是用不同语言的程序共享对象的管道。为了在D e l p h i中使用这个对象,必须做两件事。首先,必须引入D L L中用于创建和释放对象实例的函数。然后,要声明一个虚拟抽象的Object Pascal类,用它来映射C + +类。注意当为C++类定义Object Pascal包装时,不必去考虑两个函数的名称。因为它们在内部被如何调用并不重要。但因为它们的调用都是通过虚拟方法表实现的,所以这两个函数的声明顺序绝不可任意改变,一定要与C++中声明的顺序一致。
解决方案3: 接口函数的有塞,根据接口函数跟参数就可以了
解决方案4: 我没有仔细你的代码,太多了哈,不过昨天我自己写了个dll(VC的)在delphi中调用了,希望对你有帮组:
dll的函数:extern "C" _declspec(dllexport) int ChangePrinterName(TCHAR CharSource[],TCHAR CharResult[])
delphi调用:
type
TChangePrinterName=function(source,result:pchar):integer stdcall;: stdcall需要
其他调用你都知道我就不说了。
本来原来DLL参数是: CString的,不过程序不稳定,所以改为了:TCHAR,对 delphi的兼容很好。
解决方案5: 不好意思,今天才看完你的代码:)
楼上的意见very good。
强烈建议你用com(或者不要在接口中用类或对象)
因为你返回的是对象指针。而delphi和vc在对象内部结构上不尽相同。
如何做com--可以用atl应该只需要<10分钟改代码
如何用com--@_@看看书就知道了
解决方案6: 那你说功能,直接用DElphi开发好了啊,delphi里面直接socket的控件都那么强,为什么一定要用vc做的dll呢
解决方案7: 首先要搞清他的接口,然后再改写成DELPHI形式的就可以了.
动态加载这个DLL也可以.
解决方案8:
{///====================Export function====================== }
{///Create the SocketDll object }
var
CreateSocketDll: var function: CSOCKETDLL {$IFDEF WIN32} stdcall {$ENDIF};
{///Release the SocketDll resource }
var
ReleaseSocketDll: procedure {$IFDEF WIN32} stdcall {$ENDIF};
{$ENDIF}
var
DLLLoaded: Boolean { is DLL (dynamically) loaded already? }
{$IFDEF WIN32} = False; {$ENDIF}
implementation
var
SaveExit: pointer;
DLLHandle: THandle;
{$IFNDEF MSDOS}
ErrorMode: Integer;
{$ENDIF}
procedure NewExit; far;
begin
ExitProc := SaveExit;
FreeLibrary(DLLHandle)
end {NewExit};
procedure LoadDLL;
begin
if DLLLoaded then Exit;
{$IFNDEF MSDOS}
ErrorMode := SetErrorMode($8000{SEM_NoOpenFileErrorBox});
{$ENDIF}
DLLHandle := LoadLibrary('TEST.DLL');
if DLLHandle >= 32 then
begin
DLLLoaded := True;
SaveExit := ExitProc;
ExitProc := @NewExit;
@InitSocketDll := GetProcAddress(DLLHandle,'InitSocketDll');
{$IFDEF WIN32}
Assert(@InitSocketDll <> nil);
{$ENDIF}
@GetGlobVarAddr := GetProcAddress(DLLHandle,'GetGlobVarAddr');
{$IFDEF WIN32}
Assert(@GetGlobVarAddr <> nil);
{$ENDIF}
@StartServ := GetProcAddress(DLLHandle,'StartServ');
{$IFDEF WIN32}
Assert(@StartServ <> nil);
{$ENDIF}
@ConnectServ := GetProcAddress(DLLHandle,'ConnectServ');
{$IFDEF WIN32}
Assert(@ConnectServ <> nil);
{$ENDIF}
@ConnectServ := GetProcAddress(DLLHandle,'ConnectServ');
{$IFDEF WIN32}
Assert(@ConnectServ <> nil);
{$ENDIF}
@DisConnect := GetProcAddress(DLLHandle,'DisConnect');
{$IFDEF WIN32}
Assert(@DisConnect <> nil);
{$ENDIF}
@DisConnect := GetProcAddress(DLLHandle,'DisConnect');
{$IFDEF WIN32}
Assert(@DisConnect <> nil);
{$ENDIF}
@ServSendPhotMsg := GetProcAddress(DLLHandle,'ServSendPhotMsg');
{$IFDEF WIN32}
Assert(@ServSendPhotMsg <> nil);
{$ENDIF}
@ClientSendCtrlMsg := GetProcAddress(DLLHandle,'ClientSendCtrlMsg');
{$IFDEF WIN32}
Assert(@ClientSendCtrlMsg <> nil);
{$ENDIF}
@ClientSendCtrlMsg := GetProcAddress(DLLHandle,'ClientSendCtrlMsg');
{$IFDEF WIN32}
Assert(@ClientSendCtrlMsg <> nil);
{$ENDIF}
@ServSendCtrlMsg := GetProcAddress(DLLHandle,'ServSendCtrlMsg');
{$IFDEF WIN32}
Assert(@ServSendCtrlMsg <> nil);
{$ENDIF}
@ServSendAlarmMsg := GetProcAddress(DLLHandle,'ServSendAlarmMsg');
{$IFDEF WIN32}
Assert(@ServSendAlarmMsg <> nil);
{$ENDIF}
@OpenChannelReq := GetProcAddress(DLLHandle,'OpenChannelReq');
{$IFDEF WIN32}
Assert(@OpenChannelReq <> nil);
{$ENDIF}
@OpenChannelReq := GetProcAddress(DLLHandle,'OpenChannelReq');
{$IFDEF WIN32}
Assert(@OpenChannelReq <> nil);
{$ENDIF}
@CloseChannelReq := GetProcAddress(DLLHandle,'CloseChannelReq');
{$IFDEF WIN32}
Assert(@CloseChannelReq <> nil);
{$ENDIF}
@CloseChannelReq := GetProcAddress(DLLHandle,'CloseChannelReq');
{$IFDEF WIN32}
Assert(@CloseChannelReq <> nil);
{$ENDIF}
@ReverseRouteInf := GetProcAddress(DLLHandle,'ReverseRouteInf');
{$IFDEF WIN32}
Assert(@ReverseRouteInf <> nil);
{$ENDIF}
@ReadCtrlMsg := GetProcAddress(DLLHandle,'ReadCtrlMsg');
{$IFDEF WIN32}
Assert(@ReadCtrlMsg <> nil);
{$ENDIF}
@CreateSocketDll := GetProcAddress(DLLHandle,'CreateSocketDll');
{$IFDEF WIN32}
Assert(@CreateSocketDll <> nil);
{$ENDIF}
@ReleaseSocketDll := GetProcAddress(DLLHandle,'ReleaseSocketDll');
{$IFDEF WIN32}
Assert(@ReleaseSocketDll <> nil);
{$ENDIF}
end
else
begin
DLLLoaded := False;
{ Error: TEST.DLL could not be loaded !! }
end;
{$IFNDEF MSDOS}
SetErrorMode(ErrorMode)
{$ENDIF}
end {LoadDLL};
begin
LoadDLL;
end.
解决方案9: unit TEST;
interface
uses
{$IFDEF WIN32}
Windows;
{$ELSE}
Wintypes, WinProcs;
{$ENDIF}
{$IFNDEF SOCKETDLL_H}
{$DEFINE SOCKETDLL_H}
{$INCLUDE }
{$INCLUDE }
{///==================macro define===================== }
const
OK = 0;
const
FAIL = -1;
const
INVALID_CHANNEL_NUM = (short)-1;
const
MAX_PEER_NUM = 64;
{///====================®ø®§©w?======================== }
const
WM_CONNECT_OK = WM_USER+10;
const
WM_CONNECT_FAIL = WM_USER+11;
const
WM_READ_CTRL_MSG = WM_USER+12;
{///===================?¤å?«¬©w?===================== }
const
MT_OPEN_CHANNEL = 'A';
const
MT_CLOSE_CHANNEL = 'B';
const
MT_CHANNEL_PHOT_DATA = 'C';
const
MT_FILE_PHOT_DATA = 'D';
const
MT_ALARM_MSG = '$';
const
MT_NET_TEST_REQU = '>';
const
MT_NET_TEST_RESP = '<';
{///====================??«¬©w?====================== }
const
FT_HEAD = 'H';
const
FT_IFRAME = 'I';
const
FT_BFRAME = 'B';
const
FT_PFRAME = 'P';
const
FT_AUDIO = 'A';
{///====================?ÌÛ©w?======================== }
{///?ªí??? }
type
HeadNode = record
nlNodeNum: LongInt;
lpNext: Pointer;
end {HeadNode};
{///?¤å??ÌÛ }
type
MsgHead = record
nlMsgBodyLen: LongInt;
cMsgType: Char;
cFrameType: Char;
stxPhotSourAddr: IN_ADDR;
usPhotServPort: U_SHORT;
nsChannel: SmallInt;
nsRouteNum: SmallInt;
nsNextPos: SmallInt;
{///??¦¬¨ì?¤å?¡Aªí¥Ü¥»??¦b¸ô¥Ñªíªº§Ç? }
{///?°e?¤å?¡A?§Ç??¼W1 }
{///eg: |·½??|¸ô¥Ñ??1|¸ô¥Ñ??2|...|¸ô¥Ñ??n|¥Øªº??| }
lTimeStamp: TIME_T;
nsWaitTime: SmallInt;
end {MsgHead};
{///¥DÉó«H®§?ÌÛ¡]°ß¤@??¤@¥x¥DÉó¡^ }
type
RouteInf = record
ulAddr: IN_ADDR;
usPort: U_SHORT;
end {RouteInf};
{///ºÝ¤f³q¹D«H®§¡]¥Î¤_¦V¥D?µ{?°e?±µ«H®§¡^ }
type
PortCh = record
case Word of
1: (struct: Integer;);
2: (nsChannel: SmallInt;);
3: (usPort: U_SHORT;);
end {PortCh};
{///==================function define================= }
type
APIENTRY GETROUTEINF(in_addr*lpRemoteAddr, u_short usRemotePort, ROUTEINF RouteInf[]); = int;
}: ///====================?ÌÛ©W?========================;
{///?¥X¥þ§½?¶q?ÌÛ }
GlobVar: TYPEDEF;
hWnd: HWND;
hReadEvent: THandle;
TransferListFile: Array[0..MAX_PATH-1] of Char;
ReconnListFile: Array[0..MAX_PATH-1] of Char;
LoclReqListFile: Array[0..MAX_PATH-1] of Char;
lpGetRouteInf: PGETROUTEINF;
lpLocalMsgHandle: PLOCALMSGHANDLE;
end {PortCh};
{///==================== Class define ======================== }
{///Initial SocketDll }
var
InitSocketDll: function {$IFDEF WIN32} stdcall {$ENDIF};
{///Transfer the global var address pointer }
var
GetGlobVarAddr: var function: VIRTUAL GLOBVAR {$IFDEF WIN32} stdcall {$ENDIF};
{///Start Server }
var
StartServ: function(ServPort: U_SHORT): VIRTUAL {$IFDEF WIN32} stdcall {$ENDIF};
{///Connect Server }
var
ConnectServ: function(var lpRemoteAddr: IN_ADDR;
usServPort: U_SHORT): VIRTUAL {$IFDEF WIN32} stdcall {$ENDIF};
var
ConnectServ: function(lpRemoteAddr: PChar;
usServPort: U_SHORT): VIRTUAL {$IFDEF WIN32} stdcall {$ENDIF};
{///Disconnect from server }
var
DisConnect: function(var lpRemoteAddr: IN_ADDR;
usServPort: U_SHORT): VIRTUAL {$IFDEF WIN32} stdcall {$ENDIF};
var
DisConnect: function(lpRemoteAddr: PChar;
usServPort: U_SHORT): VIRTUAL {$IFDEF WIN32} stdcall {$ENDIF};
{///Server send the photo data message }
var
ServSendPhotMsg: function(nsChannel: SmallInt;
cMsgType: Char;
cFrameType: Char;
var lpPhotData: BYTE;
niPhotDataLen: Integer) {$IFDEF WIN32} stdcall {$ENDIF};
{///Client send the control message }
var
ClientSendCtrlMsg: function(var lpPhotServAddr: IN_ADDR;
usPhotServPort: U_SHORT;
nsChannel: SmallInt;
cMsgType: Char;
var lpMsg: BYTE;
niMsgLen: Integer) {$IFDEF WIN32} stdcall {$ENDIF};
var
ClientSendCtrlMsg: function(lpPhotServAddr: PChar;
usPhotServPort: U_SHORT;
nsChannel: SmallInt;
cMsgType: Char;
var lpMsg: BYTE;
niMsgLen: Integer) {$IFDEF WIN32} stdcall {$ENDIF};
{///Server send the control message }
var
ServSendCtrlMsg: function(RouteInf[]: ROUTEINF;
niRouteNum: Integer;
nsChannel: SmallInt;
cMsgType: Char;
cFrameType: Char;
var lpMsg: BYTE;
niMsgLen: Integer) {$IFDEF WIN32} stdcall {$ENDIF};
{///Server send the alarm message }
var
ServSendAlarmMsg: function(nsChannel: SmallInt;
cMsgType: Char;
var lpMsg: BYTE;
niMsgLen: Integer) {$IFDEF WIN32} stdcall {$ENDIF};
{///Local Photo Request }
var
OpenChannelReq: function(var lpPhotSrcAddr: IN_ADDR;
usPhotSrcPort: U_SHORT;
nsChannel: U_SHORT) {$IFDEF WIN32} stdcall {$ENDIF};
var
OpenChannelReq: function(lpPhotSrcAddr: PChar;
usPhotSrcPort: U_SHORT;
nsChannel: U_SHORT) {$IFDEF WIN32} stdcall {$ENDIF};
{///Close channel Request }
var
CloseChannelReq: function(var lpPhotSrcAddr: IN_ADDR;
usPhotSrcPort: U_SHORT;
nsChannel: U_SHORT) {$IFDEF WIN32} stdcall {$ENDIF};
var
CloseChannelReq: function(lpPhotSrcAddr: PChar;
usPhotSrcPort: U_SHORT;
nsChannel: U_SHORT) {$IFDEF WIN32} stdcall {$ENDIF};
{///reverse the route information in the message body }
var
ReverseRouteInf: function(nsRouteNum: SmallInt;
RouteInf[]: ROUTEINF) {$IFDEF WIN32} stdcall {$ENDIF};
{///read the control message from list }
var
ReadCtrlMsg: function(var lpMsgHead: MSGHEAD;
var lpMsgBody: BYTE): VIRTUAL {$IFDEF WIN32} stdcall {$ENDIF};
以上介绍了“ 请教调用VC创建的DLL的相关问题”的问题解答,希望对有需要的网友有所帮助。
本文网址链接:http://www.codes51.com/itwd/3594803.html