VC macro afx_ext_class

Source: Internet
Author: User

Original article: http://xinbaojiang.blog.163.com/blog/static/119970729200952411266197/

Use afx_ext_class to export and import data

The extension DLL uses the afx_ext_class macro export class. The executable file linked to the extension DLL uses this macro import class. Use the afx_ext_class macro to generate the same header file of the extended DLL and use it together with the executable file linked to the DLL.

In the DLL header file, add the afx_ext_class keyword to the class declaration, as shown below:

Class afx_ext_class cmyclass: Public cdocument {//};

When preprocessing _ afxdll and _ afxext are defined, this macro is defined as _ declsp (dllexport) by MFC ). however, when _ afxdll is defined but _ afxext is not defined, the macro is defined as _ declspec (dllimport ). After definition, the Preprocessor symbol _ afxdll indicates that the shared MFC version is being implemented by the target executable file (DLL or applicationProgram. When both _ afxdll and _ afxext are defined, this indicates that the target executable file is an extension DLL.

When exporting from an extended DLL, afx_ext_class is defined as _ declspec (dllexport). Therefore, you can export the entire class without placing the modifier names of all the symbols of the class in the. Def file. This method is used by the MFC example dllhusk.

Although this method can avoid creating all the modified names of the. Def file and class, it is more efficient to create a. Def file because the names can be exported by serial number. To use the. Def File Export methodCodePut it at the beginning and end of the header file:

# UNDEF afx_data

# Define afx_data afx_ext_data

//

# UNDEF afx_data

# Define afx_data

Warning when exporting inline functions, be careful because they may cause version conflicts. Inline functions are extended to application code. Therefore, inline functions will not be updated unless the application itself is re-compiled if inline functions are rewritten later. (Generally, you can update the DLL function without generating a new application using the DLL function .)

Export individual members in the class

Sometimes, you may want to export individual members in the class. For example, if you export a cdialog derived class, you may only need to export the constructor and domodal call. You can use afx_ext_class for individual members to export.

For example:

Class cexampledialog: Public cdialog

{

Public:

Afx_ext_class cexampledialog ();

Afx_ext_class int domodal ();

...

// Rest of Class Definition

...

};

You no longer export all the members of the class, but you may encounter other problems due to the way the MFC macro works. Several MFC helper macros actually declare or define data members. Therefore, you must export the data members from the DLL.

For example, when an extended DLL is generated, the declare_dynamic macro is defined as follows:

# Define declare_dynamic (class_name)

Protected:

Static cruntimeclass * Pascal _ getbaseclass ();

Public:

Static afx_data cruntimeclass class # class_name;

Virtual cruntimeclass * getruntimeclass () const;

The internal static object of the row Declaration class headers with static afx_data. To export the class correctly and access runtime information from the executable file on the client, you must export this static object. Because static objects are declared with the afx_data modifier, you only need to define afx_data AS _ declspec (dllexport) when generating the DLL ), when the client executable file is generated, afx_data is defined as _ declspec (dllimport ). Because afx_ext_class has been defined in this way, you only need to refer to the class definition and redefine afx_data to be the same as afx_ext_class.

For example:

# UNDEF afx_data

# Define afx_data afx_ext_class

Class cexampleview: Public cview

{

Declare_dynamic ()

//... Class definition...

};

# UNDEF afx_data

# Define afx_data

MFC always uses the afx_data symbol on the data items defined in its macro, so this technology applies to all such situations. For example, it applies to declare_message_map.

Note: If you export the entire class rather than the selected class member, the static data member will be automatically exported.

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.