Jump to content
  • Advertisement
Sign in to follow this  
brwarner

Dll and GetProcAddress

This topic is 4351 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'm having trouble creating a DLL then getting a function out of it using GetProcAddress.
typedef int (*CREATEPUZZLE)(Puzzle*,int);

CreateNewPuzzle(Puzzle *p)
{
hDll = LoadLibrary("MyLib.dll");
CREATEPUZZLE _CreatePuzzle = 0;
_CreatePuzzle = (CREATEPUZZLE)GetProcAddress(hDll, "CreatePuzzle"); 
//!_CreatePuzzle = true
_CreatePuzzle(p,1); //Access Violation here
return 1;
}

That is the code that loads the funtion. Inside the DLL I have:
__declspec(dllexport) int CreatePuzzle(Puzzle *p, int i)
{
//Does stuff
}

The problem is the GetProcAddress cant find the function and I get an Access Violation when I try to use it.

Share this post


Link to post
Share on other sites
Advertisement
Don't forget to add a .DEF file to the project. Here is the format:

LIBRARY MyLib
EXPORTS
CreatePuzzle

Share this post


Link to post
Share on other sites
Thanks, that worked. I now though have a new problem :(
The function I call in the Dll is sent a pointer to an abstract class named Puzzle. The function in the Dll assigns that pointer to a class that derives from it, however when I try to run a function from the abstract class in the application I get an access violation:


//In DLL
extern "C"
{
__declspec(dllexport) int CreatePuzzle(Puzzle* p, int i)
{
p = new LogicPuzzle();
//LogicPuzzle contains definition for all virtual members of Puzzle
return 1;
}
}
..
//In app
hDll = LoadLibrary("MyDll.dll");
_CreatePuzzle = (CREATEPUZZLE)GetProcAddress(hDll, "CreatePuzzle); //Works
_CreatePuzzle(p, 1); //Works
p->Func(); //Access Violation



EDIT: I used __declspec, no point in using DEF files no more

Share this post


Link to post
Share on other sites
That's a pretty common mistake: Realize that you want to write to a pointer through the CreatePuzzle function. So, you don't need to pass a pointer to Puzzle, you need to pass a pointer to a pointer. The code would be:


//In DLL
extern "C"
{
__declspec(dllexport) int CreatePuzzle(Puzzle** p, int i)
{
*p = new LogicPuzzle();
//LogicPuzzle contains definition for all virtual members of Puzzle
return 1;
}
}
..
//In app
hDll = LoadLibrary("MyDll.dll");
_CreatePuzzle = (CREATEPUZZLE)GetProcAddress(hDll, "CreatePuzzle); //Works
_CreatePuzzle(&p, 1); //Works
p->Func();



Or better yet just use a reference:


//In DLL
extern "C"
{
__declspec(dllexport) int CreatePuzzle(Puzzle* &p, int i)
{
p = new LogicPuzzle();
//LogicPuzzle contains definition for all virtual members of Puzzle
return 1;
}
}
..
//In app
hDll = LoadLibrary("MyDll.dll");
_CreatePuzzle = (CREATEPUZZLE)GetProcAddress(hDll, "CreatePuzzle); //Works
_CreatePuzzle(p, 1); //Works
p->Func();



Share this post


Link to post
Share on other sites
Quote:
Original post by brwarner
Thanks, that worked. I now though have a new problem :(
The function I call in the Dll is sent a pointer to an abstract class named Puzzle. The function in the Dll assigns that pointer to a class that derives from it, however when I try to run a function from the abstract class in the application I get an access violation:

*** Source Snippet Removed ***

EDIT: I used __declspec, no point in using DEF files no more




I didn't know they could be mutually exclusive, thank you! (Die .DEF Die)

Share this post


Link to post
Share on other sites
Is there a way to make a DLL part of the project, and then you can just make the calls without all of that stuff?

Share this post


Link to post
Share on other sites
Yes, by linking against the import library of the DLL and including the correct header files (provided you built the DLL header files correctly with API #defines).

Share this post


Link to post
Share on other sites
I didnt make the DLL, so I suppose I will need to do it this way.

I made a header file for the DLL multisync, it was made for DarkBasicPro.

multisync.h

HINSTANCE hNetDLL = LoadLibrary("Network.dll");
void *NetJoin = GetProcAddress(hNetDLL,"netjoin");



then I try to call NetJoin("127.0.0.1"); but it says

--------------------Configuration: InsaneCombat - Win32 Debug--------------------
Compiling...
main.cpp
c:\documents and settings\owner\desktop\insane game engine\igenetworking.h(36) : error C2064: term does not evaluate to a function
Error executing cl.exe.

InsaneCombat.exe - 1 error(s), 0 warning(s)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!