tags:

views:

180

answers:

2

So i have this code in a .NET 2.0 console app:

[DllImport("kernel32.dll")]
private static extern bool SetConsoleTextAttribute(IntPtr hConsoleOutput, 
    int wAttributes);

[DllImport("kernel32.dll")]
private  static extern IntPtr GetStdHandle(uint nStdHandle);

private static readonly IntPtr hConsole;

And then in a method i have this:

const uint STD_OUTPUT_HANDLE = 0xfffffff5;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

And when i try to execute this app through Mono on a clean install of Ubuntu 8.10 i get this exception:

Unhandled Exception: System.TypeInitializationException: 
An exception was thrown by the type initializer for 
    IMAPShell.Helpers.ColorConsole ---> 
        System.EntryPointNotFoundException: GetStdHandle
  at (wrapper managed-to-native) IMAPShell.Helpers.ColorConsole:GetStdHandle (uint)
  at IMAPShell.Helpers.ColorConsole..cctor () [0x00000] --- End of inner exception stack trace ---

  at IMAPShell.Program.PrintWelcome () [0x00000] 
  at IMAPShell.Program.Main (System.String[] args) [0x00000]

Basically what i am doing is changing the color of the console output. I wrote this handy method that you can embed color codes into a string of text and it will parse out the codes and change the color of the output until it hits another color code.

It works great on windows, and i would like it to work the same under Mono. Does anyone have any suggestions as to what i can try to get it to work under both OSes?

+1  A: 

Can't you use Console.BackgroundColor and Console.Foreground color instead? This way you won't need to go through the interop layer.

Sean
+1  A: 

Mono implement the .Net framework for you. It doesn't implement windows API. PInvoke is just calls into Win32 API using LoadLibrary and GetProcAddress. You can't expect that PInvoke that calls Win32 API will work on Linux.

Alex Reitbort