這段代碼有什么錯誤?

LiangWu25 2005-02-03 09:31:57
TestDll:
TestDll.c :
==================================
int Test(int a)
{
return 10;
}
==================================
TestDll.def:
===================================
LIBRARY "TestDll.Dll"

EXPORTS
Test @1;
============================================================
Delphi調用代碼:
TTest = function(a:integer): integer; stdcall;
var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.Button2Click(Sender: TObject);
Var
ChgFunc:TTest;
DllHandle:THandle;
DllName:string;
Ack:Byte;
begin
DllName:='TestDll.dll';
DllHandle:=LoadLibrary(PChar(DllName));
Ack:=0;
If DllHandle>0 then
Begin
@ChgFunc:=GetProcAddress(DllHandle,'Test');
If Assigned(@ChgFunc) then
Begin
Ack:=ChgFunc(0);
End;
End;
ShowMessage(inttostr(Ack));
FreeLibrary(DllHandle);
end;
==================================
Delphi中調用時,ShowMessage顯示為10,但是調用完成后,出現"應用程序錯誤,即將關閉“這樣的錯誤提示,然后應用程序非法關閉了。這是怎么回事?是否這個Dll有什么問題?
...全文
119 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
LiangWu25 2005-02-03
  • 打赏
  • 举报
回复
Thanks!謝謝各位的答復。結貼了!
lemon520 2005-02-03
  • 打赏
  • 举报
回复
#include <Windows.h>

int WINAPI Test(int a)
{
return 10;
}
这样是没有错的,因为 WINAPI这个宏本身就是 #define WINAPI __stdcall
lemon520 2005-02-03
  • 打赏
  • 举报
回复
http://www.allaboutprogram.com/index.php?option=content&task=view&id=29
这个讲得很清楚的
LiangWu25 2005-02-03
  • 打赏
  • 举报
回复
lemon520:
謝謝你的提醒,要加__stdcall,然后在Delphi中調用就無錯了!不過有沒有相關的鏈接,能否告訴我是怎么回事?呵呵

另外,如果如果test.c為
#include <Windows.h>

int WINAPI Test(int a)
{
return 10;
}
這樣也是無錯的。
lemon520 2005-02-03
  • 打赏
  • 举报
回复
关键就在函数调用约定上。
参数压栈的方式不一样。
当没有参数(为void)时,就不存在这样的区别了,所以就不会出现问题。
LiangWu25 2005-02-03
  • 打赏
  • 举报
回复
CMyMfc:Ack改為integer型結果是一樣的,inttostr是Delphi的函數,可以把integer型轉為string,Delphi也是強類型語言,不這樣就不能夠Showmessage了,不過也不是這里的原因。
LiangWu25 2005-02-03
  • 打赏
  • 举报
回复
xiangwangz,你說的沒錯,我在網上搜索的代碼都和你的代碼差不多,我嫌麻煩便改了一下:)
不過問題不是出在這里,我還不清楚原因。
xiangwangz 2005-02-03
  • 打赏
  • 举报
回复
//没试过你的,不过我的是经过测试的
呵呵,我以前用delphi多,上面的可以作为动态调用DLL标准形式!!
LiangWu25 2005-02-03
  • 打赏
  • 举报
回复
xiangwangz的Delphi代碼其實和我的區別不大啊
LiangWu25 2005-02-03
  • 打赏
  • 举报
回复
在中間加__stdcall可以!而且我試過如果這樣定義
int Test(void)
{
return 10;
}
即不傳任何參數進來,也沒有問題,這是怎么回事呢?
shenzhen_1 2005-02-03
  • 打赏
  • 举报
回复
Up!
xiangwangz 2005-02-03
  • 打赏
  • 举报
回复
//c中的无错,delphi中的动态调用如下
procedure TForm1.Button1Click(Sender: TObject);
type
TMain =function (a:integer):integer;stdcall;
var main:tmain;
myhandle:thandle;
begin
myhandle:= LoadLibrary('testdll.dll');
if myhandle<=0 then
raise exception.Create('DLL调用失败,错误代码为:'+inttostr(getlasterror))
else
@main:=GetProcAddress(myhandle, 'Test');
if not assigned(main) then
raise exception.Create('GetProcAddress调用失败,错误代码:'+inttostr(getlasterror))
else //具体操作
begin
showmessage(inttostr(main(1)));
end;
freelibrary(myhandle);
end;
lemon520 2005-02-03
  • 打赏
  • 举报
回复
把int Test(int a)
{
return 10;
}
改为int __stdcall Test(int a)
{
return 10;
}
试试?
boatzm 2005-02-03
  • 打赏
  • 举报
回复
@ChgFunc:=GetProcAddress(DllHandle,'Test');
If Assigned(@ChgFunc) then
Begin
Ack:=ChgFunc(0);
End;

没有必要用@
ChgFunc本来就是一个函数指针。加个取地址没有用,去掉试试?~

还有主要Delphi定义的调用方式和C 的是否一样,
Test = function(a:integer): integer; stdcall;

C 的也始StdCal 才可以哦~~~
CMyMfc 2005-02-03
  • 打赏
  • 举报
回复
dll没错啊, 跟踪检察下dephi, 我看到ACK 定义为Byte类型, 虽然这样应该也没错, 你改为整型试试呢,还有inttostr, 转到的str存到哪个内存中?呵呵我对dephi一窍不通
daylove 2005-02-03
  • 打赏
  • 举报
回复
delphi?
不知道

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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