4

我对如何在我的场景中正确使用指针感到有点困惑。我有一个包含一些嵌入式资源的 DLL。我在这个 DLL 中公开了一个函数,它将其中一个资源的二进制数据传递回其调用应用程序。在这种情况下,我嵌入了一个 JPG 图像文件。我的 DLL 确实将文件正确加载到资源流中。但是,从那里将其传递回应用程序会变得混乱。

这是我的 DLL 代码(加载并命名为 JPG SOMERESOURCE):

library ResDLL;

{$R *.dres}

uses
  System.SysUtils,
  System.Classes,
  Winapi.Windows;

{$R *.res}

function GetResource(const ResName: PChar; Buffer: Pointer;
  var Length: Integer): Bool; stdcall;
var
  S: TResourceStream;
  L: Integer;
  Data: array of Byte;
begin
  Result:= False;
  try
    S:= TResourceStream.Create(HInstance, UpperCase(ResName), RT_RCDATA);
    try
      S.Position:= 0;
      L:= S.Size;
      Length:= L;
      SetLength(Data, L);
      S.Read(Data[0], L);
      Buffer:= @Data[0];
      Result:= True;
    finally
      S.Free;
    end;
  except
    Result:= False;
  end;
end;

exports
  GetResource;

begin
end.

这是我的应用程序代码(只有一个TBitBtnand TImage):

function GetResource(const ResName: PChar; Buffer: Pointer;
  var Length: Integer): Bool; stdcall; external 'ResDLL.dll';

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  Buffer: array of Byte;
  Size: Integer;
  S: TMemoryStream;
  P: TPicture;
begin
  if GetResource('SOMERESOURCE', @Buffer[0], Size) then begin
    S:= TMemoryStream.Create;
    try
      SetLength(Buffer, Size);
      S.Write(Buffer, Size);
      S.Position:= 0;
      P:= TPicture.Create;
      try
        P.Graphic.LoadFromStream(S);
        Image1.Picture.Assign(P);
      finally
        P.Free;
      end;
    finally
      S.Free;
    end;
  end else begin
    raise Exception.Create('Problem calling DLL');
  end;
end;

看起来好像整个 DLL 调用是成功的,但是接收到的数据是空的(全是 0)。我对 likeData应该如何被称为 as Data[0],在什么场景下应该使用,以及在什么场景下需要使用充满好奇@Data。我完全在 DLL 中编写了该代码,而且我不熟悉此类工作,所以我确定我在某个地方搞砸了。我哪里错了?

4

3 回答 3

10

在 DLL 方面,GetResource()将资源数据读取到本地数组中,而不是将其复制到传递给函数的缓冲区中。将本地数组分配给Buffer指针不会复制所指向的数据。

在应用程序端,BitBtn1Click()没有分配任何内存GetResource()来写入资源数据。即使是这样,您也没有将缓冲区TMemoryStream正确写入。即使你是,你也没有TMemoryStream正确加载TPicture

您可以采取几种方法来解决缓冲区问题:

1)GetResource()分配一个缓冲区并将其返回给应用程序,然后让应用程序在完成后将缓冲区传递回DLL以释放它:

library ResDLL;

{$R *.dres}

uses
  System.SysUtils,
  System.Classes,
  Winapi.Windows;

{$R *.res}

function GetResourceData(const ResName: PChar; var Buffer: Pointer;
  var Length: Integer): Bool; stdcall;
var
  S: TResourceStream;
  L: Integer;
  Data: Pointer;
begin
  Result := False;
  try
    S := TResourceStream.Create(HInstance, UpperCase(ResName), RT_RCDATA);
    try
      L := S.Size;
      GetMem(Data, L);
      try
        S.ReadBuffer(Data^, L);
        Buffer := Data;
        Length := L;
      except
        FreeMem(Data);
        raise;
      end;
      Result := True;
    finally
      S.Free;
    end;
  except
  end;
end;

procedure FreeResourceData(Buffer: Pointer); stdcall;
begin
  try
    FreeMem(Buffer);
  except
  end;
