Passing binary data to a DLL function in D7

Can someone provide me with a working example of passing an arbitrary number of bytes through a parameter into a dll function?

I would like to do this without an additional block of memory, just work only with the main types of windows.

I need to β€œsend” about 300 kb of data for each call.

If the memory allocated on the client side will be free on the client side?

+4
source share
1 answer

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 might 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 advisable to define an interface that requires memory to be allocated and freed in the same module.

In the above example, it is allocated and deallocated in the caller module. This means that the Test method must either fully process the Buffer before returning, or return a copy of the contents of the Buffer before returning.

Now, although there is the possibility of distribution and release in the called party module, this is less common. This is less common because it is usually less convenient to do so. It often entails more API functions, or perhaps a more complex interface. You will be redirected to the distribution route of the called party when the calling party cannot determine the appropriate size for the buffer.

When data is transferred from the caller to the callee, than allocating the caller is always a better choice. When data moves in the other direction, it is more likely that the allocation of the called party will be appropriate.

+7
source

Source: https://habr.com/ru/post/1396645/


All Articles