- Home /
Message Boxes and running .exe files
I'm creating a game and i want to make it crash for plot purposes. I thought that easier than crashing the game is to simulate the crash (closing the game and creating message box that looks like error message). And here is first problem. Windows MessageBox is located in System.Windows.Forms, but I can't add it to script by "using System.Windows.Forms; ". I looked for solution and 99% of them suggested to use unity message boxes or to create my own ones, but I really want it to be Windows MessageBox. So first question: is this possible to add that library somehow? My other idea to solve the problem was to write program that shows MessageBox and use its executable file. Something like that:
Process pr = new Process();
pr.StartInfo.FileName = System.IO.Directory.GetCurrentDirectory() + "/Assets/Executables/CrashMessageBox.exe";
pr.Start();
All is OK as long as i work in editor, but it won't work after I build my game, so here is the second problem. Is there any option to add this .exe file to the project in a way that allows to run it later?
Sorry for my english and thanks for help in advance.
Answer by Zaeran · Oct 12, 2018 at 03:54 PM
You should be able to add the .exe to the StreamingAssets folder to use it at runtime.
Answer by dmr32 · Jun 04, 2019 at 06:03 PM
@AdamBlaszczyk If it's just Windows you're wanting it for, don't forget that you don't have to use System.Windows.Forms - you can use PInvoke to bypass .Net and call the Win32 dlls directly. For example:
#if UNITY_STANDALONE_WIN
using System.Runtime.InteropServices;
public static class Win32MessageBox
{
public enum ButtonID
{
Ok = 1,
Cancel = 2,
Abort = 3,
Retry = 4,
Ignore = 5,
Yes = 6,
No = 7,
TryAgain = 10,
Continue = 11,
}
[System.Flags]
public enum Flags : uint
{
// button selection:
MB_ABORTRETRYIGNORE = 0x00000002, // Abort, Retry, Ignore
MB_CANCELTRYCONTINUE = 0x00000006, // Cancel, Try Again, Continue. Use this message box type instead of MB_ABORTRETRYIGNORE.
MB_HELP = 0x00004000, // Help
MB_OK = 0x00000000, // OK [default]
MB_OKCANCEL = 0x00000001, // OK, Cancel
MB_RETRYCANCEL = 0x00000005, // Retry, Cancel
MB_YESNO = 0x00000004, // Yes, No
MB_YESNOCANCEL = 0x00000003, // Yes, No, Cancel
// icon selection
MB_ICONEXCLAMATION = 0x00000030, // An exclamation-point icon appears in the message box.
MB_ICONWARNING = 0x00000030, // An exclamation-point icon appears in the message box.
MB_ICONINFORMATION = 0x00000040, // An icon consisting of a lowercase letter i in a circle appears in the message box.
MB_ICONASTERISK = 0x00000040, // An icon consisting of a lowercase letter i in a circle appears in the message box.
//MB_ICONQUESTION = 0x00000020, // A question-mark icon appears in the message box. Deprecated
MB_ICONSTOP = 0x00000010, // A stop-sign icon appears in the message box.
MB_ICONERROR = 0x00000010, // A stop-sign icon appears in the message box.
MB_ICONHAND = 0x00000010, // A stop-sign icon appears in the message box.
// default button
MB_DEFBUTTON1 = 0x00000000, // first button [default]
MB_DEFBUTTON2 = 0x00000100, // second button
MB_DEFBUTTON3 = 0x00000200, // third button
MB_DEFBUTTON4 = 0x00000300, // fourth button
// modal control (since hWnd=null, these don't provide much)
MB_APPLMODAL = 0x00000000, // default
MB_SYSTEMMODAL = 0x00001000, // Same as MB_APPLMODAL except that the message box has the WS_EX_TOPMOST style
MB_TASKMODAL = 0x00002000, // Maybe the best option
// other flags:
MB_DEFAULT_DESKTOP_ONLY = 0x00020000,
MB_RIGHT = 0x00080000, // text is right justified
MB_RTLREADING = 0x00100000, // right-to-left on Hebrew/Arabic
MB_SETFOREGROUND = 0x00010000, // message box becomes foreground window
MB_TOPMOST = 0x00040000, // adds WS_EX_TOPMOST window style
MB_SERVICE_NOTIFICATION = 0x00200000,
}
[DllImport("user32")]
private static extern int MessageBoxW([MarshalAs(UnmanagedType.LPWStr)]string dummyHWnd, [MarshalAs(UnmanagedType.LPWStr)]string text, [MarshalAs(UnmanagedType.LPWStr)]string caption, uint type);
public static ButtonID ShowMessage(string text, string caption, Flags flags)
{
return (ButtonID)MessageBoxW(null, text, caption, (uint)flags);
}
}
#endif
A test case:
public void Wibble()
{
var result = Win32MessageBox.ShowMessage(
"Would you like me to open the pod bay doors?",
"HAL 9000",
Win32MessageBox.Flags.MB_ICONSTOP | Win32MessageBox.Flags.MB_YESNO | Win32MessageBox.Flags.MB_DEFBUTTON2
| Win32MessageBox.Flags.MB_TASKMODAL | Win32MessageBox.Flags.MB_SETFOREGROUND);
Debug.LogFormat("Dave says \"{0}, HAL\"", result);
if (result == Win32MessageBox.ButtonID.No)
Application.Quit();
}
Answer by Bunny83 · Jun 04, 2019 at 05:58 PM
There is absolutely no need for an external program. The MessageBox function is part of the win API and is there since i can remember (this goes back to windows 95). The .NET managed class MessageBox in the System.Windows.Forms namespace is essentially just a wrapper around the native API. You can still call the method directly once you added the external declaraion somewhere:
[DllImport("user32.dll", CharSet=CharSet.Auto)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);
Now if you do
MessageBox(IntPtr.Zero, "This application has crashed!!", "Your application title", 0);
You will get a message box which will block your application until the user closes the message box. Instead of "0" you can also pass for example 0x10 as the last option parameter (which is a bit flag) in order to show the red error icon. For more information on the usage of this method, consult the documentation
Your answer
Follow this Question
Related Questions
Unity Build EXE Stuck 0 Answers
Message Box Question(please read) 2 Answers
Creating an .EXE problem. 2 Answers
(Programming languages) Can Javascript/Unityscript be executed as standalone ? 1 Answer