非托管 C++ 是否有可能?应用程序仅在需要托管类型时才加载 CLR?
更重要的是,我有一个本机 C++ 应用程序,它可能永远不需要使用托管类型。我希望 CLR 保持卸载状态,直到实际依赖于托管类型的代码路径实际被命中。
我试图使用 Visual Studio 2005 中的 /clr 开关来完成此操作,但据我所知,一旦使用该开关,整个 C++ 应用程序就会变成托管应用程序。有没有办法只为某个编译单元或函数指定它?我尝试在我的测试应用程序中使用 #pragma unmanaged 标记我的 main() 函数,但这并没有阻止它在启动时加载 CLR。
More to the point, I have a native C++ application, that may never need to use managed types. I would like the CLR to remain unloaded until I the codepath that actually depends on managed types is actually hit.
I was trying to accomplish this using the /clr switch in Visual Studio 2005, but as far as I can tell as soon as I use that switch, the entire C++ app becomes a managed app. Is there a way to specify it only for a certain compilation unit or function? I tried to mark my main() function in my test app with #pragma unmanaged, but that didn't stop it from loading the CLR at startup.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有混合模式 C++ DLL,一旦您的 DLL / EXE 加载到进程中,CLR 就会加载。没有办法改变这种行为。
实现您正在寻找的内容的最佳方法是将 DLL 分成两部分
您可以通过控制 #2 何时加载到进程中来控制 CLR 何时启动。这需要一些设置工作,但应该会得到您正在寻找的结果。
If you have a mixed mode C++ DLL the CLR will load as soon as your DLL / EXE is loaded into the process. There is no way to change this behavior.
The best way to achieve what you're looking for is to break up your DLL into 2 parts
You can control when the CLR starts up by controlling when #2 is loaded into the process. This requires a bit of setup work but should get the result you're looking for.