Hide ctfserver popup window when using OutOfProcessAttribute with MCR .NET Data API?

3 views (last 30 days)
I'm working on a .NET 6 application using the newly-released .NET Data API from R2022b to call Matlab functions contained in a ctf archive. I'd like to run Matlab out-of-process, and the OutOfProcessAttribute provided by the Matlab API appears to work beautifully for this purpose, except it pops up an empty command prompt window for each instance of ctfserver.exe that gets launched that lasts until the Matlab process is terminated. Is there a way to run the Matlab runtime out-of-process without displaying this window?
Thanks.
  1 Comment
Claudio Alvarez
Claudio Alvarez on 2 May 2023
I have the same problem but with the C++ Data API ctf archive. When I run the my application using the OUT_OF_PROCESS flag the ctfserver.exe runs in a command window and the app consume 50 MB, and the app runs without any problem. When I use the IN_PROCESS flag the app consum 500 MB and crash. I still can't figure out how to hide the ctfserver.exe window.

Sign in to comment.

Answers (2)

Claudio Alvarez
Claudio Alvarez on 3 May 2023
Edited: Claudio Alvarez on 3 May 2023
This is what I did to remove the ctfserver.exe command window.
I am writing an application in Windows 10 using .NET 6 (the .exe) and MATLAB C++ Data API (the CTF is loaded in a native DLL) and a C++/CLI wrapper (another DLL) to connect the MATLAB DLL to .NET 6. Doing this allows me to work with the MATLAB arrays directly in C++ with zero copy, specially with complex number. Matlab uses std::complex<double>, so it is very convinient.
Now I was able to run matab using the IN_PROCESS flags. I disable every UI elements with the next options
std::vector<std::u16string> options = {u"-noFigureWindows", u"-nosplash", u"-nodesktop", u"-nodisplay"};
auto mode = MATLABApplicationMode::IN_PROCESS;
Since it worked, I stated to remove options until I found you only need the -nodisplay one
std::vector<std::u16string> options = {u"-nodisplay"};
auto mode = MATLABApplicationMode::IN_PROCESS;
So what I do is in debug mode I compile with the OUT_OF_PROCESS flag to avoid the lag when loading all the matlab DLL symbols (it can take forever). In release mode I switch to IN_PROCESS.
My guess is that some display or UI element are not compatible with the manage .NET 6 and it crash.

Shaojun Xiao
Shaojun Xiao on 20 Jun 2024
Edited: Shaojun Xiao on 20 Jun 2024
The following work around from AK of the MathWorks Support:
[DllImport("kernel32")]
static extern bool AllocConsole();
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
//in the initialisation function
if (GetConsoleWindow() == IntPtr.Zero)
{
AllocConsole();
}
var consoleWindowHandle = GetConsoleWindow();
// Hide the console window
if (consoleWindowHandle != IntPtr.Zero)
ShowWindow(consoleWindowHandle, SW_HIDE);
...
var _matlabRuntime = MATLABRuntime.StartMATLAB(ctfPath);
...
// To show the console window again (if needed)
//ShowWindow(consoleWindowHandle, SW_SHOW);

Categories

Find more on C Shared Library Integration in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!