<small id='n3AWd'></small><noframes id='n3AWd'>

      <legend id='n3AWd'><style id='n3AWd'><dir id='n3AWd'><q id='n3AWd'></q></dir></style></legend>
        <bdo id='n3AWd'></bdo><ul id='n3AWd'></ul>
    1. <i id='n3AWd'><tr id='n3AWd'><dt id='n3AWd'><q id='n3AWd'><span id='n3AWd'><b id='n3AWd'><form id='n3AWd'><ins id='n3AWd'></ins><ul id='n3AWd'></ul><sub id='n3AWd'></sub></form><legend id='n3AWd'></legend><bdo id='n3AWd'><pre id='n3AWd'><center id='n3AWd'></center></pre></bdo></b><th id='n3AWd'></th></span></q></dt></tr></i><div id='n3AWd'><tfoot id='n3AWd'></tfoot><dl id='n3AWd'><fieldset id='n3AWd'></fieldset></dl></div>
      <tfoot id='n3AWd'></tfoot>
      1. 加载 DLL 不初始化静态 C++ 类

        时间:2023-08-01
        <i id='PCvBz'><tr id='PCvBz'><dt id='PCvBz'><q id='PCvBz'><span id='PCvBz'><b id='PCvBz'><form id='PCvBz'><ins id='PCvBz'></ins><ul id='PCvBz'></ul><sub id='PCvBz'></sub></form><legend id='PCvBz'></legend><bdo id='PCvBz'><pre id='PCvBz'><center id='PCvBz'></center></pre></bdo></b><th id='PCvBz'></th></span></q></dt></tr></i><div id='PCvBz'><tfoot id='PCvBz'></tfoot><dl id='PCvBz'><fieldset id='PCvBz'></fieldset></dl></div>

      2. <legend id='PCvBz'><style id='PCvBz'><dir id='PCvBz'><q id='PCvBz'></q></dir></style></legend>
            <bdo id='PCvBz'></bdo><ul id='PCvBz'></ul>
          • <small id='PCvBz'></small><noframes id='PCvBz'>

              • <tfoot id='PCvBz'></tfoot>

                    <tbody id='PCvBz'></tbody>
                  本文介绍了加载 DLL 不初始化静态 C++ 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个正在运行时加载的 DLL.DLL 依赖于内部工作的静态变量(它是 std::map),该变量在 DLL 中定义.

                  I have a DLL that is being loaded at run-time. The DLL relies on a static variable for internal workings (it is a std::map), this variable is defined within the DLL.

                  当我在加载后从 DLL 调用第一个函数时,我从 DLL 得到一个 SegFault,映射从未被初始化.从我从 DLL Loading 中读到的所有内容中,静态和全局数据初始化应该在调用 DLLMain 之前发生.

                  When I call the first function from the DLL after loading, I get a SegFault from the DLL, the map was never initialized. From everything I have read from DLL Loading, static and global data initialization should happen before even the call to DLLMain.

                  为了测试静态初始化,我添加了一个静态结构来打印一条消息,甚至还加入了一个断点以进行良好的衡量.

                  To test static initialization I added a static struct that prints out a message, and even threw in a breakpoint for good measure.

                  static struct a
                  {
                    a(void) { puts("Constructing
                  "); }
                  }statica;
                  

                  在调用 DLLMain 或函数之前没有消息或中断.

                  There was no message, or break before DLLMain or the function is called.

                  这是我的加载代码:

                    dll = LoadLibrary("NetSim");
                    //Error Handling
                  
                    ChangeReliability   = reinterpret_cast<NetSim::ChangeReliability>
                                          (GetProcAddress(dll, "ChangeReliability"));
                  
                  
                  ChangeReliability(100);
                  

                  我已经验证了 dll 版本是正确的,多次重建整个项目,但没有区别.我的想法很新鲜.

                  I have verified that the dll version is the correct one, rebuilt the entire project multiple times, but no difference. I am fresh out of ideas.

                  推荐答案

                  链接 DLL 时,是否指定了/ENTRY 开关?如果是这样,它将覆盖链接器的默认设置,即 DllMainCRTStartup.因此,_CRT_INIT 不会被调用,反过来,全局初始化器也不会被调用,这将导致未初始化的全局(静态)数据.

                  When you linked your DLL, was the /ENTRY switch specified? If so, it'll override the linker's default which is DllMainCRTStartup. As a result, _CRT_INIT won't be called and, in turn, the global initializers won't be called which will result in uninitialized global (static) data.

                  如果您为自己的入口点指定/ENTRY,则需要在进程附加和进程分离期间调用 _CRT_INIT.

                  If you are specifying /ENTRY for your own entry point, you need to call _CRT_INIT during process attach and process detach.

                  如果您未指定/ENTRY,则链接器应使用 CRT 的入口点,该入口点在调用 DllMain 之前在进程附加/分离上调用 _CRT_INIT.

                  If you are not specifying /ENTRY, the linker should be using the CRT's entry point which calls _CRT_INIT on process attach/detach before calling into your DllMain.

                  这篇关于加载 DLL 不初始化静态 C++ 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:使用 Windows API 获取用户的桌面文件夹? 下一篇:使用 MinGW、freeglut 和 glew 编译简单的静态 OpenGL

                  相关文章

                  最新文章

                1. <legend id='iQ6HS'><style id='iQ6HS'><dir id='iQ6HS'><q id='iQ6HS'></q></dir></style></legend>

                        <bdo id='iQ6HS'></bdo><ul id='iQ6HS'></ul>
                      <tfoot id='iQ6HS'></tfoot>

                      <small id='iQ6HS'></small><noframes id='iQ6HS'>

                    1. <i id='iQ6HS'><tr id='iQ6HS'><dt id='iQ6HS'><q id='iQ6HS'><span id='iQ6HS'><b id='iQ6HS'><form id='iQ6HS'><ins id='iQ6HS'></ins><ul id='iQ6HS'></ul><sub id='iQ6HS'></sub></form><legend id='iQ6HS'></legend><bdo id='iQ6HS'><pre id='iQ6HS'><center id='iQ6HS'></center></pre></bdo></b><th id='iQ6HS'></th></span></q></dt></tr></i><div id='iQ6HS'><tfoot id='iQ6HS'></tfoot><dl id='iQ6HS'><fieldset id='iQ6HS'></fieldset></dl></div>