# Calling native binary code from C#

19 replies to this topic

### #1Nick

Senior Member

• Members
• 1227 posts

Posted 13 May 2006 - 09:26 AM

Hi all,

I was wondering whether this C++ code has any C# equivalent:

void main()

{

unsigned char function[] = {0x90,    // NOP (no operation)

0xC3};   // RET (return)

typedef void(*VoidFunction)();

((VoidFunction)&function)();

}


This is the first step towards run-time code generation. I've read about delegates and unsafe pointers, but this would require a real function pointer to native code. Any ideas?

Thanks!

Nick

### #2Axel

Valued Member

• Members
• 119 posts

Posted 13 May 2006 - 09:56 AM

I don't think that's possible. C# is a purely managed language.

If you want to do something like that from C# you likely need a C++/CLI class.

### #3Ed Mack

Senior Member

• Members
• 1239 posts

Posted 13 May 2006 - 11:29 AM

Would something like this not do the job (albeit with a huge overhead compared to c++) scroll a bit down to 'high-level assembler':
http://www.codeproje...egenruntime.asp

### #4kariem2k

Valued Member

• Members
• 207 posts

Posted 13 May 2006 - 11:30 AM

Hi Nick
I don't think that there is inline assembly nor function pointers in c# so this is a solution that came to my mind you can write your unmanged c++ code , assembly or shellcode in unmanaged DLL and call functions in it using Interop Services in C#.
i hope i have helped.

### #5Nick

Senior Member

• Members
• 1227 posts

Posted 14 May 2006 - 12:15 PM

Thanks! Would it be possible to pass a C# memory buffer filled with x86 binary code to a native DLL and execute it there?

### #6dave_

Senior Member

• Members
• 584 posts

Posted 14 May 2006 - 02:23 PM

You can do whatever you want once you're in the DLL

Search for "C# DLLimport"

Of course windows wont let you run arbitary code.
You have to allocate a buffer and change the protection on it.
I cant remember the particular code

### #7kariem2k

Valued Member

• Members
• 207 posts

Posted 14 May 2006 - 02:44 PM

Sure
http://msdn2.microso...y/bd99e6zt.aspx
Also see
http://www.pinvoke.net/ for listing of most Windows API for using with c#'s pinvoke.
and like dave said you should reallocate the buffer in the native DLL and copy the passed contents to that new buffer to use it.

### #8Axel

Valued Member

• Members
• 119 posts

Posted 14 May 2006 - 03:21 PM

Nick said:

Thanks! Would it be possible to pass a C# memory buffer filled with x86 binary code to a native DLL and execute it there?
I would really recommend using C++/CLI for such things. Its much easier to handle. Since .NET allows to mix languages freely you can do that stuff in a C++/CLI class (C++/CLI allows to mix managed and unmanaged code) and use that one in C#.

### #9roel

Senior Member

• Members
• 698 posts

Posted 15 May 2006 - 08:34 AM

Axel said:

I would really recommend using C++/CLI for such things. Its much easier to handle. Since .NET allows to mix languages freely you can do that stuff in a C++/CLI class (C++/CLI allows to mix managed and unmanaged code) and use that one in C#.
Well, I have the feeling that Nick is aiming for a softwire-alike system for c# :) (which isn't a bad idea at all, imho)

### #10Nick

Senior Member

• Members
• 1227 posts

Posted 15 May 2006 - 11:55 AM

roel said:

Well, I have the feeling that Nick is aiming for a softwire-alike system for c# :) (which isn't a bad idea at all, imho)
Exactly. I wish to combine the nice language that is C# with the incredible power offered by soft-wiring technology. C++ is great too but it has clear flaws and stopped evolving...

### #11.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 15 May 2006 - 12:07 PM

C++ hasn't stopped evolving, but besides that, C++/CLI has a lot of new features that can be used on the unmanaged side as well
-
Currently working on: the 3D engine for Tomb Raider.

### #12juhnu

Valued Member

• Members
• 292 posts

Posted 15 May 2006 - 12:33 PM

.oisyn said:

C++ hasn't stopped evolving, but besides that, C++/CLI has a lot of new features that can be used on the unmanaged side as well :)

I would do a thin wrapper with the C++/CLI, which then uses a native dll. C++/CLI is definitely a real improvement compared to the Managed C++, but as it being a new technology there might be some bugs and not sure the quality of the compiled code either. At least the Managed C++ is almost useless for a real production use because of that (like calling destructors twice for an object in some cases...).

### #13Prozac

New Member

• Members
• 15 posts

