1

I am developing a .NetCore Console Application that will be used on both Windows and MacOS. I have a requirement that the Console Application should not be visible to the end-user in any way.

I have achieved this on Windows by making use of kernel32 and user32 dll's:

[DllImport("kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

const int SW_HIDE = 0;
const int SW_SHOW = 5;
const int SW_RESTORE = 9;

public static void EnableBackgroundMode()
{
    var handle = GetConsoleWindow();
    ShowWindow(handle, SW_HIDE);
}
public static void DisableBackgroundMode()
{
    var handle = GetConsoleWindow();
    ShowWindow(handle, SW_SHOW);
}

When trying to use these methods on MacOS I get a 'System.DllNotFoundException', which is expected because these dll's are windows specific to my understanding.

Is there any way to achieve this on MacOS?

Ruan
  • 175
  • 1
  • 9
  • 2
    Possible duplicate of [Is it possible to run .NET Core console application silently (hide console window)?](https://stackoverflow.com/questions/38776695/is-it-possible-to-run-net-core-console-application-silently-hide-console-windo) – Franck Apr 12 '19 at 11:53
  • @Franck I do not believe this is a duplicate as this question is only aimed as osx and not Windows. the link provided shows how to solve this solution in Windows and not in Mac(osx). – Ruan Apr 17 '19 at 07:36
  • @Ruan did you find solution? I am looking for the same. – Naina Soni Jul 20 '20 at 07:47
  • @NainaSoni I managed to come up with a solution yes. Solution turned out to be quite some work as I had to create a Cocoa app for Mac separately. This in turn downloads the .Net Core files and launches them in a "hidden" terminal, with an added bonus that you can use the Cocoa app to show a icon in the system try that allows your users some level of interaction. That was the only way I could achieve this. Hope that helps :) – Ruan Jul 21 '20 at 09:21
  • @Ruan Thanks for replying! I have also come to this understanding that maximum I can utilize the C# code is through Xamarin.mac. So I am also writing separate app from scratch with using some C# code from windows app. – Naina Soni Jul 22 '20 at 11:59
  • @NainaSoni, that is the best approach I could find. Keep all your business logic and long running processes in the .NetCore console app (since that can be shared on all platforms) and only let the platform specific app (a.k.a Xamarin.Mac app), run and monitor your background app. This approach worked really well for me – Ruan Jul 24 '20 at 04:56
  • @Ruan but now I am facing another issue, might be IDE issue. But storyboard UI changes are not getting synchronised with Visual Studio Mac. If you have come across with the same problem please suggest some solution if you already know. – Naina Soni Jul 28 '20 at 14:34
  • @NainaSoni I remember I had the same issue. Guessing you are using XCode? My solution was quite simple to just update XCode and Visual Studio Mac to their latest versions and everything started working again – Ruan Aug 04 '20 at 09:32
  • No the updated version of both the tool (XCode11.6 and Visual Studio for Mac2019)has same problem. – Naina Soni Aug 07 '20 at 09:40

0 Answers0