CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2010
    Posts
    20

    [RESOLVED] Embed main() into a DLL

    Hi All

    Is it possible to embed the main() function into a DLL?

    I have searched the forums here, but none of 28 threads seem to have a relevant information. Note that I'm not talking about DllMain.

    For those who are interested why I want to do something like this. I have written my own service stuff (plain C/WIN32 API), and now I want to put it in a library. The concept works as a static library (I can create a static lib, link the application against this lib, implement in the app the functions required by the service lib, the service lib calls for example an application defined servicestopping() function when someone wants to stop the server, ..., this is all ok). Now I want to transform this static service library to a service dll. Is this possible?

    I don't need all the service related baggage as an example. If this is possible, could someone point me to an example where a main() function resides in a dll, and this main() acts as the main function of another project. Thanks

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Embed main() into a DLL

    No, it's not possible. Sure, you can have the dll export a function named main, but it won't be the entry point for the process.

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Embed main() into a DLL

    Now I want to transform this static service library to a service dll. Is this possible?
    Sure thing. You do this like with any other dll function. You just name your dll function Main (or something) and instruct user to call nothing but Main from his main.

    Code:
    #pragma comment(lib, "my_cool_service_stuff.lib")
    int Main(); // or place this to some header file in your service SDK
    
    int main()
    {
        return Main();
    }
    Best regards,
    Igor

  4. #4
    Join Date
    Jan 2010
    Posts
    20

    Re: Embed main() into a DLL

    Thanks to both of you. Igor, altough Arjay hit the mark I must admit that your post is insightful
    In this case 1) is there a way for my DLL to invoke a function implemented in the user application, other than passing the pointer to the function to MyMain() and, 2) if I decide to use a static lib is there a way to preserve the function namespace in my static library other than declaring all my functions static (like static void cool_function(int arg) ) ?

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Embed main() into a DLL

    Quote Originally Posted by stolencoin View Post
    In this case 1) is there a way for my DLL to invoke a function implemented in the user application, other than passing the pointer to the function to MyMain()
    There must be a sort of contract: user app either passes function pointer explicitly, or exports it with pre-defined name. (EXE may export functions the same way like DLL does)

    2) if I decide to use a static lib is there a way to preserve the function namespace in my static library other than declaring all my functions static (like static void cool_function(int arg) ) ?
    So, why don't you use C++ namespace? I believe it was intended exactly for solving this kind of things.
    Best regards,
    Igor

  6. #6
    Join Date
    Jan 2010
    Posts
    20

    Re: Embed main() into a DLL

    Quote Originally Posted by Igor Vartanov
    So, why don't you use C++ namespace?
    This is a C-only project; the use of C++ is out of question.
    Thanks for the replies, I'm marking this as solved.

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
  •  





Click Here to Expand Forum to Full Width

Featured