user1077865
user1077865

Reputation: 61

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

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?

Upvotes: 6

Views: 2441

Answers (5)

user2723810
user2723810

Reputation: 21

Maybe you can change the name by:

#pragma comment(linker, "/export:MyFunc=_MyFunc@8")

Upvotes: 2

Raymond Chen
Raymond Chen

Reputation: 45172

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.)

Upvotes: 7

David Heffernan
David Heffernan

Reputation: 613461

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.

Upvotes: 7

Luchian Grigore
Luchian Grigore

Reputation: 258648

You should get a compiler or linker error if you link two objects files that export the same symbol. Something like "multiple definition for symbol f1()".

That is, of course, if you include in one dll the header that declares the other function or link the two binaries together.

To resolve this, place the functions inside namespaces.

Upvotes: 2

parapura rajkumar
parapura rajkumar

Reputation: 24413

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

Upvotes: 3

Related Questions