C# to C++/CLI to C DLL System.IO.FileNotFoundException

前端 未结 6 1758
野的像风
野的像风 2021-01-05 09:52

I\'m getting System.IO.FileNotFoundException: The specified module could not be found when running C# code that calls a C++/CLI assembly which in turn

6条回答
  •  梦毁少年i
    2021-01-05 10:14

    The reason why this happens is because you either are loading DLLMAIN from managed code, before the CRT has an opportunity to be initialized. You may not have any managed code, be executed DIRECTLY or INDERECTLY from an effect of DllMain notifications. (See: Expert C++/CLI: .Net for Visual C++ Programmers, chapter 11++).

    Or you have no native entrypoint defined wahtsoever, yet you have linked to MSVCRT. The CLR is automatically initialized for you with /clr, this detail causes a lot of confusion and must be taken into account. A mixed mode DLL actually delay loads the CLR through the use of hot-patching all of the managed entry point vtables in your classes.

    A number of class initialization issues surround this topic, loader lock and delay loading CLR are a bit trickey sometimes. Try to declare global's static and do not use #pragma managed/unmanaged, isolate your code with /clr per-file.

    If you can not isolate your code from the managed code, and are having trouble, (after taking some of these steps), you can also look towards hosting the CLR yourself and perhaps going through the effort of creating a domain manager, that would ensure your fully "in-the-loop" of runtime events and bootstrapping.

    This is exactally why, it has nothting todo with your search path, or initialization. Unfortunately the Fusion log viewer does not help that much (which is the usual place to look for .NET CLR assembly binding issues not dependency walker).

    Linking statically has nothing todo with this either. You can NOT statically link a C++/CLI application which is mixed mode.

    1. Place your DLLMAIN function into a file by itself.
    2. Ensure that this file does NOT have /CLR set in the build options (file build options)
    3. Make sure your linking with /MD or /MDd, and all your dependencies which you LINK use the exact same CRT.
    4. Evaluate your linker's settings for /DEFAULTLIB and /INCLUDE to identify any possiable reference issues, you can declare a prototype in your code and use /INCLUDE to override default library link resolution.

    Good luck, also check that book it's very good.

提交回复
热议问题