Mixed Language Assembly (C# - C++/CLI) class library. RRS feed

  • Question

  • Hello,

    I'm attempting to build a mixed language assembly from both C# and C++/CLI.

    I've been following instructions for doing this from the following:
    • http://blogs.msdn.com/texblog/archive/2007/04/05/linking-native-c-into-c-applications.aspx
    • http://blogs.msdn.com/junfeng/archive/2006/05/20/599434.aspx
    Instead of building and linking as an executable assembly, I've been attempting to create a class library for use by other .NET applications.

    I have two files: managed.cpp and class_library.cs,  I've been performing the following steps to compile and link into a single library:

    cl /clr /c /MD managed.cpp 
    csc /target:module /addmodule:managed.obj class_library.cs 
    link /dll /ltcg /out:class_library.dll managed.obj class_library.netmodule 

    As for my two files, managed.cpp wraps some native calls (let's say printf() for now) inside a managed class.  Then class_library.css has some more classes, that rely on the class exposed by managed.cpp.  For example, managed.cpp is an InterOp class that compiles into a mixed mode assembly, to avoid the use of DllImport (P/Invoke), and to simplify the marshalling process.

    Now, the whole thing works and outputs class_library.dll as expected.  I then reference this library from another assembly for use.  Now when I run the assembly, I get the following exception:

    Unhandled Exception: System.IO.FileNotFoundException: The specified module could not be found. (Exception from HRESULT: 0x8007007E) at TestApp.Program.Main(String[] args)

    Now, if I compile the ENTIRE thing into a single executable assembly, avoiding the production of a class library (i.e. during the CSC, I also compile in the Program.cs that uses the classes from class_library.cs) and change the link to produce an executable assembly with the entry point of Program.Main and execute the assembly, it runs just fine.  Unfortunately this is not what I want.  I want to be able to make a stand alone class library from both managed.cpp and class_library.cs that I can then redistribute to others for use.

    Is there any possible way to fix this exception, or some tips to get me set on the right direction?  If you need anymore information to help, I will gladly supply it.


    Wednesday, June 25, 2008 3:32 AM

All replies

  • Your .exe probably just can't find the assembly.  You'd normally set Copy Local = True when you add the reference to the assembly so the IDE will automatically copy the assembly into the build folder for the .exe.  Putting the assembly in the GAC is another standard approach, although you might have some trouble getting it signed.  An app.exe.config file is yet another way to help the CLR assembly loader find it.

    Use Fuslogvw.exe to troubleshoot assembly loading problems.  Also be sure to put any unmanaged DLLs that your assembly might depend on in a place where the Windows (not assembly) loader can find them.

    Hans Passant.
    • Edited by nobugz Wednesday, June 25, 2008 10:52 AM fuslogvw note
    Wednesday, June 25, 2008 10:48 AM
  • Thanks for the reply.

    I've been looking and the class_library.dll that I compiled is in the same location as the executable, so this doesn't seem to be the problem.

    Here's the log from the bind:

    *** Assembly Binder Log Entry  (6/25/2008 @ 10:35:01 AM) ***

    The operation was successful.
    Bind result: hr = 0x0. The operation completed successfully.

    Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
    Running under executable C:\[Removed]\Documents\Visual Studio 2008\Projects\TestApp\TestApp\bin\Debug\TestApp.exe
    --- A detailed error log follows.

    === Pre-bind state information ===
    LOG: User = [Removed]
    LOG: DisplayName = test, Version=, Culture=neutral, PublicKeyToken=null
    LOG: Appbase = file:///C:/[Removed]/Documents/Visual Studio 2008/Projects/TestApp/TestApp/bin/Debug/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = NULL
    Calling assembly : TestApp, Version=, Culture=neutral, PublicKeyToken=null.
    LOG: This bind starts in default load context.
    LOG: No application configuration file found.
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///C:/[Removed]/Documents/Visual Studio 2008/Projects/TestApp/TestApp/bin/Debug/class_library.DLL.
    LOG: Assembly download was successful. Attempting setup of file: C:\[Removed]\Documents\Visual Studio 2008\Projects\TestApp\TestApp\bin\Debug\class_library
    LOG: Entering run-from-source setup phase.
    LOG: Assembly Name is: test, Version=, Culture=neutral, PublicKeyToken=null
    LOG: Binding succeeds. Returns assembly from C:\[Removed]\Documents\Visual Studio 2008\Projects\TestApp\TestApp\bin\Debug\class_library.dll
    LOG: Assembly is loaded in default load context.

    • Edited by ByteMR Wednesday, June 25, 2008 4:48 PM More edits
    Wednesday, June 25, 2008 4:40 PM
  • Unfortunately this issue has never been resolved.  It does not seem to be an issue with referencing other assemblies.
    Thursday, July 24, 2008 8:44 PM