请问:FreeLibrary在win98和win2K下的用法是不是不一样呀?(50分)

  • 主题发起人 主题发起人 jianguobu
  • 开始时间 开始时间
J

jianguobu

Unregistered / Unconfirmed
GUEST, unregistred user!
我在98下正常,而在2k下报错。
 
什么错误?<br>GetLastError看<br><br>-----<br>http://www.8421.org
 
报错信息是:"0x00000000"指令引用的"0x00000000"内存,该内存不能为"read".
 
不可能吧.<br><br>能不能贴点源码?<br>
 
library CalendarLib;<br><br>uses<br>&nbsp; ShareMem,<br>&nbsp; SysUtils,<br>&nbsp; Classes,<br>&nbsp; DLLFrm in 'DLLFrm.pas' {DLLForm},<br>&nbsp; LoginFrm in 'LoginFrm.pas' {LoginForm};<br><br>exports<br>&nbsp; ShowCalendar,<br>&nbsp; Password;<br><br>begin<br>end.<br><br>unit LoginFrm;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br>&nbsp; Dialogs, StdCtrls;<br><br>type<br>&nbsp; TLoginForm = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; Edit1: TEdit;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>{ Declare the export function }<br>function Password(AHandle: THandle; ACaption: String; Password:string): Pchar; StdCall;<br><br>implementation<br><br>{$R *.dfm}<br><br>function Password(AHandle: THandle; ACaption: String; Password:string): Pchar;<br>var<br>&nbsp; LoginForm: TLoginForm;<br>begin<br>&nbsp; Application.Handle := AHandle;<br>&nbsp; LoginForm := TLoginForm.Create(Application);<br>&nbsp; try<br>&nbsp; &nbsp; LoginForm.Caption := ACaption;<br>&nbsp; &nbsp; LoginForm.ShowModal;<br>&nbsp; &nbsp; Result := Pchar(LoginForm.Edit1.text); <br>&nbsp; finally<br>&nbsp; &nbsp; LoginForm.Free;<br>&nbsp; end;<br>end;<br><br>procedure TLoginForm.Button1Click(Sender: TObject);<br>begin<br>&nbsp; Close;<br>end;<br><br>end.<br><br>unit DLLFrm;<br><br>interface<br><br>uses<br>&nbsp; SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,<br>&nbsp; Forms, Dialogs, Grids, Calendar;<br><br>type<br><br>&nbsp; TDLLForm = class(TForm)<br>&nbsp; &nbsp; calDllCalendar: TCalendar;<br>&nbsp; &nbsp; procedure calDllCalendarDblClick(Sender: TObject);<br>&nbsp; end;<br><br>{ Declare the export function }<br>function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;<br><br>implementation<br>{$R *.DFM}<br><br>function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;<br>var<br>&nbsp; DLLForm: TDllForm;<br>begin<br>&nbsp; Application.Handle := AHandle;<br>&nbsp; DLLForm := TDLLForm.Create(Application); <br>&nbsp; try<br>&nbsp; &nbsp; DLLForm.Caption := ACaption;<br>&nbsp; &nbsp; DLLForm.ShowModal;<br>&nbsp; &nbsp; Result := DLLForm.calDLLCalendar.CalendarDate;<br>&nbsp; finally<br>&nbsp; &nbsp; DLLForm.Free;<br>&nbsp; end;<br>end;<br><br>procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);<br>begin<br>&nbsp; Close;<br>end;<br><br>end.<br><br>unit MainFfm;<br><br>interface<br><br>uses<br>&nbsp; SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,<br>&nbsp; Forms, Dialogs, StdCtrls;<br><br>type<br>&nbsp; TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall;<br>&nbsp; TPassword = function (AHandle: THandle; ACaption: String; Password:string): Pchar; StdCall;<br><br>&nbsp; EDLLLoadError = class(Exception);<br><br>&nbsp; TMainForm = class(TForm)<br>&nbsp; &nbsp; lblDate: TLabel;<br>&nbsp; &nbsp; btnGetCalendar: TButton;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure btnGetCalendarClick(Sender: TObject);<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; end;<br><br>var<br>&nbsp; MainForm: TMainForm;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TMainForm.btnGetCalendarClick(Sender: TObject);<br>var<br>&nbsp; LibHandle &nbsp; : THandle;<br>&nbsp; ShowCalendar: TShowCalendar;<br>begin<br><br>&nbsp; LibHandle := LoadLibrary('CALENDARLIB.DLL');<br>&nbsp; try<br>&nbsp; &nbsp; if LibHandle = 0 then<br>&nbsp; &nbsp; &nbsp; raise EDLLLoadError.Create('Unable to Load DLL');<br>&nbsp; &nbsp; @ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');<br>&nbsp; &nbsp; if not (@ShowCalendar = nil) then<br>&nbsp; &nbsp; &nbsp; lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; RaiseLastWin32Error;<br>&nbsp; finally<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; FreeLibrary(LibHandle);<br>&nbsp; &nbsp;end;<br>&nbsp; end;<br>end;<br><br>procedure TMainForm.Button1Click(Sender: TObject);<br>var<br>&nbsp; LibHandle &nbsp; : THandle;<br>&nbsp; Password &nbsp; &nbsp;: TPassword;<br>begin<br>&nbsp; LibHandle := LoadLibrary('CALENDARLIB.DLL');<br>&nbsp; try<br>&nbsp; &nbsp; if LibHandle = 0 then<br>&nbsp; &nbsp; &nbsp; raise EDLLLoadError.Create('不能装载Dll文件');<br>&nbsp; &nbsp; @Password := GetProcAddress(LibHandle, 'Password');<br>&nbsp; &nbsp; if not (@Password = nil) then<br>&nbsp; &nbsp; &nbsp; showmessage(Password(Application.Handle, Caption,''))<br>&nbsp; &nbsp; else<br>&nbsp; &nbsp; &nbsp; RaiseLastWin32Error;<br>&nbsp; finally<br>&nbsp; &nbsp; FreeLibrary(LibHandle);<br>&nbsp; end;<br>end;<br><br>end.
 
可能是string参数的问题,lib要 uses ShareMem,请看ShareMem帮助<br>我一般用pchar,都没有问题
 
接受答案了
 
后退
顶部