none
Dialog resources in a static library RRS feed

  • Question

  • Hi,

    I have a problem with dialog and icon resources in my static library.
    I have created a static library (MFC static library) with Visual Studio 2008.

    I am calling Func() in the static library from win32 application, It tries to launch  a MFC dialog in the static libraray.

    When trying to access the resource I am getting afxCurrentResourceHandle is NULL assertion.

    I add this line AFX_MANAGE_STATE(AfxGetStaticModuleState());  in the Func() as the first line. But it didn't help.

    I need to use only static library. As per requirement, I should not use dll.

    Please help me how to launch a dialog in MFC static libraray from non MFC application?

    Wednesday, October 9, 2019 1:06 PM

Answers

  • Hi,

    Thank you for posting here.

    >>When trying to access the resource I am getting afxCurrentResourceHandle is NULL assertion.
    I add this line AFX_MANAGE_STATE(AfxGetStaticModuleState());  in the Func() as the first line. But it didn't help.

    When you create a win32 application with MFC support, then source code with a main function is created and AfxWinInit is called first to set up the MFC.

    int main(int args, char* argv[]) //, char *envp[])
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))  
        {       
            // TODO: change error code to suit your needs       
            cerr << _T("Fatal Error: MFC initialization failed") << endl;       
            return 1;   
        }   

    Best Regards,

    Jeanine Zhang

    • Marked as answer by sgrm123 Thursday, October 10, 2019 1:11 PM
    Thursday, October 10, 2019 6:19 AM
    Moderator

All replies

  • >I have a problem with dialog and icon resources in my static library.

    static libraries can't contain resources.
    You can create a DLL that contains Windows resources.

    Dave

    Wednesday, October 9, 2019 4:33 PM
  • Visual Studio doesn't expose the way to do this, you will have to either create some kind of make file or do this step manually.

    Normally resources will not have any external symbols in them, this means that the linker will never reference them if you put them in a static library. This means that you are stuck with two options.

    1) Generate the compiled resource (.res) file separately and provide this as input to the linker's command line while linking your executable.

    2) Use the CVTRES utility yourself to generate an object with a symbol that the linker can reference, add this to the static library and then use the linker's /include option to forcefully include this symbol.

    CVTRES is what the Visual C++ linker uses to take a .res file and be able to link to it. I know of no way to customise how the linker uses it, so to be able to do what you are aiming for, you would have to call it yourself.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Edited by Darran Rowe Wednesday, October 9, 2019 6:54 PM
    Wednesday, October 9, 2019 6:53 PM
  • Hi,

    Thank you for posting here.

    >>When trying to access the resource I am getting afxCurrentResourceHandle is NULL assertion.
    I add this line AFX_MANAGE_STATE(AfxGetStaticModuleState());  in the Func() as the first line. But it didn't help.

    When you create a win32 application with MFC support, then source code with a main function is created and AfxWinInit is called first to set up the MFC.

    int main(int args, char* argv[]) //, char *envp[])
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))  
        {       
            // TODO: change error code to suit your needs       
            cerr << _T("Fatal Error: MFC initialization failed") << endl;       
            return 1;   
        }   

    Best Regards,

    Jeanine Zhang

    • Marked as answer by sgrm123 Thursday, October 10, 2019 1:11 PM
    Thursday, October 10, 2019 6:19 AM
    Moderator
  • Hi,

    Thank you for posting here.

    >>When trying to access the resource I am getting afxCurrentResourceHandle is NULL assertion.
    I add this line AFX_MANAGE_STATE(AfxGetStaticModuleState());  in the Func() as the first line. But it didn't help.

    When you create a win32 application with MFC support, then source code with a main function is created and AfxWinInit is called first to set up the MFC.

    int main(int args, char* argv[]) //, char *envp[])
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))  
        {       
            // TODO: change error code to suit your needs       
            cerr << _T("Fatal Error: MFC initialization failed") << endl;       
            return 1;   
        }   

    Best Regards,

    Jeanine Zhang

    Thanks for the reply.

    My exe is win32 application in cprogramming language, 

    I added the header file "afxwin.h" and removed "windows.h"and  if I call AfxWinInit inside winmain(), I am getting fatal error C1189: #error :  MFC requires C++ compilation (use a .cpp suffix).

    I need to use c file only.

    Thursday, October 10, 2019 9:17 AM
  • Then you are out of luck. MFC (Microsoft Foundation Classes) is a C++ class library that also uses some templates, C doesn't understand classes or templates.

    In this case you have the choice between C or MFC, there is no cheating this without using C++ somewhere along the way. If you just want to use resources then you could just use the Windows API itself if you can't use C++.

    But then again, what semi-modern C compiler also doesn't have C++.


    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    • Edited by Darran Rowe Thursday, October 10, 2019 12:18 PM
    Thursday, October 10, 2019 12:15 PM
  • You are working against your tools.

    For example, your C Win32 application will probably not contain an MFC application object (i.e., CWinApp(Ex)).  Even if you could get a trivial example to build successfully it would likely encounter run-time problems.

    Thursday, October 10, 2019 12:28 PM