dll导出函数名称和系统API名字相同冲突问题

来源:互联网 发布:叮咚智能音箱软件 编辑:程序博客网 时间:2024/06/06 19:32

参考文章:http://blog.163.com/zhangjinqing1234@126/blog/static/307302602012111295026614/

-------------------------------------------------------------------------------------------------------------------------------------------------

事情是这样的,在实际开发中,dll导出的函数名称是由客户定义的,不幸的是客户定义 的导出函数名称

和系统API的名称是一样的,这样的话API名称就产生了冲突,导致调用失败,以下就是我测试的例子和解决方法。

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

头文件TestDll.h, API原型定义如下:

#pragma once// 操作系统有个API:DWORD GetVersion(VOID);void GetVersion(char *pszBuf, int *pBufLen); // 客户定义的API

cpp文件TestDll.cpp,API实现代码如下:

#include "stdafx.h"#include "TestDll.h"void GetVersion(char *pszBuf, int *pBufLen){    if (pszBuf != NULL && pBufLen != NULL && *pBufLen >= 10)    {        strcpy(pszBuf, "1.0.0.1");    }}

dll 导出模块定义文件TestDll.def内容如下:

LIBRARY "TestDll"
EXPORTS
GetVersion

测试程序代码如下:

#include "stdafx.h"#include "../TestDll/TestDll.h"#ifdef _DEBUG#pragma comment(lib, "../Debug/TestDll.lib")#else#pragma comment(lib, "../Release/TestDll.lib")#endifint _tmain(int argc, _TCHAR* argv[]){    char szVersion[16] = {0};    int iBufLen = 16;    GetVersion(szVersion, &iBufLen);    printf("%s ", szVersion);    getchar();return 0;}

编译运行,产生错误提示信息如下:

1>  TestDemo.cpp
1>TestDemo.obj : error LNK2019: unresolved external symbol "void __cdecl GetVersion(char *,int *)" (?GetVersion@@YAXPADPAH@Z) referenced in function _wmain

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

解决方法:

TestDll.h头文件内容修改如下:

#pragma once// 操作系统有个API:DWORD GetVersion(VOID);void GetVersion(char *pszBuf, int *pBufLen); // 客户定义的APIvoid MyGetVersion(char *pszBuf, int *pBufLen);  

TestDll.cpp内容修改如下:

#include "stdafx.h"#include "TestDll.h"void GetVersion(char *pszBuf, int *pBufLen){    // 实现在MyGetVersion中}void MyGetVersion(char *pszBuf, int *pBufLen){    if (pszBuf != NULL && pBufLen != NULL && *pBufLen >= 10)    {        strcpy(pszBuf, "1.0.0.1");    }}

TestDll.def内存修改如下:

LIBRARY "TestDll"
EXPORTS
GetVersion=MyGetVersion

最后编译运行,效果如下:













0 0
原创粉丝点击