Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Need help using TD1Hdl in C++

So I've generated a .dll to use in VC++.  After almost 3 full days of searching forums and messing with my code I really need to get something done.  The variables I'm trying to use are declared as follows:

 

typedef struct {
	long dimSize;
	LStrHandle elt[1];
	} TD1;
typedef TD1 **TD1Hdl;

 The function I need to call is:

void __cdecl getFileList(TD1Hdl *SpecResults);

According to LabVIEW SpecResults is an output variable that I should be able to use.  It is an array of file names retrieved from a database.

I've gotten my code to compile many different ways but no matter what I do I can't get to dimSize or elt.

Here's the closest I've gotten:

int main(int argc, char *argv[])
{	
	TD1 *tdptr = new TD1;//(TD1 *)malloc(sizeof(long) + sizeof(LStrHandle));
	TD1Hdl tdhdl = &tdptr;

	SetupWO();
	getFileList(&tdhdl);

	return 0;
}

SetupWO() starts the main vi.  getFileList() is the subvi that i want to get the array from.

Debugging with watches shows nothing in tdptr->dimSize or tdptr->elt[0].

 

Any help you can provide is greatly appreciated.  Thanks in advance.

0 Kudos
Message 1 of 6
(6,627 Views)

obp42,

 

This is some pretty abstract stuff, but lets see what we can do.  It looks like you have things set up properly, but in your third code block, it doesn't look like you are doing anything with the data you return.  Have you tried simply outputting the data using cout or similar?  i think the syntax would be

 

cout << tdptr.dimSize << "  "  << tdptr.elt[0];

 

Try this instead of using debugging with watches let us know how it goes.  Also, if you can, post your dll so we can play with it.

Drew T.
Camber Ridge, LLC.
0 Kudos
Message 2 of 6
(6,617 Views)

Hi Drew!

 

First of all thanks for getting back to me.  After almost a full week of debugging this, I think I've finally found a way to access what I need to.  I've separated the code into classes now so it looks a little different but I'll post the important stuff below.

 

class wrapper
{
public:
	void run();

private:
	void LStrToCStr(LStrHandle lstr, char *cStrBuff, long buffLen);

	TD1 tdptr;
	TD1Hdl tdhdl;
};

 

 

void wrapper::run()
{
	tdhdl = NULL;

	SetupWO();
	getFileList(&tdhdl);

	tdptr.dimSize = (*tdhdl)->dimSize;
	for(int i = 0; i < tdptr.dimSize; i++)
	{
		tdptr.elt[i] = (*tdhdl)->elt[i];

		char atpString[256];
		LStrToCStr(tdptr.elt[i], atpString, 256);
		printf(atpString);
		printf("\n");
	}
}

void wrapper::LStrToCStr(LStrHandle lstr, char* cStrBuff, long buffLen)
{
	int32 len = LHStrLen(lstr);
		if(len >= buffLen)
			len = buffLen - 1;

	memcpy(cStrBuff, LHStrBuf(lstr), len);
	cStrBuff[len] = 0;
}

 

This seems to be working.  I figured it out probably half an hour before I went home on Friday so I haven't had a whole lot of time to mess around with it yet.  The only problem I'm having is that the list I want should have 6 elements in it, but I'm only getting the first 2 when I go through the for loop before it errors.  I'm sure this must be because the array was declared: LStrHandle elt[1];

 

I'm thinking I need to use the NumericArrayResize() function?  I haven't looked into it much yet, but I plan on checking it out today.  However in the mean time if you can provide any more advice that would be great!

 

Thanks again!

 

0 Kudos
Message 3 of 6
(6,609 Views)

obp42,

 

What kind of error is being thrown?  If it has to do with the size of the array, and judging from what you have told me about the array definition, I think using the resize function is a good idea.

 

Have you seen any results using this method?  Let me know!

Drew T.
Camber Ridge, LLC.
0 Kudos
Message 4 of 6
(6,594 Views)

Drew,

 

I've had trouble getting the resize function to work properly, so for the time being I've moved on by "cheating" a little bit and changing the initial size of the array.  It is now declared as:

 

typedef struct {
	long dimSize;
	LStrHandle elt[10];
	} TD1;
typedef TD1 **TD1Hdl;

The data I'm getting should never have more than 10 values in it, so this works for now but I'd really like to get it working the right way.

 

Anyway, the error I'm getting is:

Unhandled exception at 0x0041218c in labview_practice.exe: 0xC0000005: Access violation reading location 0x00000041.

on attempting to print out the third value.

 

Debugging it shows that this happens on the first line of the LStrToCStr() function, probably because the array is out of bounds and it's trying to read a null value.

 

I guess I'm just not exactly sure how or where to use the NumericArrayResize() function (and possibly the DSNewHandle() function?).  Any ideas?

0 Kudos
Message 5 of 6
(6,591 Views)

obp42,

 

The error you described does sound like the computer is trying to access memory that isn't allocated.

 

The link below should shed a bit more light on the NumericArrayResize function:

http://zone.ni.com/reference/en-XX/help/371361D-01/lvexcode/numericarrayresize/

 

However, I don't think this is the best way to do what you are trying to accomplish.  Have you thought about defining elt as a pointer, and then dynamically allocate the array once you know dimSize?  I will admit that C is not my area of expertise, but this sounds like a better way to do it.

 

 

Drew T.
Camber Ridge, LLC.
0 Kudos
Message 6 of 6
(6,574 Views)