Trying to dynamically load a class at rutime from DLL


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Trying to dynamically load a class at rutime from DLL

  1. #1
    Join Date
    Apr 2009
    Posts
    13

    Smile Trying to dynamically load a class at rutime from DLL

    Hi,

    I am new to coding with macros. I have this code and I am trying to run this by dynamically loading a class from a DLL.
    Code:
    #pragma once
    #ifndef DLLEXPORTS_H
    #define DLLEXPORTS_H
    #ifdef _LoadPCDigDlls_h_
    #define _LoadPCDigDlls_h_
    #define CALLBACK __stdcall
    #define IFC_IMP_DLL  __declspec(dllimport)
    #endif //_LoadPCDigDlls_h_
    
    #define IFCAPI __stdcall
    #define IFCCALL IFC_IMP_DLL IFCAPI
    
    typedef enum {
    	IFC_CAP_MBOARD,		// Image Capture motherboard, e.g., IC-PCI,PCVision
    	IFC_MNG_MBOARD,		// Image Manager motherboard, e.g., IM-PCI
    	IFC_CMC_MBOARD,		// Computational module motherboard, e.g., CMC-PCI
    	IFC_AM_DBOARD,
    	IFC_CM_DBOARD,
    	IFC_DM_DBOARD,
    	IFC_NUM_BOARD_KINDS
    } IFC_BOARD_KIND;
    
    #define IFC_ACTIVE_CAMERA_FLAG 0x8000
    typedef unsigned char    * pBYTE;  
    #define IFC_INFINITE_FRAMES -1
    typedef void *HIFCGRAB;
    
    
    class IFC_IMP_DLL CIobject
    {
    public:
    
    // Object model (types, destruction, allocation)
    	virtual ~CIobject();  // virtual destructors are necessary
    
    #ifdef new
    #define _IFC_SAVE_NEW_
    #undef new
    #endif
    	void* IFCAPI operator new(size_t nSize);
    	void* IFCAPI operator new(size_t nSize,void* p);
    	void  IFCAPI operator delete(void* p);
    #if _MSC_VER >= 1200
    	void IFCAPI operator delete(void* p, void* pPlace);
    #endif
    
    	// for file name/line number tracking using DEBUG_NEW
    	void* IFCAPI operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
    
    #if _MSC_VER >= 1200
    	void IFCAPI operator delete(void* p, LPCSTR lpszFileName, int nLine);
    #endif
    
    #if defined(_IFC_SAVE_NEW_) && defined(_DEBUG) && defined(DEBUG_NEW)
    #define new DEBUG_NEW
    #endif
    #ifdef _IFC_SAVE_NEW_
    #undef _IFC_SAVE_NEW_
    #endif
    
    
    	// Disable the copy constructor and assignment by default so you will get
    	//   compiler errors instead of unexpected behaviour if you pass objects
    	//   by value or assign objects.
    protected:
    	CIobject();
    private:
    	CIobject(const CIobject& objectSrc);              // no implementation
    	void operator=(const CIobject& objectSrc);       // no implementation
    };
    
    class IFC_IMP_DLL CModule : public CIobject 
    {
    public:
    		CModule();
    	virtual ~CModule();
    };
    
    class IFC_IMP_DLL CParentMod : public CModule  
    {
    public:
    	CParentMod();
    	virtual ~CParentMod();
    };
    
    
    typedef CParentMod * PCParentMod;
    
    typedef PCParentMod  (CALLBACK* LPIfxCreateParentModule)(IFC_BOARD_KIND modKind, DWORD modSubkind, DWORD seqnum, char *cfgFile=NULL, BOOL inspectOnly=FALSE);
    typedef PCParentMod  (CALLBACK* LPIfxCreateCaptureModule)(char *modName,DWORD seqnum, char *cfgFile);
    typedef void (IFC_IMP_DLL* LPProcessCameraFilesInDir)(const pSTRING szPath, BOOL bRecurse = FALSE);
    typedef BOOL (IFC_IMP_DLL* LPEnableJitGrabMemLock)(void);
    typedef CICamera (IFC_IMP_DLL* LPGetCam)(DWORD camPortNum=IFC_ACTIVE_CAMERA_FLAG);
    typedef HIFCGRAB (IFC_IMP_DLL* LPGrab)(DWORD dwOptions,pBYTE dest, int ringNumFrames,int totalNumFrames=IFC_INFINITE_FRAMES, int x=0, int y=0, int dx=0, int dy=0);
    typedef BOOL (IFC_IMP_DLL* LPFreeze)(CIFrame *frame);
    typedef int (IFC_IMP_DLL* LPGrabWaitFrameEx)(HIFCGRAB grabID, BYTE **frameAddr=NULL, int FrameSeqNum=IFC_WAIT_NEWER_FRAME, DWORD milliseconds=IFC_WAIT_FOREVER, BOOL lock=TRUE,DWORD *acquiredDy=NULL, BOOL *VirtualFrameEnd=NULL, GRAB_EXT_ATTR *ExtendedAttr=NULL);
    typedef void (IFC_IMP_DLL* LPGrabRelease)(HIFCGRAB grabID, int seqNum);
    typedef void (IFC_IMP_DLL* LPGetAttr)(LPCAM_ATTR lpAttr, BOOL zoomCorrected=TRUE);
    
    extern LPIfxCreateParentModule IfxCreateParentModule;
    extern LPIfxCreateCaptureModule IfxCreateCaptureModule;
    extern LPProcessCameraFilesInDir ProcessCameraFilesInDir;
    extern LPEnableJitGrabMemLock EnableJitGrabMemLock;
    extern LPGetCam GetCam;
    extern LPGrab Grab;
    extern LPFreeze Freeze;
    extern LPGrabWaitFrameEx GrabWaitFrameEx;
    extern LPGrabRelease GrabRelease;
    extern LPGetAttr GetAttr;
    
    extern int LoadPCDigDlls();
    extern int FreePCDigDlls();

    When I build the solution I am getting these errors

    Code:
    1>------ Build started: Project: sprayQC, Configuration: Debug Win32 ------
    1>Compiling...
    1>LoadPCDigDlls.cpp
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(92) : error C2383: 'LPIfxCreateParentModule' : default-arguments are not allowed on this symbol
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(94) : error C2059: syntax error : '__declspec(dllimport)'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(95) : error C2059: syntax error : '__declspec(dllimport)'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(96) : warning C4042: 'LPGetCam' : has bad storage class
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(96) : error C2091: function returns function
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(96) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(97) : error C2059: syntax error : '__declspec(dllimport)'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(98) : error C2059: syntax error : '__declspec(dllimport)'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(99) : error C2059: syntax error : '__declspec(dllimport)'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(100) : error C2059: syntax error : '__declspec(dllimport)'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(101) : error C2059: syntax error : '__declspec(dllimport)'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(105) : error C2146: syntax error : missing ';' before identifier 'ProcessCameraFilesInDir'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(105) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(105) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(106) : error C2146: syntax error : missing ';' before identifier 'EnableJitGrabMemLock'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(106) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(106) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(107) : error C2146: syntax error : missing ';' before identifier 'GetCam'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(107) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(107) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(108) : error C2146: syntax error : missing ';' before identifier 'Grab'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(108) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(108) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(109) : error C2146: syntax error : missing ';' before identifier 'Freeze'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(109) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(109) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(110) : error C2146: syntax error : missing ';' before identifier 'GrabWaitFrameEx'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(110) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(110) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(111) : error C2146: syntax error : missing ';' before identifier 'GrabRelease'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(111) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(111) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(112) : error C2146: syntax error : missing ';' before identifier 'GetAttr'
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(112) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(112) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\projects\sprayqc2601\loadpcdigdlls.h(116) : fatal error C1070: mismatched #if/#endif pair in file 'c:\projects\sprayqc2601\loadpcdigdlls.h'
    1>Build log was saved at "file://c:\Projects\sprayQC2601\Debug\BuildLog.htm"
    1>sprayQC - 35 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    Somebody please help me.

    Thanks in advance.
    Edit/Delete Message

  2. #2
    Join Date
    Dec 2007
    Posts
    401
    this header is an absolute mess. it should not compile on any conforming compiler. the major errors are:

    1. default values for parameters are not allowed in a typedef (or in a pointer to a function)
    2. you can import variables and functions from a dll, but not a typedef (a typedef is required at compile-time.)
    3. use of types that have not been declared

    with these corrected, the header would compile. but you must be a very brave programmer if you decide to use it (trust the idiot who wrote this header).
    Code:
    #pragma once
    #ifndef DLLEXPORTS_H
    #define DLLEXPORTS_H
    #ifdef _LoadPCDigDlls_h_
    #define _LoadPCDigDlls_h_
    #define CALLBACK __stdcall
    #define IFC_IMP_DLL  __declspec(dllimport)
    #endif //_LoadPCDigDlls_h_
    
    #define IFCAPI __stdcall
    #define IFCCALL IFC_IMP_DLL IFCAPI
    
    typedef enum {
    	IFC_CAP_MBOARD,		// Image Capture motherboard, e.g., IC-PCI,PCVision
    	IFC_MNG_MBOARD,		// Image Manager motherboard, e.g., IM-PCI
    	IFC_CMC_MBOARD,		// Computational module motherboard, e.g., CMC-PCI
    	IFC_AM_DBOARD,
    	IFC_CM_DBOARD,
    	IFC_DM_DBOARD,
    	IFC_NUM_BOARD_KINDS
    } IFC_BOARD_KIND;
    
    #define IFC_ACTIVE_CAMERA_FLAG 0x8000
    typedef unsigned char    * pBYTE;  
    #define IFC_INFINITE_FRAMES -1
    typedef void *HIFCGRAB;
    
    
    class IFC_IMP_DLL CIobject
    {
    public:
    
    // Object model (types, destruction, allocation)
    	virtual ~CIobject();  // virtual destructors are necessary
    
    #ifdef new
    #define _IFC_SAVE_NEW_
    #undef new
    #endif
    	void* IFCAPI operator new(size_t nSize);
    	void* IFCAPI operator new(size_t nSize,void* p);
    	void  IFCAPI operator delete(void* p);
    #if _MSC_VER >= 1200
    	void IFCAPI operator delete(void* p, void* pPlace);
    #endif
    
    	// for file name/line number tracking using DEBUG_NEW
    	void* IFCAPI operator new(size_t nSize, LPCSTR lpszFileName, int nLine);
    
    #if _MSC_VER >= 1200
    	void IFCAPI operator delete(void* p, LPCSTR lpszFileName, int nLine);
    #endif
    
    #if defined(_IFC_SAVE_NEW_) && defined(_DEBUG) && defined(DEBUG_NEW)
    #define new DEBUG_NEW
    #endif
    #ifdef _IFC_SAVE_NEW_
    #undef _IFC_SAVE_NEW_
    #endif
    
    
    	// Disable the copy constructor and assignment by default so you will get
    	//   compiler errors instead of unexpected behaviour if you pass objects
    	//   by value or assign objects.
    protected:
    	CIobject();
    private:
    	CIobject(const CIobject& objectSrc);              // no implementation
    	void operator=(const CIobject& objectSrc);       // no implementation
    };
    
    class IFC_IMP_DLL CModule : public CIobject 
    {
    public:
    		CModule();
    	virtual ~CModule();
    };
    
    class IFC_IMP_DLL CParentMod : public CModule  
    {
    public:
    	CParentMod();
    	virtual ~CParentMod();
    };
    
    
    typedef CParentMod * PCParentMod;
    
    ////////////////////////////////////////////////////////
    // the following declaraions are required. replace these 
    // by including the header files containing the declarations
    ////////////////////////////////////////////////////////
    class CICamera ;
    class CIFrame;
    class GRAB_EXT_ATTR;
    class LPCAM_ATTR;
    ///////////////////////////////////////
    
    typedef PCParentMod  (CALLBACK* LPIfxCreateParentModule)(IFC_BOARD_KIND modKind, DWORD modSubkind, DWORD seqnum, char *cfgFile, BOOL inspectOnly);
    typedef PCParentMod  (CALLBACK* LPIfxCreateCaptureModule)(char *modName,DWORD seqnum, char *cfgFile);
    typedef void (CALLBACK* LPProcessCameraFilesInDir)(const PSTR szPath, BOOL bRecurse );
    typedef BOOL (CALLBACK* LPEnableJitGrabMemLock)(void);
    typedef CICamera (CALLBACK* LPGetCam)(DWORD camPortNum);
    typedef HIFCGRAB (CALLBACK* LPGrab)(DWORD dwOptions,pBYTE dest, int ringNumFrames,int totalNumFrames, int x, int y, int dx, int dy);
    typedef BOOL (CALLBACK* LPFreeze)(CIFrame *frame);
    typedef int (CALLBACK* LPGrabWaitFrameEx)(HIFCGRAB grabID, BYTE **frameAddr, int FrameSeqNum, DWORD milliseconds, BOOL lock,DWORD *acquiredDy, BOOL *VirtualFrameEnd, GRAB_EXT_ATTR *ExtendedAttr);
    typedef void (CALLBACK* LPGrabRelease)(HIFCGRAB grabID, int seqNum);
    typedef void (CALLBACK* LPGetAttr)(LPCAM_ATTR lpAttr, BOOL zoomCorrected);
    
    extern IFC_IMP_DLL LPIfxCreateParentModule IfxCreateParentModule;
    extern IFC_IMP_DLL LPIfxCreateCaptureModule IfxCreateCaptureModule;
    extern IFC_IMP_DLL LPProcessCameraFilesInDir ProcessCameraFilesInDir;
    extern IFC_IMP_DLL LPEnableJitGrabMemLock EnableJitGrabMemLock;
    extern IFC_IMP_DLL LPGetCam GetCam;
    extern IFC_IMP_DLL LPGrab Grab;
    extern IFC_IMP_DLL LPFreeze Freeze;
    extern IFC_IMP_DLL LPGrabWaitFrameEx GrabWaitFrameEx;
    extern IFC_IMP_DLL LPGrabRelease GrabRelease;
    extern IFC_IMP_DLL LPGetAttr GetAttr;
    
    extern int LoadPCDigDlls();
    extern int FreePCDigDlls();
    #endif // DLLEXPORTS_H

  3. #3
    Join Date
    Apr 2009
    Posts
    13

    Smile

    Thanks for the reply. I trust you and yeah I know its a big mess. I am trying to clear it. It might take some time but I am sure that I will clear it.

    I have been given strict instructions that, I am not allowed to include any header files, instead write all the necessary classes in the same header file.

    Thanks again.

Similar Threads

  1. Replies: 0
    Last Post: 07-14-2006, 09:12 AM
  2. JDOM Errors & How to set the classpath
    By kpandya in forum Java
    Replies: 0
    Last Post: 01-14-2006, 07:01 PM
  3. Replies: 5
    Last Post: 10-17-2002, 01:58 PM
  4. Replies: 0
    Last Post: 08-19-2002, 03:20 AM
  5. Dynamically Load a Drop Down List Box
    By Larry Hunter in forum ASP.NET
    Replies: 2
    Last Post: 11-13-2000, 10:06 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center