解决方案 »
- 谁能帮我解释这句代码?
- 进行网络编程时为什么要启动一个定时器?
- 关于静态树视图双击响应消息
- 紧急求助::如何从本地windows把文件传输到远端linux!!!!
- 如何取得神龙卡中的IBasicAudio接口!从而控制左右声道!在线等!做过的来
- 我想对一个内存文件进行CRC校验应该怎么做?
- 请问哪有VC的函数库的资料啊?
- 产生随机数和_variant_t的.dblVal变量转成字符串
- 数据库问题。请各位达人进来看看
- 谁能告诉我com和ActiveX 有什么区别和联系?
- 按钮消息不响应 设置全局变量程序崩溃!!高手进来帮帮小弟的啊!
- visual studio 2010 运行完程序之后怎么不关闭对话框
头文件里的函数是怎么定义的?
==============
在隐式调用DLL时,应用程序一般是先加载DLL模块的,在加载DLL出错的情况下会出现应用程序正常初始化失败的错误,所以不应该是调用DLL中函数出了问题,而是加载的过程出错了。
按照书上说是
“当所有的D L L模块都找到并且映射到进程的地址空间中之后,加载程序就会确定对输入的符号的全部引用。为此,它要再次查看每个模块的输入节。对于列出的每个符号,加载程序都要查看指定的D L L的输出节,以确定该符号是否存在。”
当该符号不存在时将发生我出现的这个错误,问题是如何解决这个错误....
#pragma once
#define DLLTESTAPI extern "C"__declspec(dllexport)
DLLTESTAPI int add(int a,int b);
cpp里面是这样的
#include "newdll.h"
int add(int a,int b)
{
return (a+b);
}
自己检查一下lib是否正常正确
#define DLLTESTAPI extern "C"__declspec(dllexport)
DLLTESTAPI int WINAPI add(int a,int b);
cpp里面是这样的
#include "newdll.h"
DLLTESTAPI int WINAPI add(int a,int b)
{
return (a+b);
}
1>e:\projects\newdll\newdll\newdll.h(8) : error C2146: syntax error : missing ';' before identifier 'add'
1>e:\projects\newdll\newdll\newdll.h(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\newdll.cpp(7) : error C2146: syntax error : missing ';' before identifier 'add'
1>.\newdll.cpp(8) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
额,调用时是直接像这样调用的吧 int x=add(1,2);
难道调用时有特殊的事情要做的?
#pragma once
#define DLLTESTAPI __declspec(dllexport)
DLLTESTAPI int add(int a,int b);
cpp里面是这样的
#include "newdll.h"
int __stdcall add(int a,int b)
{
return (a+b);
}然后在调用dll的地方
#pragma comment(lib,"newdll.lib")
__declspec(dllimport) int add(int,int);
再调用就行了。如果在生成导出库的时没加extern "C"话在导入时就不用加extern "C"了
newdll.h#pragma once
#define DLLTESTAPI extern "C" __declspec(dllexport)
DLLTESTAPI int add(int a,int b);
newdll.cpp#include "stdafx.h"
#include "newdll.h"
int add(int a,int b)
{
return a+b;
}
调用的CPP#include "stdafx.h"
#include "newdll.h"
#include "iostream"
#pragma comment(lib,"newdll.lib")
extern "C" __declspec(dllimport) int add(int,int);
using namespace std;int _tmain(int argc, _TCHAR* argv[])
{
int a;
cout<<add(1,2)<<endl;
cin>>a;
return 0;
}以上是照lostying的说法修改过的代码,调试仍然报相同的错
另外,build时报 warning C4273: 'add' : inconsistent dll linkage
所有代码
newdll.h#pragma once#if !defined(DLLTESTAPI)
#define DLLTESTAPI extern "C" __declspec(dllimport)
#endifDLLTESTAPI int add(int a,int b);
newdll.cpp#include "stdafx.h"
#define DLLTESTAPI extern "C" __declspec(dllexport)
#include "newdll.h"
int add(int a,int b)
{
return a+b;
}
调用的CPP#include "stdafx.h"
#include "newdll.h"
#include "iostream"
#pragma comment(lib,"newdll.lib")using namespace std;int WINAPI _tmain(int argc, _TCHAR* argv[])
{
int a;
cout < <add(1,2) < <endl;
cin>>a;
return 0;
}
无法定位程序输入点add于动态链接库 newdll.dll上
newdll.h
newdll.cpp 都改了
报错跟原来一样
#include "stdafx.h"
#include "newdll.h"
#include "iostream"
#pragma comment(lib,"newdll.lib")
using namespace std;int _tmain(int argc, _TCHAR* argv[])
{
int a;
cout<<add(1,2)<<endl;
cin>>a;
return 0;
}跟最初的错误一样,毫无变化
cin和cout本来是想直接看看add的运行结果的-_- 。。