11-01-2007 10:33 AM
11-01-2007 11:00 AM
You have declared a type of structure, but not an instance of it. Maybe you need something like:
typedef struct { // Define the 'shape' of an object
...
} structType;
structType F_text; // Need to declare an actual object
JR
11-01-2007 11:26 AM
thanks for the response,
but I've already tried that and it didn't work.
11-01-2007 11:37 AM
You define the TLS struct as a typedef, then refer to your TLS with a pointer of type "pointer to TLS struct".
Like this:
typedef struct ThreadData {
CHAR ErrorString[MAX_STRING_SIZE];
CHAR StationID[MAX_STRING_SIZE];
} ThreadData, * LPTHREADDATA;
DWORD g_dwTlsIndex; // TLS index declaration as a top level variable
// =========== Code ============
LPTHREADDATA lpThreadData = TlsGetValue (g_dwTlsIndex); // Get pointer to TLS as thread data struct.
// Could make it clearer with a cast I suppose ... TlsGetValue return is of type "pointer to void" that you need to cast / coerce to some other type to use.
LPTHREADDATA lpThreadData = (LPTHREADDATA) TlsGetValue (g_dwTlsIndex);
strcpy (lpThreadData->StationID, "None"); // and copy a string into it
So you can see, there never is a declaration of the struct, it is just a way of considering the layout of the TLS. I'm not sure just what's happening with the NI stuff, I just code the use of the TLS directly per the Win32 API.
Menchar
11-01-2007 03:19 PM
11-01-2007 04:22 PM
11-02-2007 04:07 AM
Thankyou everybody,
Luis_G I agree your example does compile and does work within the main. I am calling the thread local variable from multiple functions and the above example only sets the data locally (see below example).
I find it odd that you cannot delare a pointer as you can with and int as per the ni example.
#include <cvirte.h>
void test(void);
void function(void);
typedef struct {
char Flash[100]; //Flash test fault text
} textstruct;
int FaultText; //Fault text structure - handle
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
CmtNewThreadLocalVar (sizeof(textstruct), NULL, NULL, NULL, &FaultText);
test();
return 0;
}
void test(void)
{
textstruct threadtext; //Thread local variable
CmtGetThreadLocalVar (FaultText,&threadtext);
function();
MessagePopup ("threadtext", threadtext.Flash);
}
void function (void)
{
textstruct threadtext; //Thread local variable
CmtGetThreadLocalVar (FaultText,&threadtext);
sprintf(threadtext.Flash,"help");
}
11-02-2007 04:46 AM
I think I have cracked it (see below).
#include <utility.h>
#include <userint.h>
#include <ansi_c.h>
#include <cvirte.h>
void test(void);
void function(void);
typedef struct {
char Flash[100]; //Flash test fault text
} textstruct, *pointstruct;
int FaultText; //Fault text structure - handle
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
CmtNewThreadLocalVar (sizeof(textstruct), NULL, NULL, NULL, &FaultText);
test();
return 0;
}
void test(void)
{
pointstruct ptr;
CmtGetThreadLocalVar (FaultText,&ptr);
function();
MessagePopup ("test", ptr->Flash);
}
void function (void)
{
pointstruct ptr;
CmtGetThreadLocalVar (FaultText,&ptr);
sprintf(ptr->Flash,"help");
MessagePopup ("function", ptr->Flash);
}
11-02-2007 02:05 PM
11-05-2007 02:40 AM
Thanks for all the help
Simon