NI Home > Community > NI Discussion Forums

LabVIEW

Showing results for 
Search instead for 
Do you mean 
Reply
Member
dsavir
Posts: 180
0 Kudos

Re: dll, matlab an LadVIEW

Hi,

I also saw this problem.

My solution was to call the terminate immediately before calling the "exit Labview" function at the end of the application. If you have an application that doesn't close when stopped, maybe you can call the terminate only when the window is closed? And have a boolean to check if the dll was initialized so you don't intialize repeatedly.

I hope this helps,

Danielle

Member
DarkLordOfG
Posts: 137

Re: dll, matlab an LadVIEW

I have beat on this issue for a while and conversed with NI and here is what I found.

THe MATLAB runtime DLL needs to attach to a process.  If you compile an application then the process that the DLL attaches to is the application.  Simply initilize the MATLAB runtime at startup and call the terminate just before you call the exit LabVIEW subVI.  You must call the exit because MATLAB expects that the process will be terminated after it has had it's runtime terminated.

If you want to run the MATLAB DLL in the development environment then the DLL will attach to the LabVIEW process.  This is why it does not work in the debugger.  You can try to control the terminate call all you want it will somehow see that the LabVIEW process is still in memory and the DLL will not unload.  You still can do some testing.  When you run your vi have a boolean control on the front that will have the intitialize for MATLAB called only once.  Then you can call the processing function all you want.  Do not call the terminate.  If you run again, do not recall the initialize routine.  Simply call the processing function since it is still attached to the LabVIEW process.  However, if you screw things up it really screws things up.  The best is to delete the folder that the MATLAB runtime creates, close LabVIEW and try again.  This doesn't always seem to work.  The only way I found to "clear the problem" was to actually run an .EXE that uses the same library.  Have it simply initialize, process, terminate and close labview.

In the end you'll probably be like me and just keep creating EXE's and using indicators and messge boxes to do your debugging.  Hope this helps.  It is hard to explain, but simply understand that the issue is the MATLAB dll does not unattach to the LabVIEW process unless LabVIEW is exited right after the terminate call is made.  Here are some excerpts from my discussion with NI

 

Thanks for the E-mail! I called you earlier today and tried to leave you a

voicemail, but I wanted to make sure that you got the information either

way. What it sounds like to me based on the research that you did about

that specific function you're calling, is that this is the behavior that is

invoked from how the function operates. I have seen cases before where

specific functions are attached to the process until the process is

completely closed, which is what sounds like you found out. Even though it

is LabVIEW that holds on to the resources, it is often because of how the

function implements itself with the process. The reason we tried to

dynamically load and unload the DLL inside of LabVIEW is because this will

detach the DLL from the thread. Unfortunately, it looks like what you are

seeing is that the DLL remains attached to the process, even with the Call

Library Function Node being used like we talked about. When we last spoke,

it sounded like you could get the exe to work if you used the Quit LabVIEW

function since that will completely close LabVIEW. Is this still working

in this way? As far as that particular function goes, this may just be

expected behavior since it is attached to the process, but please let me

know your thoughts on this. You can give me a call at 1-866-275-6964 and

by referencing your service request of 7189310. Thanks and I hope to hear

from you soon!

 

 

Member
mugger
Posts: 60
0 Kudos

Re: dll, matlab an LadVIEW

I had this serious problem by following your guide in that doc file. So I was stuck at the compiling phase in VC++.

My configuration:

Matlab 2009Rb

Microsoft Visual Studio 2010, C++

Labview 2009.

 

In your guide you mentioned:

13 Add in the dllexample source file at the beginning:

#include "mclmcr.h"

#include "libmatrix.h"

14 Right click on ‘dll example files’ -> Add Files to Project -> libmatrix.lib & C:\MATLAB701\extern\lib\win32\microsoft\msvc60\mclmcrrt.lib

15 Settings: Click on the C/C++ Tab -> Code Generation ->In the Use Runtime library listbox, select Multithreaded DLL.

16 Settings. Click on the C/C++ Tab -> Preprocessor. Add to the Additional include directories field: C:\MATLAB701\extern\include + the directory where libmatrix.h is located.

 

