How to export classes in a dynamic library (DLL) (1)

Source: Internet
Author: User

How to export classes in a dynamic library (DLL)Ø development system: Windows 2000 + Service Pack 4 ø development tool: C ++ Builder 6.0 + Service Pack 4 ø preface I would like to thank all those who have helped me in csdn, especially Ji shiping and Xie Wei! Thank you for your help and support! I don't need to say much about how to write a dynamic library (DLL !! If you do not know much about this, you can find the article "BCB writing the ultimate DLL manual" on the Internet, which is very detailed !! When I write a dynamic library (DLL) and a class, I am wondering how to write a class in the dynamic library (DLL) and let the program (exe) it can be called like a "own" class, so that you can not hide the code of your own writing class! But this idea has never been realized! Later, three methods were developed to meet the needs of the work. The following is a brief introduction! N Method 1: (provided by Xie Wei) write the class handle as an export function for the EXE call. The problem to be solved is to pass the class handle to the EXE, that is, its handle, this method is complicated. You need to write an export function for each member function of the class, and the EXE must be added to the Lib file of the dynamic library (DLL! DLL file: maindll. cpp
// ----------------------------------------------------------------------------- # Include <VCL. h> # include <windows. h> # pragma hdrstop # pragma argsused // specify int winapi dllentrypoint (hinstance hinst, unsigned long reason, void * lpreserved) {return 1;} // do not forget this sentence Add! Define the DLL export function # include "makedll. H "// classes // makedll class, which is equivalent to the class (a simple class) Class makedll {PRIVATE: int mnumber; public: makedll (); int setnumber (INT ivalue); void showoldvalue (); void shownewvalue () ;}; // define // class definition // define makedll: makedll () {mnumber = 100;} // specify int makedll: setnumber (INT ivalue) {mnumber = ivalue; return errok;} // define void makedll: shownewvalue () {showmessage ("the new value is:" + inttostr (mnumber);} // your void makedll: showoldvalue () {showmessage ("the old value is: "+ inttostr (mnumber);} // examples // The above is the definition of the class // examples // use the makedll class inside the DLL, it is equivalent to calling your own class directly in the program. // publishing this function is to test this function in the application // using general_apiexport int TESTA (INT ivalue) {makedll T1; t1.showoldvalue (); t1.setnumber (ivalue); t1.shownewvalue (); Return errok ;} // outputs // The following interface is provided to hide the definition of your class. // ------------------------------------------------------------ // handle) export to exegeneral_apiexport handle initmakedll () {makedll * T1; T1 = new makedll; return handle (T1 );} // else // use this function in the original place where Testa was used. // EXE transfers the class handle (handle) obtained from the above function to general_apiexport int testb (handle hmakedll, int ivalue) {makedll * T1; // converts a class handle to a class pointer T1 = (makedll *) hmakedll; T1-> showoldvalue (); T1-> setnumber (ivalue ); t1-> shownewvalue (); Return errok;} // do not forget to provide a class release function general_apiexport int releasemakedll (handle hmakedll) {makedll * T1; t1 = (makedll *) hmakedll; Delete T1; return errok;} // --------------------------------------------------------------------------- DLL file: makedll. h // you can use this file by yourself, you can also publish it to the person who wants to use your DLL. // This header file will not expose any of your class definitions # ifndef h_peripheral # define h_peripheral // else # define general_apiexport extern" C "_ declspec (dllexport) # define general_apiimport extern "C" _ declspec (dllimport) // specify static int errok = 0; static int errwrong = 1; // specify general_apiimport int TESTA (INT ivalue ); // export the test function general_apiimport handle initmakedll (); // export the class handle general_apiimport int testb (handle hmakedll, int ivalue ); // export the class in the member function general_apiimport int releasemakedll (handle hmakedll); // export and release the function // outputs # endif

 

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.