0

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:

  1. Dependency Errors: When attempting to retrieve the flag value from the EXE within the DLL, I am facing dependency-related issues.

  2. 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?

askedOct 2, 2024 at 12:44
Mukesh Rajput's user avatar
7
  • 2
    Isg_bEnableBluetoothPortCheckingdefined somewhere in the dll project source? Theextern declaration doesn't really allocate space for it.CommentedOct 2, 2024 at 12:58
  • Possible dupstackoverflow.com/questions/78427140/…CommentedOct 2, 2024 at 14:23
  • The header-file you created rather suggests that both theg_bEnableBluetoothPortChecking flag and theGet()/Set() functions are defined in the dll module, exported, and "used" (imported) by the executable, as they are alldllexport for the DLL anddllimport for 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.CommentedOct 2, 2024 at 15:27
  • Just flip this and have the dll contain the flag and the associated functions.CommentedOct 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 fileCommentedOct 3, 2024 at 4:16

0

Know someone who can answer? Share a link to thisquestion viaemail,Twitter, orFacebook.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.