So my problem:

1. Cannot pass the compiling, error reported:

>c:\users\arnold ju\desktop\matlabdll\matlabdll\matlabdll\matlabdll.cpp(6): fatal error C1083: Cannot open include file: 'mclmcr.h': No such file or directory

 

2. Cannot find the "Add to the Additional" under the C/C++ Tab as mentioned in step 16 above.

 

3. I can find the file mclmcrrt.lib, but can't find mclmcr.h

 

I enclosed some snapshots for my VC setup.

 

 

Arnold
Member
mugger
Posts: 60
0 Kudos

Re: dll, matlab an LadVIEW

I had this serious problem by following your guide in that doc file. So I was stuck at the compiling phase in VC++.

My configuration:

Matlab 2009Rb

Microsoft Visual Studio 2010, C++

Labview 2009.

 

In your guide you mentioned:

13 Add in the dllexample source file at the beginning:

#include "mclmcr.h"

#include "libmatrix.h"

14 Right click on ‘dll example files’ -> Add Files to Project -> libmatrix.lib & C:\MATLAB701\extern\lib\win32\microsoft\msvc60\mclmcrrt.lib

15 Settings: Click on the C/C++ Tab -> Code Generation ->In the Use Runtime library listbox, select Multithreaded DLL.

16 Settings. Click on the C/C++ Tab -> Preprocessor. Add to the Additional include directories field: C:\MATLAB701\extern\include + the directory where libmatrix.h is located.

 

So my problem:

1. Cannot pass the compiling, error reported:

>c:\users\arnold ju\desktop\matlabdll\matlabdll\matlabdll\matlabdll.cpp(6): fatal error C1083: Cannot open include file: 'mclmcr.h': No such file or directory

 

2. Cannot find the "Add to the Additional" under the C/C++ Tab as mentioned in step 16 above.

 

3. I can find the file mclmcrrt.lib, but can't find mclmcr.h

 

I enclosed some snapshots for my VC setup.

 

 

Arnold
Member
dsavir
Posts: 180
0 Kudos

Re: dll, matlab an LadVIEW

Hi,

 

The document was tested with Matlab 7.1 and Labview 8.2.1, and MSVC6 only. I do not have the programs you have so I cannot test the files you posted, but here are the suggestions I can offer:

1. Find out where mclmcr.h is located in the Matlab directory, and try to add this directory to the build (it could be that solving problem 2 will solve this problem as well, so try solving problem 2 first)

2. In 16: the field is "Additional include directories". I'm sorry for the misunderstanding. I do not know what it is called in MSVC 2010, but there should be something similar in the build process. The purpose of this field is to add directories with additional data. When you find the field, do not erase any folders already there, just add the new ones.

 

Good luck!

Danielle

Member
mugger
Posts: 60
0 Kudos

Re: dll, matlab an LadVIEW

Thanks for your reply. 2010 is different from VC6. But I still find the Additional include field under the Project -> Properties -> C/C++ -> General tab.  

 

So far, the include part is working. I got the new errors in my VC++2010

1>d:\program files\microsoft visual studio 10.0\vc\include\yvals.h(576): error C2371: 'char16_t' : redefinition; different basic types
1>          d:\program files\matlab\r2009b\extern\include\matrix.h(332) : see declaration of 'char16_t'

 

I don't know what's wrong.

Arnold
Member
dsavir
Posts: 180
0 Kudos

Re: dll, matlab an LadVIEW

Hi,

 

The problem seems to be that both matlab and MCS10 define a type called char16_t. Try not including  d:\program files\matlab\r2009b\extern\include and see what happens. Alternatively, try to open matrix.h and comment out the definition of char16_t. If you get the same error again with matrix.h, try to uninclude matrix.h by moving it to another folder and see what happens.

 

Good luck!

Danielle

Member
mugger
Posts: 60
0 Kudos

Re: dll, matlab an LadVIEW

Hey, some updates:

 

1. I think I have to add the directory:   d:\program files\matlab\r2009b\extern\include

Otherwise the debugger gives me

