How can I show a splash screen when the MCR is loading in "Windows Standalone Application" mode?

9 views (last 30 days)
A stand-alone application takes a while to load because of the MCR. The users of my stand-alone application sometimes start it multiple times since they don't see any feedback that the program is loading. It would be great to have the ability to show an optional splash screen or some kind of feedback to the user.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 3 Oct 2023
Edited: MathWorks Support Team on 4 Oct 2023
As of MATLAB R2014a, if you are generated a Windows Standalone Executable through the Application Compiler, then the splash screen will show up automatically on launch.
However, the ability to show a splash screen or some other kind of user feedback when the MCR is loading is not available for a Windows console application generated by MATLAB Compiler.
A console application is one that opens a Windows command shell (console) for execution, which is an additional runtime setting available to Application Compiler users. When using "mcc", this console appears by default.
To work around this issue for console applications, there are two options:
A. 
Verified in MATLAB 2023a 
- Multi EXE wrapper
This workaround involves separating the splash screen functionality into its own application, and then executing the splash screen app and your main app together by using a "wrapper" script.
The "wrapper" script (i.e. "wrapper.m") would contain the following MATLAB code:
% Use '&' Operator after calling startupScreen.exe so that your app is launched
% immediately after this line, instead of after startupScreen.exe terminates.
!startupScreen.exe &
% Call your application with some extra argument to indicate you are launching the app in
% “wrapper mode”. The main app "myApp" should handle this argument and kill the
% "startupScreen.exe" task
!myApp.exe closeStartupScreen
For a very simple text box splash screen, "startupScreen.m" could include the following MATLAB code:
h = figure('units', 'pixels', 'position', [500 500 200 50], 'windowstyle', 'modal');
uicontrol('style','text','string','Your Message Here!','units','pixels','position',[75 10 50 30]);
This custom splash screen code can be customized however necessary. It is important to note that the splash screen should be computationally light, in order to display quickly.
When the optional "closeStartupScreen" argument is provided to your main application, the first thing for it to do in the entry function is to kill the splash screen application. The following code can be used at the beginning of your main application's source code.
% Check if optional argument to close splash screen was provided.
if strcmp(varargin{1},'closeStartupScreen')
  % If so, kill the task.
!taskkill -f -im startupScreen.exe
end
The executables: "startupScreen.exe", "myApp.exe", and "wrapper.exe" would be generated separately by MATLAB Compiler and should all be packaged together in the same directory so that the "wrapper" can access the other EXE files.
For the console to appear and print output from your main application, the "wrapper" executable must also be a console application. Therefore, do not suppress the Windows command shell display when compiling the "wrapper" script. If using the Application Compiler App, please uncheck "Do not display the Windows Command Shell for execution" under additional runtime settings.
To run your main application with the splash screen, simply execute "wrapper.exe".
If deploying the application to users, hide this complexity by changing the name of your "wrapper" application to the name of your main application. Users should still run the "wrapper" executable in order to see the splash screen before your program.
Note:
 If the MATLAB "wrapper" script causes performance regression in the initialization of your main application, then it is possible to write the "wrapper" as a Windows batch program instead of in MATLAB code. The splash screen functionality can also be written as a compiled C++ or VisualBasic program, or similar. The main approach to the workaround still applies as a "wrapper" to call multiple EXE files.
B. Verified in MATLAB 2013a - MessageBox:
1. You will have to compile your stand-alone without the "deploytool", for example as follows:
mcc -m -v -C -e hello.m
The options:
-m: Generate en executable (.exe)
-v: Verbose output.
-C: Create separate CTF file (necessary since R2008a embeds the CTF file in the exe).
-e: Suppress DOS window (Windows Standalone Application mode).
For more details check the documentation:
doc mcc
2. When the compilation is done, look at the verbose output and try to find and entry that looks like this:
Executing command: mbuild -O -v -output "hello" "hello_main.c" "hello_mcc_component_data.c" -link exeThis is mbuild Copyright 1984-2006 MathWorks, Inc.
You will need this "mbuild" command to recompile the stand-alone after making changes to the C file.
3. Open the main C file with a text editor, in this case it would be "hello_main.c".
4. Look for the main function, which should be at the bottom of the file, and add a call to MessageBox just before mclInitializeApplication, which initializes the MCR. In this case it should look like this:
----------------------------------------
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
#else
int main(int argc, const char **argv)
#endif
{
MessageBox(NULL, "Starting application, please click OK to continue...", "My Standalone", MB_OK);
if (!mclInitializeApplication(
__MCC_hello_component_data.runtime_options,
__MCC_hello_component_data.runtime_option_count))
return 0;
return mclRunMain(run_main, argc, argv);
}
----------------------------------------
5. Save the file and recompile it by using the mbuild command from step 2.:
mbuild -O -v -output "hello" "hello_main.c" "hello_mcc_component_data.c" -link exe
6. To deploy your application, you will need to always distribute the EXE file and the CTF file. This is the way it was done before MATLAB R2008a.
7. The MessageBox creates a "modal" dialog window, this means that the execution will not proceed until you click away the box. That's why you need to explain this to your user. Creating a splash screen or graphical output is fairly complex when working in plain C for Windows. You might be able to find an example on the Internet to help you with this.

More Answers (0)

Categories

Find more on Standalone Applications in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!