end;

exports
  GetResourceData,
  FreeBufferData;

begin
end.

.

unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    Image1: TImage;
    procedure BitBtn1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Vcl.Imaging.jpeg;

{$R *.dfm}

function GetResourceData(const ResName: PChar; var Buffer: Pointer;
  var Length: Integer): Bool; stdcall; external 'ResDLL.dll';

procedure FreeResourceData(Buffer: Pointer); stdcall; external 'ResDLL.dll';

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  Buffer: Pointer;
  Size: Integer;
  S: TMemoryStream;
  JPG: TJPEGImage;
begin
  if GetResourceData('SOMERESOURCE', Buffer, Size) then
  begin
    try
      S := TMemoryStream.Create;
      try
        S.WriteBuffer(Buffer^, Size);
        S.Position := 0;
        JPG := TJPEGImage.Create;
        try
          JPG.LoadFromStream(S);
          Image1.Picture.Assign(JPG);
        finally
          JPG.Free;
        end;
      finally
        S.Free;
      end;
    finally
      FreeResourceData(Buffer);
    end;
  end else begin
    raise Exception.Create('Problem calling DLL');
  end;
end;

end.

2) 让应用程序向 DLL 查询资源的大小,然后分配一个缓冲区并将其传递给 DLL 以填充:

library ResDLL;

{$R *.dres}

uses
  System.SysUtils,
  System.Classes,
  Winapi.Windows;

{$R *.res}

function GetResourceData(const ResName: PChar; Buffer: Pointer;
  var Length: Integer): Bool; stdcall;
var
  S: TResourceStream;
  L: Integer;
  Data: Pointer;
begin
  Result := False;
  try
    S := TResourceStream.Create(HInstance, UpperCase(ResName), RT_RCDATA);
    try
      L := S.Size;
      if Buffer <> nil then
      begin
        if Length < L then Exit;
        S.ReadBuffer(Buffer^, L);
      end;
      Length := L;
      Result := True;
    finally
      S.Free;
    end;
  except
  end;
end;

exports
  GetResourceData;

begin
end.

.

unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    Image1: TImage;
    procedure BitBtn1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Vcl.Imaging.jpeg;

{$R *.dfm}

function GetResourceData(const ResName: PChar; Buffer: Pointer;
  var Length: Integer): Bool; stdcall; external 'ResDLL.dll';

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  Buffer: array of Byte;
  Size: Integer;
  S: TMemoryStream;
  JPG: TJPEGImage;
begin
  if GetResourceData('SOMERESOURCE', nil, Size) then
  begin
    SetLength(Buffer, Size);
    if GetResourceData('SOMERESOURCE', @Buffer[0], Size) then
    begin
      S := TMemoryStream.Create;
      try
        S.WriteBuffer(Buffer[0], Size);
        S.Position := 0;
        // alternatively, use TBytesStream, or a custom
        // TCustomMemoryStream derived class, to read
        // from the original Buffer directly so it does
        // not have to be copied in memory...

        JPG := TJPEGImage.Create;
        try
          JPG.LoadFromStream(S);
          Image1.Picture.Assign(JPG);
        finally
          JPG.Free;
        end;
      finally
        S.Free;
      end;
      Exit;
    end;
  end;
  raise Exception.Create('Problem calling DLL');
end;

end.

或者:

library ResDLL;

{$R *.dres}

uses
  System.SysUtils,
  System.Classes,
  Winapi.Windows;

{$R *.res}

function GetResourceData(const ResName: PChar; Buffer: Pointer;
  var Length: Integer): Bool; stdcall;
var
  S: TResourceStream;
  L: Integer;
  Data: Pointer;
begin
  Result := False;
  if (Buffer = nil) or (Length <= 0) then Exit;
  try
    S := TResourceStream.Create(HInstance, UpperCase(ResName), RT_RCDATA);
    try
      L := S.Size;
      if Length < L then Exit;
      S.ReadBuffer(Buffer^, L);
      Length := L;
      Result := True;
    finally
      S.Free;
    end;
  except
  end;
