Click here to Skip to main content
15,909,498 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone

I have the following code in main file from where i start execution and call the function present in DLL.

C++
#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);
}


The code of dll is here:

C++
 // test2.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "stdio.h"
#include "conio.h"

 void funcall()
{
	printf("hello");
}


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
Posted
Updated 12-Aug-11 16:29pm
v3

GetProcAddress proporbly returns NULL.
you should mark funcall for export:
http://msdn.microsoft.com/en-us/library/3y1sfaz2(v=vs.71).aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Aug-11 22:09pm    
My 5. See also my solution which adds some useful advice to is about name mangling, "extern C" and dumpbin.
--SA
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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Aug-11 22:03pm    
Correct, my 5. OP think that .def file provides a solution, but actually this is not yet a solution.
--SA
Sergey Alexandrovich Kryukov 13-Aug-11 22:09pm    
See also my solution which adds some useful advice to is about name mangling, "extern C" and dumpbin.
--SA
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
 
Share this answer
 
Comments
Richard MacCutchan 14-Aug-11 4:12am    
+5, good links.
Sergey Alexandrovich Kryukov 14-Aug-11 14:56pm    
Thank you, Richard.
--SA
Yes i found the solution. The exception is because there was no .def file associated with dll. Creating def file with Library and exports overcomes this problem......
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900