writing to a shared variable in a dll

This is a discussion on writing to a shared variable in a dll within the Windows Programming forums, part of the Platform Specific Boards category; Hi all, I have a dll file which im loading using LoadLibrary. I wish to pass the dll my windows ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    writing to a shared variable in a dll

    Hi all,

    I have a dll file which im loading using LoadLibrary. I wish to pass the dll my windows HWND by including the dll header file in my exe and using decl(dllimport) to assign to it. Problem is that i'm getting the warning.

    c4273 g_TargetHwnd inconsistent dll linkage

    This is my code:

    Dll Header File:

    Code:
    #if !defined MYDLL_DLL_H
    #define MYDLL_DLL_H
     
      #ifdef MYDLL_DLL_EXPORTS
      #define MYDLL_DLL_API __declspec(dllexport)
      #else
      #define MYDLL_DLL_API __declspec(dllimport)
      #endif
     
     extern MYDLL_DLL_API HWND g_TargetHwnd;
    
    #endif // !defined(MYDLL_DLL_H)
    Now this is the code in the cpp file which is causing me the headache:

    Code:
    #pragma data_seg (".shared")
    HWND	g_TargetHwnd = 0;
    #pragma data_seg ()
    
    #pragma comment(linker,"/SECTION:.shared,RWS")
    Now for some reason I keep getting that warning. I have no idea why because i have used this technique before fine.

    Any ideas?

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by cloudy View Post
    Code:
    #pragma data_seg (".shared")
    extern HWND    g_TargetHwnd = 0;
    #pragma data_seg ()
     
    #pragma comment(linker,"/SECTION:.shared,RWS")
    It wasnt really clear whether the cpp includes the header file, but im assuming it does. Better yet, just remove that section, since if you are declaring it as extern, you dont need to redeclare the variable in the cpp proper
    Last edited by abachler; 07-17-2007 at 01:46 PM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Thanks for the reply

    When i add the extern inside the #pragma shared block it makes no difference.

    If i completely remove the #pragma shared stuff i get a link error saying:

    unresolved external symbol __declspec(dllimport) struct HWND__ * g_TargetHwnd

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    extern __declspec(dllimport)HWND g_TargetHwnd;

    never redefine an external variable by assignment. Is there some reason why you cant use the lib from the DLL project? is it a cross compiler project, or is the DLL from a 3rd party?
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Thanks for your help, i manged to get rid of the warning but it still isnt working.

    I belive the problem is either due to the DLL not exporting the variable correctly or the app not importing it, maybe both.

    I changed the dll to not use __declspec(dllexport)and to export it using a module definition file like so,

    EXPORTS
    g_TargetHwnd

    The app does the following:

    Code:
    __declspec(dllimport) HWND g_TargetHwnd

    Everything compiles fine, but during runtime when the app does this:

    g_TargetHwnd = myHwnd ;

    the g_TargetHwnd in the dll file doesnt get updated, it remains 0.

    Is there something i should know about using __declspec(dllimport), like an option i need to turn on in the project settings or anything?

    Thanks again mate.

    Edit:
    On Msdn it says that to use __declspec(dllimport) i must be including the library. So i take it that just means to add MyDll.lib to the AdditionalDependencies in project setttings-> Linker->Input?
    Last edited by cloudy; 07-17-2007 at 03:12 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-18-2005, 09:06 PM
  2. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. mfc as shared dll
    By zMan in forum Windows Programming
    Replies: 3
    Last Post: 09-06-2002, 01:23 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21