VC++ resources in a static library

前端 未结 7 1965
天涯浪人
天涯浪人 2020-11-29 21:30

Is it possible to build resources into a static library and reuse them by simply linking with the library?

I\'m primarily thinking about the case where you call a fu

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 22:13

    It can be done, but it's quite painful: You can't do it by simply linking with the static library.

    Consider this: resources are embedded in an EXE or DLL. When some code in the static library calls (e.g.) LoadIcon, it'll get the resources from the EXE or DLL that it's linked with.

    So, if your static library requires resources to be available, you've got a couple of options:

    1. You can have the library build them on the fly, and then use (e.g.) CreateDialogIndirect. See Raymond Chen's "Building a dialog template at run-time".
    2. You can have them embedded in the library as simple arrays (i.e.) char my_dialog_resource[] = { .... };, and then use (e.g.) CreateDialogIndirect. You'll probably need to find (or write) a utility that converts from .RES files to .CPP files.
    3. You can ship the LIB file with a resource script (.RC file) and corresponding header file. You then #include them as relevant. You'll need to reserve a range of resource IDs for the LIB to use, so that they don't collide with those of the main EXE or DLL. This is what MFC does when used as a static library. Or you can use string resource IDs (this doesn't work for STRINGTABLE resources).
    4. Your static library can ship with a separate resource DLL.

提交回复
热议问题