0

I am trying to call the WinAPI DialogBox() function in C# (I cannot access the System.Windows libraries to use MessageBox) but am unable to determine the correct types to use for the parameters with the exception of HWND which is IntPtr. MSDN has:

INT_PTR WINAPI DialogBox(
  _In_opt_ HINSTANCE hInstance,
  _In_     LPCTSTR   lpTemplate,
  _In_opt_ HWND      hWndParent,
  _In_opt_ DLGPROC   lpDialogFunc
);

So far, I have:

[DllImport( "user32.dll" )]
static extern IntPtr DialogBox( ? hInstance, string lpTemplate, IntPtr hWnd, ? lpDialogFunc );

Would anyone be able to assist with this? (I thought HINSTANCE may be an int but it can be set to NULL).

Roddy
  • 2,018
  • 3
  • 16
  • 21
  • 2
    Wouldn't you rather solve "cannot access the System.Windows libraries" ? – H H May 21 '18 at 09:04
  • https://stackoverflow.com/questions/11885247/how-do-i-call-the-dialogbox-function-in-the-user32-dll – Steve May 21 '18 at 09:05
  • This is part of Unity, which would entail dragging the correct value of DLLs into the project. – Roddy May 21 '18 at 09:06
  • I read that question and the answers @Steve; it doesn't have the parameters. – Roddy May 21 '18 at 09:06
  • I have just found that there is also a MessageBox() function in the user32.dll, which is still modal and has much simpler parameters, so am going to try that... – Roddy May 21 '18 at 09:08

2 Answers2

0

This was taken directly from the Winforms source a little while ago, I suggest you review said code before using.

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr GetActiveWindow();

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern int MessageBox(HandleRef hWnd, string text, string caption, int type);


     private static DialogResult GetResult(int val)
    {
        switch (val)
        {
            case 1:
                return DialogResult.OK;
            case 2:
                return DialogResult.Cancel;
            case 3:
                return DialogResult.Abort;
            case 4:
                return DialogResult.Retry;
            case 5:
                return DialogResult.Ignore;
            case 6:
                return DialogResult.Yes;
            case 7:
                return DialogResult.No;
            default:
                return DialogResult.No;
        }
    }

   IntPtr handle = GetActiveWindow();
        DialogResult dialogResult = GetResult(MessageBox(new HandleRef((object) this.Handle, handle), "Test", "test", 1));
garty
  • 406
  • 3
  • 11
  • Thanks for that; I was unaware of the HandleRef and haven't seen it anywhere else thus far. I am curious about the this.handle though as it seems to be assuming I am inheriting from a Windows class, which I am not. – Roddy May 21 '18 at 09:46
-1

There is also a MessageBox() function in the user32.dll; the MSDN page is here. The declaration code in C# is:

[DllImport( "user32.dll" )]
static extern int MessageBox( IntPtr hInstance, string lpText, string lpCaption, uint type );

private const uint MB_OK = 0x0;
private const uint MB_ICONASTERISK = 0x00000040;

I hope this helps anyone else who may have come across a similar issue.

Roddy
  • 2,018
  • 3
  • 16
  • 21
  • 1
    This does not answer the question that was asked. It's also a very poor p/invoke declaration since it does not use Unicode text. – David Heffernan May 21 '18 at 09:33
  • Bit rough since it was my own question and the answer was what I used... and I don't actually need to use Unicode text. – Roddy May 21 '18 at 09:45
  • I'm sorry but are you saying I shouldn't answer my own question to the needs that I have? I didn't realise omniscience was a requirement... – Roddy May 21 '18 at 10:07
  • 2
    No. I'm saying that you must answer the question that was asked. And the question asks about calling `DialogBox`. So any answers need to address that. Now, perhaps what you meant to ask was how to show a simple message box. But you didn't ask that and that constrains what answers are possible. – David Heffernan May 21 '18 at 10:10