Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

function from one dll calling the same name function of another dll

Tags:

c++

visual-c++

I have a peculiar problem. I have two DLLs, let's call them DLL-A and DLL-B.

DLL-A has a function named f1() and DLL-B also has function with the same name, f1(). Now in DLL-A, f1() is calling f1() of DLL-B like this.

DLL-A:

f1()
{
    f1(); //this is DLL-B function, but having the same name
}

Now my question is that, will it be a recursive call of f1() from DLL-A?

like image 768
user1077865 Avatar asked Dec 02 '11 17:12

user1077865


3 Answers

The f1() inside the function body calls itself leading to an infinite recursion, as you suspected. Some possible solutions:

  • Put the imported DLL function in a separate namespace so that you can distinguish its name.
  • Change the names of these function to avoid a clash.
  • Import explicitly rather than implicitly by using GetProcAddress. That allows you to call the imported function anything you like.
like image 184
David Heffernan Avatar answered Oct 04 '22 19:10

David Heffernan


You can change the name of the function in DLL-A to A_f1.

A_f1()
{
  f1() //this calls DLL-B's f1
} 

In your DEF file, write

EXPORTS
    f1 = A_f1

This says "The function I internally called A_f1 should be exported under the name f1 to other components." That way everybody who had been using DLL-A and calls f1 (expecting to get the function A) will get A_f1.

I'm assuming that renaming the exported functions is not possible. If it is possible, then that is a much cleaner solution. (My guess is that it isn't possible because you are trying to hack a video game.)

like image 34
Raymond Chen Avatar answered Oct 04 '22 18:10

Raymond Chen


They way you have written it f1 within f1 will not call DLL-B but be a infinite recursion. If you want to call DLL-B function you will have to use GetProcAddress

like image 44
parapura rajkumar Avatar answered Oct 04 '22 19:10

parapura rajkumar