天天看點

Dll 導出類 [示例代碼]

1、Dll相關代碼

MyDll.h

#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

#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;

};

MyDll.cpp

#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)

#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;

編譯連結,如下圖:

Dll 導出類 [示例代碼]

2、調用dll中類

Main.cpp

#include <iostream.h> 

#include <windows.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>

#include <windows.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;

Dll 導出類 [示例代碼]

from:

<a href="http://blog.csdn.net/wangningyu/article/details/5467550">http://blog.csdn.net/wangningyu/article/details/5467550</a>