If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

There is no need to link anything from mydll. The function pointer is returned by GetProcAddress() at runtime, so trying to find the function at link time makes no sense whatsoever. What you need to do is probably remove all of your user-defined import libraries from your linker options. You don't need them if you're using LoadLibrary.

The only thing you need to make sure is that the "HelloWorld" function is exported, so that GetProcAddress() can find it when you call LoadLibrary on the module that contains HelloWorld.

In other words, you don't need any of this in the code where you want to call this function:

There is no "HelloWorld" function that lives in the executable you want to call it from. You have LoadLibrary, GetProcAddress, and a function pointer returned by GetProcAddress that points to an exported function. Nothing else. So trying to write a prototype for HelloWorld and including the prototype is a waste of time if that function is not there.

Within the module that defines HelloWorld(), then yes, you would build the module with the prototype, just like any other C/C++ module. But from the module you are calling HelloWorkd from using GetProcAddress(), you should not be prototyping HelloWorld() function, because that function doesn't exist at link time anywhere, as it will be "retrieved" at runtime using GetProcAddress.

What you want to do is define a function pointer that is compatible with the one returned by GetProcAddress.

Also, I believe you should use GetModuleHandle() from the DLL, so that you get the HMODULE of the executable that is running. It serves the same purpose as LoadLibrary() without having to go through the DllMain() and other initializations that LoadLibrary() will do.

There is no need to link anything from mydll. The function pointer is returned by GetProcAddress() at runtime, so trying to find the function at link time makes no sense whatsoever. What you need to do is probably remove all of your user-defined import libraries from your linker options. You don't need them if you're using LoadLibrary.

The only thing you need to make sure is that the "HelloWorld" function is exported, so that GetProcAddress() can find it when you call LoadLibrary on the module that contains HelloWorld.

In other words, you don't need any of this in the code where you want to call this function:

There is no "HelloWorld" function that lives in the executable you want to call it from. You have LoadLibrary, GetProcAddress, and a function pointer returned by GetProcAddress that points to an exported function. Nothing else. So trying to write a prototype for HelloWorld and including the prototype is a waste of time if that function is not there.

Within the module that defines HelloWorld(), then yes, you would build the module with the prototype, just like any other C/C++ module. But from the module you are calling HelloWorkd from using GetProcAddress(), you should not be prototyping HelloWorld() function, because that function doesn't exist at link time anywhere, as it will be "retrieved" at runtime using GetProcAddress.

What you want to do is define a function pointer that is compatible with the one returned by GetProcAddress.

Also, I believe you should use GetModuleHandle() from the DLL, so that you get the HMODULE of the executable that is running. It serves the same purpose as LoadLibrary() without having to go through the DllMain() and other initializations that LoadLibrary() will do.

Re: How to call a C function (in the host process) from a DLL

Originally Posted by alexe100

I am sorry, but you did not understood my problem!

I understand it quite well.

You have two options:

1) Either you pass the pointer to the HelloWorld function to the DLL as a callback and from the DLL just issue the callback.
or
2) You use GetModuleHandle() using the process handle, use GetProcAddress() to get the HelloWorld function pointer (assuming it's exported), and then call the function.

In neither of these scenarios do you need to link to the HelloWorld function.

HelloWorld function does not belongs to dll! It belongs to the process!

How does that change things? There is nothing stopping you from doing this:

Code:

LoadLibrary("myexe.exe"); // or
GetModuleHandle("myexe.exe");

an EXE and a DLL are modules that can have exported functions and can be loaded using LoadLibrary()/GetModuleHandle(). So why were you not attempting to do this before? From the Func() DLL, just call GetModuleHandle("yourexename.exe") and retrieve the HelloWorld function pointer, then call it.

Re: How to call a C function (in the host process) from a DLL

I never thought that I could call LoadLibrary for an EXE!

Now all makes sense for me!

Thanks a lot

Alex

Originally Posted by Paul McKenzie

I understand it quite well.

You have two options:

1) Either you pass the pointer to the HelloWorld function to the DLL as a callback and from the DLL just issue the callback.
or
2) You use GetModuleHandle() using the process handle, use GetProcAddress() to get the HelloWorld function pointer (assuming it's exported), and then call the function.

In neither of these scenarios do you need to link to the HelloWorld function.
How does that change things? There is nothing stopping you from doing this:

Code:

LoadLibrary("myexe.exe"); // or
GetModuleHandle("myexe.exe");

an EXE and a DLL are modules that can have exported functions and can be loaded using LoadLibrary()/GetModuleHandle(). So why were you not attempting to do this before? From the Func() DLL, just call GetModuleHandle("yourexename.exe") and retrieve the HelloWorld function pointer, then call it.

Re: How to call a C function (in the host process) from a DLL

Exactly. As long as you declare HelloWorld as imported, you're obliged to provide an import library for the function. As the module that exports it is your EXE, so the implib must be test1.lib. And you built it prior to building your dll. This scenario makes sense only in case your dll is strictly intended to be used with module named test1.exe and exporting HelloWorld function. And you won't be able to load it to any other process. I would recommend to think twice before implicit linking dll to exe function.

Re: How to call a C function (in the host process) from a DLL

Originally Posted by Paul McKenzie

You have two options:

1) Either you pass the pointer to the HelloWorld function to the DLL as a callback and from the DLL just issue the callback.
or
2) You use GetModuleHandle() using the process handle, use GetProcAddress() to get the HelloWorld function pointer (assuming it's exported), and then call the function.

Just to add, a third option could be to place the function to be called from the DLL in a second DLL that both the application and (the first) DLL can link against. That way you can be sure the function exists at compile time, rather than at run time.

Cheers, D Drmmr

Please put [code][/code] tags around your code to preserve indentation and make it more readable.

As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky