LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Global Variables between different project

Hello everyone,
I have a question for you. I'm working on a CVI project that is composed by a DLL project and a standalone one. The standalone recall some functions that are exported from the DLL. I have defined a global structure in a *.h file that is included both in the DLL and Standalone but the two projects seem to view two different structure (the standalone recalls an exported function that should write on the global structure but if I print the structure's fields after the calling from the standalone, they are all set to zero. If I print them inside the called function from the DLL, they are correct). Is there a way to define the structure global respect to both the DLL and the standalone? Thanks


0 Kudos
Message 1 of 7
(3,391 Views)

Try the following:

  • In the common .h file, define the structure as a typedef
  • In your standalone program, declare the structure object instance as extern
  • In your dll, declare the structure object instance with the same name, as __declspec(dllexport)
  • In the Build->Target Settings menu for your dll, make sure you select the "Include File And Marked Symbols" option in the Export window

JR

0 Kudos
Message 2 of 7
(3,384 Views)
Thank you very much for your reply.

1)I have declared the structure  as typedef in the common *.h
2)I have declared the structure as extern in the Main.c of the standalone
3)I have declared the structure named as 2)  as __declspec(dllexport) in Main.h of the dll and I have included this *.h file in all the *.c files of the dll (all the functions of all the *.c files of the dll have to be able to write inside the structure).

Unfortunately it doesn't work. I have also tried  to  declare and modify the structure inside Main.c of the dll instead of Main.h and to declare the structure as  __declspec(dllimport) inside the Main.c of the standalone, it works but the problem is that the structure is no longer global for all the other *.c files of the dll. 
0 Kudos
Message 3 of 7
(3,374 Views)
I think you went wrong at step (3). The structure instance must only be declared once, so including it in a .h file for all .c files to #include will not work. Think of it this way: all declarations of the structure, whether in the standalone or in the dll, no matter how many .c files reference it, should be declared as extern. This stops the compiler actually reserving space for it every time a file is compiled, which could result in having multiple copies of the variable in a program. Then, in just one place only (somewhere in your dll project) you declare the structure without the extern reference. So the compiler will, at last, allocate space for the (single) instance of the structure. When you build the dll, the linker will resolve all the extern references in the various compiled modules to the single actual structure. Exporting this final declaration from the dll will also allow the standalone program to reference the same structure instance.
 
JR
0 Kudos
Message 4 of 7
(3,368 Views)
Ok I have done as you have suggested me but when I compile the DLL project I get an error:

"Inconsisten type declaration for external symbol....in modules Main.c and File.c"

In File.c I modify the structure. If I do the same identical things but declaring the structure in Main.c as simply esternal it works.
0 Kudos
Message 5 of 7
(3,365 Views)

Can you modify the function you are calling to take the structure as an argument?

If so, you can pass the structure into the function.

Example:

typedef struct
{
int a;
int b;
}TestStruct;

//calling program

TestStruct Obj;

TestFunc(&Obj, other args);

//dll

void TestFunc(TestStruct  *Obj, other args)
{

//fill in data

}

If you are not sure how many objects you are going to get you need to pass a pointer to the dll and allocate the memory in the dll.

Example:

TestStruct *Obj;

TestFunc(&Obj, other args);

//dll

void TestFunc(TestStruct  **Obj, other args)
{

(*Obj) = calloc(num of objects, sizeof(TestFunc))

(*Obj)[i].a = 5;
(*Obj)[i].b = 6;

}

 


 

 

 

 

 

 

0 Kudos
Message 6 of 7
(3,329 Views)
Yes, It's exactly what I have done at the end. I have declared the structure inside the standalone and I have passed it as parameters to the exported function and also to all the dll functions that write on it.
0 Kudos
Message 7 of 7
(3,320 Views)