tags:

views:

726

answers:

3

I need in a DLL to use a class, defined in an executable (DLL and executable are compiled by the same compiler). But I don't want the source code of this class definition to be available to DLL, only declaration.

One possible way to do it is to make all the necessary class methods to be virtual (so that DLL linker will not need these methods' definitions). The disadvantages of this approach:

  1. I cannot create objects of exported classes in DLL code using new (a have to create additional functions in executable's code).
  2. I have to make all these methods virtual, even if otherwise they don't need to be virtual.

There is a way to export a class from a DLL to an executable using Microsoft's __declspec(dllexport) storage-class extended attribute. Is there a way to export a class from executable to DLL using the same technique?

My old Borland C 6 compiler does not allow me to create import library during the build of executable project. (So, when compiling the DLL, linker gives me unresolved external error messages for all imported non-virtual class methods.) Is it a limitation of this very compiler, or maybe I'm missing something important?

+2  A: 

You could put the class in a second DLL if you really didn't want it in the first one.

I'm having a hard time understanding your reasoning for not just putting the class in the DLL though.

ETA: Did some more digging and found this link that explains how to generate an import library from an EXE project in Visual Studio 2008. As for how to export them, it looks like you just use the regular __declspec(dllexport).

Eric Petroelje
E.g., this class represents some API, that should be used in DLL. DLL is developed by third party. There is a 'know-how' issue not to let API implementation externals out to third parties.Though, putting this API class in another DLL will go, thanks.
Alex Che
It is too possible to export symbols from an executable. It has to be, DLLs are executables.
Rob K
Example: ntoskrnl.exe has exported symbols.
bk1e
Interesting, I didn't think it was possible to do that. I edited the answer and removed the inaccurate information there. Learn something new every day :)
Eric Petroelje
Thanks everybody for help!
Alex Che
+2  A: 

So far as I know, it is ok to use MS VS's dllexport to export a class or function from a exe and use it in a DLL. and it runs cool if your DLL and Exe execute in one process.

lz_prgmr
Yes, you're right. Just tried on MS VS 2008 - it's possible to generate an import library for an executable with all __declspec symbols and to link it with dll. So, it's seems to be a limitation of my old Borland C++ 6.0 compiler. Thank you.
Alex Che
A: 

OK, new answer in light of new information here. If you can't generate an export library for your EXE with your compiler, and you really really have to do it this way, here's a creative, hacky and generally not recommended solution:

Step 1: Create a C wrapper API for your class, kinda like this (probably wont compile, but you get the idea):

// Yes, need some 32 bit/64 bit checks here
#define MYHANDLE unsigned int

__declspec(dllexport) MYHANDLE MyClassNewInstance() {
   MyClass* ptr = new MyClass();
   return (MYHANDLE)ptr;
}

__delspec(dllexport) MyClassDoSomething( MYHANDLE handle, int parm ) {
  MyClass* ptr = (MyClass*)handle;
  ptr->DoSomething(parm);
}

etc..

Step 2: To actually get the C functions from the EXE for use in the DLL, use the Win32 API functions GetModuleHandle() and GetProcAddress().

Step 3: Create a proxy class in your DLL. The methods in the proxy class do nothing but call their counterpart C functions from the EXE.

This would keep the "real" implementation of your class out of the DLL. It's a hack, but it would probably work.

Eric Petroelje
Thanks for an alternative!
Alex Che