Einzelnen Beitrag anzeigen

Benutzerbild von maximus Caesar
maximus Caesar

Registriert seit: 26. Sep 2004
Ort: Biberach
319 Beiträge
 
Turbo Delphi für Win32
 
#1

dll - gemeinsam genutzte Unit - OOP - Problem

  Alt 7. Nov 2004, 18:38
Hab ein kleines Problem:
Ich will eine Art Plugin für meine Anwendung schreiben.
Das Tutorial, dass hier so oft genannt wird habe ich schon.
Also, inch habe ein Hauptprogramm, eine dll und eine Unit, auf die Beide zugreifen.

Code in der Unit:

Delphi-Quellcode:
unit testUnit;

interface

type
TTestObjekt = class
public
 String1:String;
 Strign2:String
end;

implementation

end.

Text in der dll:

Delphi-Quellcode:
library Project1;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }


uses
  SysUtils,
  Classes,
  testUnit in '..\shared Units\testUnit.pas';

  procedure test; stdcall;
  var obj:TTestObjekt;
  begin
   obj.Create;
   obj.String1:='FUNKTIONIERT!!!!!!!';
  end;

  exports
   test;

{$R *.res}

begin
end.

Text im Programm:

Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TShellexecute = procedure; stdcall;
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    procedure Button3Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure calldll;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;


implementation

uses testUnit;

procedure TForm1.calldll;
var
  hLib: cardinal;
  MyShellExecute: TShellexecute;
begin
 hLib := LoadLibrary('test.dll');
 if hLib<>0 then
 begin
 @MyShellexecute := GetProcAddress(hLib, 'test');
    if not Assigned(MyShellexecute) then
    begin
      RaiseLastOSError;
      exit;
    end;
  end
  else
  begin
    RaiseLastOSError;
    exit;
  end;
  MyShellexecute;

end;

  
{$R *.dfm}

procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;

procedure TForm1.Button1Click(Sender: TObject);
var Obj:TTestObjekt;
begin
Obj.Create;
showmessage(obj.String1);

end;

procedure TForm1.Button2Click(Sender: TObject);
begin
Form1.calldll;
end;

end.
FreeandNil(obj) hab ich erstmal mit absicht weggelassen.

Jedesmal wenn ich auf Button2 clicke bekomme ich folgende Fehlermeldung:
"Acess vialation at adress 00000000. Read of adress 00000000."

Wo liegt der Fehler?

PS: Bin noch neu im OOP und dll-Programmieren!
  Mit Zitat antworten Zitat