从DLL VC ++显式调用函数 [英] Explicit calling of function from dll vc++
问题描述
大家好
我在开始执行并调用DLL中存在的函数的主文件中具有以下代码.
Hello everyone
I have the following code in main file from where i start execution and call the function present in DLL.
#include "stdafx.h"
#include "windows.h"
#include "conio.h"
#include "windef.h"
int _tmain(int argc, _TCHAR* argv[])
{
HINSTANCE instcall=LoadLibrary(TEXT("c:\\test2.dll"));
if(instcall==NULL)
printf("Failed to load dll");
FARPROC getproc=GetProcAddress(instcall,"funcall");
if(getproc==NULL)
printf("failed entry point");
typedef int(__stdcall * picFunc)();
picFunc funcall;
funcall=picFunc(getproc);
funcall();
getch();
//LPTSTR path=(LPTSTR)"c:\test2.dll";
/*system("notepad.exe");
DWORD threadId=dwThredId*/
/*typedef void *(_stdcall *creatfn)();
creatfn myfunc=(creatfn) GetProcAddress(instcall,"call");
void *objptr=myfunc();*/
/*picFunc funcall;*/
//=picFunc(getproc);
//funcall();
///call=mycall(getproc);
}
dll的代码在这里:
The code of dll is here:
// test2.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "stdio.h"
#include "conio.h"
void funcall()
{
printf("hello");
}
当我执行要获取的代码时,test1.exe中0x00000000的未处理异常:0xC0000005:访问冲突读取位置0x00000000.并且输出将是失败的入口点....我正在使用Visual Studio 2008进行执行.
任何人都可以帮助我解决这个问题,我是系统编程的新手.同时也请推荐一本学习系统编程的好书.我有基本的C ++知识.
谢谢大家
When i execute the code i am getting, Unhandled exception at 0x00000000 in test1.exe: 0xC0000005: Access violation reading location 0x00000000. And the output will be failed entry point.... I am using Visual studio 2008 for execution.
CAn anyone please help me in sorting out this problem i am newbie to system programming. And also please suggest good book for learning system programming. I have the knowledge of basic C++.
THank you all
推荐答案
GetProcAddress
可能会返回NULL.
您应该将funcall标记为要导出:
http://msdn.microsoft.com/en-us/library/3y1sfaz2 (v = vs.71).aspx [
GetProcAddress
proporbly returns NULL.
you should mark funcall for export:
http://msdn.microsoft.com/en-us/library/3y1sfaz2(v=vs.71).aspx[^]
您的代码有缺陷;如果您的任何函数调用失败,您将打印一条错误消息,然后像执行成功一样继续执行.遇到失败情况时,您需要脱离main()
.
Your code is flawed; if any of your function calls fail you print an error message, but then continue execution as if they had succeeded. You need to break out of main()
when you get a fail condition.
除了Richard和Simon说的话:
有多种方法可以将功能标记为已导出.由于诸如名称修改之类的事情,您不应该假定导出的名称是"funcall",请参见 http ://en.wikipedia.org/wiki/Name_mangling [ ^ ].
您可以使用"extern C {...}
",但也可以使用某些二进制转储工具(例如dumpbin.exe)检查实际导出的内容.您可以从Visual Studio命令提示符运行它.请参阅 http://msdn.microsoft.com/en-us/library/c1h23y6c(v = vs.71).aspx [
In addition to what Richard and Simon say:
There is more than one way to mark a function as exported. You should not assume that the exported name is "funcall", because of such thing as name mangling, see http://en.wikipedia.org/wiki/Name_mangling[^].
You can use "extern C {...}
", but also you can check what actually was exported using some binary dump tool such as dumpbin.exe. You can run it from Visual Studio Command Prompt. See http://msdn.microsoft.com/en-us/library/c1h23y6c(v=vs.71).aspx[^].
—SA
这篇关于从DLL VC ++显式调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!