Thread: DLL intercomunication

  1. #1
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14

    DLL intercomunication

    Hi all, I have another question.

    I hope this is an interesting topic and thanks for all the help you can give.

    I have two dlls:
    1 - dll_prep links against a static library, lib1. Lib1 contains a vector of objects, various classes and member functions. When the main program executes, dll_prep is loaded and its objects (defined in the static library) are correctly initialized. This lib1 is written in C++ and contains an STL vector of objects, amongst other things.

    2 - dll_calc is a dll generated by compiling/linking fortran subroutines. It is called by dll_prep at runtime. dll_calc performs some calculations on objects defined in the lib1, part of dll_prep. dll_calc is linked against the lib1 static library.

    dll_prep loads a function pointer in dll_calc and executes the corresponding Fortran subroutine.
    dll_calc also makes some function calls into dll_prep.

    The problem is that when the Fortran subroutine in dll_calc calls the functions defined in its static library lib1, it seems to be returning bogus/uninitialized memory (the vector of objects is now empty).

    So to summarize:
    - dll_prep depends on a static library, lib1.
    - dll_calc also depends on the same static library, lib1.

    I suspect that both dlls have an independent copy of the same static library, lib1 and therefore what one dll does the other doesn't know.

    So my question and request for help:
    when two dll are loaded and call functions (defined in a static librarY) between each other, what should I do?

    I set up the project such that:
    dll_prep depends on lib1
    dll_calc depend on lib1 (I'm using the VS 2005 project dependencies dialog box here).

    Again, thanks for the help.
    a72.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you want THE SAME lib1 to be used by both DLL's, you'll have to do "something different". There are several solutions, but none are entirely trivial.

    The simplest would be to just combine the two DLL's into a single DLL - that would solve the entire problem in one fell swoop.

    Another solution is to turn lib1 into a DLL too - this may of course not work right, depending on what lib1 contains.

    A further solution would be to make lib1 part of the APPLICATION that calls the DLL's, and have some sort of mechanism for lib1's content to be passed to the two DLL's.

    The "right" choice depends quite a bit on the exact usage and content of Lib1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Location
    US
    Posts
    14

    DLL intercomunication

    matsp, you say:
    Another solution is to turn lib1 into a DLL too - this may of course not work right, depending on what lib1 contains.
    This could be possible. If lib1 becomes a dll itself, is this really all it takes?
    In other words: is the problem the fact that lib1 is a static library linked in both original dlls?
    Thanks!
    and72.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you haven't described in DETAIL what the different libraries contain and how it's used - there are some differences between how a DLL works and how a library works. But if you make a DLL of lib1, then it will be the same one for both of your other DLL's as long as both of the other DLL's are uses in the same process [rather than two different .exe files that use two different DLL's].

    As a summary: Yes, I think that would work.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    The issue here is that when you create an instance of the library in one DLL that instance does not exist in the other DLL. So anything you do to the state of the library internally will not be evident in the other DLL.

    We have run into this at work and as I understand it, DLL's by nature have their own independent memory. So if you were to create an instance of an object in DLL1 that instance does not exist across module boundaries. DLL2 has no instance of the object. So you are working with two independent instances.

    There are several solutions but they are highly dependent on what you are trying to do and how much dev time you want to devote to the project. The easiest solution is to create a common wrapper that instantiates the object being shared and then have both DLL's call into the wrapper. This way they are both guaranteed to be accessing the same object and allows you to create a common interface to the object. So both DLLs will access the object in exactly the same manner.

    DLL1 ---> Wrapper -> Library ---> Object instance <--- Library <--- Wrapper <---- DLL2

    This isn't a completely accurate diagram since there is really only one instance of the wrapper.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Bubba View Post
    DLL1 ---> Wrapper -> Library ---> Object instance <--- Library <--- Wrapper <---- DLL2

    This isn't a completely accurate diagram since there is really only one instance of the wrapper.
    Code:
    DLL1 ----v v---- DLL2
           Wrapper ------> Library ------> Object Instance
    Any better?

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Yes, much better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. Using class with DLL
    By greg2 in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2003, 05:24 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM