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 2 von 3     12 3      
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#11

Re: How to - string in DLL

  Alt 19. Jun 2011, 21:11
Delphi-Quellcode:
library StringDLL;

uses
  SysUtils;

function Foonction(Data: PWideChar; Buffer: PWideChar; lenBuffer: Cardinal): Cardinal; stdcall;
var
  S: string;
begin
  S := 'foo' + Data;

  if Assigned(Buffer) then
    StrLCopy(Buffer, PWideChar(S), lenBuffer)
  ;

  Result := Length(S);
end;

exports
  Foonction;

begin
end.
Dynamic - working:

Delphi-Quellcode:
var
  hLib: THandle;
  func1: 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
    ListBox1.Items.Add('hlib: ' + IntToStr(hLib));
    @func1 := GetProcAddress(hLib, 'Foonction');
    if Assigned(func1) then
    begin
      ListBox1.Items.Add('@func1: ' + IntToStr(Integer(@func1)));
      len := func1('bar', nil, 0);
      ListBox1.Items.Add('len: ' + IntToStr(len));
      try
        GetMem(Buffer, len + 1);
        len := func1('bar', Buffer, len + 1);
        ListBox1.Items.Add(String(Buffer)+ ' [' + IntToStr(len) + ']');
      finally
        FreeMem(Buffer);
      end;
    end;
  end;
end;
Static - don't working:

Delphi-Quellcode:
function Foonction(Data: PWideChar; Buffer: PWideChar; lenBuffer: Cardinal): Cardinal; external 'StringDLL.dll';

var
  Buffer: PWideChar;
  BufferSize: DWORD;
begin
  BufferSize := Foonction('bar', nil, 0);
  ShowMessage(IntToStr(BufferSize));

  GetMem(Buffer, BufferSize + 1);
  try
    Foonction('bar', Buffer, BufferSize + 1);
    ShowMessage(Buffer);
  finally
    FreeMem(Buffer);
  end;
end;
Now I have identical sizes, what's wrong?
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

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

AW: How to - string in DLL

  Alt 19. Jun 2011, 21:18
Does Length return the count of characters or the count of Bytes? If it returns the count of characters you have to multiply it by 2 if it's a WideChar.
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

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

AW: How to - string in DLL

  Alt 19. Jun 2011, 22:25
What about this:
Delphi-Quellcode:
function Foonction(Data: PWideChar; Buffer: PWideChar; lenBuffer: Cardinal): Cardinal; external 'StringDLL.dll';

var
  Buffer: string;
  BufferSize: DWORD;
begin
  BufferSize := Foonction('bar', nil, 0);
  if BufferSize > 0 then
  begin
    SetLength(Buffer, BufferSize);
    Foonction('bar', PWideChar(Buffer), BufferSize);
  end
  else
    Buffer := '';
  ShowMessage(Buffer);
end;
But it would be better if you made lenBuffer a variable parameter. This way you can return the buffer size and a return value.
Sebastian Jänicke
AppCentral
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

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

AW: How to - string in DLL

  Alt 20. Jun 2011, 01:44
Delphi-Quellcode:
                                                                              vvvvvvv
function func1(s: PWideChar; Buffer: PWideChar; lenBuffer: Integer): Integer; stdcall;
begin
end

function func1(s: PWideChar; Buffer: PWideChar; lenBuffer: Integer): Integer; external 'StringDLL.dll';
                                                                              ^^^^???
$2B or not $2B
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

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

AW: How to - string in DLL

  Alt 20. Jun 2011, 05:42
Oh ja, darauf hatte ich gar nicht geachtet...
Sebastian Jänicke
AppCentral
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#16

Re: How to - string in DLL

  Alt 20. Jun 2011, 10:24
DLL:
Delphi-Quellcode:
library StringDLL;

uses
  SysUtils;

function Foonction(Data: PWideChar; Buffer: PWideChar; lenBuffer: Cardinal): Cardinal; stdcall;
var
  S: string;
begin
  S := 'foo' + Data;

  if Assigned(Buffer) then
    StrLCopy(Buffer, PWideChar(S), lenBuffer)
  ;

  Result := Length(S);
end;

exports
  Foonction;

begin
end.
@Luckie, Length() is function from Delphi

