看看我的程序,为什么会出错?(关于VC调用DELPHI写的DLL)(100分)

L

lwaif

Unregistered / Unconfirmed
GUEST, unregistred user!
Project2.dpr:
library Project2;
uses
SysUtils,
Classes,
Unit1 in 'Unit1.pas' {Form1};
{$R *.res}
exports
LoadAboutbox;
begin
end.
Unit1.pas:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
ADOQuery1: TADOQuery;
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
var
WAbout: TForm1;
procedure LoadAboutbox(Handle: THandle); export;
implementation
{$R *.dfm}
procedure LoadAboutbox(Handle: THandle); //-- Our procedure
begin
Application.Handle := Handle; //-- Delphi default application calling
WAbout := TForm1.Create(Application); //-- Create the Aboutbox
try
WAbout.ShowModal; //-- Try and make the Aboutbox modal
finally //-- Finally, or "make sure"
WAbout.Free; //-- Unload the instance of our Aboutbox
end;
end;
end.
VC调用的事件:
void CMainFrame::Onyyyyy()
{
HINSTANCE hinstDlltest =::LoadLibrary("Project2.dll");
if (hinstDlltest!= NULL)
{
typedef BOOL (WINAPI *MYFUNC)(HWND);
MYFUNC fun = NULL;
fun=(MYFUNC)::GetProcAddress(hinstDlltest, "LoadAboutbox");
if(fun)
fun(this->GetSafeHwnd());
}
FreeLibrary(hinstDlltest);
}
VC程序编译没问题,也可以运行,调用事件就弹出错误窗口并退出了
不知道问题在哪里啊?
 
呵呵,我也是,不过我是DELPHI调用BC++写的DLL,调用没问题,但是程序退出就有错误,不解,听课!
 
看楼上的兄弟用的是普通DLL吧?用COM对象吧,面向对象的,
不会出错,比普通DLL强了不要太多了……呵呵
 
procedure LoadAboutbox(Handle: THandle); stdcall;
 
tseug说得对!
函数调用一定要指明入栈方式;在引用时必须和编写的入栈方式相对应。
例如:静态连编时,delphi定义的函数入栈方式为stdcall,在Vc内引用时也要声明为
stdcall。当然,动态引入DLL时,可试着将delphi定义的库函数设为Cdecl。这是C的风格。
其实,入栈方式很多,register、pascal、cdecl、stdcall、safecall。详细可看delphi
的帮助,查关键字stdcall就行。我只凭记忆写的不见得很准确!
 
嘿嘿,COM对象正在琢磨中,关于入栈方式,我是对的,肯定没错,我怀疑是共享内存的问题,不过我写的
里面只有一个返回boolean类型的函数,也不行,真不明白!继续听课
 
用COM对象是可以,但下面的控件却不能使用,去掉了就没问题
ADOQuery1: TADOQuery;
要使用这个控件是不是得动态生成呢?
 
建议:
ADOQUERY1 在程序中创建
另外需要引用ACTIVEX单元,在创建ADOQUERY1之前进行初始化,添加如下语句
coinitialize(nil);
 
在调用DLL中的代码有时出现问题, 除了共享内存的的问题外(影响String和动态数组,Variant)
还有就是RTTI的问题, 这种情况下如果主程序中使用了is, as等都不会返回正确的结果。
 

WAbout := TForm1.Create(Application);
改成
WAbout := TForm1.Create(nil);
 
否则退出时会因多次释放报地址冲突(因为窗体在 Application 里注册了,不能简单 Free)。
 
多人接受答案了。
 
顶部