如何从我的应用程序传递和检索内存流到/从DLL? [英] How do I pass and retrieve memory stream from my Application to/from DLL?

查看:83
本文介绍了如何从我的应用程序传递和检索内存流到/从DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 TMemoryStream 我需要传递给我的DLL,并从 TMemoryStream (Bitmap stream)这个DLL。



我在想我的DLL会有:

 程序过程(
InBuff:指针;
InBuffSize:Integer;
var OutBuff:指针;
var OutBuffSize:Integer
);标准

InBuff 很容易(我想) 。我通过 TMemoryStream.Memory TMemoryStream.Size



问题是如何在DLL中分配 OutBuff ,调用者应用程序可以将其转换回 TMemoryStream 和后来释放内存(由调用者应用程序)?



调用者将使用动态 LoadLibrary / FreeLibrary 每个DLL调用。



我非常喜欢一个示例代码。希望我不太粗鲁。



注意1:调用者应用程序不知道输出大小,并假定它不能指定MAX buff大小。 p>

注意2:我不知道我的DLL签名。请原谅我,如果我错了。我正在寻找一个可以正常运行的模式(可能不仅适用于Delphi,而且适用于C ++ / C#Caller以及=我的奖金)

解决方案

两个明显的选择,假设被调用者要分配内存:



1。使用共享堆



例如,您可以使用COM堆。在被调用者中,您的写入:

  OutBuffSize:= ...; //你知道这个值是
OutBuff:= CoTaskMemAlloc(OutBuffSize);
//填充缓冲区

调用者使用 CoTaskMemFree 。您可以使用 LocalAlloc HeapAlloc ,如果您愿意,这并不重要。



2。使用被调用者的堆并导出一个deallocator



这里使用被调用方的本地堆:

  OutBuffSize:= ...; //你知道这个值是
GetMem(OutBuff,OutBuffSize);
//填充缓冲区

您还需要导出一个deallocator:

  procedure DeallocateMemory(Ptr:Pointer);标准
begin
FreeMem(Ptr);
结束






我拒绝的另一个选项是使用共享内存管理器。我倾向于避免这一点,因为它将调用者限制为Delphi程序。



要从缓冲区调用 WriteBuffer 填充流:



Stream.WriteBuffer(Buff ^,BuffSize);

   

其中 Buff 是指向缓冲区的指针。


Suppose I have a TMemoryStream I need to pass to my DLL and get back TMemoryStream (Bitmap stream) from the DLL.

I was thinking my DLL would have:

procedure Process(
  InBuff: Pointer; 
  InBuffSize: Integer; 
  var OutBuff: Pointer; 
  var OutBuffSize: Integer
); stdcall;

The InBuff is easy (I think). I pass TMemoryStream.Memory and TMemoryStream.Size.

Question is how do I allocate the OutBuff in the DLL, and the caller application can convert it back to TMemoryStream and later free that memory (by the caller application)?

The caller will use dynamic LoadLibrary/FreeLibrary each DLL call.

I would very much like a sample code. Hope I'm not being too rude.

Note 1: The caller application dose not know the output size, and assume it can not specify a MAX buff size.

Note 2: I am not sure about my DLL signature. Please forgive me if I did it wrong. I am Looking for a pattern that will work well (maybe not only for Delphi but for C++/C# Caller as well = Bonus for me)

解决方案

Two obvious options, assuming the callee is to allocate the memory:

1. Use a shared heap

For instance you can use the COM heap. In the callee your write:

OutBuffSize := ...; // you know what this value is
OutBuff := CoTaskMemAlloc(OutBuffSize);
// populate the buffer

The caller destroys this with CoTaskMemFree. You can use LocalAlloc, or HeapAlloc if you prefer, it doesn't really matter.

2. Use the callee's heap and export a deallocator

Here you use the native heap of the callee:

OutBuffSize := ...; // you know what this value is
GetMem(OutBuff, OutBuffSize);
// populate the buffer

You also need to export a deallocator:

procedure DeallocateMemory(Ptr: Pointer); stdcall;
begin
  FreeMem(Ptr);
end;


Another option that I rejected is to use a shared memory manager. I tend to avoid that because it constrains the caller to be a Delphi program.

To fill a stream from a buffer call WriteBuffer:

Stream.WriteBuffer(Buff^, BuffSize);

where Buff is a pointer to the buffer.

这篇关于如何从我的应用程序传递和检索内存流到/从DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