1. dll-relatedCode
Mydll. h
[CPP] View plaincopyprint?
-
- # Ifdef dll.pdf API
-
- # Else
-
- # Define dllsort API extern "C" _ declspec (dllimport)
-
- # Endif
-
- Dll.pdf APIIntAdd (IntA,IntB );
- Dll.pdf APIIntSub (IntA,IntB );
-
- Class _ Declspec(Dllexport) Person
-
- {
-
- Public:
-
- Person (Char* Name );
-
- Char* M_name;
-
- IntM_age;
-
- };
# Ifdef dll1_api <br/> # else <br/> # define dll1_api extern "C" _ declspec (dllimport) <br/> # endif <br/> dllw.api int add (int A, int B); <br/> dllw.api int sub (int A, int B ); <br/> class _ declspec (dllexport) person <br/>{< br/> Public: <br/> person (char * Name ); <br/> char * m_name; <br/> int m_age; <br/> };
Mydll. cpp
[CPP] View plaincopyprint?
-
- # Define dll1_api extern "C" _ declspec (dllexport)
-
- # Include "mydll. H"
- # Include <windows. h>
-
- # Include <stdio. h>
-
- # Pragma comment (linker, "/DLL ")
-
- # Pragma comment (linker, "/entry: dllmain ")
-
- IntAdd (IntA,IntB)
-
- {
-
- ReturnA + B;
-
- }
- IntSub (IntA,IntB)
-
- {
-
- ReturnA-B;
-
- }
-
- Person: Person (Char* Name)
-
- {
-
- M_name = Name;
-
- }
# Define dll1_api extern "C" _ declspec (dllexport) <br/> # include "mydll. H "<br/> # include <windows. h> <br/> # include <stdio. h> <br/> # pragma comment (linker, "/DLL") <br/> # pragma comment (linker, "/entry: dllmain ") <br/> int add (int A, int B) <br/>{< br/> return A + B; <br/>}< br/> int sub (int A, int B) <br/>{< br/> return A-B; <br/>}< br/> person: Person (char * Name) <br/>{< br/> m_name = Name; <br/>}
Compile the link, for example:
2. Call the class in DLL
Main. cpp
[CPP] View plaincopyprint?
-
- # Include <iostream. h>
-
- # Include <stdio. h>
-
- # Include <windows. h>
-
- # Include "mydll. H"
-
- # Pragma comment (Lib, "mydll. lib ")
-
- VoidMain ()
-
- {
-
- IntX = 3;
-
- IntY = 9;
- IntZ = add (x, y );
-
- Printf ("% D + % d = % d/R/N", X, y, z );
-
-
- Person Pt ("123");
-
- Cout <pt. m_name <Endl;
-
- }
# Include <iostream. h> <br/> # include <stdio. h> <br/> # include <windows. h> <br/> # include "mydll. H "<br/> # pragma comment (Lib," mydll. lib ") <br/> void main () <br/>{< br/> int x = 3; <br/> int y = 9; <br/> int z = add (x, y); <br/> printf ("% d + % d = % d/R/N", x, y, z); </P> <p> person Pt ("123"); <br/> cout <PT. m_name <Endl; <br/>}
Compile the link, for example:
From:
Http://blog.csdn.net/wangningyu/article/details/5467550