1

In work, I have to call two classes with the same name in two different dlls. But I don't know which class is instantiated.

For example:

FolderA/C.h

class__desllpec(export) C{}

FolderB/C.h

class__desllpec(export) C{}

main.h

int main(){
    C c; // 
}

My question is:

  1. Which class will be instantiated?
  2. What shall I do if I want to instantiated one class?

Thanks for you help.

I've read the question Calling two functions with the same name from two different C DLLs and I am not sure the answer is out of date.

Update:

Answer from comments:

  1. use namespace in the headers.
  2. use LoadLibrary() and GetProcess() to get specific functions.

But I can not figure out which class will be instantiated and why.

Community
  • 1
  • 1
Steve
  • 1,448
  • 11
  • 18
  • ...fire your developers for not using namespaces? – IdeaHat Dec 01 '14 at 14:28
  • Which ever library is in the link path first is likely to be first, if it even links at all. You would need to use load library and get Proc address with a factory function – Chris Dec 01 '14 at 14:31
  • @IdeaHat yeah.... You are right. Namespaces shall be used! – Steve Dec 01 '14 at 14:34
  • Actual answer to the question is here: http://stackoverflow.com/questions/14281089/unmanaged-c-dlls-with-same-name-coexisting-in-same-process (this is, of course, windows specific but the cross platform fix for this is don't allow it to happen) – IdeaHat Dec 01 '14 at 14:39
  • @Chris. Thanks for the help. Do you mean `GetProcAddress()`? In this case, I have to load the library with `LoadLibrary()` while we usually load the library in the configuration of the project. – Steve Dec 01 '14 at 14:51
  • @IdeaHat. I am afraid it's not the same problem. There are two dlls with the same name in the question. In this question, two class, which is implemented in two different dll, have the same name. – Steve Dec 01 '14 at 14:55
  • @Steve is actually *is* the same problem, with the same solution. The naming (all the way down) of your two dlls is basically too ambiguous to use implicit linking. Explicit linking can get you to load everything correctly, but will require you to make local aliases for everything and manually load everything you want...a huge pain, and would probably require you to be using some sort of factory as well. Welcome to dll hell! – IdeaHat Dec 01 '14 at 14:59
  • You may Change those classes, put in different namepsaces. e.g. Namespace A for Header file A and B for other. I don't know if you can Change the headers or not. – Wafeeq Dec 01 '14 at 15:17
  • Yes that's the one sorry on mobile quick reply – Chris Dec 01 '14 at 15:21
  • @IdeaHat namespaces would work but keep in mind that one of the libs could be third party so he may have no control over it. If they are both in house libs then all they need is a little inter team communication and clashes would be resolved. One thing I've learned coding for 25 years is that there should never be one way of doing anything with anything else being unacceptable/bad – Chris Dec 01 '14 at 15:26
  • @Chris Currently, they are both in house libs so I can change them :). But I can not figure out which class will be instantiated and why. And I want to find another way in case that I do not have the source codes. :) – Steve Dec 01 '14 at 15:37
  • @Chris That's why I posted the explicit linking solution... My more pessimistic way of looking at the "one way" problem is that you can never control the full system and someone will always build something in an "incorrect" way, and you'll have to handle that. – IdeaHat Dec 01 '14 at 15:43
  • Recommend firing and email to library owners so they can resolve the issue, personally. I would assume that first lib/header would be the first used, but cannot be one hundred percent, the second may eclipse it. You could write a simple test with a cout in constructors to test it out... Not sure it will change your situation in the slightest though – Chris Dec 01 '14 at 15:43

0 Answers0