DllImport to import Win32 functions

ansalc 436 Reputation points
2020-05-06T16:37:55.417+00:00

Is it possible to use DLLImport to import the Win32 MessageBox function?

using System.Runtime.InteropServices;
[DllImport("user32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)] public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

The following

https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute?view=netcore-3.1

seems to indicate that it is valid for UWP

However, although it compiles in my UWP C# app, it does not run.

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Roy Li - MSFT 32,731 Reputation points Microsoft Vendor
    2020-05-07T02:05:06.433+00:00

    Hello,

    Welcome to Microsoft Q&A!

    Not all win32 functions are available in UWP apps. See Win32 and COM APIs for an overview of Win32 API available to UWP apps. In the Extension APIs for Windows 10 devices part, you could see that only a few functions come from user32.dll could be used in UWP apps and the MessageBox function is not listed.

    Thank you.

    1 person found this answer helpful.
    0 comments No comments