I have two C++ projects: one is an executable (EXE), and the other is a dynamic link library (DLL). In the EXE project, I am using a flag corresponding to a checkbox, and I need to access the value of this flag from within the DLL project.
To achieve this, I created a header file that utilizes__declspec(dllexport) and__declspec(dllimport) for proper symbol sharing between the EXE and DLL. Here's how I have defined it:
#pragma once#ifdef BUILDING_DLL #define SHARED_API __declspec(dllexport)#else #define SHARED_API __declspec(dllimport)#endif// Declare the shared variableextern "C" SHARED_API bool g_bEnableBluetoothPortChecking;// Declare the getter and setter functionsextern "C" SHARED_API bool GetBluetoothPortCheckingState();extern "C" SHARED_API void SetBluetoothPortCheckingState(bool state);This header is included in both the EXE and DLL projects. However, I am encountering the following issues:
Dependency Errors: When attempting to retrieve the flag value from the EXE within the DLL, I am facing dependency-related issues.
Linking Errors: When trying to set the global variable, linking errors occur.
Could you advise on the correct approach to ensure the flag is properly shared and accessed between the EXE and DLL, avoiding these dependency and linking errors in Visual Studio 2010?
- 2Is
g_bEnableBluetoothPortCheckingdefined somewhere in the dll project source? Theexterndeclaration doesn't really allocate space for it.Constantine Georgiou– Constantine Georgiou2024-10-02 12:58:33 +00:00CommentedOct 2, 2024 at 12:58 - Possible dupstackoverflow.com/questions/78427140/…Ahmed AEK– Ahmed AEK2024-10-02 14:23:58 +00:00CommentedOct 2, 2024 at 14:23
- The header-file you created rather suggests that both the
g_bEnableBluetoothPortCheckingflag and theGet()/Set()functions are defined in the dll module, exported, and "used" (imported) by the executable, as they are alldllexportfor the DLL anddllimportfor the EXE. If this is not what you wanted you need to rethink or restructure it. And finally I don't understand why you want to export the flag while you also have getter/setter functions.Constantine Georgiou– Constantine Georgiou2024-10-02 15:27:20 +00:00CommentedOct 2, 2024 at 15:27 - Just flip this and have the dll contain the flag and the associated functions.Ahmed AEK– Ahmed AEK2024-10-02 15:34:58 +00:00CommentedOct 2, 2024 at 15:34
- Yes I have defined the global variable g_bEnableBluetoothPortChecking in the EXE file, where I am setting the flag. In this file, I have also implemented the GetBluetoothPortCheckingState and SetBluetoothPortCheckingState functions. Through these, I am try to get the value in my DLL fileMukesh Rajput– Mukesh Rajput2024-10-03 04:16:14 +00:00CommentedOct 3, 2024 at 4:16