The principle and use of Windows dynamic link Library 2

Source: Internet
Author: User
Tags variables

3 using DLLs to realize data transmission

Global memory in the 3.1 DLLs

Windows rules: DLLs does not own any files it opens or any global memory blocks it allocates. These objects are owned by an application that invokes DLLs directly or indirectly. Thus, when the application aborts, the open file it owns is automatically closed and the global memory block it owns is automatically released. This means that the files and global memory block variables saved in the DLLs global variable are in the DLLs

It becomes illegal without being notified. This will create difficulties for other applications that use the DLLs.

To avoid this, file and global memory block handles should not be used as global variables for DLLs, but as parameters for procedures or functions in DLLs to DLLs. Applications that invoke DLLs should be responsible for their maintenance.

However, under certain circumstances, DLLs can also have its own block of global memory. These memory blocks must be allocated with the Gmem_ddeshare property. Such blocks of memory remain in effect until they are DLLs display release or DLLs exit.

The global memory block managed by DLLs is another way to transfer data between applications, and we will discuss this issue specifically.

3.2 Using DLLs to achieve data transfer between applications

The steps for implementing data transfer between applications using DLLs are:

1. Write a DLLs program, which has a global memory block allocated with the Gmem_ddeshare attribute;

2. The server program calls the DLLs, writes the data to the global memory block;

3. Client programs call DLLs to read data from the global memory block.

3.2.1 For the DLLs of data transmission

The DLLs used to implement the data transfer is essentially the same as for the General DLLs, where special is:

1. Define a global variable handle:

    var
      hMem: THandle;

2. Define a procedure that returns the handle to the global variable. The procedure is to be included in the exports clause. Such as:

    function GetGlobalMem: THandle; export;
     begin
      Result := hMem;
     end;

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.