C++ DLL调用

以生成一个TestDll.dll为例

第一步:创建一个工程TestDll:


//TestDll.h文件--------------------------------------------------------

#pragma once

#ifdef  _BUILD_DLL
#define DLL_API  __declspec(dllexport)
#else
#define DLL_API  __declspec(dllimport)
#endif

extern "C" DLL_API int int_max_dll(int a, int b);
extern "C" DLL_API double double_max_dll(double a, double b);

class DLL_API testClass
{
public:
	testClass();
	int getValue();
private:
	int iValue;
};

//TestDll.cpp文件--------------------------------------------------------

#define _BUILD_DLL

#include "TestDll.h"

int int_max_dll(int a, int b)
{
	return a>b?a:b;
}

double double_max_dll(double a, double b)
{
	return a>b?a:b;
}

testClass::testClass():iValue(20){}

int testClass::getValue()
{
	return iValue;
}

编译生成dll,copy到另一个工程的对应的目录下,我这里是CallDll.exe同级

第二步:调用(创建工程CallDll)

静态连接调用(非常简单):

#include "stdafx.h"
#include <Windows.h>
#include <iostream>
//添加动态库的头文件
#include "../TestDll/TestDll.h"
using namespace std;
//////----------------------------------------------------------------------------
//////隐式连接方式----加载LIB导入库
#pragma comment(lib,"../debug/TestDll.lib")

void main()
{
	int a=6,b=11;
	double aa=2.43, bb=12.36;

	testClass tc;

	cout<<"iValue is:"<<tc.getValue()<<endl;
	cout<<"the max int is:"<<int_max_dll(a,b)<<endl;
	cout<<"the max double is:"<<double_max_dll(aa,bb)<<endl;
	getchar();
}
////----------------------------------------------------------------------------
动态连接调用

1>添加一个头文件,主要是对TestDll.h再次包装(本例不需,但我下面写一个做例子)

//co_tuner_api.h-----------------------------------------------------

#pragma once

typedef enum
{
	TUNER_OP_START,
	TUNER_OP_SEEK_UP,
	TUNER_OP_SEEK_DOWN,
	TUNER_OP_SCAN,
	TUNER_OP_STEP_UP,
	TUNER_OP_STEP_DOWN,
	TUNER_OP_AS,
	//............................
	TUNER_OP_STOP_HANDLE,
	TUNER_OP_SEEK_STOP,
}TUNER_OP_TYPE;

typedef enum 
{ 
	CO_SUCCESSFUL			=  0,	//Successful
	CO_ERR_CONNECT_DEVICE_FAILED	= -1,	//Connect to device failed
	CO_ERR_NO_RESOURCE_TO_FIND	= -2,	//No resource to find
	CO_ERR_OUT_OF_RESOURCE		= -3,	//Out of resource
}CO_SUCCESS_ERROR;

//Band枚举
typedef enum
{
	FM,
	AM,
}CO_TUNER_FMAM_ENUM;

//按钮状态
typedef enum
{
	Normal,
	Press,
	Focus,
}CO_TUNER_BTN_STATE_ENUM;

//收音列表数据结构
typedef struct
{
	int    nListNum;                   //列表索引号
	int    nListlen;                      //列表信息长度
	WCHAR  *wInfo;					 //列表信息内容
}TUNER_LIST_STRUCT;

//声明函数指针类型
typedef int(*MAXINT)(int a, int b);
typedef double(*MAXDOUBLE) (double a, double b);

//typedef CO_SUCCESS_ERROR (*Co_Tuner_OperatorHandle_Fun)(TUNER_OP_TYPE eTunerOpType);
//typedef CO_SUCCESS_ERROR (*Co_Tuner_Band_Fun)(int index);
//typedef CO_SUCCESS_ERROR (*Co_Tuner_Send_Freq_Fun)(int Freq);

//===不是bool的可以这样写=========================
//CO_SUCCESS_ERROR Co_Tuner_DeInit(void)
//{
//	if(pCo_Tuner_DeInit)
//	{
//		return pCo_Tuner_DeInit();	
//	}
//	return CO_ERR_CONNECT_DEVICE_FAILED;;
//}
//===============================================

class CCoTunerDll
{
public:
	CCoTunerDll()
	{
		ZeroTunerDllFun();
		m_hCoTunerDll = NULL;
	}

	~CCoTunerDll()
	{
		ZeroTunerDllFun();
		FreeTunerDll();
	}

	//===============================================
	// 函数名  :                
	// 功能    : 			 
	// 入口参数: 
	// 返回值  :                      
	//===============================================
	BOOL Co_Tuner_Load_Dll(void)
	{
		if(LoadTunerDll() != 0)
		{
			return false;
		}
		if(NULL == m_hCoTunerDll)
		{
			return false;
		}
		pMaxInt    =  (MAXINT)GetProcAddress(m_hCoTunerDll, "int_max_dll");
		pMaxDouble =  (MAXDOUBLE)GetProcAddress(m_hCoTunerDll, "double_max_dll");

		if(	NULL == pMaxInt || NULL == pMaxDouble)
		{
			return false;
		}
		return true;
	}

	//===============================================
	int DLL_MAX_INT(int a,int b)
	{
		if(pMaxInt)
		{
			OutputDebugString(L"---Call: DLL_MAX_INT\r\n");
			return pMaxInt(a,b);
		}
		return -1;
	}    

	//===============================================
	double DLL_MAX_DOUBLE(double a,double b)
	{
		if(pMaxDouble)
		{
			OutputDebugString(L"---Call: DLL_MAX_DOUBLE\r\n");
			return pMaxDouble(a,b);
		}
		return -1.0;
	}

private:

	int LoadTunerDll(void)
	{
		if(m_hCoTunerDll)
		{
			return true;
		}

		m_hCoTunerDll =::LoadLibrary(L"TestDll.dll");

		if(NULL == m_hCoTunerDll)
			return 1;

		return 0;
	}

	void FreeTunerDll(void)
	{
		if(NULL != m_hCoTunerDll)
		{
			::FreeLibrary(m_hCoTunerDll);
			m_hCoTunerDll = NULL;
		}
	}

	void ZeroTunerDllFun()
	{
		pMaxInt = NULL;
		pMaxDouble = NULL;
	}


private:
	HMODULE	m_hCoTunerDll;

	MAXINT           pMaxInt;
	MAXDOUBLE    pMaxDouble;			
};


//使用方式
TUNER_LIST_DATA *pData = (TUNER_LIST_DATA*)lPar;
CO_TUNER_FMAM_ENUM fmam = (CO_TUNER_FMAM_ENUM)wPar;
bool bFm = (fmam == FM);

如在一个对话框的按钮事件中调用dll。( 不要忘记添加#include "co_tuner_api.h")

void CCallDllDlg::OnBnClickedOk()
{
	CCoTunerDll m_CoTunerDll;
	if (m_CoTunerDll.Co_Tuner_Load_Dll())
	{
		WCHAR str[100];
		//swprintf_s(str,L"DLL successful,DLL_MAX_INT(2,9)=%d", m_CoTunerDll.DLL_MAX_INT(2,9));
		swprintf_s(str,L"DLL successful,DLL_MAX_DOUBLE(1.2,6.7)=%.2f", m_CoTunerDll.DLL_MAX_DOUBLE(1.2,6.7));
		GetDlgItem(IDC_STATIC)->SetWindowText(str);
	}
	else
	{
		GetDlgItem(IDC_STATIC)->SetWindowText(L"DLL Failed");
	}
}

现在大功告成!

 

 

posted @ 2015-06-10 20:09  YOUNG++  阅读(261)  评论(0编辑  收藏  举报