DLL导出类对象
我建立了一个简单DLL工程,并将示例代码注释掉,自己写了个类,并导出其对象。问题是我在测试程序中调用它,得到对象后,使用该对象调用该对象的方法时,编译通过连接通不过,说找...
我建立了一个简单DLL工程,并将示例代码注释掉,自己写了个类,并导出其对象。问题是我在测试程序中调用它,得到对象后,使用该对象调用该对象的方法时,编译通过连接通不过,说找不到函数?
这是.h文件:********************************************
#ifndef _YCT_DATAMAKER_H
#define _YCT_DATAMAKER_H
#ifdef YCT_DATAMAKER_EXPORTS
#define YCT_DATAMAKER_API __declspec(dllexport)
#else
#define YCT_DATAMAKER_API __declspec(dllimport)
#endif
////////////////////////////////////////////////////////////////////
class CYCTDataMaker
{
public:
CYCTDataMaker(){};
bool ONTS_Request(unsigned char ucSamSlot, unsigned long ulCmdLength, unsigned char *pCmdData);
};
////////////////////////////////////////////////////////////////////
extern "C"
{
YCT_DATAMAKER_API CYCTDataMaker* _cdecl CreateYCTDataMaker();
YCT_DATAMAKER_API void DeleteYCTDataMaker();
}
#endif // #ifndef _YCT_DATAMAKER_H
这是.cpp文件:*************************************************
#include "stdafx.h"
#include "YCT_DataMaker.h"
BOOL APIENTRY DllMain( HANDLE 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;
}
bool CYCTDataMaker::ONTS_Request(unsigned char ucSamSlot, unsigned long ulCmdLength, unsigned char *pCmdData)
{
if (ucSamSlot != 0x01 || ucSamSlot != 0x02)
{
return false;
}
if (pCmdData == NULL)
{
return false;
}
ulCmdLength = 5;
pCmdData[0] = 0xBA;
pCmdData[1] = 0x03;
pCmdData[2] = 0x81;
pCmdData[3] = ucSamSlot;
pCmdData[4] = pCmdData[0] ^ pCmdData[1] ^ pCmdData[2] ^ pCmdData[3];
return true;
}
////////////////////////////////////////////////////////////////////
CYCTDataMaker *pDM = NULL;
CYCTDataMaker* CreateYCTDataMaker()
{
pDM = new CYCTDataMaker;
return pDM;
}
void DeleteYCTDataMaker()
{
if (pDM != NULL)
{
delete pDM;
pDM = NULL;
}
}
这是测试程序连接时报的错:
DataMaker_TesterDlg.obj : error LNK2001: unresolved external symbol "public: bool __thiscall CYCTDataMaker::ONTS_Request(unsigned char,unsigned long,unsigned char *)" (?ONTS_Request@CYCTDataMaker@@QAE_NEKPAE@Z)
..\..\bin\DataMaker_Tester.exe : fatal error LNK1120: 1 unresolved externals
为什么会找不到对象的方法呢? 展开
这是.h文件:********************************************
#ifndef _YCT_DATAMAKER_H
#define _YCT_DATAMAKER_H
#ifdef YCT_DATAMAKER_EXPORTS
#define YCT_DATAMAKER_API __declspec(dllexport)
#else
#define YCT_DATAMAKER_API __declspec(dllimport)
#endif
////////////////////////////////////////////////////////////////////
class CYCTDataMaker
{
public:
CYCTDataMaker(){};
bool ONTS_Request(unsigned char ucSamSlot, unsigned long ulCmdLength, unsigned char *pCmdData);
};
////////////////////////////////////////////////////////////////////
extern "C"
{
YCT_DATAMAKER_API CYCTDataMaker* _cdecl CreateYCTDataMaker();
YCT_DATAMAKER_API void DeleteYCTDataMaker();
}
#endif // #ifndef _YCT_DATAMAKER_H
这是.cpp文件:*************************************************
#include "stdafx.h"
#include "YCT_DataMaker.h"
BOOL APIENTRY DllMain( HANDLE 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;
}
bool CYCTDataMaker::ONTS_Request(unsigned char ucSamSlot, unsigned long ulCmdLength, unsigned char *pCmdData)
{
if (ucSamSlot != 0x01 || ucSamSlot != 0x02)
{
return false;
}
if (pCmdData == NULL)
{
return false;
}
ulCmdLength = 5;
pCmdData[0] = 0xBA;
pCmdData[1] = 0x03;
pCmdData[2] = 0x81;
pCmdData[3] = ucSamSlot;
pCmdData[4] = pCmdData[0] ^ pCmdData[1] ^ pCmdData[2] ^ pCmdData[3];
return true;
}
////////////////////////////////////////////////////////////////////
CYCTDataMaker *pDM = NULL;
CYCTDataMaker* CreateYCTDataMaker()
{
pDM = new CYCTDataMaker;
return pDM;
}
void DeleteYCTDataMaker()
{
if (pDM != NULL)
{
delete pDM;
pDM = NULL;
}
}
这是测试程序连接时报的错:
DataMaker_TesterDlg.obj : error LNK2001: unresolved external symbol "public: bool __thiscall CYCTDataMaker::ONTS_Request(unsigned char,unsigned long,unsigned char *)" (?ONTS_Request@CYCTDataMaker@@QAE_NEKPAE@Z)
..\..\bin\DataMaker_Tester.exe : fatal error LNK1120: 1 unresolved externals
为什么会找不到对象的方法呢? 展开
1个回答
展开全部
这个是因为对象导出的时候C++对类和函数重新命名的关系。
编译DLL的时候试图使用.def模块定义文件来规范导出的类和函数的名称。
编译DLL的时候试图使用.def模块定义文件来规范导出的类和函数的名称。
参考资料: www.javaeye.com/wiki/topic/441300
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询