fenri's diary

基本的には勉強し始めたC#のメモ。後は140字で収まらない駄文。

DLLの作成方法 使用方法(暗黙的、静的 リンク)

暗黙的(静的)リンク の方法

DLLを作成するようにプロジェクトを作成
[新しいプロジェクト]-[Visual C++]-[Win32プロジェクト]
[アプリケーションの種類]-[DLL]
ヘッダー

#include "stdafx.h"

// MAKE_DLLを宣言するとDLLを作る
#ifdef	MAKE_DLL
#define	__PORT	__declspec(dllexport)	/*	DLLを作る場合	*/
#else
#define	__PORT	__declspec(dllimport)	/*	DLLを使う場合	*/
#endif

__PORT	int __stdcall MyFunction(int x);

ソース

// DllMake.cpp : DLL アプリケーション用にエクスポートされる関数を定義します。
//
#include "stdafx.h"

#define		MAKE_DLL	// Include順によってはエラーになるので注意
#include	"DllMake.h"

/*
外部に公開され共有される関数MyFunction
その機能は、与えられた整数を100で割った余りを返す。
*/
__PORT int __stdcall MyFunction(int x)
{
	return	x % 100;
}


DLLを使用する側のプロジェクト作成
[新しいプロジェクト]-[Visual C++]-[Win32プロジェクト]
[アプリケーションの種類]-[なんでもいい]

ソース

// DllUse.cpp : コンソール アプリケーションのエントリ ポイントを定義します。
//
#include "stdafx.h"

#pragma comment(lib, "DllMake.lib")
__declspec(dllimport)
int __stdcall MyFunction(int x);


// DLLを呼び出す関数
int _tmain(int argc, _TCHAR* argv[])
{
	printf("%d\n", MyFunction(12345));
	return 0;
}