LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to use preprocessor directives (#define) in C++ header file with LabVIEW 8.2

I have a C++ header file that contains around 2000 preprocessor directives:
 
#define MEM_1   0xC
#define MEM_2   0xD
#define MEM_3   0x18
"
"
"
 
I want to be able to "access" these memory offsets by identifier name (MEM_1) in my LabVIEW program like I would in a C++ program.  I do not want the overhead of parsing through the header file and storing all the offsets into an array or similar structure. 
 
I've written a simple Win32 console program to return the memory offset given the identifier (see code below), and created a DLL to use with my LabVIEW program.  In the console program, you notice that I can call a function and pass in the identifer name, and get the offset back correctly:
 
getOffset(MEM_1);
 
In LabVIEW, I was hoping to be able to pass in the identifier (MEM_1) but was unsure what datatype to use.  In my C++ code, I defined the parameter as an int.  But in LabVIEW, I can't enter in MEM_1 as an int.   Can someone advise on how to do this?  Or if there is an alternate way to use #define's from external code inside LabVIEW?
 
---------------------
 
#include "stdafx.h"
#include "scrmem.h"
#include "stdio.h"
void getOffset (int var);
int _tmain(int argc, _TCHAR* argv[])
{
 getOffset(MEM_1);
canf("%d");
 return 0;
}
void getOffset (int var)
{
 printf("The address of MEM_1 is %x", var); 
}
 
 
0 Kudos
Message 1 of 15
(8,612 Views)


@kaycea114 wrote:
I have a C++ header file that contains around 2000 preprocessor directives:
 
#define MEM_1   0xC
#define MEM_2   0xD
#define MEM_3   0x18
"
"
"
 
I want to be able to "access" these memory offsets by identifier name (MEM_1) in my LabVIEW program like I would in a C++ program.  I do not want the overhead of parsing through the header file and storing all the offsets into an array or similar structure. 
 
I've written a simple Win32 console program to return the memory offset given the identifier (see code below), and created a DLL to use with my LabVIEW program.  In the console program, you notice that I can call a function and pass in the identifer name, and get the offset back correctly:
 
getOffset(MEM_1);
 
In LabVIEW, I was hoping to be able to pass in the identifier (MEM_1) but was unsure what datatype to use.  In my C++ code, I defined the parameter as an int.  But in LabVIEW, I can't enter in MEM_1 as an int.   Can someone advise on how to do this?  Or if there is an alternate way to use #define's from external code inside LabVIEW?
 
---------------------
 
#include "stdafx.h"
#include "scrmem.h"
#include "stdio.h"
void getOffset (int var);
int _tmain(int argc, _TCHAR* argv[])
{
 getOffset(MEM_1);
canf("%d");
 return 0;
}
void getOffset (int var)
{
 printf("The address of MEM_1 is %x", var); 
}
 
 


How about a string?

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 2 of 15
(8,588 Views)
Hi,
 
Where do you think I should use the string? 
 
The way that getOffset is currently defined in the DLL, I have to connect an integer input into the LabVIEW function.  This prevents me from entering in: MEM_1 as the input to the LabVIEW function.
 
Are you suggesting that I change getOffset to receive a String parameter ("MEM_1")?  Does that mean I need to do a string compare (line by line) through the header file to get the offset?  It seems like doing this search through the header file would degrade performance, but if that's the only work around, then I'll do it.
 
Please advise.
0 Kudos
Message 3 of 15
(8,581 Views)


@kaycea114 wrote:
Hi,
 
Where do you think I should use the string? 
 
The way that getOffset is currently defined in the DLL, I have to connect an integer input into the LabVIEW function.  This prevents me from entering in: MEM_1 as the input to the LabVIEW function.
 
Are you suggesting that I change getOffset to receive a String parameter ("MEM_1")?  Does that mean I need to do a string compare (line by line) through the header file to get the offset?  It seems like doing this search through the header file would degrade performance, but if that's the only work around, then I'll do it.
 
Please advise.


Well, what you want to do is indeed entering a string and getting back the assigned integer. That is what the C preprocessor is doing too although there it is done only once at the preprocessor stage of course and not at runtime anymore. But LabVIEW is not a C preprocessor.

What you did so far seems to be to define getOffset() that accepts an enum that needs to be created from the C source code to then return the assigned constant. That's of course not very helpful.

And writing a VI that could parse the C header file and create a name/constant array is really a lot easier than doing the same in C. You don't even need to parse the file each time again, but can instead cache them in an uninitialized shift register (LV2 style global).
Even more easy would be to create from that data a ring control using property nodes and save it as custom control and voila you have the most direct lookup you can get in LabVIEW and it works just as comfortable as using the define in C code. It would mean that you need to seperate your header file possibly into several different files to only get related constants into the same ring control, but that is easily done.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 4 of 15
(8,569 Views)
Hi kaycea114,
 
I don't quite get your problem.  I'm not sure if the function you posted is just an analog for what you are actually doing but the "getOffset" function that you posted doesn't actually do anything.  Your "#define" is a preprocessor directive, the compiler finds all instances of MEM_1 in your program and replaces it with "0xC".  So, in reality you are calling "getOffset(0xC)", and your program prints out "The address of MEM_1 is 0xC".  All the function does is spit out the literal int that you are feeding it.  So, calling this function as a dll would not actually do you any good since you need to know the number (0xC) in the first place to call it.
 
This being said, there are several different ways that you can store some pre-defined constants in LabVIEW.  I would suggest parsing through the header file in LabVIEW and storing the variables locally somewhere.  I wrote some code a while ago to do something similar and I have attached it to this post.  As always, this LabVIEW code is provided "as is" without a warranty of any kind 🙂
 
Regards,
Justin D
0 Kudos
Message 5 of 15
(8,569 Views)
"
In LabVIEW, I was hoping to be able to pass in the identifier (MEM_1) but was unsure what datatype to use.  In my C++ code, I defined the parameter as an int.  But in LabVIEW, I can't enter in MEM_1 as an int.   Can someone advise on how to do this?  Or if there is an alternate way to use #define's from external code inside LabVIEW?
"
 
Can you use an enum structure in Labview?  Then typcast the enum into an int (I16) to pass into the C DLL.  Or maybe the typecast is not needed since the enum output is actually a numeric.
- tbob

Inventor of the WORM Global
0 Kudos
Message 6 of 15
(8,564 Views)
Thank you for the wonderful suggestions!
 
I like the idea of storing all the constants into an enum using property nodes.  The sample code was also appreciated!  Thank you to all!
0 Kudos
Message 7 of 15
(8,552 Views)


@tbob wrote:
"
In LabVIEW, I was hoping to be able to pass in the identifier (MEM_1) but was unsure what datatype to use.  In my C++ code, I defined the parameter as an int.  But in LabVIEW, I can't enter in MEM_1 as an int.   Can someone advise on how to do this?  Or if there is an alternate way to use #define's from external code inside LabVIEW?
"
 
Can you use an enum structure in Labview?  Then typcast the enum into an int (I16) to pass into the C DLL.  Or maybe the typecast is not needed since the enum output is actually a numeric.


And using a Ring Control you can have sparse "enums" meaning the actual values need not to be continous. So you can define "enums" that directly contain the string that identifies a constant and the according value that is the constant you want to be returned. Then you have the whole lookup process in the "Ring Control" itself (actually it is not even a lookup anymore, just an index array, so faster than that is not possible, unless you really go to C compile everything in C and have the preprocessor do it all).

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 8 of 15
(8,548 Views)

This is an old thread.  Has there been anything new added to more recent versions of labVIEW do deal with this problem?

 

Since I'm doing a wrapper DLL, I can create a function to accept a string constant matching the constant symbol and return the value, but if there is a more elegant way to do this I would like to use it.

Message Edited by garya505 on 01-28-2010 03:48 PM
0 Kudos
Message 9 of 15
(6,578 Views)

Hey garya505,

 

Are you looking to send an integer to your main DLL but pass in a string from LabVIEW? So you have created a wrapper DLL to take the string from LabVIEW and convert it to the appropriate integer? If this is the case, could you not use an enumerated control to implement this in your LabVIEW code? Or, use similar logic that you have in your wrapper DLL to pass integers to your DLL.

 

It might be benificial to start your own thread and reference this one; as the last post on this thread was a few years ago.

Message Edited by BCho on 01-29-2010 04:33 PM
Hope this helps.
-Ben

WaterlooLabs
0 Kudos
Message 10 of 15
(6,539 Views)