我想写个exe导出函数,但是老出问题,而改为dll就没有问题了。
下面是我的程序,请大家帮忙看看:
exe导出函数程序:
//library Project1;
program Project1;
uses
windows,sysutils;
//{$R *.RES}
function TestDll(i:integer):integer;stdcall;
begin
MessageBox(0,pchar(IntToStr(i)),'dll msg',0);
Result:=i;
end;exports
TestDll;begin
end.下面是调用的程序:
program Project2;{$IMAGEBASE $13140000}uses
Windows,Sysutils;type
TIntFunc=function(i:integer):integer;stdcall;function Main: longword;
var
hModal:longint;
Tp:TFarProc;
Tf:TIntFunc;
begin
hModal:=Loadlibrary('g:\Project1.exe');
if hModal = 0 then
MessageBox(0,'failed to load dll !','',0)
else
begin
MessageBox(0,'DLL loaded!','',0) ;
Tp:=getprocaddress(hModal,'TestDll');
if Tp<>nil
then begin
MessageBox(0,'function found!','',0);
Tf:=TIntFunc(Tp);
tf(1);
end
else
MessageBox(0,'no function found!','',0);
Freelibrary(hModal);
end;
Result := 0;
end;begin
Main;end.如果把program改成library,编译成dll,那么就没有问题。
而如果编译成exe,调用的时候会出现access violation问题,请问怎么解决?
下面是我的程序,请大家帮忙看看:
exe导出函数程序:
//library Project1;
program Project1;
uses
windows,sysutils;
//{$R *.RES}
function TestDll(i:integer):integer;stdcall;
begin
MessageBox(0,pchar(IntToStr(i)),'dll msg',0);
Result:=i;
end;exports
TestDll;begin
end.下面是调用的程序:
program Project2;{$IMAGEBASE $13140000}uses
Windows,Sysutils;type
TIntFunc=function(i:integer):integer;stdcall;function Main: longword;
var
hModal:longint;
Tp:TFarProc;
Tf:TIntFunc;
begin
hModal:=Loadlibrary('g:\Project1.exe');
if hModal = 0 then
MessageBox(0,'failed to load dll !','',0)
else
begin
MessageBox(0,'DLL loaded!','',0) ;
Tp:=getprocaddress(hModal,'TestDll');
if Tp<>nil
then begin
MessageBox(0,'function found!','',0);
Tf:=TIntFunc(Tp);
tf(1);
end
else
MessageBox(0,'no function found!','',0);
Freelibrary(hModal);
end;
Result := 0;
end;begin
Main;end.如果把program改成library,编译成dll,那么就没有问题。
而如果编译成exe,调用的时候会出现access violation问题,请问怎么解决?
http://www.52blog.net/user5/148501/archives/2007/1111745.shtml
这里的资料或许有点用
使用dll,没有任何问题,而使用exe,则会出问题,能够加载并找到TestDll函数,但是在调用这个函数的时候就会出问题。
程序很简单,就上面的代码,第一个保存为project1.dpr,第二个保存为project2.dpr,希望有时间的朋友帮调试一下,我实在是没找到问题出在哪里。
楼主查一下返回值看看。