0

I have been beating my head against a brick wall for the last week. That brick wall: CPUID.

Everything I've stumbled across has said to build a class library in C++ implementing CPUID, then consume that in the C# project.

For various reasons, I have been unable to get a C++ class library to work properly in my C# project, and I'm at the point that I am about to give up and tell the client that their request can't be fulfilled. However, I'm at the end of my rope and want to beg for assistance.

Is there ANY way to get __cpuid() or asm to compile in C# without having to build an external assembly?

Skudd
  • 684
  • 2
  • 12
  • 28
  • Hrm, I just found [x86/x64 CPUID in C#](http://stackoverflow.com/questions/3216535/x86-x64-cpuid-in-c-sharp) which may be useful. I'm open to comments and additional ideas though. – Skudd Mar 03 '12 at 04:13

2 Answers2

0

An non-C# assembly is required, since the C# compiler generates pure MSIL and there is no MSIL instruction for CPUID. The question you linked includes CPUID as data, not code, which is quite fragile and you have to call outside .NET to Win32 APIs to mark the data executable, which seems to contradict your desire for "no external assembly".

However, you may be able to compile your C# code as a .netmodule, and use the C++/CLI linker to combine some functions using CPUID with the C# compiler's output. You would end up with a single mixed-mode assembly produced by the C++/CLI toolchain, but most of it would be written in C#.

For this option, the easiest thing would probably be to create a Func<uint> or Func<uint, uint> delegate from the code calling CPUID, and pass that delegate to the C# code, instead of trying to have the C# code call the function by name (this avoid a circular reference between the two projects).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
0

Actually... I've never done it and it's not recommended if you want your code to be portable or even forward compatible with future .NET versions. However:

http://www.atrevido.net/blog/2005/01/28/Inline+X86+ASM+In+C.aspx

This seems like a cool trick. Note that in newer operating systems data is marked as Not-Executable (NX or DEP or whatever MS calls it). An array in C# is data. The last comment on that page gives a way to work around that.

mirh
  • 514
  • 8
  • 14
Jannes
  • 1,784
  • 1
  • 17
  • 20