Link to home
Start Free TrialLog in
Avatar of zhongbing
zhongbing

asked on

transfer a standalone ap to DLL

  I have a standalone application(win32, C source code, with a main window). Now i want to convert it into a DLL, and provides a C++ class interface.
   
   maybe you experts can provide me these information:
how to make a C++ class interface DLL?
how to move a winmain ap to a DLL?
any other comments are also welcome
   Pls don't answer , just comment.
ASKER CERTIFIED SOLUTION
Avatar of WxW
WxW

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WxW
WxW

Or you can do the export from the source :

extern "C" int _stdcall _export MyFunc(...)
   {

   }

then create an import library ( LIB ) file using Lib or ImpLib ( borland ) , then link this file against the application that wishes to use your DLL .
Avatar of zhongbing

ASKER

 Thand you very much for your soon reply, but maybe you didn't notice that i have said(don't answer, only comment), of cause i will give points to good comments.
  And I need a C++ CLASS work in DLL, not a common function.
   my another question is how to transter a C appliation(with window, WinMain, message loop) into a DLL, you didn't mention it.
   Please give me more information. I don't like to reject , because i don't like the feeling my answer was reject by others.
First , I have no problem if you reject my answer (you may not like it for a thousand reasons - its ok ! )

To make a whole C program a DLL , you must first have the DLLEntryPoint to initialize it , then export a function that has the same code as main() in your program ( with another name of course - main() as a name can't be used )

Then the calling application calls this function .

I never did a class export , but I think its the same with ordinary exports . Just try this :

class MyClass
   {
   .... // data of the class
   };

Then create an exported function in your DLL that creates an instance of the class and returns it to the caller , for example :

MyClass* pClass;

MyClass* _export GetMyClass()
   {
   pClass = new MyClass();
   return pClass;
   }


zhongbing:
if you wanna export a class use the syntax:
in Dll:
class  __declspec(dllexport) MyClass
{.....}

in Exe:
declare as:

class __declspec(dllimport) MyClass
{...}


Then you can use it this way in your winmain:

MyClass my;
my.somefunction;

Regards
Wyn.

Btw,the __declspec(dllimport) is a specifier only for VC.What IDE you use?

Regards.
Wyn
Btw,the __declspec(dllimport) is a specifier only for VC.What IDE you use?

Regards.
Wyn
the dllimport works as well for Borland compilers .
  Thanks a lot, quite clear, and sounds workable, please wait 1 or 2 days for me to work it out before i grade.
   I use VC, maybe i shoud use DllMain instead of DllEntryPoint?
Oh,you use VC,then DllMain is your choose.The DllEntryPoint is earlier version.
Finaly,convert a .exe to .dll is not as easy as it sounds,please pay a caution.
Regards.
Wyn.
Can i use DLLMain just like WinMain?
Hmm,zhongbing:
The DllMain is quite different but it still acts as WinMain in some aspects.

Here is an excerpt from msdn:

The DllMain function is an optional method of entry into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated, or upon calls to the LoadLibrary and FreeLibrary functions.

DllMain is a placeholder for the library-defined function name. Earlier versions of the SDK documentation used DllEntryPoint as the entry-point function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools.
......................................

Thus,DllMain may be called more than once but WinMain can be called only once.When you attach a .dll or release it or a thread is created in your process after you'v loaded the thread or a thread is released , in all above situation,DllMain will be called but you can shield some ways.
So,if you wanna it act like a winmain ,you can achieve in some degree but Dll can not have a message cycle thus you should pay attention on this.
Regards.
Wyn
For example,if you wanna keep it from being call when a thread attach your process you can do this way:

int APIENTRY DllMain (HINSTANCE hInstance,DWORD dwReason,LPVOID lpReserved)
{ if ( dwReason == DLL_PROCESS_ATTACH)
......//Here you can initialize some variables and do some chores
 
return (int)(DisableThreadLibraryCalls (HinstDll));
//here you shield it from thread calls.
}

Regards.
Wyn
For example,if you wanna keep it from being call when a thread attach your process you can do this way:

int APIENTRY DllMain (HINSTANCE hInstance,DWORD dwReason,LPVOID lpReserved)
{ if ( dwReason == DLL_PROCESS_ATTACH)
......//Here you can initialize some variables and do some chores
 
return (int)(DisableThreadLibraryCalls (HinstDll));
//here you shield it from thread calls.
}

Regards.
Wyn