Sign in to follow this  

run time error while using dll in dev-cpp

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I have created one simple dll which contain a function which return the sum of two integer values. Now i want to call this function that is residing in the dll file. So i followed the intructions given on http://www.gamedev.net/community/forums/topic.asp?topic_id=375613 the code compiles sucessfully but when trying to run the exe it throws runtime error with a dialog box "Exe has encountered a error and need to close" What can be the reason for this. If any other input required from my side let me know that Kindly help Thanks in advance

Share this post


Link to post
Share on other sites
You did something wrong. The usual error for a missing DLL would be "The application configuration is not correct," so it sounds like your application is finding the DLL. You probably just have a runtime error (most likely a pointer-related screwup) in your code. You'll have to post your code for more help.

Share this post


Link to post
Share on other sites
My code is as follows :-
DLLMain.cpp

/* Replace "dll.h" with the name of your header */
//#include "dll.h"
#include <windows.h>

BOOL APIENTRY DllMain (HINSTANCE hInst /* Library instance handle. */ ,
DWORD reason /* Reason this function is being called. */ ,
LPVOID reserved /* Not used. */ )
{
switch (reason)
{
case DLL_PROCESS_ATTACH:
break;

case DLL_PROCESS_DETACH:
break;

case DLL_THREAD_ATTACH:
break;

case DLL_THREAD_DETACH:
break;
}

return TRUE;
}

int sum(int a , int b)
{
int add;
add=a+b;
return add;
}

dll.h
#ifndef _DLL_H_
#define _DLL_H_

#if BUILDING_DLL
# define DLLIMPORT __declspec (dllexport) int sum(int, int);
#else
# define DLLIMPORT __declspec (dllimport)
#endif /* Not BUILDING_DLL */

#ifndef R1_HPP

#define R1_HPP

#endif

This is the part which creates the dll file it creates .dll, the library file and the .def file.

Now i have created a new project which call this dll file. For this what i did is added the generated .a file(library file)from the Linker option and added it's path to the library directories. Following is my code.

#include <windows.h>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
HMODULE myDll;
myDll = LoadLibrary(".\\libdlltesting.a");
typedef int (*sum)(int,int);
sum myFuncHolder;
myFuncHolder = (sum)GetProcAddress(myDll,"sum");
int j;
j = myFuncHolder(5,6);
FreeLibrary(myDll);
cout<<"value of j is " <<j;
system("PAUSE");
return EXIT_SUCCESS;
}

Now the code compile well but when trying to run the exe it throws me the a runtime error.
Kindly help

Share this post


Link to post
Share on other sites
When i'm printing the value of myFuncHolder it is giving value as 0. What need to be done . Also let me know if we need to specify .dll or .def file in any of the project options.

Share this post


Link to post
Share on other sites
i used to have that problem

make sure to use extern "C" to prevent c++ name mangling so getprocaddress can find it
it should be FuncName in a dll export viewer and not *random data*FuncName@@#*random data*

Share this post


Link to post
Share on other sites
Use a tool like "Dependency Viewer" to look at the DLL. It should list all exported symbols.

There's probably some name mangling going on (i suspect DevC does this as well). Try to wrap the export with

extern "C"
{

...dllexport...

}

Share this post


Link to post
Share on other sites

This topic is 3664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this