delphi 调用C++的DLL问题,急!(送500分)

ljl0206 2005-04-10 02:05:12
http://njhuadian.com/Mpeg4Decoder.dll
http://njhuadian.com/Mpeg4Decoder.h
http://njhuadian.com/Mpeg4Decoder.lib

文件在上面下载,我调用的时间总是提示找不到入口地址,请高手帮忙看一下什么原因,给一个最简单成功调用的例子
...全文
423 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
deng_dengUp 2005-04-12
  • 打赏
  • 举报
回复
如果使用导出类可能不行,最好还是把导出类改成到处函数,或者com我以前也是过导出类数据结构不符合调用要求,接口不统一,最好改写成com
wangk 2005-04-12
  • 打赏
  • 举报
回复
把.H的头文件,改写成.pas。不过很麻烦,汗~。
柯本 2005-04-12
  • 打赏
  • 举报
回复
to : muroachanf(菜鸟中的霸王)
-------------------------------------------------------------------------------------
在vc里面用extern "C"修饰,必须是__stdcal调用方式,必须使用def文件来生成输出函数借口
--------------------------------------------------------------------------------------
在delphi中调用时,可以是__cdecl方式,且不用def文件,我一直这样用的(更方便些)
上面的程序当然也经过调试,没有问题

fvan 2005-04-12
  • 打赏
  • 举报
回复
把它改写成函数的形式咯,导出类的dll对其他语言的调用不是很兼容阿。找不到接口是自然的
柯本 2005-04-11
  • 打赏
  • 举报
回复
这只是一个方法,函数定义很多要你自已去写,C++的常量也要换成delphi的方式
其中
Initm_pDecoder/UnInitm_pDecoder是我自已加的,
Initm_pDecoder主要为VC++初始化m_pDecoder类
因为m_pDecoder是类指针,只能在VC中初始化
同理UnInitm_pDecoder为释放m_pDecoder用
柯本 2005-04-11
  • 打赏
  • 举报
回复
1.建一VC DLL项目, 如DLL.dsw内容如下:
// dll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include "Mpeg4Decoder.h"
#pragma comment (lib,"Mpeg4Decoder.lib")

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" __declspec(dllexport) void Initm_pDecoder()
{
m_pDecoder = new CMpeg4Decoder();
}
extern "C" __declspec(dllexport) void UnInitm_pDecoder()
{
delete m_pDecoder ;
}

extern "C" __declspec(dllexport) bool InitDecoder()
{
return m_pDecoder->InitDecoder();

}
extern "C" __declspec(dllexport) void SetUserName(char*sName)
{
m_pDecoder->SetUserName(sName);
}
//...其它的自已定义
编译成dll.dll,并拷放delphi项目的目录中
2.在delphi中写:

unit test;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

procedure Initm_pDecoder;cdecl;
procedure UnInitm_pDecoder;cdecl;
function InitDecoder:boolean;cdecl;
procedure SetUserName(sName:pchar);cdecl;
var
Form1: TForm1;

implementation
{$R *.dfm}
procedure Initm_pDecoder;cdecl; external 'dll.dll';
procedure UnInitm_pDecoder;cdecl; external 'dll.dll';
function InitDecoder:boolean;cdecl;external 'dll.dll';
procedure SetUserName(sName:pchar);cdecl;external 'dll.dll';

procedure TForm1.Button1Click(Sender: TObject);
begin
Initm_pDecoder;
InitDecoder;
SetUserName('test');

// ...
UnInitm_pDecoder;
end;

end.

muroachanf 2005-04-11
  • 打赏
  • 举报
回复
在vc里面用extern "C"修饰,必须是__stdcal调用方式,必须使用def文件来生成输出函数借口(dllexport关键字的做法我没弄成功过,后来懒得研究了),你的代码俺就不看了,做到了就绝对不可能无法调用,当然还有其他原因,比如结构体不一致(字节对齐问题,建议全按1字节对齐),参数入栈方式不一致(比如你用了fastcall方式),对象引用(这个我就不说了,两者之间几乎没有任何共通点,打算这样用的人嘿嘿,自己专研吧)。另外c++里面的指针,可以在delphi里面直接用var修饰,也可以用指针,还有一些细节问题,比如什么资源问题啦,狗屎问题啦,就自行体会啦,我也讲不了那么多。
ljl0206 2005-04-10
  • 打赏
  • 举报
回复
不会用VC,给个简单的例子,调用一个函数就行
柯本 2005-04-10
  • 打赏
  • 举报
回复
我看了一下,该DLL导出的VC的类,delphi是不能找不到入口地址的,用tdum看看它的导出函数就知了
RVA Ord. Hint Name
-------- ---- ---- ----
00004DA0 1 0000 ??0CMpeg4Decoder@@QAE@XZ
00004DC0 2 0001 ??1CMpeg4Decoder@@QAE@XZ
00004CD0 3 0002 ??4CMpeg4Decoder@@QAEAAV0@ABV0@@Z
00005250 4 0003 ?BeginSaveToFile@CMpeg4Decoder@@QAEHPAD@Z
000052F0 5 0004 ?CaptureFrameToBmp@CMpeg4Decoder@@QAE_NPAD@Z
00005360 6 0005 ?CloseSerial232Device@CMpeg4Decoder@@QAE_NXZ
000051D0 7 0006 ?ConnectStream@CMpeg4Decoder@@QAE_NHH@Z
00005240 8 0007 ?DisConnectStream@CMpeg4Decoder@@QAEXXZ
.....
(记得以前CSDN的BCB版中有人问过此类问题,BCB怎样调VC++带类的DLL)
解决的方法是,用VC++重新写一DLL,调用上面DLL中的函数,而导出函数用
extern "C" 来定义,这样delphi就可调用了(只是多了一个DLL,作为接口)
oyljerry 2005-04-10
  • 打赏
  • 举报
回复
参考Delphi调用DLL的方法
ljl0206 2005-04-10
  • 打赏
  • 举报
回复
在线等。。。

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