我有点困惑的是,如何指针应该在我的情况下可以正常使用了。 我有它的一些嵌入资源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.
这是我的应用程序的代码(只有一个TBitBtn
和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的)。 我充满了好奇至于如何像Data
需要被称为Data[0]
以及在什么情况下我应该,而且在我需要用什么样的场景@Data
。 我写的代码在DLL完全,我不熟悉这样的工作,所以我敢肯定,我把事情弄糟了某处。 我要去哪里错了?
在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.
你并不需要在所有从DLL导出任何函数。 你只可以直接使用DLL的模块句柄从您的主机的可执行文件。
您已经传递了一个模块句柄资源流构造。 你逝去的可执行文件的模块句柄。 相反,通过库的模块句柄。
var
hMod: HMODULE;
....
hMod := LoadLibrary('ResDLL');
try
S:= TResourceStream.Create(hMod, ...);
....
finally
FreeLibrary(hMod);
end;
如果你不希望调用任何DLL中的函数,你如果是一个资源只有DLL,然后使用LoadLibraryEx
和LOAD_LIBRARY_AS_IMAGE_RESOURCE
改为:
hMod := LoadLibraryEx('ResDLL', 0, LOAD_LIBRARY_AS_IMAGE_RESOURCE);
也许你知道该DLL已经加载。 例如,它与您的可执行含蓄。 在这种情况下,你可以更简单地使用GetModuleHandle
而不是LoadLibrary
或LoadLibraryEx
。
hMod := GetModuleHandle('ResDLL');
S:= TResourceStream.Create(hMod, ...);
请注意,我省略了所有的错误检查简单博览会的缘故。
从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的内容复制到这个职位,因为有来自提到页面的代码没有版权信息。