Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using ILMerge to embedd C++ DLL into C# EXE

Using ILMerge to embedd C++ DLL into C# EXE

Scheduled Pinned Locked Moved C#
csharpc++helptutorial
8 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikester222
    wrote on last edited by
    #1

    Hello, 1) I have a C# console application that currently is compiled and linked to an external C++ DLL file that contains unmanaged C++ code. This compiles and executes fine. 2) I would like to compile the C# application so that the C++ DLL is compiled into a single C# EXE application so that I no longer need to deliver the C++ DLL separately. NOTE: My current files separately compiled files are: C# EXE: POST2STK_Console.exe (size=20 KB) C++ DLL: POST2STK_LIB.dll (size=41 KB) I am trying to merge these together using ILMerge, but I keep running into the following issue: If I compile with the following "Post-build Event Command Line" parameter set in my project's build properties as: ilmerge /target:winexe /out:TEST.exe POST2STK_Console.exe POST2STK_LIB.dll /zeroPeKind I do NOT get any compiler errors and get output of TEST.exe as a 25 KB size file. This makes no sense when the sum of orginal EXE and DLL files noted above = 61 KB. More importantly, if I try to run TEST.exe, I also get a runtime Exception "System.TypeLoadException" has occurred in TEST. Which from what I can tell is a result of the fact that the DLL was actually NOT compiled into the TEST executeable when ILMerge was run, even though there were no errors shown when this was done. Anyone that can offer any assistance on how to get this to work, it would be greatly appreciated. Thanks!

    J M 2 Replies Last reply
    0
    • M mikester222

      Hello, 1) I have a C# console application that currently is compiled and linked to an external C++ DLL file that contains unmanaged C++ code. This compiles and executes fine. 2) I would like to compile the C# application so that the C++ DLL is compiled into a single C# EXE application so that I no longer need to deliver the C++ DLL separately. NOTE: My current files separately compiled files are: C# EXE: POST2STK_Console.exe (size=20 KB) C++ DLL: POST2STK_LIB.dll (size=41 KB) I am trying to merge these together using ILMerge, but I keep running into the following issue: If I compile with the following "Post-build Event Command Line" parameter set in my project's build properties as: ilmerge /target:winexe /out:TEST.exe POST2STK_Console.exe POST2STK_LIB.dll /zeroPeKind I do NOT get any compiler errors and get output of TEST.exe as a 25 KB size file. This makes no sense when the sum of orginal EXE and DLL files noted above = 61 KB. More importantly, if I try to run TEST.exe, I also get a runtime Exception "System.TypeLoadException" has occurred in TEST. Which from what I can tell is a result of the fact that the DLL was actually NOT compiled into the TEST executeable when ILMerge was run, even though there were no errors shown when this was done. Anyone that can offer any assistance on how to get this to work, it would be greatly appreciated. Thanks!

      J Offline
      J Offline
      Joel Lucsy
      wrote on last edited by
      #2

      Quick answer: you can't. ILMerge will only merge .NET assemblies. Long answer: yes or maybe. One method is to embed the dll as a resource and extract and load it at runtime. This method works as I've done it before. Another method is to extract the resource into memory and directly call the C++ functions. I know its possible to do it from unmanaged, but haven't attempted it from managed yet. -- Joel Lucsy

      L 1 Reply Last reply
      0
      • J Joel Lucsy

        Quick answer: you can't. ILMerge will only merge .NET assemblies. Long answer: yes or maybe. One method is to embed the dll as a resource and extract and load it at runtime. This method works as I've done it before. Another method is to extract the resource into memory and directly call the C++ functions. I know its possible to do it from unmanaged, but haven't attempted it from managed yet. -- Joel Lucsy

        L Offline
        L Offline
        leppie
        wrote on last edited by
        #3

        Joel Lucsy wrote:

        One method is to embed the dll as a resource and extract and load it at runtime. This method works as I've done it before.

        There is a catch though! Mixed mode assemblies must be loaded from file, it cant be loaded as a byte[]. ;)

        xacc.ide-0.1.3.2

        A 1 Reply Last reply
        0
        • L leppie

          Joel Lucsy wrote:

          One method is to embed the dll as a resource and extract and load it at runtime. This method works as I've done it before.

          There is a catch though! Mixed mode assemblies must be loaded from file, it cant be loaded as a byte[]. ;)

          xacc.ide-0.1.3.2

          A Offline
          A Offline
          AB7771
          wrote on last edited by
          #4

          Hi, I am a newbie in .Net, can u tell me how to use the ILMerge utility i have downloaded the utility from Microsoft's site. I am using VS2003 Thanks Pramod

          1 Reply Last reply
          0
          • M mikester222

            Hello, 1) I have a C# console application that currently is compiled and linked to an external C++ DLL file that contains unmanaged C++ code. This compiles and executes fine. 2) I would like to compile the C# application so that the C++ DLL is compiled into a single C# EXE application so that I no longer need to deliver the C++ DLL separately. NOTE: My current files separately compiled files are: C# EXE: POST2STK_Console.exe (size=20 KB) C++ DLL: POST2STK_LIB.dll (size=41 KB) I am trying to merge these together using ILMerge, but I keep running into the following issue: If I compile with the following "Post-build Event Command Line" parameter set in my project's build properties as: ilmerge /target:winexe /out:TEST.exe POST2STK_Console.exe POST2STK_LIB.dll /zeroPeKind I do NOT get any compiler errors and get output of TEST.exe as a 25 KB size file. This makes no sense when the sum of orginal EXE and DLL files noted above = 61 KB. More importantly, if I try to run TEST.exe, I also get a runtime Exception "System.TypeLoadException" has occurred in TEST. Which from what I can tell is a result of the fact that the DLL was actually NOT compiled into the TEST executeable when ILMerge was run, even though there were no errors shown when this was done. Anyone that can offer any assistance on how to get this to work, it would be greatly appreciated. Thanks!

            M Offline
            M Offline
            mikester222
            wrote on last edited by
            #5

            Hello Joel / All, Thank you for your responses. My C++ DLL POST2STK_LIB.dll is a .NET Assembly. It was created and compiled in Visual Studio .NET as a C++ project. The only real difference between it and my C# executable is the DLL contains unmanaged C++ code. But it is still a .NET Assy as best as I can tell. So it would seem there should be a way to use ILMerge to create the single executeable. For what it's worth, since my dll contains unmanaged code, you can see in my "call" to run ILMerge, I had added the /zeroPeKind switch as my understanding is this is required to attempt to merge the unmanaged code DLL into the executeable. And, if this switch is not used, you get the following compiler error during the ILMerge process stating: "Performing Post-Build Event... An exception occurred during merging: ILMerge.Merge: The assembly 'POST2STK_LIB' is not marked as containing only managed code. (Consider using the /zeroPeKind option -- but read the documentation first!)" Thanks again. Mike

            L U 2 Replies Last reply
            0
            • M mikester222

              Hello Joel / All, Thank you for your responses. My C++ DLL POST2STK_LIB.dll is a .NET Assembly. It was created and compiled in Visual Studio .NET as a C++ project. The only real difference between it and my C# executable is the DLL contains unmanaged C++ code. But it is still a .NET Assy as best as I can tell. So it would seem there should be a way to use ILMerge to create the single executeable. For what it's worth, since my dll contains unmanaged code, you can see in my "call" to run ILMerge, I had added the /zeroPeKind switch as my understanding is this is required to attempt to merge the unmanaged code DLL into the executeable. And, if this switch is not used, you get the following compiler error during the ILMerge process stating: "Performing Post-Build Event... An exception occurred during merging: ILMerge.Merge: The assembly 'POST2STK_LIB' is not marked as containing only managed code. (Consider using the /zeroPeKind option -- but read the documentation first!)" Thanks again. Mike

              L Offline
              L Offline
              leppie
              wrote on last edited by
              #6

              mikester222 wrote:

              For what it's worth, since my dll contains unmanaged code, you can see in my "call" to run ILMerge, I had added the /zeroPeKind switch as my understanding is this is required to attempt to merge the unmanaged code DLL into the executeable.

              And did it work? AFAIK there is now way to do this, if you however find an answer, be kind enough to share :) Thanks

              xacc.ide-0.1.3.2

              M 1 Reply Last reply
              0
              • L leppie

                mikester222 wrote:

                For what it's worth, since my dll contains unmanaged code, you can see in my "call" to run ILMerge, I had added the /zeroPeKind switch as my understanding is this is required to attempt to merge the unmanaged code DLL into the executeable.

                And did it work? AFAIK there is now way to do this, if you however find an answer, be kind enough to share :) Thanks

                xacc.ide-0.1.3.2

                M Offline
                M Offline
                mikester222
                wrote on last edited by
                #7

                Hi Leppie, It compiles and ILMerge "works" in so much as it does not give any errors during compile and build of the merged EXE. BUT... what does not "work" is the actual EXE created does not contain the DLL's code embedded within it as described in my original post above. As you say, there may be no way to do this, that is what I'm trying to understand. Thx. Mike

                1 Reply Last reply
                0
                • M mikester222

                  Hello Joel / All, Thank you for your responses. My C++ DLL POST2STK_LIB.dll is a .NET Assembly. It was created and compiled in Visual Studio .NET as a C++ project. The only real difference between it and my C# executable is the DLL contains unmanaged C++ code. But it is still a .NET Assy as best as I can tell. So it would seem there should be a way to use ILMerge to create the single executeable. For what it's worth, since my dll contains unmanaged code, you can see in my "call" to run ILMerge, I had added the /zeroPeKind switch as my understanding is this is required to attempt to merge the unmanaged code DLL into the executeable. And, if this switch is not used, you get the following compiler error during the ILMerge process stating: "Performing Post-Build Event... An exception occurred during merging: ILMerge.Merge: The assembly 'POST2STK_LIB' is not marked as containing only managed code. (Consider using the /zeroPeKind option -- but read the documentation first!)" Thanks again. Mike

                  U Offline
                  U Offline
                  User 11642949
                  wrote on last edited by
                  #8

                  To "ilmerge" unmanaged DLLs consider bxilmerge, it's not github[

                  https://github.com/boxedapp/bxilmerge

                  ](https://github.com/boxedapp/bxilmerge)

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups