将二进制数据传递给D7中的dll函数 [英] Passing binary data to a dll function in D7

查看:106
本文介绍了将二进制数据传递给D7中的dll函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以为我提供通过参数传递任意数量的字节
到dll函数的工作示例?



我想做没有任何额外的内存单元,只能在基本的Windows类型下运行。



我需要发送每个呼叫约300 kb的数据。



客户端上分配的内存是否也可以在客户端空闲?

解决方案

函数应该如下所示:

  procedure Test(Buffer:Pointer; Length:Integer);标准
begin
//缓冲区指向缓冲区的开头。
//如果Length字节为缓冲区大小。
结束

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

  procedure Test(Buffer:Pointer; Length:Integer);标准外部'test.dll'; 

程序CallTest;
var
缓冲区:字节数组;
begin
SetLength(Buffer,1000);
// populate Buffer
Test(@Buffer [0],Length(Buffer));
结束

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



上述示例在调用者模块中分配和释放。这意味着 Test 方法将在返回之前完全处理缓冲区,或者获取内容副本

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



当从呼叫者传送到被叫方的数据比调用者分配一直是最好的选择。当数据流向另一个方向时,被调用者分配的可能性更可能是适当的。


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?

解决方案

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.

这篇关于将二进制数据传递给D7中的dll函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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