Thread: Creating an object in a dll & sending it to an exe file

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    4

    Creating an object in a dll & sending it to an exe file

    Hi,
    Im currently creating an object that inherit a base class named BaseObject, in my DLL, and Im sending it to my application using a static function declared in my application.

    The problem is that the object looks corrupted when the exe receives it.

    Does anyone knows what could make this problem?

    Here is how my object looks like after I created it in my DLL: http://prookle.com/vbimghost.php?do=...yimg&imgid=127

    Then, I pass this object using a static function.

    When my application receives it, the object looks like that:
    http://prookle.com/vbimghost.php?do=...yimg&imgid=126

    As you can see, the data is not the same.

    Here is what the BaseObject class looks like:

    Code:
    #ifndef _BaseObject
    #define _BaseObject
    
    
    #ifndef API_DLL
    	#ifdef CORE_EXPORTS
    		#define API_DLL	//__declspec(dllimport)
    	#else
    		#define API_DLL	__declspec(dllexport)
    	#endif
    #endif
    
    
    
    #include <vector>
    #include <iostream>
    using namespace std;
    
    #include "APITypes.h"
    
    class API_DLL BaseObject;
    
    typedef void (BaseObject::*EnterExitFn)(uint32);
    typedef void (BaseObject::*StateFn)(float);
    
    struct State
    {
    	EnterExitFn mEnterFn;
    	StateFn		mStateFn;
    	EnterExitFn	mExitFn;
    	uint32		muiStateId;
    };
    
    struct Test
    {
    	uint32 uiId;
    	float fValue;
    };
    
    
    class API_DLL BaseObject
    {
    	public:
    		BaseObject(uint32 _uiObjectId);
    	
    		void SetStateFn(uint32 _uiStateId, StateFn _stateFn, 
    						EnterExitFn _stateEnter = NULL, 
    						EnterExitFn _stateExitFn = NULL,
    						bool _bStopSet = false);
    
    		void SetState(uint32 _uiState, bool _bStopSet = false);
    		void Update(float _fDelta);
    
    		uint32 GetObjectId();
    
    		//virtual:
    		virtual uint32 SendMessage(uint32 _uiMessageType, void* _messageData){return 0;};
    	private:
    		uint32 FindStateIndex(uint32 _uiStateId);
    	protected:
    		vector<State>	mvuiStates;
    
    		uint32			muiObjectId;
    		uint32			muiCurrentState;
    		uint32			muiLastFrameState;
    
    		bool			mbHasStates;
    		bool			mbCallEnterFn;
    		bool			mbCallExitFn;
    };
    
    #endif

    Here is how I pass the object from the DLL to the application:


    Code:
    
    Mouse* pMouse = new Mouse(6);
    APIGameObject::RegisterObject((BaseObject*) pMouse);


    The mouse class inherit the BaseObject class. The RegisterObject function is a static function declared in the application.

    When I break in the RegisterObject static function, the object in parameter looks like this: http://prookle.com/vbimghost.php?do=...yimg&imgid=126

    But, when I create the mouse object, it looks like this: http://prookle.com/vbimghost.php?do=...yimg&imgid=127

    Hope someone can help me out,
    Steve
    Last edited by SteveRodrigue; 12-14-2006 at 08:15 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    it seems to be a problem of function prototyping
    your dll thinks that function parameters are slightly different from the actual ones, so the pointer extracted from the stack is incorrect.

    How do you pass the function pointer to your dll?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Quote Originally Posted by vart
    it seems to be a problem of function prototyping
    your dll thinks that function parameters are slightly different from the actual ones, so the pointer extracted from the stack is incorrect.

    How do you pass the function pointer to your dll?
    You mean, the APIGameObject::RegisterObject function?

    I use the same .h for both the dll & the exe file.

    Here is the .h file of that APIGameObjct class:

    Code:
    #ifndef _APIGameObject
    #define _APIGameObject
    
    
    #ifndef API_DLL
    	#ifdef CORE_EXPORTS
    		#define API_DLL	//__declspec(dllimport)
    	#else
    		#define API_DLL	__declspec(dllexport)
    	#endif
    #endif
    
    #include "BaseObject.h"
    #include "Includes.h"
    
    #ifndef CORE_EXPORTS
    	#include "BaseResource.h"
    	#include "ObjectsManager.h"
    	#include "ResourceImageManager.h"
    #else
    	class BaseResource;
    	class ObjectsManager;
    	class ResourceImageManager;
    #endif
    
    #define GO_CURRENT 1
    class API_DLL APIGameObject
    {
    	public:
    		static void SetObjectsManager(ObjectsManager* _pObjectsManager);
    		static void SetResourceImageManager(ResourceImageManager* _pResourceImageManager);
    
    		//API:
    		static void RegisterObject(BaseObject* _pObject);
    		static void RegisterResource(BaseResource* _pResource);
    
    		static uint32 SendMessage(uint32 _uiObjId, uint32 _uiMessageType, void* _messageData);
    	protected:
    		static ObjectsManager* mObjectsManager; 
    		static ResourceImageManager* mResourceImageManager;
    };
    
    #endif
    Thanks a lot for your help,
    Steve
    Last edited by SteveRodrigue; 12-14-2006 at 10:41 AM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I don't understand how dll can be linked... there is no function body available at the moment when the dll is created
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Quote Originally Posted by vart
    I don't understand how dll can be linked... there is no function body available at the moment when the dll is created
    The functions are linked since I import the lib file of my application into my dll, in visual studio.

    Should I link them doiing something else? Sorry, I'm new to DLLs.

    Thanks a lot for your help,
    Steve

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I don't know this approach...
    I'm using call by callBack when I need access functions from the exe in the dlls
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    4
    Quote Originally Posted by vart
    I don't know this approach...
    I'm using call by callBack when I need access functions from the exe in the dlls
    I think I'll try it out with callback and see how it works. http://www.codeguru.com/cpp/cpp/cpp_...le.php/c10557/

    Thanks,
    Steve
    Last edited by SteveRodrigue; 12-14-2006 at 02:05 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  2. DLL Global Object Constructor String Error
    By n00b3 in forum Windows Programming
    Replies: 1
    Last Post: 06-29-2008, 07:42 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM