1. Def File
1 exports2 3 g_ntest data; export global variable 4 5 getglobalvar; export Function
2. Call
1 extern int g_ntest; // declare 2 3 int main (INT argc, char * argv []) 4 {5 * (int *) g_ntest = 1; // note the previous conversions 6 7 return 0; 8}
Note that using extern int g_ntest to declare that the imported global variable is not the DLL's global variable itself, but its address. The application must use the global variable in the DLL through forced pointer conversion. This is from
* (Int *) g_ntest.
3. Better call
1 extern int _ declspec (dllimport) g_ntest; // use _ declspec (dllimport) to import 2 int main (INT argc, char * argv []) 3 {4 g_ntest = 1; // can be directly used without converting 5 6 RETURN 0; 7}
The _ declspec (dllimport) method is used to import the global variable itself, instead of its address.
Export global variables in DLL