AGB  ·  Datenschutz  ·  Impressum  







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

How to - string in DLL

Ein Thema von WojTec · begonnen am 16. Jun 2011 · letzter Beitrag vom 22. Jun 2011
Antwort Antwort
Seite 1 von 3  1 23      
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#1

How to - string in DLL

  Alt 16. Jun 2011, 18:54
Delphi-Version: 2010
This is the bigest problem I currently have: how to use strings in DLL.

I know have to use PChar in exports. How about internal code that operates on Delphi's strings? I want to share this DLL with any language, so I can't use external memory manager (as I did with DLLs that can be used by my software only). For example function returns string or parameter is string or variable or constant is string - what to do whit this?

With numbers there is no problem. Please help me with strings
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#2

AW: How to - string in DLL

  Alt 16. Jun 2011, 18:56
Here you go: http://www.michael-puff.de/Programmi...tringDLL.shtml
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Benutzerbild von geskill
geskill

Registriert seit: 17. Feb 2007
Ort: NRW
420 Beiträge
 
Delphi 2010 Professional
 
#3

AW: How to - string in DLL

  Alt 16. Jun 2011, 23:04
i always use the WideString type, it is fully COM compatible and i don't have to cast. for me the most easiest way!
Sebastian
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.350 Beiträge
 
Delphi 11 Alexandria
 
#4

AW: How to - string in DLL

  Alt 17. Jun 2011, 04:58
You can use PWideChar or PAnsiChar just as the Windows API internally uses too.

If you want to return such a value:
Just give a PWideChar and its size as variable parameter. If the size is not sufficient, you return the size you need. If the size is sufficient you copy the data into the buffer.

This is the way the API manages it. And it works well.
Sebastian Jänicke
Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.152 Beiträge
 
Delphi 12 Athens
 
#5

AW: How to - string in DLL

  Alt 17. Jun 2011, 06:38
WideString = MSDN-Library durchsuchenSysAllocStringLen, MSDN-Library durchsuchenSysReAllocStringLen, MSDN-Library durchsuchenSysFreeString and MSDN-Library durchsuchenSysStringLen

- no delphi type > OLE-String
- no delphi memory manager
- no RTTI required
- no problems

- without reference counting
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#6

Re: How to - string in DLL

  Alt 17. Jun 2011, 13:11
Thanks @Luckie!

Can I ask for example how to use functions @himitsu listed? Or just use WideString as Delphi's string?
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#7

Re: How to - string in DLL

  Alt 19. Jun 2011, 16:49
I tested @Luckies code and it working in example, but don't work in this case:

Delphi-Quellcode:
function func1(s: PChar; Buffer: PChar; lenBuffer: Integer): Integer; external 'StringDLL.dll';

var
  Buffer: array [0..MAX_PATH] of Char;
  BufferSize: DWORD;
begin
  BufferSize := High(Buffer);
  func1('bar', Buffer, BufferSize);
  ShowMessage(Buffer);
end;
Raises AV. Windows functions working, this one not. Why?

Geändert von WojTec (19. Jun 2011 um 17:02 Uhr) Grund: Added static linked function
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.350 Beiträge
 
Delphi 11 Alexandria
 
#8

AW: How to - string in DLL

  Alt 19. Jun 2011, 17:57
One inportant issue first:
Always use PWideChar or PAnsiChar in DLL interfaces. Otherwise it depends on the Delphi version and what PChar means in it whether your program works or not...

Regarding your problem:
I don't see an error handling (you ignore the return value). And where does the exception occur? Inside the DLL function?

And could you please post the source of the function inside the DLL too?
Sebastian Jänicke
Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
480 Beiträge
 
Delphi XE6 Professional
 
#9

Re: How to - string in DLL

  Alt 19. Jun 2011, 19:14
Code (function from @Luckie article).

Delphi-Quellcode:
library StringDLL;

uses
  SysUtils;

function func1(s: PWideChar; Buffer: PWideChar; lenBuffer: Integer): Integer; stdcall;
var
  foo: String;
begin
  foo := 'foo'+ s;
  if Assigned(Buffer) then
    StrLCopy(Buffer, PWideChar(foo), lenBuffer);
  Result := Length(foo);
end;

exports
  func1;

begin
end.

function func1(s: PWideChar; Buffer: PWideChar; lenBuffer: Integer): Integer; external 'StringDLL.dll';

procedure TForm1.Button1Click(Sender: TObject);
var
  Buffer: array [0..MAX_PATH] of Char;
  BufferSize: DWORD;
begin
  BufferSize := High(Buffer);
  func1('bar', Buffer, BufferSize);
  ShowMessage(Buffer);
end;
Windows functions I'm using in the sam way, exception in DLL.

Original example is working:

Delphi-Quellcode:
procedure TForm1.FormCreate(Sender: TObject);
var
  hLib: THandle;
  s: String;
  foo1: function(s: PChar; Buffer: PChar; lenBuffer: Integer): Integer; stdcall;
  len: Integer;
  Buffer: PChar;
begin
  Buffer := nil;
  hLib := LoadLibrary('StringDLL.dll');
  if hLib = 0 then
  begin
    Str(GetLastError, s);
    ListBox1.Items.Add('LE: ' + s);
    exit;
  end;
  Str(hLib, s);
  ListBox1.Items.Add('hlib: ' + s);
  @foo1 := GetProcAddress(hLib, 'func1');
  if (not Assigned(foo1)) then
  begin
    Str(GetLastError, s);
    ListBox1.Items.Add('AE: ' + s);
    exit;
  end;
  Str(Integer(@foo1), s);
  ListBox1.Items.Add('@func1: ' + s);
  len := foo1('', nil, 0);
  Str(len, s);
  ListBox1.Items.Add('len: ' + s);
  try
    GetMem(Buffer, len + 1);
    len := foo1('', Buffer, len + 1);
    Str(len, s);
    ListBox1.Items.Add(String(Buffer)+ ' [' + s + ']');
  finally
    FreeMem(Buffer);
  end;
end;
I prefer 1st method (as in WinAPI). Why it don't working?

Geändert von WojTec (19. Jun 2011 um 19:18 Uhr) Grund: PWideChar
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

Registriert seit: 10. Jun 2003
Ort: Berlin
9.350 Beiträge
 
Delphi 11 Alexandria
 
#10

AW: Re: How to - string in DLL

  Alt 19. Jun 2011, 19:45
Delphi-Quellcode:
  foo := 'foo'+ s;
  if Assigned(Buffer) then
    StrLCopy(Buffer, PWideChar(foo), lenBuffer);
You try to copy lenBuffer Bytes from foo, but it does not have so many Bytes.

By the way: I prefer the way most of the API functions work:
First I ask for the buffer size needed, then I reserve enough memory and ask for the data itself.
Sebastian Jänicke
Alle eigenen Projekte sind eingestellt, ebenso meine Homepage, Downloadlinks usw. im Forum bleiben aktiv!
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 3  1 23      


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 02: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