2

I have a problem with dialog and icon resources in my static library. I have created a 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 library.

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 library from a non MFC application.

EylM
  • 5,967
  • 2
  • 16
  • 28
userrmgs
  • 323
  • 1
  • 9
  • 2
    Possible duplicate of [VC++ resources in a static library](https://stackoverflow.com/questions/531502/vc-resources-in-a-static-library) – Algirdas Preidžius Oct 09 '19 at 14:18
  • @Algirdas The link u suggested does not solve the problem. – userrmgs Oct 09 '19 at 14:22
  • 1
    OT: This question strikes me close to home, since I am, currently, in process of fixing a defect, which occurred because someone moved code to a static lib (from a .dll), which resulted in certain dialog failing to be displayed.. – Algirdas Preidžius Oct 09 '19 at 14:23
  • "_The link u suggested does not solve the problem._" What solution do you want? Static libs can't contain embedded resources. And the linked question explains that, while providing several solutions, around such limitation. – Algirdas Preidžius Oct 09 '19 at 14:27
  • @AlgirdasPreidžius, I am also converting MFC dll to MFC static library. I am calling a function in static library from main application which launches a mfc dialog. After converting to static library it is not launching dialog and it is crashing. – userrmgs Oct 09 '19 at 14:28
  • 1
    Yes, that's the problem, that I was seeing as well. Please re-read the duplicate suggestion, so you would understand the possible ways to solve the problem. TLDR version of it, as I already stated: static library can't contain resources. – Algirdas Preidžius Oct 09 '19 at 14:32
  • @Algirdas, I saw that link, I could not understand. I want to go for include the static library's associated .res file solution. I could not understand this.Where( in exe or in lib) and how to set it? – userrmgs Oct 09 '19 at 14:35
  • Static libraries can't have a `.res` file. And if you do put one in a static library project it will be silently ignored. Follow the duplicates suggestions. – Richard Critten Oct 09 '19 at 14:40
  • @userrmgs You link .res file in the same way, as you would link .lib file. Do take note, if multiple .res files, compiled into the same binary (.exe, or .dll) contain icons, you may hit this issue: [fatal error CVT1100: duplicate resource. type:ICON, name:1](https://stackoverflow.com/questions/31323596/fatal-error-cvt1100-duplicate-resource-typeicon-name1-c-visual-studio-c). – Algirdas Preidžius Oct 09 '19 at 14:41

1 Answers1

2

The problem here is that a static library doesn't have an 'associated .res file'. If you are trying to migrate a DLL with resources to a static library, then you will need to also 'export' the resource script (its .rc file plus any associated .rc2 files and other referenced resources) to the client program!

So, just as you would have an #include "module.h" line in the .cpp source(s), you will also need an #include "module.rc" in your program's main .rc file (or, at least, in a file that it includes).

Note: Other fixes that folks have tried, like linking explicitly with extra (pre-compiled) .res files won't work! Although the internal structure of a binary .res file is very similar to any other .obj file, the linker will only ever include one!

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 1
    If using the Visual Studio resource editor, any changes done manually on the .rc file will get lost. So mind to include additional .rc files via the "resource includes" option of Visual Studio or via the .rc2 file. – Nick Oct 09 '19 at 15:56
  • @Nick - Excellent point! I forgot about that, as I disable the use of the visual resource editor in/on my similarly to-be-included resource scripts - all of which are .rc2 files, BTW. – Adrian Mole Oct 09 '19 at 15:59