LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Matlab Dll Initialize and Terminate

I'm interested in example code because LabVIEW doesn't seem to do a very good job handling the underlying dlls.
0 Kudos
Message 11 of 27
(2,405 Views)

Well I found som C++ code:

I define the functions of the subDLL to load in global.h:

typedef int  (*tsubDllGetStatus)   ();
typedef int   (*tsubDllGetErrorMsg)  (LPCSTR,int);
typedef int   (*tsubDllInit)      (LPCSTR,int,int);

In subDllLoader.h:

#pragma once

#include "globals.h"

extern tsubDLLInit       subDllInit;
extern tsubDLLGetStatus     subDllGetStatus;
extern tsubDLLGetErrorMsg      subDllGetErrorMsg;

void UnLoadsubDll();
BOOL LoadsubDll();
BOOL IsLoadedsubDll();

In subDLLLoader.cpp

tsubDLLGetStatus     subDLLGetStatus;
tsubDLLGetErrorMsg    subDLLGetErrorMsg;
tsubDLLInit       subDLLInit;

HINSTANCE hinstsubDLL = 0;
char szsubDLLLibName[80];

BOOL IsLoadedsubDll()
{
 return (hinstsubDLL!=0);
}

void UnLoadsubDll()
{
  if (hinstRPC)
 {
  FreeLibrary(hinstsubDLL);
  hinstsubDLL = 0;
  subDLLInit       = NULL;
  subDLLGetErrorMsg   = NULL;
  subDLLGetStatus     = NULL;
 };
}

void ErrorReport(const char* name,const char* what)
{
 char szTemp[300];
 _snprintf(szTemp,sizeof(szTemp)-1,"Error: %s, %s",what,name);
  MessageBox(0,szTemp,szsubDLLLibName,MB_ICONEXCLAMATION | MB_OK);
}

HINSTANCE LoadLib(const char* name)
{
  HINSTANCE h = LoadLibrary(name);
  if (!h)
 {
  ErrorReport("Unable to load library","");
 };
 return h;
};

FARPROC GetProc(const char* name)
{
 FARPROC fn = GetProcAddress(hinstsubDLL,name);
  if (!fn) ErrorReport("GetProcAddress",name);
 return fn;
};

#pragma warning(disable: 4706) //warning C4706: assignment within conditional expression

BOOL LoadsubDll()
{

 UnLoadsubDll();

  strncpy(szsubDLLLibName,sizeof(szsubDllLibName),"theDlltoLoad.dll");
  
// try to load the sub dll and setup function handles

 if (!(hinstsubDLL  = LoadLib(szsubDllLibName))) return FALSE;
 TRACE ("Loaded %s!\n",szsubDllLibName);

  if (!(subDllInit       = (tsubDllInit)    GetProc("Init"))) return FALSE;
  if (!(subDllGetErrorMsg   = (tsubDllGetErrorMsg) GetProc("GetErrorMsg"))) return FALSE;
  if (!(subDllGetStatus     = (tsubDllGetStatus) GetProc("GetStatus"))) return FALSE;
  return TRUE;
}

then in your DLL you do:

if (!LoadsubDLL()) .... do error processing

int initstatus = subDllInit("string",0,0);

int status = subDllGetStatus();

....

unLoadsubDLL();

 

you can also use this with diffrent sub dlls, that may not contain all the functions, in that case you just test the function pointer  before use:
if (subDllGetStatus)
  status = subDllGetStatus;
else
  status = what ever ... // this loaded dll does not support this function.
 
 
Hope it make some sence!
 
 
0 Kudos
Message 12 of 27
(2,397 Views)

Hi,

I have the same problam with matlab dll's in labview. It runs once and the second time it crashes. I'm trying to use the c code in the newsgroup, but I get compilation errors like "undefined variable hintRPC" and a few other variables. Is there an include that I haven't written? Or is there some other reason for this problem? I'm using matlab 7, MSVC 6, labview 7.

Is there another way for labview to work with matlab dll's twice?

Thanks!

"Wisdom comes from experience. Experience is often a result of lack of wisdom.”
― Terry Pratchett
0 Kudos
Message 13 of 27
(2,332 Views)
Sorry the hinstRPC, should be hinstsubDLL


0 Kudos
Message 14 of 27
(2,266 Views)

I am fighting the same issue with the intialize and terminate.  Did you ever get it to work?

 

John

0 Kudos
Message 15 of 27
(1,965 Views)

Hi John,

Using the MathScript Node may be another option for you.  More information can be found here as well. 

Jennifer R.
National Instruments
Applications Engineer
0 Kudos
Message 16 of 27
(1,948 Views)
Thanks Jennifer but the Matchscript node does not have all the functions I need.
0 Kudos
Message 17 of 27
(1,932 Views)
Hi John,
 
In that case, there is also the MATLAB® Script Node which uses ActiveX. 
Jennifer R.
National Instruments
Applications Engineer
0 Kudos
Message 18 of 27
(1,913 Views)

Thanks, but the machine that it will be deployed on will not have MATLAB.

 

There is some hope with dynamically loading the dll using an empty path to the call library function vi.

0 Kudos
Message 19 of 27
(1,900 Views)

Hi,

My solution was to split the function into three: "init", "runFunction", and "terminate". At the beginning of the program I run "init"; when the user quits I run "terminate". During the program "runFunction" is executed every time user presses the doFunction button.

Good luck!

Danielle

"Wisdom comes from experience. Experience is often a result of lack of wisdom.”
― Terry Pratchett
0 Kudos
Message 20 of 27
(1,886 Views)