c:\users\arnold ju\desktop\matlabdll\matlabdll\matlabdll\matlabdll.cpp(6): fatal error C1083: Cannot open include file: 'mclmcr.h': No such file or directory.

 

2. I comment out the "char16_" in matrix.h, then a lot of errors showing lots of undefined variable type related to the char16_, so I think I also have to keep the typedef for char16_

 

3. I went to the Matlab forum and found following solutions, but I may need your help

http://www.mathworks.com/matlabcentral/newsreader/view_thread/281754

Let me cite their discussion to you:

 

I figured out how to get rid of the problem by using namespaces.
I placed #include <matrix.h> inside its own namespace as below:
namespace mat { #include <matrix.h>}


The reason you are getting a conflict is because, as Fifo found out, the matrix.h redefines the function that is in yvals.h.
What Fifo suggests is one way to do it.
However, I always hesitate to edit the .h files provide by someone else. Simply put, you could end up destroying the interface functionality they are providing you and therefore not a good programming practice.

Instead, my encapsulating the matrix.h into its own namespace you avoid conflict:

namespace mat {#include <matrix.h>}

What this means is that you will have to address the variables/functions within matrix.h by adding the "mat::" prefix to them. For instance, instead of being able to call

      mxArray x;
you will have to use
      mat::mxArray x;
where "mat" is the namespace within which you included "matrix.h"
Cheers,
-Venn

 

Considering my problem, I'd like to use this namespsace, but I really dont know where to put them. If you take a look at my project setup as enclosed in the figure, by following your guide, I don't directly include "matrix.h", I think the file "mclmcr.h" include the "matrix.h" I tried to only use this namespace method in matrix.h, but I still got the same problem when I build the whole project. Do you know where to put this namespace in terms of my matrixadd project?

 

4. Another question for your guide, if I have multiple .m files, one of them calls the other two. Is there a way to put this together in a dll with your method? Any suggestions?

 

 

Arnold
Member
mugger
Posts: 60
0 Kudos

Re: dll, matlab an LadVIEW

More updates, I finally figured out the compatibility issue between Matlab 2009Rb and VS 2008 (Actually 2010 is not compatible with 2009Rb yet, I have to downgrade). Anyway I move to the phase of implementing my dll to labview. However I was stuck again.

 

My built dll file is called matlabDll2.dll, I enclosed my VS2008 project so you might help me if possible. I continue to follow your guide, but it seems the my Labview 2009 can't find some .lib files. The error from Labview is (Check the attached snapshot):

The program can't start because libmyadd2.dll is missing from your computer. Try reinstalling the program to fix this problem.

When I finished my mcc build in Matlab 2009, it gave out the following files:

libmyhadd2.dll

libmyhadd2.lib

libmyhadd2.exports

libmyhadd2.exp

libmyhadd2.ctf

libmyhadd2_mcc_component_data.c

libmyhadd2.cpp

libmyhadd2.h

Since I'm gonna compile the dll in C++ environment, so I copied 3 files: libmyhadd2.lib, libmyhadd2.dll, libmyhadd2.h to my VC project folder, however is parallel to the Debug folder.

Then I suspect it might be the path problem. So I copied the libmyhadd2.lib to Debug folder, so that the lib file is under the same folder with my output dll file, matlabDll2.dll.

OK, I configure the call function library again in labview, similar errors pop up,but not for libmyhadd2.dll file this time, instead it says:

The program can't start because mclmcr.dll is missing from your computer. Try reinstalling the program to fix this problem.

 

So I think there must be something I missed so that the labview can't find the link to other files. Any suggestion at this moment?

Arnold
Member
dsavir
Posts: 180
0 Kudos

Re: dll, matlab an LadVIEW

Hi,

 

The mclmcrrt error  means that matlab runtime is either not installed on your computer or the paths are incorrect. Matlab component runtime can be found on the computer you have matlab installed according to explanations in http://www.mathworks.com/support/solutions/en/data/1-5RPSPI/?solution=1-5RPSPI. Alternatively, you should check your PATH environment variable and make sure that the folder where all the dlls are is in the path. You can get more information from http://www.mathworks.com/help/toolbox/compiler/brtm1xm-8.html.

 

Good luck,

Danielle