Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 327683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T09:25:29+00:00 2026-05-12T09:25:29+00:00

I have a WinForms Application that I want to run at Mono at some

  • 0

I have a WinForms Application that I want to run at Mono at some point. However, it is using some P/Invoke against user32.dll, which will cause an obvious problems.

However, this is not a critical functionality, just some flavor stuff. So I would like to ignore it when running Mono.

I know I could just use an #if directive, but then I have to offer two different assemblies, which is bad. I know I can check on Runtime if I am running in Mono, but that won’t help me removing the declarations to the functions.

So I wonder: Is there a way where I can say “If running Mono, ignore this completely”? If it helps: The P/Invoke stuff is in a separate .cs file and implemented as a Partial Class.

The source code in question is here: http://pastie.org/588940
It is part of my Main Form, overriding the WndProc message to add an item to the system menu. (Some other stuff snipped). My problem is that while the WndProc stuff is easy, I do not know what to do with the two private extern declarations – can I put them into another (static) class that I just never call on Mono? Or would that be russian roulette?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T09:25:29+00:00Added an answer on May 12, 2026 at 9:25 am

    Why not encapsulate the platform-dependent stuff in an interface, then have some way of getting the “right” implementation for the current platform. Then the calling code can use it blithely, and you can gradually fill in bits to run on Mono as and when you want. I would at least hope that if you never even load the class containing the P/Invoke bits, you should be okay…

    EDIT:

    I don’t see why this approach shouldn’t work, although you may not even need the factory. Here’s what I’d do:

    MainForm.cs:

    PlatformServicesFacade.InitializeSystemMenu();
    

    IPlatformServices.cs:

    public interface IPlatformServices
    {
        void InitializeSystemMenu();
    }
    

    MonoPlatformServices.cs:

    public class MonoPlatformServices : IPlatformServices
    {
        // Prevent early type initialization
        static WindowsPlatformServices() {}
    
        public void InitializeSystemMenu()
        {
            // Maybe log what you would have done?
        }
    }
    

    WindowsPlatformServices.cs:

    public class WindowsPlatformServices : IPlatformServices
    {
        // Prevent early type initialization
        static WindowsPlatformServices() {}
    
        public const Int32 SystemMenuAboutSWikiId = 1000;
        [DllImport("user32.dll")]
        private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
        [DllImport("user32.dll")]
        private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, 
                                              Int32 wFlags, Int32 wIDNewItem,
                                              string lpNewItem);
    
        public void InitializeSystemMenu()
        {
            const Int32 MF_SEPARATOR = 0x800;
            const Int32 MF_BYPOSITION = 0x400;
    
            IntPtr systemMenuPtr = GetSystemMenu(Handle, false);
            InsertMenu(systemMenuPtr, 5, MF_BYPOSITION | MF_SEPARATOR, 0, "");
            InsertMenu(systemMenuPtr, 6, MF_BYPOSITION, SystemMenuAboutSWikiId, 
                       "About SWiki...");
        }
    }
    

    PlatformServicesFacade.cs:

    public class PlatformServicesFacade
    {
        private static readonly IPlatformServices services;
    
        static PlatformServiceFacade()
        {
            services = RunningOnWindows() ? new WindowsPlatformServices()
                : (IPlatformServices) new MonoPlatformServices();
        }
    
        public static void InitializeSystemMenu()
        {
            services.InitializeSystemMenu();
        }
    }
    

    I think that should work… if it doesn’t, please tell us what’s going wrong 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WinForms application that, at some point, will do some calculations that
I have an winforms application that was built using MVC. The controller is subscribing
I have a WinForms application (I'm using VB) that can be minimized to the
I have a winforms application that normally is at about 2-4% CPU. We are
I have C# winforms application that needs to start an external exe from time
I have a winforms application that presently ships with a chm file for context-sensitive
I have a winforms application that is doing the following: on each query: Db
I have a desktop (winforms) application that uses a Firebird database as a data
I have a .net 3.5 WinForms application that runs fine on my machine, but
We have a WinForms application written in C# that uses the AxAcroPDFLib.AxAcroPDF component to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.