Thank you so much for your response, Greg. However, I still have problem after making
corresponding modification for LStrPrintf approach:
int32 len;
char tempCStr[255] = "";
strcpy(temCStr, testinstance.test);
len = StrLen(tempCStr);
LStrPrintf(testcluster->test, (CStr) "%s", tempCStr);
LStrLen(*testcluster->test) = len;
LabVIEW crashes. Any ideas?
Greg McKaskle wrote:
> The LStrPrintf example works correctly with the "test" string literal because
> tempCStr= "test"; assigns the pointer tempCStr to point to the buffer containing
> "text". Calling strcpy to move any string to tempStr will cause
> problems because it is copying the string to an uninitialized pointer,
> not to a string buffer. There isn't anything wrong with the LStrPrintf
> call, the damage is already done.
>
> In the moveblock case, the code:
> tempCStr = (char *)&testinstance.test; //since strcpy didn't work, I
> used this way to try to copy the const char* to char*.
>
> doesn't copy the buffer, it just changes a pointer, tempCStr to point to
> the testinstance string buffer. This is not completely necessary, but
> does no harm and is very different from a call to strcpy.
>
> I believe the reason that LV cannot see the returned string is that the
> string size hasn't been set.
> Again, I'm not looking at any documentation, but I believe that you may
> want to look at LStrLen(*testcluster->test). I think it will be size of
> the string passed into the CIN, and it should be set to len before returning.
>
> Greg McKaskle
>
> > struct teststrct{
> > ...
> > const char* test;
> > ...
> > };
> >
> > struct teststrct testinstance;
> >
> > typedef struct {
> > ...
> > LStrHandle test
> > ...
> > } TD1;
> >
> > CIN MgErr CINRun(TD1 *testcluster, LVBoolean *Error) {
> >
> > char *tempCStr = NULL;
> >
> > ...
> >
> > strcpy(tempCStr, testinstance.test); // this would cause LabVIEW crash!
> > LStrPrintf(testcluster->test, (CStr) "%s", tempCStr);
> > // but if I assigned tempCStr as tempCStr = "test", the string value
> > "test" could be passed to LabVIEW without any problems.
> >
> > ...
> > }
> >
> > 2. MoveBlock
> >
> > #include "extcode.h"
> > #include "hosttype.h"
> > #include "windows.h"
> >
> > struct teststrct{
> > ...
> > const char* test;
> > ...
> > };
> >
> > struct teststrct testinstance;
> >
> > typedef struct {
> > ...
> > LStrHandle test
> > ...
> > } TD1;
> >
> > CIN MgErr CINRun(TD1 *testcluster, LVBoolean *Error) {
> >
> > char *tempCStr = NULL;
> > int32 len;
> > ...
> >
> > tempCStr = (char *)&testinstance.test; //since strcpy didn't work, I
> > used this way to try to copy the const char* to char*.
> > len = StrLen(tempCStr);
> >
> > if (err = NumericArrayResize(uB, 1L, (UHandle*)&testcluster->test,
> > len))
> > {
> > *Error = LVFALSE;
> > goto out;
> > }
> >
> > MoveBlock(&tempCStr, LStrBuf(*testcluster->test), len); // the string
> > was able to passed to LabVIEE, but it was unreadable.
> > ...
> >
> > out:
> > ...
> >
> > }
> >
> > Did I do anything wrong? Any thougths or suggestions would be very
> > appreciated!