VS2008开发之模板化DLL

序言:

使用VS2008开发DLL库模块化!

正文:

1、Jimmy_Math.h

#pragma once

class Jimmy_Math{
public:
	Jimmy_Math();
	~Jimmy_Math();

	int Add(int n1, int n2);
	int Sub(int n1, int n2);
	int Multiply(int n1, int n2);
	int Divide(int n1, int n2);
};

2、Jimmy_Math.cpp

#include "Jimmy_Math.h"

Jimmy_Math::Jimmy_Math()
{

}

Jimmy_Math::~Jimmy_Math()
{

}

int Jimmy_Math::Add(int n1, int n2)
{
	return n1 + n2;
}

int Jimmy_Math::Sub(int n1, int n2)
{
	return n1 - n2;
}

int Jimmy_Math::Multiply(int n1, int n2)
{
	return n1 * n2;
}

int Jimmy_Math::Divide(int n1, int n2)
{
	if (n2)
	{
		return n1 / n2;
	}
	else
	{
		return -1;
	}
}

3、TW_MPSSE.h

#pragma once

#define TW_MPSSE_DLL_EXPORT __declspec(dllexport)

TW_MPSSE_DLL_EXPORT
int ex_add(int n1, int n2);

TW_MPSSE_DLL_EXPORT
int ex_sub(int n1, int n2);

TW_MPSSE_DLL_EXPORT
int ex_multiply(int n1, int n2);

4、TW_MPSSE.cpp

#include "Jimmy_Math.h"

#include "TW_MPSSE.h"

static Jimmy_Math *p_mm = NULL;

BOOL APIENTRY DllMain( HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		if (p_mm == NULL)
		{
			p_mm = new Jimmy_Math();
		}
		break;
	case DLL_THREAD_ATTACH:
		break;
	case DLL_THREAD_DETACH:
		break;
	case DLL_PROCESS_DETACH:
		if (p_mm != NULL)
		{
			delete p_mm;
		}
		break;
	}
	return TRUE;
}

//////////////////////////////////////////////////////////////////////////
int ex_add(int n1, int n2)
{
	return p_mm->Add(n1, n2);
}

int ex_sub(int n1, int n2)
{
	return p_mm->Sub(n1, n2);
}

int ex_multiply(int n1, int n2)
{
	return p_mm->Multiply(n1, n2);
}

备注:

VS2008开发之模板化DLL_第1张图片

---- The End.

 

你可能感兴趣的:(VS2008开发)