DLL Export function

Source: Internet
Author: User
Tags win32

There are two ways to link a DLL: implicit linking and explicit linking

DLL exported functions and export classes at the time of invocation, there are some differences, here for the moment, say simple export function;


Implicit links:


#include "stdafx.h"
#include "MathDLL.h"

#pragma comment (lib, "MathDLL.lib")//You can also set the link of the library in the project properties

int main ()
{
        Add);
        return (1);
}


Explicit Links:

Add a function pointer under the header file or the containing header file, the function has two int type parameters, the function return value is the int type, as follows:

typedef int (*DLLFUNC) (int,int);


DllFunc Padd;
	HInstance h_instance = LoadLibrary (_t ("MathDLL.dll"));

	if (h_instance = = NULL)
	{
		freelibrary (h_instance);
	}

	Padd = (dllfunc) GetProcAddress (H_instance, ("Add"));

	if (Padd = = NULL)
	{
		freelibrary (h_instance);
	}

	int c = Padd (5,9);
	CString str;
	Str. Format (_t ("Output addition number is:%d"), c);

	MessageBox (str,_t ("addition algorithm"));

	FreeLibrary (h_instance);

I am here to test when creating the MFC dialog box program, so do not need to include a special header file, if not, consider to include # include "Windows.h", while explicit linking does not need to include the DLL's header file,

Here are some explanations for DLLs, which I took from other articles.

A hinstance is a Windows data type: A handle to an instance, in which case the instance will be the DLL. You can get an instance of the DLL by using the function LoadLibrary (), which obtains a name as a parameter. After calling the LoadLibrary function, you must look at the success of the function return. You can see if hinstance is equal to null (a header file that is defined in Windows.h as 0 or Windows.h) to check for success. If it is equal to NULL, the handle will be invalid and you must release the library. In other words, you must release the memory that the DLL obtains. If the function returns successfully, your hinstance contains a handle to the DLL.

Once you get a handle to the DLL, you can now get the function back from the DLL. To do this, you must use the function GetProcAddress (), which takes the handle of the DLL (you can use hinstance) and the name of the function as a parameter. You can have the function pointer get the value returned by GetProcAddress (), and you must convert GetProcAddress () to the function pointer defined by that function. For example, for the Add () function, you must convert GetProcAddress () to Addfunc, which is why it knows the parameters and the return value. Now, it's a good idea to first determine if the function pointers are equal to null and what functions they have DLLs. This is just a simple if statement; if one equals null, you must release the library as described earlier.

Once a function pointer has a function of a DLL, you can use them now, but here's a note: You can't use the actual name of the function; You must use a function pointer to invoke them. After that, all you have to do is release the library.

Module Handle

Each DLL module in the process is identified by a globally unique 32-byte hinstance handle. The process itself also has a hinstance handle. All of these module handles are valid only within a specific process, and they represent the starting address of a DLL or EXE module in the process virtual space. In Win32, the values of hinstance and hmodule are the same, and the two types can be substituted for use. The process module handle is almost always equal to 0x400000, and the default handle to the load address of the DLL module is 0x10000000. If the program uses several DLL modules at the same time, each one will have a different hinstance value. This is because a different base address is specified when the DLL file is created, or because the loader has relocated the DLL code.
Module handles are particularly important for loading resources. Win32 has a hinstance parameter in the FindResource function. EXE and DLLs have their own resources. If your application requires resources from a DLL, specify this parameter as the module handle for the DLL. Specifies the module handle of the EXE if the resource contained in the EXE file is required.
But there is a problem before using these handles, how do you get them? If you need to get the EXE module handle, call the Win32 function GetModuleHandle with a null parameter, or if you need a DLL module handle, call the Win32 function GetModuleHandle with the DLL file name as the parameter.

How the application finds DLL files

If an application uses LoadLibrary explicit linking, the full path to the DLL file can be specified in the parameters of this function. If you do not specify a path or make an implicit link, Windows locates the DLL in the following search order:
1. The directory that contains the EXE file,
2. The current working directory of the process,
3. Windows system directory,
4. Windows directory,
5. A list of directories listed in the PATH environment variable.
Here is a trap that is prone to errors. If you use VC + + for project development and create a project specifically for the DLL module, then copy the generated DLL file to the system directory and call the DLL module from the application. So far, everything is fine. Next the DLL module has made some modifications to regenerate the new DLL file, but you forget to copy the new DLL files to the system directory. The next time you run the application, it still loads the old version of the DLL file, so be careful.

Debugging DLL Programs

Microsoft VC + + is an effective tool for developing and testing DLLs, just run the debugger from the DLL project. When you do this for the first time, the debugger asks you for the path to the exe file. The debugger automatically loads the exe file every time after you run the DLL in the debugger. The exe file then discovers the DLL file with the search sequence above, which means you must set the PATH environment variable to include the disk path to the DLL file, or you can copy the DLL file to the directory path in the search sequence.
Or, when you debug an EXE program, in Project setting, set the category in the Debug tab to additional DLLs. You can debug both the EXE and the DLL it calls (of course, you need to have the DLL's source code).

Program run:



Complete

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.