LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to determine memory address of an array

Hi,

I need to determine the memory address of an array allocated in LabVIEW.

I'm calling a DLL that requires a pre-allocated buffer - an array of U8.  The DLL populates this buffer with a serialized linked-list of structures, that is:

The first four bytes of returned buffer represent the first DWORD of the first structure, this is a pointer to the first byte of the next structure stored in the buffer.

 

The pointers are absolute memory addresses.  Knowing the memory address of the array allows conversion of the pointers into array indexes.

 

I've seen this thread, but don't understand how to use the memory manager functions to produce an array for which I know the absolute memory address.

 

Any help is appreciated!

0 Kudos
Message 1 of 18
(3,493 Views)

I can probably help, but it would be easier to do with real code. Can you post the structure, the function prototype of the call that you're making, example code (in C or similar, if you have it) or documentation, and any LabVIEW code that you've tried so far?

 

I'm not clear about whether you have a true linked list, or just an array, and how much of it you need to allocate in LabVIEW, which is why I'm hoping you can post the actual structure.

 

When you use DSNewPtr to allocate memory, the return value will be a pointer to that memory, ie its address in memory. Are you just trying to allocate memory to pass to a function, or are you also trying to figure out how to follow a pointer within in an array or struct after the function returns?

Message 2 of 18
(3,484 Views)

Hi nathand,

Thanks for the help!

Something is preventing "Add Attachment" from working (will keep trying).

Am making calls into the WinAPI function GetAdaptersAddresses described here.

It's really a linked list of variable-sized structures.  Each structure has two sections:

 - the fixed-size IP_ADAPTER_ADDRESSES stru (prototype as diagram text)

 - followed by a variable amount of address data

 

Cheers. 

 

 

0 Kudos
Message 3 of 18
(3,474 Views)

I remember seeing a post recently about issues adding attachments with certain web browsers, maybe try a different browser?

 

Looking at the example code for GetAdaptersAddresses function, you can duplicate the memory allocation part easily using DSNewPtr. The more challenging part is extracting the returned data. It's particularly weird because it appears that it shoves a linked list into contiguous memory, in which case it would be much easier to handle as an array, but I guess that's just how it is with Windows sometimes.

 

Here's a thread that shows how you can pass a pointer as part of a structure, and retrieve the data from that pointer. Not an exact fit for your application, but enough to get you started, I hope. If you need further clarification, or you think you have your code right but it crashes or returns an error or bad data, please let me know.

http://forums.ni.com/t5/LabVIEW/array-pointer-from-dll/td-p/1214107 Scroll down to message 15 for an image of LabVIEW code that's almost right (and then the further comments below that explain how to fix it).

Also see the thread linked from that one that mentions packing, and adding padding to the cluster to get the alignment right. You may need to do something similar.

 

EDIT: by the way, I don't think your description of the data format is quite right. It appears that it's a fixed-size struct that contains a pointer to another struct of the same size, etc; I don't see anywhere that has a variable amount of data other than the varying number of elements in the list.

Message 4 of 18
(3,438 Views)

Yeah - these structures are convoluted.  There's one IP_ADAPTER_ADDRESSES for each adapter, but each adapter may have multiple Unicast addresses, and each address may or may not have a mask, so, the pointer the Unicast address, is a pointer to a linked-list (which is nested in the main linked-list).

 

I'm rewriting my routines to be array-pointer-based instead of array-based, so the DSNewPtr will work for me after-all. Smiley Happy

 

Thanks for the help and all the great posts!

 

Cheers!

0 Kudos
Message 5 of 18
(3,431 Views)

Ahh, I see now. I didn't look that closely at all the structure elements.

 

I think I understand now how you started. Did you initially plan to pass an initialized 15kb (or whatever size) standard LabVIEW array? If you knew the base address of that array, you would be able to "dereference" the pointer within the array by subtracting the base address, which would give you an array index. I can't come up with a quick way to make that work, but you have me curious, I'll have to think about it.

 

Good luck - it looks like it will be a pain to go through all those memory addresses, but you should be able to make it work. Save often. Since you'll be dealing with pointers as numbers, be careful about endianness - if your pointers are byte- and word-swapped, they will likely crash LabVIEW, and they probably will be swapped since LabVIEW is the opposite endianness from the rest of Windows.

0 Kudos
Message 6 of 18
(3,426 Views)

@nathand wrote:

I think I understand now how you started. Did you initially plan to pass an initialized 15kb (or whatever size) standard LabVIEW array? If you knew the base address of that array, you would be able to "dereference" the pointer within the array by subtracting the base address, which would give you an array index.


That's it, exactly,  But rewrite wasn't bad - wish I could have posted the code.  Am using IE8 - maybe there's a properties-tweak.

 

Cheers!

0 Kudos
Message 7 of 18
(3,421 Views)

@550nm wrote:

That's it, exactly,  But rewrite wasn't bad - wish I could have posted the code.  Am using IE8 - maybe there's a properties-tweak.


A bit off-topic, but the issue with uploading attachments in IE8 was reported here: http://forums.ni.com/t5/Feedback-on-NI-Discussion-Forums/Problem-uploading-attachments/m-p/2374380

Looks like turning off compatibility mode makes it possible to upload.

0 Kudos
Message 8 of 18
(3,386 Views)

The most painful part of this, will be that there is no way of making this work for both 32 bit and 64 bit Windows without seperate code for each. This is because LabVIEW does not know a datatype that can be added as cluster element whose size is platform dependent. The easier and more portable approach would be to write an intermediate DLL in C, that translates between a more LabVIEW friendly array of data structures, and the one the WinAPI uses.

Rolf Kalbermatter
My Blog
0 Kudos
Message 9 of 18
(3,365 Views)

@nathand wrote:

@550nm wrote:

That's it, exactly,  But rewrite wasn't bad - wish I could have posted the code.  Am using IE8 - maybe there's a properties-tweak.


A bit off-topic, but the issue with uploading attachments in IE8 was reported here: http://forums.ni.com/t5/Feedback-on-NI-Discussion-Forums/Problem-uploading-attachments/m-p/2374380

Looks like turning off compatibility mode makes it possible to upload.


Thanks nathand! That was it! (...regarding upload problem)

 

Here's some code that seems to work.

 

Cheers!

 

Message 10 of 18
(3,334 Views)