1.C++定义dll接口
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the PSU_DLL_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// PSU_DLL_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#pragma once
#ifndef PSU_DLL_H
#define PSU_DLL_H#ifdef PSU_DLL_EXPORTS
# define PSU_DLL_API __declspec(dllexport)
#else
# define PSU_DLL_API __declspec(dllimport)
#endif#include <tchar.h>#ifdef __cplusplus
extern "C" {
#endif//
typedef enum
{PSU_UNKONW = -1,PSU_BEGIN = 0,PSU_Agilent_663xx = 0,PSU_Agilent_E3631A,PSU_GWINSTTEK_PPH1503D,PSU_END
} PSU_TYPE;typedef enum
{PSU_OFF = 0,PSU_ON,
} PSU_ONOFF;//
// PSU Interface
class CPSUInterface
{friend PSU_DLL_API CPSUInterface* PSUCreate(const char* pcPsuGpibAddr, PSU_TYPE ePsuType);
protected:CPSUInterface();
public:virtual ~CPSUInterface(void);virtual int PSU_Initialize(double Voltage_Limit, double Current_Limit) = 0;virtual int PSU_PowerOnOff(PSU_ONOFF OnOffState) = 0;virtual int PSU_Set_VoltCurr(double Target_Voltage, double Target_Current) = 0;virtual int PSU_Voltage_Measure(double *Voltage_Result, double *Current_Result) = 0;virtual int PSU_DeInitial() = 0;
};// Can't construct directly, use this, delete the pointer if no longer use
PSU_DLL_API CPSUInterface* PSUCreate(const char* pcPsuGpibAddr, PSU_TYPE ePsuType);#ifdef __cplusplus
}
#endif#endif // #ifndef PSU_DLL_H
2.导出接口。导出友元函数。定义基类继承接口类,存放一些通用的变量值,如地址,message等。具体类继承基类。
// PSU_DLL.cpp : Defines the exported functions for the DLL application.
//#include "stdafx.h"
#include "PSU_DLL.h"#include "AG663XX.h"
#include "AG3631A.h"
#include "GW1503D.h"CPSUInterface::CPSUInterface()
{
}CPSUInterface::~CPSUInterface(void)
{
}CPSUInterface* PSUCreate(const char* pcPsuGpibAddr, PSU_TYPE ePsuType)
{switch (ePsuType){case PSU_Agilent_663xx:return new CAG663XX(pcPsuGpibAddr, ePsuType);case PSU_Agilent_E3631A:return new CAG3631A(pcPsuGpibAddr, ePsuType);case PSU_GWINSTTEK_PPH1503D:return new GW1503D(pcPsuGpibAddr, ePsuType);default:return NULL;}
}
#include "stdafx.h"
#include "PSUBasic.h"CPSUBasic::CPSUBasic(const char* pcPsuGpibAddr,PSU_TYPE ePsuType )
{strcpy_s(m_cPsuGpibAddr, pcPsuGpibAddr);m_ePsuType = ePsuType;m_bPsuInitFlag = false;m_vsPsuHandle = NULL;memset(m_cErrorMSG, 0, sizeof(m_cErrorMSG));
}CPSUBasic::~CPSUBasic(void)
{
}void CPSUBasic::PSU_GetErrorMsg( char* pcErrorMSG, int iMsgSize)
{if (iMsgSize > 100)iMsgSize = 100;strcpy_s(pcErrorMSG, iMsgSize, m_cErrorMSG);
}
3.友元函数切换多态 返回句柄指针
4.句柄指针调用具体类
5.删除指针。