将二进制数据传递给 D7 中的 dll 函数

发布于 2025-01-06 01:27:05 字数 159 浏览 0 评论 0原文

谁能给我提供传递任意数量字节的工作示例 通过参数传递给dll函数?

我想在没有任何额外内存单元的情况下执行此操作,仅在基本 Windows 类型上进行操作。

每次调用我需要“发送”大约 300 kb 的数据。

在客户端分配的内存在客户端也应该是空闲的吗?

Could anyone provide me with working example of passing arbitrary number of bytes
through the parameter to a dll function?

I would like to do it without any extra memory unit, just only operate on basic windows types.

I need to "send" about 300 kb data per each call.

Should the memory allocated on the client side be free also on the client side?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2025-01-13 01:27:05

DLL 函数应该如下所示:

procedure Test(Buffer: Pointer; Length: Integer); stdcall;
begin
  //Buffer points to the start of the buffer. 
  //The Buffer size if Length bytes.
end;

假设您从另一个 Delphi 模块调用它,则该调用可能如下所示:

procedure Test(Buffer: Pointer; Length: Integer); stdcall; external 'test.dll';

procedure CallTest;
var
  Buffer: array of Byte;
begin
  SetLength(Buffer, 1000);
  //populate Buffer
  Test(@Buffer[0], Length(Buffer));
end;

最好定义一个需要在同一模块中分配和释放内存的接口。

上面的示例在调用者的模块中进行分配和释放。这意味着 Test 方法要么必须在返回之前完全处理 Buffer,要么在返回之前获取 Buffer 内容的副本。

现在,虽然可以在被调用者的模块中进行分配和释放,但这种情况不太常见。这种情况不太常见,因为这样做通常不太方便。它通常需要更多的 API 函数,或者可能更复杂的接口。当调用者无法确定缓冲区的适当大小时,您将被推入被调用者分配的路线。

当数据从调用者传递到被调用者时,调用者分配始终是最佳选择。当数据流向另一个方向时,被调用者分配更有可能是合适的。

The DLL function should look like this:

procedure Test(Buffer: Pointer; Length: Integer); stdcall;
begin
  //Buffer points to the start of the buffer. 
  //The Buffer size if Length bytes.
end;

Assuming you are calling it from another Delphi module the call could look like this:

procedure Test(Buffer: Pointer; Length: Integer); stdcall; external 'test.dll';

procedure CallTest;
var
  Buffer: array of Byte;
begin
  SetLength(Buffer, 1000);
  //populate Buffer
  Test(@Buffer[0], Length(Buffer));
end;

It is always preferable to define an interface which requires memory to be allocated and deallocated in the same module.

The above example allocates and deallocates in the caller's module. This means that the Test method would either have to process the Buffer completely before returning, or take a copy of the contents of Buffer before returning.

Now, whilst it is possible to have the allocation and deallocation in the callee's module, this is less common. It is less common because it is typically less convenient to do it this way. It often entails more API functions, or perhaps more a complex interface. You will be pushed into the route of callee allocation when the caller is not able to determine an appropriate size for the buffer.

When data is being passed from caller to callee than caller allocate is invariably the best choice. When the data flows in the other direction it is more likely that callee allocates would be appropriate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文