_ Declspec (dllexport) & _ declspec (dllimport)

Source: Internet
Author: User

_ Declspec (dllexport)

Declare an export function to export the function from the local DLL. I want to use it for others. It is generally used in DLL
Saves the need to manually define which functions to export in the def file. Of course, if your dll contains all c ++ classes, you cannot specify the export function in def. You can only use _ declspec (dllexport) to export the class.

_ Declspec (dllimport)

Declare an import function, that is, this function is imported from another DLL. I want to use it. It is generally used in the EXE of a DLL.
You can also compile it correctly without using _ declspec (dllimport ).CodeBut the use of _ declspec (dllimport) enables the compiler to generate better code. The compiler can generate better code because it can determine whether the function exists in the DLL, which allows the compiler to generate code that skips the indirect addressing level, these codes usually appear in function calls across DLL boundaries. However, you must use _ declspec (dllimport) to import the variables used in the DLL.


Example:
// File: simpledllclass. h

# Ifdef simpledll_export
# Define dll_export _ declspec (dllexport)
# Else
# Define dll_export
# Endif

Class dll_export simpledllclass
{
Public:
Simpledllclass ();
Virtual ~ Simpledllclass ();

Virtual getvalue () {return m_nvalue ;};
PRIVATE:
Int m_nvalue;
};
 

 

// File: simpledllclass. cpp

# Include "simpledllclass. H"

Simpledllclass: simpledllclass ()
{
M_nvalue = 0;
}

Simpledllclass ::~ Simpledllclass ()
{
}
 

Note:
1. When you include simpledllclass. h In your app, if your app project does not define simpledll_export, dll_export does not exist. At this time, the app can still run normally.

 

 

// File: simpledllclass. h
Static int m_nvalue;

// File: simpledllclass. cpp
Int simpledllclass: m_nvalue = 0;
 

Note:
1. If your app project does not define simpledll_export, dll_export does not exist. In this case, the app cannot be linked. The reason is that m_nvalue cannot be found. (Cause: the static variable m_nvalue has been exported by DLL, but simpledllclass cannot access m_nvalue)
Workaround:
# Define dll_export _ declspec (dllimport)

Conclusion:
Dllimport is used to better process static member variables (or other...) in the class ...) if there are no static member variables (or other ...), the _ declspec (dllimport) does not matter.

/////////////////////////

During windows DLL programming, you can use the _ declspec (dllimport) keyword to import functions or variables.

Function Import When you need to use a function in a DLL, the compiler can automatically import the function without displaying it. However, if you import functions explicitly, the compiler will produce code of better quality. Since the compiler knows exactly whether a function is in a DLL, it can produce better code without indirect call transfer. In Win32 PE format (portable executable format), all import addresses are placed in one import address table. The following example shows the difference between using _ declspec (dllimport) to import a function and not using it: Assume that func is a feature in a dll. currently, the main function of .exe is used to call func, if you do not explicitly import the func function (that is, there is no :__ declspec (dllimport), the code example is as follows: int main () {func ();} the compiler will generate a call code like this: Call func, then the linker translates the call into code like this: Call 0x40000001; ox40000001 is the address of "func" and, the linker will generate Thunk In the format of 0x40000001: jmp dword ptr when imp_functhe imp_funcis the address of the function slot in the import address table of func.pdf in .exe. Then, the local machine only needs to update the import address table of .exe when the local machine is running. If _ declspec (dllimport) is used to import functions explicitly, the linker will not generate thunk (if not required) and directly generate an indirect call. Therefore, the following code: _ declspec (dllimport) void func1 (void );
Void main (void)
{
Func1 ();
} The following call command will be called: Call dword ptr _ imp_func1. Therefore, the explicit import function can effectively reduce the target code (because Thunk is not generated ). In addition, functions other than dll can also be used in DLL to improve space and time efficiency. Variable Import Different from functions, when using a variable in a DLL, you must import the variable explicitly. Use the _ declspec (dllimport) keyword to import a variable. If you use. Def in the DLL to export the variable, you should use data to modify the variable instead of using the abandoned constant. Because constant may need to use pointers to indirectly access variables, it is not sure when the problem will occur. //////////////

I believe that writing Win32ProgramIf you have done a DLL, you will be very clear about the function of _ declspec (dllexport). It is to save a method for manually defining which functions to export in the def file. Of course, if your dll contains all c ++ classes, you cannot specify the export function in def. Instead, you can use _ declspec (dllexport) to export the class. However, the description of _ declspec (dllimport) in the msdn document is a bit strange. Let's take a look at what is said in msdn:
You can compile the code correctly without using _ declspec (dllimport), but using _ declspec (dllimport) enables the compiler to generate better code. The compiler can generate better code because it can determine whether the function exists in the DLL, which allows the compiler to generate code that skips the indirect addressing level, these codes usually appear in function calls across DLL boundaries. However, you must use _ declspec (dllimport) to import the variables used in the DLL.

First, it seems that this section above means that you can use DLL export without using it, but the last sentence also says that _ declspec (dllimport) must be used) to import the variables used in the DLL. What does this mean ??

Let me try it out. Assume that you only export a simple class in DLL. Note that I suppose you have defined simpledll_export IN THE PROJECT properties.
Simpledllclass. h

# Ifdef simpledll_export # define dll_export _ declspec (dllexport) # else # define dll_export # endifclass dll_export simpledllclass {public: simpledllclass (); Virtual ~ Simpledllclass (); Virtual getvalue () {return m_nvalue ;}; PRIVATE: int m_nvalue ;}; simpledllclass. cpp

# Include "simpledllclass. H" simpledllclass: simpledllclass () {m_nvalue = 0;} simpledllclass ::~ Simpledllclass () {} and then you can use this DLL class to include simpledllclass in your app. h, your app project does not need to define simpledll_export. Therefore, dll_export will not exist. At this time, you will not encounter problems in the app. This corresponds to the definitions of _ declspec (dllimport) in msdn. However, we have not encountered that variables cannot be used normally. Well, let's change simpledllclass, change its m_nvalue to static, and then add a line in the CPP file.

Int simpledllclass: m_nvalue = 0; if you don't know why to add this line, go back and check the basics of C ++. After the change, link your app and check the result. The result is that link tells you that the m_nvalue cannot be found. Why is there no such thing as clearly defined ?? It must be because I defined m_nvalue as static. But if I must use the design pattern of Singleton, then this class must have a static member, and there is no link each time. Isn't that all done? If you have the Platform SDK, use the depend program to check whether the m_nvalue is exported in the DLL.
Go back and check out the last sentence I mentioned in msdn. Let's change simpledllclass. H to the following:

# Ifdef simpledll_export # define dll_export _ declspec (dllexport) # else # define dll_export _ declspec (dllimport) # endif link again, everything is normal. Originally, dllimport was used to better process static member variables in the class. If there is no static member variable, this _ declspec (dllimport) does not matter.

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.