Recently, MATLAB was used in the project. However, due to the need to synthesize it with other projects, Matlab cannot be used directly, so we thought about whether to compile the functions written in Matlab into a DLL, for C # calls, so I checked some information on the Internet to study the mixed programming of MATLAB and C #, and recorded the following:
First, write your own functions in MATLAB, for example, assuming function result = mytest (parama, paramb. after the M file is compiled, enter deploytool In the MATLAB console. In the displayed Configuration window, select. NET component to create a new mytest. prj, and then put your. drag the M file and click build to generate the final DLL.
However, a problem encountered during the process is that C # Always prompts that the typeinitialization exception type initialization setting is abnormal when calling DLL to initialize the class generated by MATLAB. It has been found online for a long time, I tried a variety of solutions and finally found the cause:
It was originally selected during DLL build. the path of the M file cannot contain Chinese characters or spaces. the M file is directly moved to a folder without spaces and Chinese characters in the root directory, and. drag the M file to mytest. in prj, the new DLL generated is re-referenced in build and VS, and it is found thatProgramCorrect.
However, I transplanted the program to another computer. The computer did not install MATLAB, But I installed MATLAB compilor runtime. when I ran the program, I found that another type initialization setting exception occurred, depressed. I continued to search for a solution on the internet, tried various methods, and finally got it done. But I don't know what the solution actually worked, so paste all the methods you have tried:
1. reinstall MCR and select the installation path c: \ MCR (excluding spaces and Chinese characters)
2. Make sure that the environment variable path is correct and add: C: \ MCR \ v713 \ Runtime \ Win32.
3. Run-> regsvr32 mwcomutil. dll
4. re-build. DLL file, select Add MCR in package, set assembly type to shared in setting, encryption Keyfile to null, and then copy the newly generated DLL file to that computer, replace it, And rereference it.
5. Restart or restart?
After the above steps, I find that the program is running correctly, but it is unclear which step is useful at the end :)
In fact, this has been done before, and it is estimated that it has encountered similar problems. After a long time, I forget my lessons and experience, as a result, it took a long time to find information on the Internet. Therefore, record the information so that you will not encounter it again, which is a waste of time.