Linking Mixed Mode and Managed Assemblies

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?aWduaGVucnk=?=

    Linking Mixed Mode and Managed Assemblies

    I have a managed C++ project and two C# projects. All are class library
    projects. The C++ project links with native C++ static libraries and
    references to one C# project. The projects structure goes something like this.

    Proj2_MCPP --(references)--Proj1_CSharp
    Proj3_CSharp --(references)--Proj2_MCPP and Proj1_CSharp

    My objective is to link the DLLs produced by the 3 projects into a single DLL.

    I tried the following scenario.
    1. csc Proj1_CSharp into a netmodule
    2. cl Proj2_MCPP with /LN and /clr:oldSyntax switch to produce .obj files
    3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
    as input
    4. Link 1, 2 and 3 into a dll.

    and I also tried this.
    1. csc Proj1_CSharp into a netmodule
    2. Build Proj2_MCPP from VS.NET 2005 (based on
    http://support.microsoft.com/kb/309805) to produce .netmodule and obj .files.
    3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
    as input
    4. Link 1, 2 and 3 into a dll.

    I never managed to reach step 4. I just stuck with error messages resulted
    from step 3.

    Am I doing the right thing? Or it is just not possible?

    Thanks - Henry
  • =?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=

    #2
    RE: Linking Mixed Mode and Managed Assemblies

    Class library projects are DLLs. You can't directly link DLLs together into
    a new DLL.

    You could try a utility called ILMerge [1]; but I haven't tried it with
    mixed-mode DLLs.

    [1] http://research.microsoft.com/~mbarnett/ILMerge.aspx

    --
    Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

    Microsoft MVP, Visual Developer - Visual C#


    "ignhenry" wrote:
    I have a managed C++ project and two C# projects. All are class library
    projects. The C++ project links with native C++ static libraries and
    references to one C# project. The projects structure goes something like this.
    >
    Proj2_MCPP --(references)--Proj1_CSharp
    Proj3_CSharp --(references)--Proj2_MCPP and Proj1_CSharp
    >
    My objective is to link the DLLs produced by the 3 projects into a single DLL.
    >
    I tried the following scenario.
    1. csc Proj1_CSharp into a netmodule
    2. cl Proj2_MCPP with /LN and /clr:oldSyntax switch to produce .obj files
    3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
    as input
    4. Link 1, 2 and 3 into a dll.
    >
    and I also tried this.
    1. csc Proj1_CSharp into a netmodule
    2. Build Proj2_MCPP from VS.NET 2005 (based on
    http://support.microsoft.com/kb/309805) to produce .netmodule and obj .files.
    3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
    as input
    4. Link 1, 2 and 3 into a dll.
    >
    I never managed to reach step 4. I just stuck with error messages resulted
    from step 3.
    >
    Am I doing the right thing? Or it is just not possible?
    >
    Thanks - Henry

    Comment

    • =?Utf-8?B?aWduaGVucnk=?=

      #3
      RE: Linking Mixed Mode and Managed Assemblies

      Thanks, Peter. I read somewhere that ILMerge, and Al.exe, only work for pure
      managed assemblies, but don't support mixed mode DLLs containing both managed
      and unmanaged code. I dropped them from my list.

      Right now I am looking at using CSharp compiler and VC++ linker. I've seen
      in internet newsgroups some people got it working successfully. My CSharp
      compiler is complaining that some classes are defined multiple times.
      Obviously, I am still missing something - probably simple.

      -- Henry

      "Peter Ritchie [C# MVP]" wrote:
      Class library projects are DLLs. You can't directly link DLLs together into
      a new DLL.
      >
      You could try a utility called ILMerge [1]; but I haven't tried it with
      mixed-mode DLLs.
      >
      [1] http://research.microsoft.com/~mbarnett/ILMerge.aspx
      >
      --
      Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

      Microsoft MVP, Visual Developer - Visual C#
      >
      >
      "ignhenry" wrote:
      >
      I have a managed C++ project and two C# projects. All are class library
      projects. The C++ project links with native C++ static libraries and
      references to one C# project. The projects structure goes something like this.

      Proj2_MCPP --(references)--Proj1_CSharp
      Proj3_CSharp --(references)--Proj2_MCPP and Proj1_CSharp

      My objective is to link the DLLs produced by the 3 projects into a single DLL.

      I tried the following scenario.
      1. csc Proj1_CSharp into a netmodule
      2. cl Proj2_MCPP with /LN and /clr:oldSyntax switch to produce .obj files
      3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
      as input
      4. Link 1, 2 and 3 into a dll.

      and I also tried this.
      1. csc Proj1_CSharp into a netmodule
      2. Build Proj2_MCPP from VS.NET 2005 (based on
      http://support.microsoft.com/kb/309805) to produce .netmodule and obj .files.
      3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
      as input
      4. Link 1, 2 and 3 into a dll.

      I never managed to reach step 4. I just stuck with error messages resulted
      from step 3.

      Am I doing the right thing? Or it is just not possible?

      Thanks - Henry

      Comment

      • =?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=

        #4
        RE: Linking Mixed Mode and Managed Assemblies

        Visual Studio doesn't support them, but compiling to netmodules could then be
        linked together as an assembly using Assembly Linker (al.exe).

        I'm not sure how to get c++ compiler to generate netmodules...

        --
        Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

        Microsoft MVP, Visual Developer - Visual C#


        "ignhenry" wrote:
        Thanks, Peter. I read somewhere that ILMerge, and Al.exe, only work for pure
        managed assemblies, but don't support mixed mode DLLs containing both managed
        and unmanaged code. I dropped them from my list.
        >
        Right now I am looking at using CSharp compiler and VC++ linker. I've seen
        in internet newsgroups some people got it working successfully. My CSharp
        compiler is complaining that some classes are defined multiple times.
        Obviously, I am still missing something - probably simple.
        >
        -- Henry
        >
        "Peter Ritchie [C# MVP]" wrote:
        >
        Class library projects are DLLs. You can't directly link DLLs together into
        a new DLL.

        You could try a utility called ILMerge [1]; but I haven't tried it with
        mixed-mode DLLs.

        [1] http://research.microsoft.com/~mbarnett/ILMerge.aspx

        --
        Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

        Microsoft MVP, Visual Developer - Visual C#


        "ignhenry" wrote:
        I have a managed C++ project and two C# projects. All are class library
        projects. The C++ project links with native C++ static libraries and
        references to one C# project. The projects structure goes something like this.
        >
        Proj2_MCPP --(references)--Proj1_CSharp
        Proj3_CSharp --(references)--Proj2_MCPP and Proj1_CSharp
        >
        My objective is to link the DLLs produced by the 3 projects into a single DLL.
        >
        I tried the following scenario.
        1. csc Proj1_CSharp into a netmodule
        2. cl Proj2_MCPP with /LN and /clr:oldSyntax switch to produce .obj files
        3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
        as input
        4. Link 1, 2 and 3 into a dll.
        >
        and I also tried this.
        1. csc Proj1_CSharp into a netmodule
        2. Build Proj2_MCPP from VS.NET 2005 (based on
        http://support.microsoft.com/kb/309805) to produce .netmodule and obj .files.
        3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
        as input
        4. Link 1, 2 and 3 into a dll.
        >
        I never managed to reach step 4. I just stuck with error messages resulted
        from step 3.
        >
        Am I doing the right thing? Or it is just not possible?
        >
        Thanks - Henry

        Comment

        • =?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=

          #5
          RE: Linking Mixed Mode and Managed Assemblies

          Here's what I've been able to do:

          csc /target:module cstest.cs
          cl /clr:pure /FUSystem.dll /LN cpptest.cpp
          al /platform:x86 /t:lib /out:test.dll cpptest.netmodu le cstest.netmodul e

          This generates a dll (test.dll) that contains the two netmodules. When I
          added to another C# project as a reference I was able to add managed types
          from both netmodules.

          --
          Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

          Microsoft MVP, Visual Developer - Visual C#


          "ignhenry" wrote:
          Thanks, Peter. I read somewhere that ILMerge, and Al.exe, only work for pure
          managed assemblies, but don't support mixed mode DLLs containing both managed
          and unmanaged code. I dropped them from my list.
          >
          Right now I am looking at using CSharp compiler and VC++ linker. I've seen
          in internet newsgroups some people got it working successfully. My CSharp
          compiler is complaining that some classes are defined multiple times.
          Obviously, I am still missing something - probably simple.
          >
          -- Henry
          >
          "Peter Ritchie [C# MVP]" wrote:
          >
          Class library projects are DLLs. You can't directly link DLLs together into
          a new DLL.

          You could try a utility called ILMerge [1]; but I haven't tried it with
          mixed-mode DLLs.

          [1] http://research.microsoft.com/~mbarnett/ILMerge.aspx

          --
          Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

          Microsoft MVP, Visual Developer - Visual C#


          "ignhenry" wrote:
          I have a managed C++ project and two C# projects. All are class library
          projects. The C++ project links with native C++ static libraries and
          references to one C# project. The projects structure goes something like this.
          >
          Proj2_MCPP --(references)--Proj1_CSharp
          Proj3_CSharp --(references)--Proj2_MCPP and Proj1_CSharp
          >
          My objective is to link the DLLs produced by the 3 projects into a single DLL.
          >
          I tried the following scenario.
          1. csc Proj1_CSharp into a netmodule
          2. cl Proj2_MCPP with /LN and /clr:oldSyntax switch to produce .obj files
          3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
          as input
          4. Link 1, 2 and 3 into a dll.
          >
          and I also tried this.
          1. csc Proj1_CSharp into a netmodule
          2. Build Proj2_MCPP from VS.NET 2005 (based on
          http://support.microsoft.com/kb/309805) to produce .netmodule and obj .files.
          3. csc Proj3_CSharp with /AddModule option and use .netmodule and .obj files
          as input
          4. Link 1, 2 and 3 into a dll.
          >
          I never managed to reach step 4. I just stuck with error messages resulted
          from step 3.
          >
          Am I doing the right thing? Or it is just not possible?
          >
          Thanks - Henry

          Comment

          • Jeffrey Tan[MSFT]

            #6
            RE: Linking Mixed Mode and Managed Assemblies

            Hi Henry,

            I have compiled the following files in a single .NET assembly (which is not
            the same as a single file as you know that a .NET assembly is a logical
            grouping of .NET modules):

            csc /t:module /out:Proj1_CShar p.net Proj1_CSharp.cs
            cl -clr -LN Proj2_MCPP.cpp
            csc /t:module /addmodule:Proj1 _CSharp.net /addmodule:Proj2 _MCPP.netmodule
            Proj3_CSharp.cs
            al /out:single.dll /platform:x86 Proj1_CSharp.ne t Proj2_MCPP.netm odule
            Proj3_CSharp.ne tmodule

            Proj3_CSharp.cs :

            public class Proj3_CSharp
            {
            public void f(Proj2_MCPP x, Proj1_CSharp y)
            {
            }
            }

            Proj2_MCPP.cpp:

            #using "Proj1_CSharp.n et"

            public ref class Proj2_MCPP
            {
            public:
            void g(Proj1_CSharp x) {}
            };

            Proj1_CSharp.cs :

            public class Proj1_CSharp
            {
            public void h(){}
            }

            single.dll is a manifest-only file of a .NET assembly with 3 .NET modules
            Proj1_CSharp.ne t, Proj2_MCPP.netm odule and Proj3_CSharp.ne tmodule.
            Depending on what you have in the C++ code -most likely- you will need a
            manifest file for the .EXE consuming types defined in the
            Proj2_MCPP.netm odule.

            Hope this helps.

            Best regards,
            Jeffrey Tan
            Microsoft Online Community Support

            Delighting our customers is our #1 priority. We welcome your comments and
            suggestions about how we can improve the support we provide to you. Please
            feel free to let my manager know what you think of the level of service
            provided. You can send feedback directly to my manager at:
            msdnmg@microsof t.com.

            =============== =============== =============== =====
            Get notification to my posts through email? Please refer to
            Gain technical skills through documentation and training, earn certifications and connect with the community

            ications.

            Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
            where an initial response from the community or a Microsoft Support
            Engineer within 1 business day is acceptable. Please note that each follow
            up response may take approximately 2 business days as the support
            professional working with you may need further investigation to reach the
            most efficient resolution. The offering is not appropriate for situations
            that require urgent, real-time or phone-based interactions or complex
            project analysis and dump analysis issues. Issues of this nature are best
            handled working with a dedicated Microsoft Support Engineer by contacting
            Microsoft Customer Support Services (CSS) at
            http://msdn.microsoft.com/subscripti...t/default.aspx.
            =============== =============== =============== =====
            This posting is provided "AS IS" with no warranties, and confers no rights.

            Comment

            • =?Utf-8?B?aWduaGVucnk=?=

              #7
              RE: Linking Mixed Mode and Managed Assemblies

              Hi Jeffrey, thanks for the reply. I tried the examples and it works.

              I finally managed to do the linking using VC++ linker. With it, I am able to
              get smaller number of assemblies (One DLL and 2 .NET modules). I just can't
              figure out how to achieve that with Al.exe (If you have any ideas, please let
              me know).

              In essence:

              1. csc /out:Proj1_CShar p.netmodule /target:module
              /recurse:..\src\ Proj1_CSharp\*. cs
              2. cl /clr /LN /MD /D "WITH_ATLAS " ..\src\Proj2_MC PP\*.cpp libf77blas.lib
              libatlas.lib /I "../External/ATLAS" /AI "../External/ATLAS" /link
              /LIBPATH:"../External/ATLAS" /OUT:"Kernel.net module"
              3. csc /addmodule:Proj1 _CSharp.netmodu le;Kernel.netmo dule
              /out:Numeric.net module /target:module /recurse:..\src\ Numeric\*.cs
              4. link /LTCG /CLRIMAGETYPE:IJ W /NOENTRY /DLL
              /ASSEMBLYMODULE: Proj1_CSharp.ne tmodule /ASSEMBLYMODULE: Kernel.netmodul e
              /OUT:Proj3_CShar p.dll Numeric.netmodu le /LIBPATH:"C:\Pro gram Files\Microsoft
              Visual Studio 8\VC\" /LIBPATH:"../External/ATLAS"

              ---output 1 dll and 2 netmodules

              Henry


              ""Jeffrey Tan[MSFT]"" wrote:
              Hi Henry,
              >
              I have compiled the following files in a single .NET assembly (which is not
              the same as a single file as you know that a .NET assembly is a logical
              grouping of .NET modules):
              >
              csc /t:module /out:Proj1_CShar p.net Proj1_CSharp.cs
              cl -clr -LN Proj2_MCPP.cpp
              csc /t:module /addmodule:Proj1 _CSharp.net /addmodule:Proj2 _MCPP.netmodule
              Proj3_CSharp.cs
              al /out:single.dll /platform:x86 Proj1_CSharp.ne t Proj2_MCPP.netm odule
              Proj3_CSharp.ne tmodule
              >
              Proj3_CSharp.cs :
              >
              public class Proj3_CSharp
              {
              public void f(Proj2_MCPP x, Proj1_CSharp y)
              {
              }
              }
              >
              Proj2_MCPP.cpp:
              >
              #using "Proj1_CSharp.n et"
              >
              public ref class Proj2_MCPP
              {
              public:
              void g(Proj1_CSharp x) {}
              };
              >
              Proj1_CSharp.cs :
              >
              public class Proj1_CSharp
              {
              public void h(){}
              }
              >
              single.dll is a manifest-only file of a .NET assembly with 3 .NET modules
              Proj1_CSharp.ne t, Proj2_MCPP.netm odule and Proj3_CSharp.ne tmodule.
              Depending on what you have in the C++ code -most likely- you will need a
              manifest file for the .EXE consuming types defined in the
              Proj2_MCPP.netm odule.
              >
              Hope this helps.
              >
              Best regards,
              Jeffrey Tan
              Microsoft Online Community Support
              >
              Delighting our customers is our #1 priority. We welcome your comments and
              suggestions about how we can improve the support we provide to you. Please
              feel free to let my manager know what you think of the level of service
              provided. You can send feedback directly to my manager at:
              msdnmg@microsof t.com.
              >
              =============== =============== =============== =====
              Get notification to my posts through email? Please refer to
              Gain technical skills through documentation and training, earn certifications and connect with the community

              ications.
              >
              Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
              where an initial response from the community or a Microsoft Support
              Engineer within 1 business day is acceptable. Please note that each follow
              up response may take approximately 2 business days as the support
              professional working with you may need further investigation to reach the
              most efficient resolution. The offering is not appropriate for situations
              that require urgent, real-time or phone-based interactions or complex
              project analysis and dump analysis issues. Issues of this nature are best
              handled working with a dedicated Microsoft Support Engineer by contacting
              Microsoft Customer Support Services (CSS) at
              http://msdn.microsoft.com/subscripti...t/default.aspx.
              =============== =============== =============== =====
              This posting is provided "AS IS" with no warranties, and confers no rights.
              >
              >

              Comment

              • Jeffrey Tan[MSFT]

                #8
                RE: Linking Mixed Mode and Managed Assemblies

                Hi Henry,

                Thanks for your feedback.

                Oh, your original statement is correct. As our product manager Steve
                confirmed in the article below, the al.exe/ilmerge approaches only work
                well for purely managed scenarios, but they don't support linking native
                code. This makes sense, since only the C++/CLI supports the single assembly
                internal interop. We need the C++/CLI link.exe to get this done.

                Steve demonstrated this walkthrough in the article below(which is similar
                as what you have done):
                "Linking native C++ into C# applications"

                lications.aspx

                Does this meet your need? If not, please feel free to tell me, thanks.

                Best regards,
                Jeffrey Tan
                Microsoft Online Community Support
                =============== =============== ===========
                Delighting our customers is our #1 priority. We welcome your comments and
                suggestions about how we can improve the support we provide to you. Please
                feel free to let my manager know what you think of the level of service
                provided. You can send feedback directly to my manager at:
                msdnmg@microsof t.com.

                This posting is provided "AS IS" with no warranties, and confers no rights.

                Comment

                • Jeffrey Tan[MSFT]

                  #9
                  RE: Linking Mixed Mode and Managed Assemblies

                  Hi Henry,

                  Have you reviewed my last reply to you? Does using link.exe following the
                  steps in that MSDN blog meet your need? If you still need any help or have
                  any concern, please feel free to tell me, thanks.

                  Best regards,
                  Jeffrey Tan
                  Microsoft Online Community Support
                  =============== =============== ===========
                  Delighting our customers is our #1 priority. We welcome your comments and
                  suggestions about how we can improve the support we provide to you. Please
                  feel free to let my manager know what you think of the level of service
                  provided. You can send feedback directly to my manager at:
                  msdnmg@microsof t.com.

                  This posting is provided "AS IS" with no warranties, and confers no rights.

                  Comment

                  • =?Utf-8?B?aWduaGVucnk=?=

                    #10
                    RE: Linking Mixed Mode and Managed Assemblies

                    Hi Jeffrey

                    Sorry for late reply.

                    Steve's article indeed helped me solve the problem.

                    Thanks!


                    ""Jeffrey Tan[MSFT]"" wrote:
                    Hi Henry,
                    >
                    Thanks for your feedback.
                    >
                    Oh, your original statement is correct. As our product manager Steve
                    confirmed in the article below, the al.exe/ilmerge approaches only work
                    well for purely managed scenarios, but they don't support linking native
                    code. This makes sense, since only the C++/CLI supports the single assembly
                    internal interop. We need the C++/CLI link.exe to get this done.
                    >
                    Steve demonstrated this walkthrough in the article below(which is similar
                    as what you have done):
                    "Linking native C++ into C# applications"

                    lications.aspx
                    >
                    Does this meet your need? If not, please feel free to tell me, thanks.
                    >
                    Best regards,
                    Jeffrey Tan
                    Microsoft Online Community Support
                    =============== =============== ===========
                    Delighting our customers is our #1 priority. We welcome your comments and
                    suggestions about how we can improve the support we provide to you. Please
                    feel free to let my manager know what you think of the level of service
                    provided. You can send feedback directly to my manager at:
                    msdnmg@microsof t.com.
                    >
                    This posting is provided "AS IS" with no warranties, and confers no rights.
                    >
                    >

                    Comment

                    • =?Utf-8?B?aWduaGVucnk=?=

                      #11
                      RE: Linking Mixed Mode and Managed Assemblies

                      Hi Jeffrey

                      Sorry for late reply.

                      Steve's article indeed helped me solve the problem.

                      Thanks!

                      ""Jeffrey Tan[MSFT]"" wrote:
                      Hi Henry,
                      >
                      Thanks for your feedback.
                      >
                      Oh, your original statement is correct. As our product manager Steve
                      confirmed in the article below, the al.exe/ilmerge approaches only work
                      well for purely managed scenarios, but they don't support linking native
                      code. This makes sense, since only the C++/CLI supports the single assembly
                      internal interop. We need the C++/CLI link.exe to get this done.
                      >
                      Steve demonstrated this walkthrough in the article below(which is similar
                      as what you have done):
                      "Linking native C++ into C# applications"

                      lications.aspx
                      >
                      Does this meet your need? If not, please feel free to tell me, thanks.
                      >
                      Best regards,
                      Jeffrey Tan
                      Microsoft Online Community Support
                      =============== =============== ===========
                      Delighting our customers is our #1 priority. We welcome your comments and
                      suggestions about how we can improve the support we provide to you. Please
                      feel free to let my manager know what you think of the level of service
                      provided. You can send feedback directly to my manager at:
                      msdnmg@microsof t.com.
                      >
                      This posting is provided "AS IS" with no warranties, and confers no rights.
                      >
                      >

                      Comment

                      • Jeffrey Tan[MSFT]

                        #12
                        RE: Linking Mixed Mode and Managed Assemblies

                        Hi Henry,

                        Thank you for the confirmation.

                        Ok, if you need any further help, please feel free to post, thanks.

                        Best regards,
                        Jeffrey Tan
                        Microsoft Online Community Support
                        =============== =============== ===========
                        Delighting our customers is our #1 priority. We welcome your comments and
                        suggestions about how we can improve the support we provide to you. Please
                        feel free to let my manager know what you think of the level of service
                        provided. You can send feedback directly to my manager at:
                        msdnmg@microsof t.com.

                        This posting is provided "AS IS" with no warranties, and confers no rights.

                        Comment

                        Working...