AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Dynamisches Array von DLL übergeben

Ein Thema von TheMiller · begonnen am 4. Dez 2006 · letzter Beitrag vom 8. Dez 2006
 
Benutzerbild von Wormid
Wormid

Registriert seit: 26. Aug 2003
Ort: Steinfurt
292 Beiträge
 
Delphi XE2 Professional
 
#21

Re: Dynamisches Array von DLL übergeben

  Alt 6. Dez 2006, 15:45
Also, dann nochmal mein Beispiel von vorhin, mit Pointern...

Bei mir ist das kompilierbar und läuft ohne Exceptions durch. Ein Speichermanager muss weder in der DLL
noch in der Anwendung eingebunden werden. (Meine erste Version scheint nämlich nur bei statischem Linken
der Testfunktion einwandfrei zu funktionieren.)

Einmal die DLL...
Delphi-Quellcode:
library Project1;

type
  TTestArray = array of ShortString; // <--- ShortString !!!
  PTestArray = ^TTestArray;

function GetTestArray: PTestArray; stdcall;
var
  a: TTestArray;
begin
  SetLength(a, 3);
  a[0] := 'Hallo';
  a[1] := 'Welt';
  a[2] := '!';
  Result := PTestArray(a);
end;

exports
  GetTestArray;

begin
end.
Und hier die Unit1 von dem Testprojekt...
Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

type
  TTestArray = array of ShortString;
  PTestArray = ^TTestArray;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  hDLL: Cardinal;
  GetTestArray: function: PTestArray; stdcall;

procedure TForm1.Button1Click(Sender: TObject);
var
  a: TTestArray;
  i: Integer;
begin
  hDLL := LoadLibrary(PChar('Project1.dll'));
  if hDLL <> 0 then
  begin
    GetTestArray := GetProcAddress(hDLL, 'GetTestArray');
    if GetTestArray <> nil then
    begin
      a := TTestArray(GetTestArray);
      ShowMessage(IntToStr(Length(a))); // Eine schicke "3" erscheint als Meldung
      for i := Low(a) to High(a) do
        ShowMessage(a[i]); // Hallo *click* Welt *click* ! *click*
      a := nil;
    end;
    FreeLibrary(hDLL);
  end;
end;

end.
Debuggers don't remove Bugs, they only show them in Slow-Motion.
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:21 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz