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

3 次查看(过去 30 天)
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 个评论
Claudio Alvarez
Claudio Alvarez 2023-5-2
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.

请先登录,再进行评论。

回答(2 个)

Claudio Alvarez
Claudio Alvarez 2023-5-3
编辑:Claudio Alvarez 2023-5-3
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 2024-6-20
编辑:Shaojun Xiao 2024-6-20
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);

类别

Help CenterFile Exchange 中查找有关 C Shared Library Integration 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by