1、Dll相關代碼
MyDll.h
[cpp] view plaincopyprint?
- #ifdef DLL1_API
- #else
- #define DLL1_API extern "C" __declspec(dllimport)
- #endif
- DLL1_API int Add(int a,int b);
- DLL1_API int Sub(int a,int b);
- class __declspec(dllexport) Person
- {
- public:
- Person(char *name);
- char* m_Name;
- int m_Age;
- };
#ifdef DLL1_API<br />#else<br />#define DLL1_API extern "C" __declspec(dllimport)<br />#endif<br />DLL1_API int Add(int a,int b);<br />DLL1_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")
- int Add(int a,int b)
- {
- return a+b;
- }
- int Sub(int a,int b)
- {
- return a-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 />}
編譯連結,如:
2、調用dll中類
Main.cpp
[cpp] view plaincopyprint?
- #include <iostream.h>
- #include <stdio.h>
- #include <windows.h>
- #include "MyDll.h"
- #pragma comment(lib,"MyDll.lib")
- void main()
- {
- int x=3;
- int y=9;
- int z=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 />}
編譯連結,如:
from:
http://blog.csdn.net/wangningyu/article/details/5467550