全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1060
推到 Plurk!
推到 Facebook!

D4業界標準手冊的範例程式執行有錯...Dll相關

尚未結案
furesu
一般會員


發表:26
回覆:10
積分:7
註冊:2004-10-22

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-10-12 14:13:32 IP:221.169.xxx.xxx 未訂閱
技術篇的第9章 [ModalDll] CalendarLib.dpr
library CalendarLib;    uses
  ShareMem,
  SysUtils,
  Classes,
  DLLFrm in 'DLLFrm.pas' {DLLForm};    exports
  ShowCalendar;
  
begin
end. 
DLLFrm.pas
unit DLLFrm;    interface    uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, Grids, Calendar;    type      TDLLForm = class(TForm)
    calDllCalendar: TCalendar;
    procedure calDllCalendarDblClick(Sender: TObject);
  end;    { Declare the export function }
function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime; StdCall;    implementation
{$R *.DFM}    function ShowCalendar(AHandle: THandle; ACaption: String): TDateTime;
var
  DLLForm: TDllForm;
begin
  // Copy application handle to DLL's TApplication object
  Application.Handle := AHandle;
  DLLForm := TDLLForm.Create(Application); 
  try
    DLLForm.Caption := ACaption;
    DLLForm.ShowModal;
    Result := DLLForm.calDLLCalendar.CalendarDate; // Pass the date back in Result
  finally
    DLLForm.Free;
  end;
end;    procedure TDLLForm.calDllCalendarDblClick(Sender: TObject);
begin
  Close;
end;    end. 
MainFfm.pas
unit MainFfm;    interface    uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls;    type
  { First, define a procedural data type, this should reflect the
    procedure that is exported from the DLL. }
  TShowCalendar = function (AHandle: THandle; ACaption: String): TDateTime; StdCall;      { Create a new exception class to refect a failed DLL load }
  EDLLLoadError = class(Exception);      TMainForm = class(TForm)
    lblDate: TLabel;
    btnGetCalendar: TButton;
    procedure btnGetCalendarClick(Sender: TObject);
  end;    var
  MainForm: TMainForm;    implementation    {$R *.DFM}    procedure TMainForm.btnGetCalendarClick(Sender: TObject);
var
  LibHandle   : THandle;
  ShowCalendar: TShowCalendar;
begin      { Attempt to load the DLL }
  LibHandle := LoadLibrary('CALENDARLIB.DLL');
  try
    { If the load failed, LibHandle will be zero.
      If this occurs, raise an exception. }
    if LibHandle = 0 then
      raise EDLLLoadError.Create('Unable to Load DLL');
    { If the code makes it here, the DLL loaded successfully, now obtain
      the link to the DLL's exported function so that it can be called. }
    @ShowCalendar := GetProcAddress(LibHandle, 'ShowCalendar');
    { If the function is imported successfully, then set lblDate.Caption to reflect
      the returned date from the function. Otherwise, show the return raise
      an exception. }
    if not (@ShowCalendar = nil) then
      lblDate.Caption := DateToStr(ShowCalendar(Application.Handle, Caption))
    else
      RaiseLastWin32Error;                                                               
  finally
    FreeLibrary(LibHandle); // Unload the DLL.
  end;
end;    end. 
我是直接拿範例程式執行(有改掉唯讀),執行時按btnGetCalendarClick show一個日期From ,在日期Form上點2下回傳回日期show在MainForm上,值是正常傳回 ,可是會跑出一個0x00000000記憶體不能為read的錯誤訊息.. 於是我在MainForm上use ShareMem,重新complier MainForm(D7),用新的MainForm去執行,值傳回到MainForm上時的錯誤訊息不會出現了,可是在關閉程式出現另一個例外錯誤訊息..軟體例外錯誤(0x0eedfade)於位置0x77e6a4e1..[RunTime Error 217 at 00413DD] 有人可以指點一下嗎??
wyndog
資深會員


發表:7
回覆:362
積分:348
註冊:2004-10-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-10-13 10:35:55 IP:60.248.xxx.xxx 未訂閱
你的 Project 裡頭也要加 Uses ShareMem;    用 Project -> View Source 看到這個,改成這樣
program Project1;    uses
  ShareMem,
  Forms,
  Unit1 in 'Unit1.pas' {Form1};    {$R *.res}    begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
furesu
一般會員


發表:26
回覆:10
積分:7
註冊:2004-10-22

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-10-13 11:50:17 IP:221.169.xxx.xxx 未訂閱
引言: 你的 Project 裡頭也要加 Uses ShareMem; 用 Project -> View Source 看到這個,改成這樣
program Project1;    uses
  ShareMem,
  Forms,
  Unit1 in 'Unit1.pas' {Form1};    {$R *.res}    begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
喔,原來ShareMem是加在那裡, 有繼續研究下去的動力了..
系統時間:2024-06-27 16:09:21
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!