In a recent project, I was required to develop a .Net assembly DLL and use it together with COMSOL with Matlab, where Matlab serves as the hub, and the .Net Assembly provides some services, Matlab consumes it, and COMSOL finally perform FEA and output the result:
To start from scratch, I first developed the DLL assembly, and tested it, fine, no error, ok, proceed.
Then I hook the .Net DLL with Matlab, fine, done.
Next when I start COMSOL with Matlab, call the .Net DLL, there are some errors now:
It is apparent that some error occurs during UGNX calls, as the dll ‘libpart’ is required by UGNX.
I then thought of copying the dll to the bin folder of my program, but still no luck.
It is weird since without COMSOL, it works great. This means there is no bug. Why it does not work, when COMSOL joins? Emm, maybe due to x86 or x64 architecture? I am using win7 x64 version, and all the UGNX, Matlab and COMSOL are 64 bits as well. I then tried in Virtual Machine with x86 platform, but still the problem does not disappear!
The I googled, and found that this is usually a DllImport problem, where managed assembly tried to load C DLL, but the dll is not found.
Em, here comes another idea. I copied my DLL to UG_FOLDER\UGII, which corresponds the “UGII_ROOT_DIR” in the system variable. I guess this time, it will be able to find the required DLL then.
Now, it seems to work, as it takes quite some time to run, which I suppose UGNX initialize its required library . ………, but …
Oh my …
Since I am calling from Matlab, how can I get the LoaderExceptions property? And then, it suddenly comes to my mind that some .net assembly are located in “UGII\managed” folder, and I need to copy them to UGII folder as well ?
Haha, it works now!
Analysis: The reason why Matlab calls .net works, while with COMSOL, it fails, is probably as follows:
- In Matlab+ .net, the .net assembly is loaded into Matlab ‘s process, and Matlab knows how to load .net assembly, and possibly by searching the system path; and the actual location of DLL does not matter.
- While for COMSOL + Matlab + UGNX, both Matlab and UGNX assembly are loaded into COMSOL process, where COMSOL has no knowledge about where the UGNX ‘s dlls (both managed and non-managed) are located. If there is a DLL to be loaded, only the folder where the .net assembly resides will be searched.