• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 257
  • Last Modified:

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.
0
zhongbing
Asked:
zhongbing
  • 7
  • 4
  • 3
1 Solution
 
WxWCommented:
To make a DLL , you need an entrypoint function DLLEntryPoint ( check documentation of the syntax ) , and also you need to export the functions that you want to use from other applications in the EXPORTS section of the DEF file :

EXPORTS
   MyFunction
   My2Function

etc .
0
 
WxWCommented:
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 .
0
 
zhongbingAuthor Commented:
 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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
WxWCommented:
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;
   }


0
 
WynCommented:
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.

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

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

Regards.
Wyn
0
 
WxWCommented:
the dllimport works as well for Borland compilers .
0
 
zhongbingAuthor Commented:
  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?
0
 
WynCommented:
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.
0
 
zhongbingAuthor Commented:
Can i use DLLMain just like WinMain?
0
 
WynCommented:
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
0
 
WynCommented:
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
0
 
WynCommented:
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
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now