1. dllSource code
Mydll. h
[CPP] View plaincopyprint?
- //////////////////////// //////////////////////////////////////// /// //
- // mydll. H
- // declare a function
- int _ stdcall add ( int , int B);
- int _ stdcall sub ( int , int B);
//////////////////////////////////////// /// // <Br/> // mydll. h <br/> // declare the function <br/> int _ stdcall add (int A, int B); <br/> int _ stdcall sub (int, int B );
Mydll. cpp
[CPP] View plaincopyprint?
-
- //////////////////////////////////////// //////////////////////////////////
-
- // Mydll. PP
-
- // Declaration implementation
-
- # Include "mydll. H"
- Int_ Stdcall add (IntA,IntB)
-
- {
-
- ReturnA + B;
-
- }
-
- Int_ Stdcall sub (IntA,IntB)
-
- {
-
- ReturnA-B;
-
- }
//////////////////////////////////////// /// // <Br/> // mydll. PP <br/> // Declaration implementation <br/> # include "mydll. H "<br/> int _ stdcall add (int A, int B) <br/>{< br/> return A + B; <br/>}< br/> int _ stdcall sub (int A, int B) <br/>{< br/> return a-B; <br/>}
Mydll. Def
[CPP] View plaincopyprint?
- Mydll is the project name
- Library mydll
- Declare the function to be exported here
- Exports
- Add
- Sub
; Mydll is the project name <br/> library mydll <br/>; declare the function to be exported here <br/> exports <br/> Add <br/> sub
2. EXE TestingCode
Demonstrate the dynamic and static loading methods. Check the code!
[CPP] View plaincopyprint?
-
- VoidCtestdlg: onbtnstatic ()
-
- {
-
- // Todo: add your control notification handler code here
- // Static Loading Method:
-
- // 1. Add the header file # include "mydll. H"
-
- // 2. Introduce the Lib library # pragma comment (Lib, "mydll. lib ")
-
- // 3. You can directly use the function imported in mydll. h.
-
- Cstring STR;
-
- Str. Format ("Static Loading: 1 + 1 = % d 1-1 = % d", Add (1, 1), sub (1, 1 ));
-
- MessageBox (STR );
-
- }
- VoidCtestdlg: onbtndynamic ()
-
- {
-
- // Todo: add your control notification handler code here
-
- // Dynamic Loading Method:
-
- // The header file and Lib file do not need to be introduced. Only one DLL is required.
-
- // Note the Treaty call conventions here _ stdcall do not forget to add (otherwise ESP errors will be introduced)
-
- Typedef Int(_ Stdcall * addproc )(Int,Int);
-
- Typedef Int(_ Stdcall * subproc )(Int,Int);
-
- HinstanceHandle;
-
- Handle = loadlibrary ("Mydll. dll");
-
- If(Handle)
- {
-
- // The second getprocaddress parameter has two methods:
-
- // 1. Use the function name in the DLL
-
- // 2. Check the ordinal index value in the depend tool.
-
- Addproc myadd = (addproc) getprocaddress (handle,"Add");
-
- Subproc mysub = (addproc) getprocaddress (handle, makeintresource (2 ));
- If(! Myadd)
-
- {
-
- MessageBox ("An error occurred while obtaining the function add address! ");
-
- Return;
-
- }
-
- If(! Mysub)
-
- {
- MessageBox ("An error occurred while obtaining the Function Sub address! ");
-
- Return;
-
- }
-
- Cstring STR;
-
- Str. Format ("Dynamic Loading: 1 + 1 = % d 1-1 = % d", Myadd (1, 1), mysub (1, 1 ));
-
- MessageBox (STR );
-
- }
-
- Freelibrary (handle );
- }