unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
ahandle:Thandle;
public
{ Public declarations }
end; Procedure ShowFrom(vHandle:THandle);stdcall External 'Videocap.dll';
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
Var P:TFarProc;
begin
Try
ahandle := LoadLibrary('Videocap.dll');
If ahandle = 0 Then
begin
Raise Exception.Create('没有找到Videocap.dll');
Exit;
end;
P := GetProcAddress(ahandle,'ShowFrom');
If P = Nil Then
begin
Raise Exception.Create('Videocap.dll中没有该函数');
Exit;
end;
ShowFrom(Application.Handle);
Finally
FreeLibrary(ahandle);
End;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
close;
end;end.
运行时报“project Project1.exe raised exception class EOleSysError with message '尚未调用CoInitialize'.”
因为我的DLL不是Com类型,所以不用注册,那么这个到底是什么问题呢?
我在别的机子上运行也是出现这个问题。
帮帮忙
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;type
TForm1 = class(TForm)
BitBtn1: TBitBtn;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
ahandle:Thandle;
public
{ Public declarations }
end; Procedure ShowFrom(vHandle:THandle);stdcall External 'Videocap.dll';
var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
Var P:TFarProc;
begin
Try
ahandle := LoadLibrary('Videocap.dll');
If ahandle = 0 Then
begin
Raise Exception.Create('没有找到Videocap.dll');
Exit;
end;
P := GetProcAddress(ahandle,'ShowFrom');
If P = Nil Then
begin
Raise Exception.Create('Videocap.dll中没有该函数');
Exit;
end;
ShowFrom(Application.Handle);
Finally
FreeLibrary(ahandle);
End;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
close;
end;end.
运行时报“project Project1.exe raised exception class EOleSysError with message '尚未调用CoInitialize'.”
因为我的DLL不是Com类型,所以不用注册,那么这个到底是什么问题呢?
我在别的机子上运行也是出现这个问题。
帮帮忙
Procedure ShowFrom(vHandle:THandle);stdcall External 'Videocap.dll';
但你的formCreate里动态调用了,不如换成动态调用的申明吧
Type TdllProc=Procedure ShowFrom(vHandle:THandle);stdcall External 'Videocap.dll';
string) ;Stdcall;
var
dllproc: Tdllproc;
ahandle :THandle;
begin
Try
ahandle := LoadLibrary('Videocap.dll');
If ahandle = 0 Then
begin
Raise Exception.Create('没有找到Videocap.dll');
Exit;
end;
@dllproc := GetProcAddress(ahandle,'ShowFrom');
If dllproc = Nil Then
begin
Raise Exception.Create('Videocap.dll中没有该函数');
Exit;
end;
ShowFrom(Application.Handle);
Finally
FreeLibrary(ahandle);
End;
end;
procedure TForm1.FormCreate(Sender: TObject);
Type TdllProc=Procedure ShowFrom(vHandle:THandle);stdcall External 'Videocap.dll';
string) ;Stdcall;
var
dllproc: Tdllproc;
ahandle :THandle;
begin
Try
ahandle := LoadLibrary('Videocap.dll');
If ahandle = 0 Then
begin
Raise Exception.Create('没有找到Videocap.dll');
Exit;
end;
@dllproc := GetProcAddress(ahandle,'ShowFrom');
If not (Assigned(dllProc)) Then
begin
showmessage('............');
Exit;
end;
ShowFrom(Application.Handle);
Finally
FreeLibrary(ahandle);
End;
end;
@P := GetProcAddress(ahandle,'ShowFrom');
吧?
是缺少了一个引用:OleCtrls.
如果引用这个单元然后运行就可以的。
但是如果把窗体关闭的话就会出错。
请帮帮忙
那也许就不是DLL本身的问题了
也许是对象没有正确的释放!你在看看其他的地方代码是否完善
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls;
type
TDLLProc = Procedure (h:THandle);stdcall;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
vHandle : THandle;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
Var vShowFrom : TDLLProc;
begin
vHandle := LoadLibrary('VideoCap.dll');
If vHandle = 0 Then
begin
Raise Exception.Create('没找到VideoCap.dll');
Exit;
end;
@vShowFrom := GetProcAddress(vHandle,'vShowFrom');
If @vShowFrom = Nil Then
begin
Raise Exception.Create('VideoCap.dll中没有vShowFrom函数');
Exit;
end;
vShowFrom(Application.Handle);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
FreeLibrary(vHandle);
end;end.
现在的问题是在我的机子上可以好好的运行,也没有报错。
但是拿到别的机子上运行时报“project Project1.exe raised exception class EOleSysError with message '尚未调用CoInitialize'.”为什么会这样呢?