Re: Importing a Class define in a Dll




Oh, sorry! Somehow skipped it, it is quite unusual way
to import C++ code. I think once you will see how tricky
is it, you will switch back to the implicit "C++" importing
or just pure "C" instead :)

Well, a little background. Any class instance is just a piece
of memory of sizeof(class). But what does the class method
call look like? Generally (for any platform) the compiler
will generate a code to place a pointer to instance
into some well know register/stack and then call the
method like normal "C" routine. Think about the instance
pointer as an extra parameter passed to each non-static
method. It is specially useful for the ARM platform as the
instance ptr is passed through the R0 register and the rest
of params are passed through R1, R2, R3, ..., i.e. almost any
method may be called using a simple "C" prototype of this sort:

ret = SomeMethod(pInstnacePtr, Param1, Param2, ...);


It is actually double useful for ARMs as VS2005 does't
support inline asm for it, note that you will need
assembler in order to setup correctly the instance
pointer and pass the parameters on a different
platform, e.g. x86.

Ok, here is what you need. I'll be using the same class
sample from my prev post, assuming the code is intended
to run on ARM platform.


#ifdef DLL_PROJECT
#define DECL_SPEC
#else
#define DECL_SPEC __declspec(dllimport)
#pragma comment(lib, "lib_name_here" )
#endif

class DECL_SPEC MyClass
{
public:
MyClass() { ... };
~MyClass() { ... };
int DoSomething(int Param1, int Param2) { ... };
};



1. Build the DLL containing your class in the same
exactly way as you build it for implicit linking.


2. Now you will need some tool to dump the method
signatures. You can use "showdep", "dumpbin" or
whatever tool capable to show exports. You will
get some symbols like these:

"??0MyClass@@QAA@XZ" -> MyClass::MyClass()
"??4MyClass@@QAAAAV0@ABV0@@Z" -> MyClass::~MyClass()
"?DoSomething@MyClass@@QAAHPB_W0@Z" -> MyClass::DoSomething()


3. In you host app, define method prototypes:


#include <MyClass.h>

typedef void (* PMyClass_Constructor)(MyClass *pThis);
typedef void (* PMyClass_Destructor)(MyClass *pThis);
typedef int (* PMyClass_DoSomething)(MyClass *pThis, LPCTSTR pszText1, LPCTSTR pszText2);


4. Load the lib in standard way:

HANDLE hLib = LoadLibrary(_T("MyClass.dll"));


5. Obtain pointers:

PMyClass_Constructor MyClass_Constructor =
(PMyClass_Constructor)GetProcAddress(hLib,
_T("??0MyClass@@QAA@XZ"));

PMyClass_Destructor MyClass_Destructor =
(PMyClass_Destructor)GetProcAddress(hLib,
_T("??4MyClass@@QAAAAV0@ABV0@@Z"));

PMyClass_DoSomething MyClass_DoSomething =
(PMyClass_DoSomething)GetProcAddress(hLib,
_T("?DoSomething@MyClass@@QAAHPB_W0@Z"));


6. Allocate an object instance. Note that you can not use
new/delete to allocate and release the object implicitly.
We will use an array of bytes instead.


MyClass *pMyClassInstance = (MyClass *)
(new BYTE[ sizeof(MyClass) ]);


7. Call constructor to init the object


MyClass_Constructor(pMyClassInstance);



8. Call method (s)


ret = MyClass_DoSomething(pMyClassInstance, 0, 1);
ret = MyClass_DoSomething(pMyClassInstance, 2, 3);
ret = MyClass_DoSomething(pMyClassInstance, 4, 5);
....


9. Cleanup

MyClass_Destructor(pMyClassInstance); // Destructor
delete[] (BYTE *)pMyClassInstance; // Free instance buffer
FreeLibrary(hLib);


Are you still looking to use explicit importing? :)



Jimmy Lim wrote:
Thanks for answer voidcoder. The method you mention to me seems like using implicit export of a class

If I wish to implement in through explicit import, is it possible. If so how do i achieve it?

"voidcoder" wrote:

Oh, copy/paste typo! Below is corrected sample:


#ifdef DLL_PROJECT
#define DECL_SPEC
#else
#define DECL_SPEC __declspec(dllimport)
#pragma comment(lib, "lib_name_here" )
#endif

class DECL_SPEC MyClass
{
MyClass();
~MyClass();
...
};



voidcoder wrote:
Of course you can do it. Here is what you need:

In your DLL project:

1. Prepare the class header to handle correctly
import case. Something like this:

#ifdef DLL_PROJECT
#define DLL_PROJECT
#else
#define DLL_PROJECT __declspec(dllimport)
#pragma comment(lib, "lib_name_here" )
#endif

class DECL_SPEC MyClass
{
MyClass();
~MyClass();
...
};

2. Go to project settings -> preprocessor defs and add
DLL_PROJECT there.

3. Build the project, you will get *.DLL and *.LIB
files.

4. Make the class header and LIB files accessible
by the application project.


In your app project:

1. Include the class header.

2. Use the class.


Finally, the DLL must reside in the same folder
with the main app binary or in \Windows\* folder.



Jimmy Lim wrote:
Hi,

I would like to ask a question. In eVC, can I import a class to an application. but the class member functions and data are define in a dll.

If this is workable. Can I do explicit import?

Sincerely
Jimmy
.



Relevant Pages

  • app verifier break when doing map.find
    ... I have a static map variable in my class name MyClass. ... passing of stl object across dll boundaries: ... indeed passed across dll boundaries.But all operations on the stl objects ... Funny thing is when I replace the map variable as a pointer to map variable ...
    (microsoft.public.vc.atl)
  • Re: problem using self built DLL
    ... called MyClass, AND YOU HAVE SAID NOTHING MORE ABOUT IT! ... declaration, wherein you promise to say something later. ... MyClass has no member function or member ... I thought I would start to add them after I get the DLL ...
    (microsoft.public.vc.mfc)
  • Re: Exception handling for loading of dependency library?
    ... typedef int (const CString & s, ... A common technique for doing this is to declare it as a global variable, ... the handle for a given dll all in one place. ... pointless because ANSI C does not require dereferencing function pointer variables. ...
    (microsoft.public.vc.mfc)
  • Re: How to program plugin DLLs
    ... build the descriptor you mentioned. ... That class is the "only" element to be exported from the DLL. ... pointer to that class and then call something like this: ... > What I might consider here is a method which fills in a popup menu. ...
    (microsoft.public.vc.mfc)
  • Re: Pointers and DLLs/shared libraries
    ... These pointer variables will point to data in the main program. ... subroutine set_pointer ... (DLLs and shared libraries do not always behave like ordinary ... ....the DLL effectively becomes a part of all of the client programs that use it. ...
    (comp.lang.fortran)

Quantcast