Here you are complete code with variants I tried to static load and call StringDLL.dll-->Foonction():

Delphi-Quellcode:
function Foonction(Data: PWideChar; Buffer: PWideChar; lenBuffer: Cardinal): Cardinal; external 'StringDLL.dll';

// 1:

var
  Buffer: string;
  BufferSize: DWORD;
begin
  BufferSize := Foonction('bar', nil, 0);
  if BufferSize > 0 then
  begin
    SetLength(Buffer, BufferSize);
    Foonction('bar', PWideChar(Buffer), BufferSize);
  end
  else
    Buffer := '';
  ShowMessage(Buffer);
end;

// 2:

var
  Buffer: PWideChar;
  BufferSize: DWORD;
begin
  BufferSize := Foonction('bar', nil, 0);
  ShowMessage(IntToStr(BufferSize));

  GetMem(Buffer, BufferSize + 1);
  try
    Foonction('bar', Buffer, BufferSize + 1);
    ShowMessage(Buffer);
  finally
    FreeMem(Buffer);
  end;
end;

// 3:

var
  Buffer: array [0..MAX_PATH - 1] of Char;
  BufferSize: DWORD;
begin
  BufferSize := High(Buffer);
  ShowMessage(IntToStr(Foonction('bar', Buffer, BufferSize)));
  Caption := Buffer;
end;
All of them working when Foonction() is in EXE, none of them working if Foonction() is in DLL - raises AV in DLL.
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

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

AW: How to - string in DLL

  Alt 20. Jun 2011, 10:36
I know that it is a Delphi function. But I don't know its result, if you use Unicode. I assume it returns the number of characters. So if you allocate the memory you have to double the result of Length.

And there is still the error with the calling convention in your code. Before you try something else correct it. The calling convention for the function in the DLL is stdcall, but in your program it is external!
Michael
Ein Teil meines Codes würde euch verunsichern.

Geändert von Luckie (20. Jun 2011 um 10:39 Uhr)
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#18

Re: How to - string in DLL

  Alt 20. Jun 2011, 11:48
Oh gosh, I forgot add stdcall before external in app. Ah Now working! Thanks

Also a few of you told that better is using WideChar. I tried this:

Delphi-Quellcode:
// In DLL

function FooncUni(Data: WideString): WideString; stdcall;
var
  S: string;
begin
  S := 'foo' + Data;

  Result := S;
end;

// In app:

function FooncUni(Data: WideString): WideString; stdcall external 'StringDLL.dll';

Caption := FooncUni('bar'); // Caption is 'foobar'
and working without any problems (as normal string type in EXE, that's cool ). But early @himitsu told:

How to use the functions, they are required to something in this case?

Also another think: to use Ansii or Unicode I have to write 2 functions with PWideChar and PAnsiChar parameters? Or one lets say Unocode version and second to convert Unicode Buffer to Ansi - how? And WideString - it is always Unicode?
  Mit Zitat antworten Zitat
Benutzerbild von jaenicke
jaenicke

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

AW: How to - string in DLL

  Alt 20. Jun 2011, 16:53
Also another think: to use Ansii or Unicode I have to write 2 functions with PWideChar and PAnsiChar parameters? Or one lets say Unocode version and second to convert Unicode Buffer to Ansi - how?
Well, for the version with PAnsiChar you append an A, and for PWideChar a W.
For example the API has ShellExecuteA and ShellExecuteW. Delphi itself maps ShellExecute to ShellExecuteA (Delphi 1 - 2007) or to ShellExecuteW (Delphi 2009+). So you don't see that you call one of the other functions.

And WideString - it is always Unicode?
Yes it is.

I myself do not want to use it because it relies on OLE and the operating system. That's why it is much slower. Of course this doesn't matter unless you call such a method often.

For COM WideStrings are very useful (I also used them to interact with .NET libraries with exported native functions), but for other purposes I do not use them.

But it is your decision, both ways work.
Sebastian Jänicke
AppCentral
  Mit Zitat antworten Zitat
WojTec

Registriert seit: 17. Mai 2007
482 Beiträge
 
Delphi XE6 Professional
 
#20

Re: How to - string in DLL

  Alt 20. Jun 2011, 18:54
Thanks guys, you helped me so much!
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 2 von 3     12 3      


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 17:55 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