Use MATLAB Engine application without adding MATLAB to the PATH - matlab

I am working on a MATLAB Engine application. In order for it to work, MATLAB needs to be added to the PATH environment variable so that the Engine application can find certain DLLs (libeng and libmx).
When MATLAB is registered as a COM server, it writes its location into the registry and I can access it. Using this information, is there a simple way to allow the Engine application to work without having to add MATLAB to the PATH? The Engine application could theoretically read the location of these DLLs from the registry, but the problem is that it won't even start up without those DLLs (when compiled the standard way, according to MATLAB Engine compilation instructions).
I am not very familiar with winapi and not at all familiar with COM.
Is there an easy solution to let the problem find its DLLs based on the information from the registry?

I found the following solution:
If we use delayed DLL loading then we get a chance to set the DLL search path before the program attempts to load the DLLs.
To do this, the following needed to be added to the linker options: /DELAYLOAD:libmx.dll /DELAYLOAD:libeng.dll and it's necessary to link against delayimp.lib. After doing this, we can add a call to SetDllDirectory() with the path to the location of the libmx.dll and libeng.dll to the beginning of the main/WinMain function.

Related

Evaluating Environment Variables in VS Code Extension

I'm developing an extension in VS Code to add language support for OpenSCAD (Script-based 3D modeling program). Currently, I have been working on a way to open / preview a file in OpenSCAD from VS Code, which I have been able to do successfully using my own preview manager.
My issue is that I want to add configurable naming formats when exporting an OpenSCAD file that use environment variables similar to those used in the tasks.json file. More info can be found here: https://code.visualstudio.com/docs/editor/variables-reference. As an example, taking the file test.scad and the export configuration ${fileBasenameNoExtension}.stl would export to the file test.stl.
Additionally, I want to add a custom variable, ${#} that would evaluate after all other variables as a unique version number to avoid duplicate exported files. Using the example file: test.scad and the export configuration, ${fileBasenameNoExtension}_${#}.stl, the extension would export to the file test_1.stl for the first time. Then, seeing that test_1.stl exists, it would export to test_2.stl, and so on. I implement similar functionality in all of my exporting utilities, so it is important I can implement it here.
Now that the intro is done, on to the actual question: To anyone who knows more about the VS Code API than I do, in order to best get the functionality described above, should I implement environment variable evaluation into my custom preview manager or reimplement the preview manager I have using tasks? Because I have already implemented my own preview manager that I am happy with, I would prefer to do the former. However, I have been unable to find any functions in the VS Code API that will evaluate the environment variables in a string. Is there a typescript function to evaluate environment variables in the API that I have missed?
If re-implementing this functionality using tasks is a better way to achieve my goal, would I have to sacrifice the control I have in my preview manager, such as being able to selectively kill open previews and dispalying active exports?
Or, is there a compromise that could use all of the power from tasks without losing any functionality I've already developed?
Link to branch of my extension's repository: https://github.com/Antyos/vscode-openscad/tree/PreviewModel

Calling MATLAB from a DLL File

I know this has been posted before but none of the answers work for me.
I'm using VISSIM v10 and made a DLL File to make an external driver model. This is working well.
But what I want is to implement the algorithms in MATLAB instead and make the DLL File send data to Matlab, then Matlab computes the updates the values and sends them back to DLL. The DLL then sends the value back to VISSIM.
So along with .vcproj, .h and .cpp files provided by VISSIM to create the DLL File (im using Microsoft Visual Studio 19), I've added engine.h file in the same project file to be able to use engine routines but it gives 'unresolved external symbol' error for engOpen and engEvalString. I try the same thing in a new .cpp file and it works but how can I link that to VISSIM..
I've added all the libraries and include directories in the properties and configuration type as .dll.
Tettamanti and Varga (2012) mentions that this is the way to be able to do it. But it isn't working for me for now. Help ?
To call MATLAB algorithms from INSIDE your Vissim External Driver Module Dll, you have the following options:
Use MATLAB Compiler SDK for C/C++ - this SDK can help wrapping the MATLAB algorithms into a separate royalty-free DLL, which you can LINK with your Vissim External Driver Module DLL.
This would provide your the most computationally efficient and minimal overhead interoperability with MATLAB runtime.
A good command of C/C++ is a must because you will have to figure out a lot of things yourself;
You would need a MATLAB add-on license for this feature;
You can distribute your MATLAB DLL royalty free (together with MATLAB runtime libs) to a third-party who does not have an MATLAB license;
Use MATLAB directly as a COM automation server inside your Vissim External Driver Module DLL - you would just have to manage MATLAB as out-of-process COM automation server yourself, and do the house-keeping carefully;
This does not require a MATLAB add-on license;
It requires a local, licensed MATLAB installed on the same computer as Vissim;
You need to figure out a lot of things yourself, too.
Use MATLAB External Engine API in C/C++, which is essentially a convoluted wrapper of MATLAB out-process COM object, but in a modern C++ flavor;
This saves you the hassle managing MATLAB COM object yourself, but it demands you, again, to have a mastery of MODERN C++ (i.e., C++11/C++14/C++17) features.
No additional MATLAB add-on license is required;
It requires a local, licensed MATLAB installed in the same computer as Vissim.
I don't see there existing an easy, and straightforward way of integrating Vissim External Driver Module DLL DIRECTLY with MATLAB. All requires non-trivia C++ and COM knowledge and skills.
If you are good with modern C++, I would suggest you go with Option 1 - it provides the best performance.

Why can't I debug into UnityEngine.UI code?

I add the UnityEngine.UI.dll and UnityEditor.UI.dll to my assets folder with their mdb files . also i add the both project to my current project. i am sure all the unity engine ugui code build success, because i debug log in the event system, and it print message. when i want to step into the event system class, i always failed . I find unity will load the code from a build path ,rather than i original code. Why does it do this ? if i want to debug unity engine ugui code, what should i do ?
To debug code, you need two things:
Symbols, the list of all functions, classes, variables used throughout the module. When using C++ symbols are stored in special .pdb files on Windows (and you obviously are using Windows since you are talking about dlls). Symbols in C# (.NET in general) are stored in the .dll itself. Having symbols will let you see the name of functions on the call stack and possibly some variables but nothing more.
Source code of the module.
U3D's source code is proprietary - you need to spend a good amount of money to receive it. And if I am guess to - UnityEngine.UI.dll is a C++ module with stripped (removed) symbols removed.
Thus you have neither, so you can't debug U3D's code at all.
Why would you need to that anyway? If you want to see how the internals of a big game engine work, there are plenty of other options (for example UE4 and Lumberyard). If you are struggling with a problem and you'd like to be able to solve it through debugging...well though luck. Your best bet would be to ask in unity community.

MATLAB compiler - preserving source code

I have a question about Matlab compiler, which I am very new to.
I am trying to compile a large Matlab program to a standalone unix application. It has a number of supporting directories (filled with .m files) which run algorithms on input data. One of the key uses of this program is in allowing users to add their own .m files with their own algorithms for the program to run.
I am able to compile the project and run it as a standalone without issue. But the user can no longer access and add supporting files once it had been compiled.
Has anyone ever come across this before and have any suggestions for dealing with it? I understand that the .m files need to be compiled to .exe and such, but I was hoping Matlab might have a workaround for keeping supporting files in their original state.
This behaviour is by design, and you can't get round it - in fact it's not only technically impossible, it's against the license agreement:
5.2.3. Licensee's Application may not provide scripting capability
similar to the capabilities of any of the Programs or provide
functionality or behavior similar to that of the MATLAB command
line.
Imagine if you created an application that simply accepted user input and passed it into the function eval. If you compiled it, you would then be able to essentially give away free copies of the whole of MATLAB to anyone.

Creating a standalone app from Matlab code

I have some Matlab code and a GUI for it and I want to make a standalone .exe so that it can be used on computers that don't have Matlab installed.
I know about the Matlab compiler and how to use it, but that creates an .exe that only works if the user has the MATLAB Compiler Run-Time (MCR) installed.
What I'm interested in, is if there are any ways to create a standalone .exe that still uses the Matlab code but does not depend on the MCR. I want it to just run when you click it without needing anything else installed.
I know C can make use of Matlab, is there any way to use the code with C? How about any open source Matlab alternatives that can it?
As a last resort, if I were to rewrite the Matlab code in another language, what language would you recommend? I was thinking C or Python.
Thanks.
It's been a while since I looked at the MATLAB compiler but if I remember correctly it used to be able to generate C/C++ code rather than going all-out and generating an executable. The hangup was always in which toolboxes your code used and whether the compiler supported them or not. Any chance you could install the MATLAB application on a server and have a desktop client phone in for results?
As far as other languages go, I'd check out NumPy and SciPy in combination with matplotlib (matplotlib.sf.net). I'm working with a MATLAB developer right now and so far he's pretty pleased with the experience.
The MCR is required, but there's a trick: it doesn't actually have to be installed, just available on the PATH. Running MCR stuff doesn't require any registry entries or anything else special from the installation process. You just need the MCR files readable and the DLLs locatable by the normal lookup mechanism, which includes checking the PATH at the time of program launch.
For example, I've run compiled Matlab apps using an MCR that's "installed" on a network drive. You run the MCR installer on one machine and have it install to network drive X:\Matlab\MCR\R2010b-win32 instead of the usual C:\Program Files location. Then have your program be launched through a wrapper script that adds X:\Matlab\MCR\R2010b-win32\bin\win32 to the PATH before calling your actual .exe file, and it'll run on any other machine that sees the same drive, even if the MCR installer hasn't been run on them. (Eventually we stopped because loading the MCR from the network is a performance hit.)
You could use this trick to bundle the MCR with your application. Stick the whole MCR installation (the dir tree resulting from installation, not the installer program itself) in a subdirectory of your application's directory tree, and have the entry point to your app be a .bat file or other wrapper script that sticks that MCR dir on the PATH before running your MCR-dependent .exe file.
Of course, check with your legal folks to make sure this doesn't violate your licensing terms.
MathWorks recently introduced a new product MATLAB Coder. It is capable of generating C\C++ code from your MATLAB code (generates source code not executables). The generated code is portable and does not depend on the MCR, though it only supports a subset of core MATLAB language features.
I have researched a bit, and I am afraid that your only option is either using the MCR, or converting your code into another language. It makes sense that MathWorks would want you to use only their compiler in order to create .exe files.
Consider another language only if you are already familiar with them, and/or you have enough time for what you are trying to accomplish. Furthermore, if you are using functions in Matlab that you didn't write, then you will have to write those also, which may be very hard/impossible.
If none of the above is a problem, then what language you should use is really up to you. You are not just limited to C and Python. Matlab, im my opinion, is very much like other common languages, at least in terms of syntax. You will have to do some learning either way, so I would suggest that you either go with a language you know, or pick a language for which finding help is easy. C# and Java are two of the more 'famous' languages.
The Matlab 'compiler' is not a compiler. It translates Matlab m-code into C++ code and then sends that to an actual compiler like the one in Visual Studio, etc. The code it makes is not really human-readable.
To make a standalone app, install the following toolboxes (at whatever price you paid):
MATLAB Builder JA, MATLAB Builder NE, MATLAB Compiler
Set up the Matlab compiler by pointing it to the actual compiler on your drive (i.e. mbuild -setup).
Select a compiler:
[1] Microsoft Visual C++ 2008 SP1 in C:\Program Files (x86)\Microsoft Visual Studio 9.0
Then use the (i.e. type deploytool) to make the main and to define various helper functions etc. Sect windows console or standalone. Then click build. It will make the exe-file for you. You can also compile to DLL, etc as needed.
Newer versions of the 'compiler' support compiling of eval, all toolboxes that are licensed, and it supports import of java classes that will allow you to use for example, a DOM for xml-file parsing etc.
To use the resulting exe-file on a computer that does not have Matlab installed on it, you need to obtain the Matlab MCR and install it. The MCR must match exactly the version of Matlab you used to make the compiled code.
Regards,
Ivan