LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to call this C++ function?

enum { nl_max_len = 32, nl_max_declen = 20 };
typedef struct {

char name[nl_max_len];
char sn[nl_max_len];
char ver[nl_max_len];
char cpuID[256];
char info[560];
int reserved;
} NLDeviceInfo;

extern "C" _declspec(dllexport) unsigned nl_GetDevice(NLDeviceInfo devInfos[], unsigned len);

Download All
0 Kudos
Message 1 of 4
(1,232 Views)

By calculating the length of your structure (and accounting for alignment which in this specific case is not an issue since the int at the end already aligns on a multiple of 4 byte address).

Then multiply it with the number of struct elements you want to receive, allocate an array of u8 bytes of that length and pass the number of struct elements to the second parameter.

 

After calling the function call a VI that loops through the byte array and copies the information out into individual bytes or strings to put in a cluster with the correct byte offset. For strings you want to create a small VI that loops through the byte array subset until it encounters a NULL byte and then convert te resulting byte array to a string.

 

Note: It’s not really a C++ function although the array syntax of the first parameter would not compile in old C compilers.

Rolf Kalbermatter
My Blog
0 Kudos
Message 2 of 4
(1,184 Views)

If the is dll is going to write to the memory you pass to it, so too much is better than too little. So if you pass 500 bytes and it keeps crashing, try 5000 bytes. It won't hurt much.

 

If it's going to read the memory\data, it's a bit trickier as it won't work unless the data is correct.

 

Once you get results, figure out exactly how (why) it works. If you don't, it will hunt you in the future... And you learn from it.

0 Kudos
Message 3 of 4
(1,172 Views)

The function in question in this post definitely tries to write into that buffer.

 

But trial and error for something like this is an awfully inefficient way of dealing with it. One of the reason is that not crashing (or causing error 1097) is no guarantee that you got it right. Not every memory corruption causes an access violation or destroys vital data that will lead to a crash, especially not an immediate crash!

 

The other reason is that it is pretty straight forward to calculate the necessary memory in most cases and definitely in this.

 

The structure as presented in this post requires exactly 3 * 32 + 256 + 560 + 4 bytes and no extra padding for alignment.

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 4
(1,156 Views)