Posted 16 May 2006 - 06:33 AM

kariem2k said:

Hi Nick
I don't think that there is inline assembly nor function pointers in c# so this is a solution that came to my mind you can write your unmanged c++ code , assembly or shellcode in unmanaged DLL and call functions in it using Interop Services in C#.
i hope i have helped.

C# can use the reflection classes to use MSIL inline generally speaking and
I do believe C# has function pointers but I'm not entirely sure (unsafe code is so much fun!!)

As for the main question, NO you CANNOT use native assembly in managed
languages since the binaries are compiled to a high-level assembler which in
turn are compiled to native assembler by the JIT engine upon execution.

You can probably use C++ with Managed Extensions to use assembler but
for most projects I wouldn't recommend it since you'll be breaking that wonderful
cross-platformness that C# has (assuming you have an environment on that platform such as .NET, ROTOR, DotGnu, or Mono).

### #14kariem2k

Valued Member

• Members
• 207 posts

Posted 16 May 2006 - 07:58 AM

Prozac:
I am afraid you did not understand me.
inline assembly is machine asm not the MSIL that can be written inside your c,c++ code directly.

Quote

As for the main question, NO you CANNOT use native assembly in managed
languages since the binaries are compiled to a high-level assembler which in
turn are compiled to native assembler by the JIT engine upon execution.
You can use(not include) native(unmanged) Dlls in c# just with DllImport attribute you can call any function from any Unmanaged dll,Microsoft has to do that to enable the programmers to use windows API.

Quote

You can probably use C++ with Managed Extensions to use assembler but
for most projects I wouldn't recommend it since you'll be breaking that wonderful
cross-platformness that C# has (assuming you have an environment on that platform such as .NET, ROTOR, DotGnu, or Mono).
I don't think he want to make his app cross-platform because of a simple thing shellcodes are not portable like any low level assembly application,you need much work to make it portable.

### #15Axel

Valued Member

• Members
• 119 posts

Posted 16 May 2006 - 08:28 AM

juhnu said:

I would do a thin wrapper with the C++/CLI, which then uses a native dll.
You don't need the native DLL. C++/CLI allows switching from managed to unmanaged code on the function level (the compiler will do that for you).

### #16juhnu

Valued Member

• Members
• 292 posts

Posted 16 May 2006 - 08:59 AM

Axel said:

You don't need the native DLL. C++/CLI allows switching from managed to unmanaged code on the function level (the compiler will do that for you).

Yeah I know that, but you have to do marshalling between .NET and native C++ types at some point anyway. If you separate your code for different dlls it's pretty easy to know what is actually compiled to IL and what not. Also as the C++/CLI is really new technology it probably has some bugs, which might make a larger use unpractical for the time being - it's predecessor the Managed C++ suffered from this very problem.

### #17.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 16 May 2006 - 09:08 AM

Quote

Also as the C++/CLI is really new technology it probably has some bugs

Well it's not that new, technically it's just managed C++ with a revised syntax. The fundamentals are basically the same.
-
Currently working on: the 3D engine for Tomb Raider.

### #18juhnu

Valued Member

• Members
• 292 posts

Posted 16 May 2006 - 12:33 PM

.oisyn said:

Well it's not that new, technically it's just managed C++ with a revised syntax. The fundamentals are basically the same.

So are the bugs too? ;) Seriously C++/CLI is a language on it's own right and addresses many shortcomings of the Managed C++. You are making it sound
merely like a face-lift.

### #19Nick

Senior Member

• Members
• 1227 posts

Posted 16 May 2006 - 12:58 PM

Axel said:

You don't need the native DLL. C++/CLI allows switching from managed to unmanaged code on the function level (the compiler will do that for you).
Interesting! I haven't looked at C++/CLI yet.

So is C++/CLI undisputably better than unmanaged C++? I mean, could it replace it completely? Does it inherit unmanaged C++'s flaws or is it more like C# with unmanaged support?

Thanks. :happy:

### #20.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 16 May 2006 - 04:07 PM

juhnu said:

So are the bugs too? Seriously C++/CLI is a language on it's own right and addresses many shortcomings of the Managed C++.
Well most of the shortcomings were actually in the syntax itself. And sure it's new, as it also targets .Net 2.0 while Managed C++ only targets 1.1, and it has some more interesting new features. And it's a language on it's own because MS took the liberty to standardize it at ECMA, but that has got nothing to do with the actual implementation of course .

But under the hood, like pinning pointers for example and the rest of the interface with .Net itself, they're very similar.