Sign in to follow this  
SteveRodrigue

Passing object to a DLL

Recommended Posts

Hi, I want to pass an object to a DLL. I'm using the same .h file for both the dll and the exe, using __declspec(dllexport) or __declspec(dllimport). The problem is that when I pass the object from the exe to the dll, then I call a function of the object in the dll, my compiler tells me that the function has no body, since my dll only has the .h, and not the .cpp. How can I fix that? ANyone knows? Thanks, Steve

Share this post


Link to post
Share on other sites
Here is an exameple of the class I am using:


#ifndef _APIGame
#define _APIGame

#ifndef API_DLL
#ifdef CORE_EXPORTS
#define API_DLL __declspec(dllexport)
#else
#define API_DLL __declspec(dllimport)
#endif
#endif

class API_DLL APIGame
{
public:
void SetBuffer2D(Buffer2D* _pBuffer);
Image* LoadImage(uint32 _uiResourceId, char* _filePath);
protected:
Buffer2D* mpBuffer2D;
};



#endif



When I call the function LoadImage, from the DLL, using the object APIGame, it tells me that the function LoadImage has no body.

How can I fix this problem?

Thanks,
Steve

Share this post


Link to post
Share on other sites
It's not obvious what's wrong from your description, it could be one of a number of things. Perhaps if you could post some code to show what you're doing we might be able to deduce what's wrong.

EDIT: I see you've posted some code. The code looks fine to me. Your problem must lie elsewhere. Is CORE_EXPORTS correctly defined. Are you linking the DLL with the export library of the module that contains the definition of the class? I think the latter is the problem. If it is, it won't be linking the class into the DLL, only the code to fix up all the symbol references when the DLL is loaded.

Skizz

Share this post


Link to post
Share on other sites
Quote:
Original post by Skizz
It's not obvious what's wrong from your description, it could be one of a number of things. Perhaps if you could post some code to show what you're doing we might be able to deduce what's wrong.

Skizz



I load the library Core.dll in the exe, then load a function in it, and call it. I'm also sending out a pointer of type APIGame to my dll, using the called function.


typedef void (*CreateObjFn) (uint32 _uiObjectId, BaseObject* _pObj);

HMODULE hMod;
hMod = LoadLibrary("Core.dll");

if (hMod == NULL)
{
printf("Error linking. Error code was: %d\n", GetLastError());
return;
}

//we call the init function:
CreateObjFn pInit = (CreateObjFn) GetProcAddress(hMod, "CreateObjMouse");

if (!pInit)
{
printf("Error calling function in InitDLL, in class PluginManager");
FreeLibrary(hMod);
return;
}

//we call the function:
APIGame* pGame = new APIGame();
pInit(_gpAPI)(5, (BaseObject*) pGame);



In the dll, I receive the APIGame pointer, and call the function LoadImage of it. The problem is that when I call the function LoadImage of the pointer, my compiler tells me that the function has no body.

The pointer looks good tought. When I call no function of it, the pojnter received is not null. The only problem is that I cannot call any function of the pointer without having compiler errors.



#ifndef API_DLL
#ifdef CORE_EXPORTS
#define API_DLL __declspec(dllexport)
#else
#define API_DLL __declspec(dllimport)
#endif
#endif

extern "C"
{
API_DLL void CreateObjMouse(uint32 _uiObjId, APIGame* _pGame);
};

extern "C" void CreateObjMouse(uint32 _uiObjId, APIGame* _pGame)
{
pGame->LoadImage(7, "media/test.png");
}



The class definition (.h) of the class APIGame is posted in my above post.

Thanks all for your help,
Steve

Share this post


Link to post
Share on other sites
Quote:

The pointer looks good tought. When I call no function of it, the pointer received is not null. The only problem is that I cannot call any function of the pointer without having compiler errors.

I really think that this is a linker error, you're not linking your "Core.dll" to "app.lib" where "app" is the name of the executable or whatever module that contains the implementation of the APIGame class.

Skizz

Share this post


Link to post
Share on other sites
Quote:
Original post by Skizz
Quote:

The pointer looks good tought. When I call no function of it, the pointer received is not null. The only problem is that I cannot call any function of the pointer without having compiler errors.

I really think that this is a linker error, you're not linking your "Core.dll" to "app.lib" where "app" is the name of the executable or whatever module that contains the implementation of the APIGame class.

Skizz


Hmm, I'll try this out and will let you know when I'll get back home.

Thanks a lot for your help,
Steve

Share this post


Link to post
Share on other sites
Add "app.lib" or whatever it's called to the list of additional dependancies used by the linker when building "core.dll". If you're using DevStudio, add "app.lib" to "Project->Properties (Alt-F7)->Linker->Input->Additional Dependancies".

Skizz

Share this post


Link to post
Share on other sites
And you might want to setup a project dependancy to ensure the app.lib is built before linking core.dll, that's "Project->Dependancies"

Skizz

Share this post


Link to post
Share on other sites
Quote:
Original post by Skizz
And you might want to setup a project dependancy to ensure the app.lib is built before linking core.dll, that's "Project->Dependancies"

Skizz


Thanks a lot, it solved my problem!

Steve

Share this post


Link to post
Share on other sites

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