DLL direct return object

Source: Internet
Author: User

An error may occur when the DLL directly returns an object. This is because the program and the DLL use different VCL, which is equivalent to different instances of an interface, therefore, an error occurs when VCL-related objects are returned from the DLL.
There are two solutions:
1. Declare a getcontrolatom method before the implementation of the controls Unit

Function getcontrolatom: pointer;
Result: = @ controlatom;
Then implement and export the following two processes in the DLL:
Procedure dllinitialize (APP: tapplication; scr: tscreen; realcontrolatom: integer );
X: pointer;
P: ^ word;
If (oldapp = nil) then
Begin // store away the current application, screen and control Atom
Oldapp: = application;
Oldscreen: = screen;
P: = getcontrolatom;
Oldcontrolatom: = P ^; // assign the EXE's application, screen and control Atom
Application: = app;
Screen: = scr;
P ^: = realcontrolatom;

Procedure dllfinalize;
P: ^ word;
Begin // restore the DLL's application, screen and control Atom
P: = getcontrolatom;
P ^: = oldcontrolatom;
Screen: = oldscreen;
Application: = oldapp;

The functions of these two processes are to synchronize the global atomic controlatom of the Host Program and the DLL during DLL initialization, and to restore relevant content before the DLL is released.
Then you can use the function in the DLL to return the VCL object. This method also applies to creating a form in the DLL in the panel from the dock to the host. The most important thing is the controlatom synchronization.

2. VCL objects are not used as return values.
It is very simple. You can use memory for swap. For example, after converting an image, you can apply for a piece of memory and write the converted image into this memory. You can simply do this by using stream operations or move operations, the key is the return pointer address and length.
Function BMP 2jpg (filename: pchar; var PTR: pointer; var size: integer): Boolean;
VaR stream: tmemorystream;
Result: = false;
Stream: = tmemorystream. Create;
JPG. savetostream (Stream );
Size: = stream. size;
Getmem (PTR, size );
Stream. Position: = 0;
Stream. Read (PTR ^, size );
Stream. Free;
Result: = true;

Declare global variables in DLL
Oldapp: tapplication;
Oldscreen: tscreen;
Oldcontrolatom: Tatom;

Exprots ....;

Oldapp: = nil;
Oldscreen: = nil;
Oldcontrolatom: = 0;



$ R *. DFM}
Function BMP fromjpg (jpgf: string): tbitmap; stdcall;
External 'jpg. dll '; // static call
Procedure tform1.button1click (Sender: tobject );
Myb: tbitmap;
I, W1, H1: integer;
Myb: = tbitmap. Create;
Myb: = BMP fromjpg ('G:/tupian/funiu.jpg ');
For I: = 0 to 30 do // open the curtain special effect
Myb. Free;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.