K
kin.ming
Unregistered / Unconfirmed
GUEST, unregistred user!
主程序中的代码:
DLL 中的相应函数的代码:
问题:
如果没有 TForm1.btn2Click() 中的 IntToStr() 一行,程序就会报错(非法访问),没有正确的结果。为什么?
代码:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ToolWin, ComCtrls, StdCtrls, ImgList, ExtCtrls;
const
TB_ADDBUTTONS = WM_USER+20;
type
itbbutton = record
iBitmap: Cardinal;
idCommand: Integer;
fsState: Byte;
fsStyle: Byte;
dwData: DWORD ;
iString: Integer ;
end;
TForm1 = class(TForm)
clbr1: TCoolBar;
tlb1: TToolBar;
mm1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
btn2: TButton;
il1: TImageList;
btn3: TButton;
img1: TImage;
procedure Exit1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
procedure btn3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
c_toolbarbutton: function: itbbutton
cdecl;
ibtn: itbbutton;
hInst: HMODULE;
ibmp: TBitmap;
idx: Integer;
implementation
{$R *.dfm}
procedure TForm1.Exit1Click(Sender: TObject);
begin
close;
end;
//从DLL中接收一个TBBUTTON,然后在 Toolbar 上添加相应的按钮
procedure TForm1.btn2Click(Sender: TObject);
begin
hInst := LoadLibrary(PChar('wxdll.dll'));
if (hInst<>0) then
begin
c_toolbarbutton := GetProcAddress(hInst,'ToolbarButton');
ibtn:= c_toolbarbutton();
IntToStr(40);
// ShowMessage(IntToStr(ibtn.iBitmap)+#13+inttostr(ibtn.idcommand));
// ShowMessage('hey')
//** This is not OK either.
ibmp := TBitmap.Create;
ibmp.LoadFromResourceID(hInst,ibtn.iBitmap);
idx:=il1.Add(ibmp,nil);
ibtn.iBitmap :=idx;
SendMessage(tlb1.Handle, TB_ADDBUTTONS, 1, LongInt(@ibtn));
end;
end;
procedure TForm1.btn3Click(Sender: TObject);
begin
if(Assigned(ibmp)) then
ibmp.Free ;
if(hinst<>0) then
FreeLibrary(hinst);
close;
end;
end.
DLL 中的相应函数的代码:
代码:
extern "C" DLLFUNCTIONS_API TBBUTTON ToolbarButton()
{
TBBUTTON tbb;
ZeroMemory(&tbb, sizeof(tbb))
tbb.iBitmap = IDTOOL_BMP ;
tbb.fsState = TBSTATE_ENABLED;
tbb.fsStyle = TBSTYLE_BUTTON;
tbb.idCommand = ID_DO_ASTYLE;
return tbb;
}
问题:
如果没有 TForm1.btn2Click() 中的 IntToStr() 一行,程序就会报错(非法访问),没有正确的结果。为什么?