首先需要先準備一個dll 檔,以下為dll檔的.h和.cpp程式碼:

HelloDLL.h

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once

//more about this in reference 1
#ifdef DLLDIR_EX
#define DLLDIR  __declspec(dllexport)   // export DLL information

#else
#define DLLDIR  __declspec(dllimport)   // import DLL information

#endif 

class DLLDIR HelloDLL
{
public:
	HelloDLL(void);
	~HelloDLL(void);

	void hello();
	static void helloStatic();

};

HelloDLL.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "HelloDLL.h"
#include <iostream>

using namespace std;

HelloDLL::HelloDLL(void)
{
}

HelloDLL::~HelloDLL(void)
{
}

void HelloDLL::hello()
{
	cout << "Hello World of DLL" << endl;
}

void HelloDLL::helloStatic()
{
	cout << "Hello World of DLL static" << endl;
}

 

撰寫好後邊亦即可以產生dll檔。


 

開始撰寫程式並將dll檔引入,首先建立一個c++專案( 習慣用empty ,請依照自己需求建立 )

image

在專案中引入DLL參考:

首先點選專案右鍵 Properties > Linker >  Input > Additional Dependencies  ,並在Additional Dependencies 內添加你的 lib檔 ( 本範例為 HelloDLL.lib  )後按OK即可。

image

 

撰寫程式(請記得引用HelloDLL.h),程式撰寫完畢後執行即可以運行

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include "HelloDLL.h"
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

int main(int argc, char* argv[])
{

	HelloDLL helloDLL;
	helloDLL.hello();
	HelloDLL::helloStatic();

	getchar();

	return 0;
}

 

完整專案(連結)

arrow
arrow
    文章標籤
    c++
    全站熱搜

    Lung-Yu,Tsai 發表在 痞客邦 留言(0) 人氣()