LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Import C code at run-time

I'd like to import and a execute functions in a C file at run time.  I used this feature in a previous version of CVI (about 7 years ago) in conjunction with the old version of Test Stand, but I can't figure out how to do this in CVI v8.  I've looked at the help for LoadExternalModule which talks about loading a C file, but it sounds like you have to have the module written and included in your project file (which seems to me to defeat the purpose).
 
Specifically I need to provide a way for a user of my program to customize a function that converts a thermistor resistance into an A-to-D count based on a specific schematic circuit.  For example, if the thermistor is in a simple voltage divider with a 10k pull-up and the A-to-D reference voltage is Vcc, then the function would be:
 
int ConvertResistanceToAtoD(int Resitance, int MaxAtoD)
{
    return MaxAtoD * Resistance / (Resistance + 10000);
}
 
The function prototype will always be the same, but the user needs to be able to specifcy the equation for their particular application. 
 
I would also appreciate any other suggestions for how to customize this equation at run time (short of including a full mathematical expression parser!).
0 Kudos
Message 1 of 3
(3,155 Views)
Hi TonyG 614,

You are correct, the function you were using before is LoadExternalModuleEx; however, this function has been made obsolete and National Instruments recommends using the Windows SDK functions LoadLibrary and GetProcAddress instead. Furthermore, as you saw in the help section, if you want to load a .c file using LoadExternalModuleEx, you must have that file added to your project at compile time. For anyone interested, more information on this topic can be found in the LabWindows/CVI Programmer Reference Manual, section 7-11 to 7-15.

As a possible solution, you could have your code defined in a DLL instead and load it dynamically at run-time using LoadLibrary. At this point, your user can specify which function they want to call in the DLL, but it would have to be predefined at compile time. In short, there is no way to dynamically compile and run code at run-time outside of programmatically saving a .c file, calling an external compiler, and then loading the DLL it creates.

Regards,
0 Kudos
Message 2 of 3
(3,117 Views)
You could probably work out a way to use cvi through the activex interface or the command line compile interface to compile user code into a dll and then load it at runtime but that seems kind of complex.  If you must have a compiled solution then CVI or a microsoft compatible compiler would need to be on the test system anyway and a simple batch file or other script could be used to compile the necessary dll.
 
An alternative approach is to create a conversion function that takes coefficients you load from an external file.  For example if your conversion is allways a liner relationship then you load the gain and offset from something like an ini file and apply it to y=mx+b where m and b are your user configured values.  This works for any relationship where you can vary the coefficients to gain the desired conversion relationship.
0 Kudos
Message 3 of 3
(3,099 Views)