end;

function GetResourceSize(const ResName: PChar): Integer; stdcall;
var
  S: TResourceStream;
begin
  Result := 0;
  try
    S := TResourceStream.Create(HInstance, UpperCase(ResName), RT_RCDATA);
    try
      Result := S.Size;
    finally
      S.Free;
    end;
  except
  end;
end;

exports
  GetResourceData,
  GetResourceSize;

begin
end.

.

unit uMain;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls;

type
  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    Image1: TImage;
    procedure BitBtn1Click(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Vcl.Imaging.jpeg;

{$R *.dfm}

function GetResourceData(const ResName: PChar; Buffer: Pointer;
  var Length: Integer): Bool; stdcall; external 'ResDLL.dll';

function GetResourceSize(const ResName: PChar): Integer; stdcall; external 'ResDLL.dll';

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  Buffer: array of Byte;
  Size: Integer;
  S: TMemoryStream;
  JPG: TJPEGImage;
begin
  Size := GetResourceSize('SOMERESOURCE');
  id Size > 0 then
  begin
    SetLength(Buffer, Size);
    if GetResourceData('SOMERESOURCE', @Buffer[0], Size) then
    begin
      S := TMemoryStream.Create;
      try
        S.WriteBuffer(Buffer[0], Size);
        S.Position := 0;
        JPG := TJPEGImage.Create;
        try
          JPG.LoadFromStream(S);
          Image1.Picture.Assign(JPG);
        finally
          JPG.Free;
        end;
      finally
        S.Free;
      end;
      Exit;
    end;
  end;
  raise Exception.Create('Problem calling DLL');
end;

end.
于 2013-02-04T06:54:37.113 回答
8

您根本不需要从 DLL 中导出任何函数。您可以直接从主机可执行文件中使用 DLL 的模块句柄。

您已经将模块句柄传递给资源流构造函数。您正在传递可执行文件的模块句柄。相反,传递库的模块句柄。

var
  hMod: HMODULE;
....
hMod := LoadLibrary('ResDLL');
try
  S:= TResourceStream.Create(hMod, ...);
  ....
finally
  FreeLibrary(hMod);
end;

如果您不想调用 DLL 中的任何函数,如果它是仅资源 DLL,则使用LoadLibraryExandLOAD_LIBRARY_AS_IMAGE_RESOURCE代替:

hMod := LoadLibraryEx('ResDLL', 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE);

也许您知道 DLL 已经加载。例如,它隐式链接到您的可执行文件。在这种情况下,您可以更简单地使用GetModuleHandle而不是LoadLibraryor LoadLibraryEx

hMod := GetModuleHandle('ResDLL');
S:= TResourceStream.Create(hMod, ...);

请注意,为了简单说明,我省略了所有错误检查。

于 2013-02-04T09:05:00.633 回答
2

将流从 DLL 传递到应用程序的另一种方法是使用接口流。

implementation
uses MemoryStream_Interface;
{$R *.dfm}

Type
TGetStream = Procedure(var iStream:IDelphiStream);stdcall;

procedure TForm1.Button1Click(Sender: TObject);
var
 h:THandle;
 p:TGetStream;
 ms :IDelphiStream;
 j:TJpegImage;
begin
   ms := TInterfacedMemoryStream.Create;
   h := LoadLibrary('ShowStream.dll');
   if h <> 0 then
      try
      @p := GetProcAddress(h,'GetJpegStream');
      p(ms);
      ms.Position := 0;
      j := TJpegImage.create;
      Image1.Picture.Assign(j);
      j.Free;
      Image1.Picture.Graphic.LoadFromStream(TInterfacedMemoryStream(ms));
      finally
      FreeLibrary(h);
      end;
end;

IDelphiStream 的代码可以在 http://www.delphipraxis.net 上找到
我不会将 MemoryStream_Interface 的内容复制到这篇文章中,因为上述页面的代码中没有版权信息。

于 2013-02-04T07:56:41.427 回答