zweigs Third party library for unmanaged machine code RRS feed

  • Question

  • What third party library can I use to make unmanaged machine code inside

    my c# program that I would call from outside program and which would

    load and initialize CLR common language runtime and call any managed

    procedure from my program.

         I am trying to have unmanaged machine code in my *.exe file which

    would be seen in PE file export table and which I would call from outside

    program to execute any managed code procedure from that unmanaged 

    machine code.

        How to do this?

    • That's not how managed code works. You're trying to apply native import/export logic to managed code and it isn't correct. Managed code can move around so there is no export table where you can auto-magically call managed code from another process. If you need that kind of behavior you're going to have to go back to a native DLL in C++.

      The general approach to writing managed code that can be called by a separate process is to use COM. You could technically use any IPC mechanism but "calling functions" would generally be limited to COM. If you simply want to call managed functions from within native code running in the same process then you can do that via marshalling.

      None of this has any relation to how the PE file is storing stuff or how PE base addresses work. Again, you're apply native code logic to a managed DLL and they don't work the same.

    • A C# DLL never has exports in the PE sense.  Remember that you can't just jump in to C# code arbitrarily.  C# code compiled to an intermediate language that needs to be executed by the Common Language Runtime.  If it were being called by a C++ program, the CLR would not be running, so there's no one that knows how to run the code.

    • There are third-party libraries that can provide exports.  They work by adding new unmanaged functions that load and initialize the CLR and use the CLR facilities to find individual functions within the DLL.

    • What third party library would make unmanaged machine code as exports to load and initialize CLR and call managed code then?

    Wednesday, May 29, 2019 11:49 PM

All replies

  • Greetings.

    I have no idea what the answer to your question is. I'm just posting to ask you to please use meaningful titles for your questions. Something like "Third party library for unmanaged machine code", for example. Using the same meaningless title all the time makes your posts look like spam (especially to someone who can read German), so some people might ignore them instead of trying to answer.

    Thursday, May 30, 2019 12:44 AM
  • The usual approach is to create mixed C++ assemblies to call .NET methods.

    For languages that does not provide .NET support, you can write COM wrapper and let them call with COM interface.

    In both cases you need not worry about the loader for CLR.

    To manually load the library is a bit more complicated than that. While I think there is no official guide for how to do it, you may get some hint on how to load CLR DLLs into windbg (also native process loading CLR scenario). There is more to load than just the clr.dll itself.

    I'm not C++ expert so you will have to do the research yourself if you want to try this path. There's no guarantee for success.

    Thursday, May 30, 2019 1:47 AM
  • Hi

    Based on your description, you want to call unmanaged code from c#.

    I find the following link may be helpful for you.

    Best Regards,


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Thursday, May 30, 2019 7:54 AM
  • This question is multipart one:

    1) His program is written in c#

    2) He want to embed machine code in it (no way to do this in C#, I think it's only possible in C++)

    3) Call managed procedure (i.e.: method) from that embedded machine code.

    Because of 2, I think using mixed mode C++ is the only way.

    For that machine code (which can come from inline asm block or come from static lib of other programming languages) to call managed method, he would need other technique. Either by chaining the code to managed C++ side, or call by COM interface.

    Thursday, May 30, 2019 8:20 AM
  • We keep telling you this isn't realistic. Why are you so set on doing this versus using the standard approaches people have already pointed you to? What problem are you trying to solve that none of the existing solutions are sufficient? Why can't you just use C++ if you need native code so badly?

    Michael Taylor

    Thursday, May 30, 2019 1:41 PM
  • You can create a DLL using C++/CLI and export managed functions which then call other managed functions (like C#  code).  When called from unmanaged code, the CLR will be initialized.  It's all automatic, and it's not even hard.  However, you can't do it with C# alone.

    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Friday, May 31, 2019 6:41 PM