现在的位置: 首页 > 综合 > 正文

c++ DLL 简单接口实例

2019年01月12日 ⁄ 综合 ⁄ 共 2875字 ⁄ 字号 评论关闭

给用户提供接口的时候应该至少有以下几个文件:

  • ISample.h 提供给用户的接口头文件,里面应该有接口函数的定义,一般都是纯虚函数。
  • SampleAPI.h 提供给用户的接口头文件,里面应该有DLL应用程序的入口点,以及ISample 对象的创建和析构。
  • CSample.h 不提供给用户,一般继承自ISample 并且实现ISample 里接口函数的功能。

以下是一个简单的例子:

/*
 * Author: zjt
 *
 * 功能:提供给用户的接口头文件
 */

#ifndef MY_ISAMPLE_H
#define MY_ISAMPLE_H

class ISample
{
public:

	// Constructor
	//
	ISample(){};

	// Destructor
	//
	virtual ~ISample(){};

	// interface function  
	// return Computer name
	virtual int GetComputerName(TCHAR* pComputerName,size_t name_size) = 0;

	//interface function
	//return System Directory
	virtual int GetSysDirectory(TCHAR* pSysDirectory,size_t dir_size) = 0;

};

#endif


/*
 * Author: zjt
 *
 * 功能:提供给用户的API头文件
 * 注意:SAMPLE_EXPORTS 应该在工程->属性->预定义设置里
 */

#ifndef __SAMPLE_API_DLL__
#define __SAMPLE_API_DLL__


#include "ISample.h"

#ifdef SAMPLE_EXPORTS
#define SAMPLE_API __declspec(dllexport)
#else
#define SAMPLE_API __declspec(dllimport)
#endif


SAMPLE_API ISample* CreateSam(void);

SAMPLE_API void DeleteSam(ISample* pISam);

#endif

/*
 * Author: zjt
 *
 * 功能:实现接口功能头文件
 */

#ifndef __CSAMPLE_H
#define __CSAMPLE_H

#include "ISample.h"

class CSample : public	ISample
{
public:
	CSample();
	~CSample();

	int GetComputerName(TCHAR* pComputerName,size_t name_size);

	int GetSysDirectory(TCHAR* pSysDirectory,size_t dir_size);
};

#endif


/*
 * Author: zjt
 *
 * 功能:SampleAPI.cpp : 定义 DLL 应用程序的入口点和导出函数。
 */


#include "stdafx.h"
#include "SampleAPI.h"
#include "CSample.h"

BOOL APIENTRY DllMain( HMODULE hModule,
					  DWORD  ul_reason_for_call,
					  LPVOID lpReserved
					  )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		break;
	}
	return TRUE;
}

SAMPLE_API ISample* CreateSam(void)
{
	ISample *pISam = NULL;
	pISam = new CSample();
	return ((pISam == NULL) ? NULL : pISam);
}


SAMPLE_API void DeleteSam(ISample* pISam)
{
	if (pISam != NULL)
	{
		CSample* pCSam = (CSample*)pISam;
		delete pCSam;
		pCSam = NULL;
	}
}

/*
 * Author: zjt
 *
 * 功能:实现接口功能cpp文件
 */

#include "stdafx.h"
#include "CSample.h"
#include <winnt.h>
#include <tchar.h>


#define INFO_BUFFER_SIZE 32767

CSample::CSample(){}

CSample::~CSample(){}


int CSample::GetComputerName(TCHAR* pComputerName,size_t name_size)
{
	//Get and display the computer name
	TCHAR  infoBuf[INFO_BUFFER_SIZE];
	DWORD  bufCharCount = INFO_BUFFER_SIZE;
	if( !::GetComputerName(infoBuf, &bufCharCount))
	{
		_tprintf(TEXT("error"));
	}
	 
	wcsncpy_s(pComputerName,name_size,infoBuf,name_size-1);
	return 0;

}

int CSample::GetSysDirectory(TCHAR* pSysDirectory,size_t dir_size)
{

	// Get and display the system directory. 
	TCHAR  infoBuf[INFO_BUFFER_SIZE];
	DWORD  bufCharCount = INFO_BUFFER_SIZE;
	if( !::GetSystemDirectory( infoBuf, INFO_BUFFER_SIZE ) )
	{
		_tprintf(TEXT("error"));
	} 
	wcsncpy_s(pSysDirectory,dir_size,infoBuf,dir_size-1);
	return 0;
}
// TestSample.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>

#include "SampleAPI.h"


#pragma comment(lib,"MyTestDll.lib")

int _tmain(int argc, _TCHAR* argv[])
{

	ISample* pISam = CreateSam();
	TCHAR tmpComputer[256];
	TCHAR tmpDir[256];
	pISam->GetComputerName(tmpComputer,256);
	_tprintf( TEXT("Computer Name:   %s\n"), tmpComputer );
	pISam->GetSysDirectory(tmpDir,256);
	_tprintf( TEXT("System Directory:   %s\n"), tmpDir );
	DeleteSam(pISam);
	system("pause");
	return 0;
}

抱歉!评论已关闭.