Jump to content
  • Advertisement
Sign in to follow this  
jbizzler

Make a .dll expect a function it doesn't have

This topic is 3999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any way a .dll can expect a function it doesn't actually have at build time? Like, I want the .dll to call functions from the .exe. I managed to export links in the .exe functions to a .lib, but since both projects rely on eachother, that only worked because they it was one-way in the previous build. Now if I do a clean rebuild, both projects can't compile. So, I just need to the .dll to rely on functions it doens't actually have access to until run through an executable. I'm using VS2005 by the way.

Share this post


Link to post
Share on other sites
Advertisement
Did you try function pointers? If might not be exactly what your looking for, but it should work.

(not actually code)

DLL
void DoSomething(FunctionPointer)
{
FunctionPointer() //Call function that the pointer points to
}

EXE
DoSomething(Fly) //Pass pointer to Fly function

void Fly()
{
//Fly
}

You should be able to find references on the Net
You're using Native, right? (If .NET use delegates)

Share this post


Link to post
Share on other sites
Sounds good. I imagine there's a better way, though. Like, when you make a Win32 project, what exactly is expecting WinMain? You compile it without WinMain, and you get the same error I get trying to compile my solution.

Is there any way to have each project do all its .lib exports before building their binary? That way, I could have my .exe export links to its functions, the .dll export links to its functions, then they build their binaries, each with the proper links to eachether.

Don't get me wrong; I appreciate your response. I just want to explore every possibility.

Share this post


Link to post
Share on other sites
In VS2005 when you create a DLL it should create a .lib file with the exports...
It just doesn't allow you to just replace the DLL that easily with recompiling the EXE...
I'm not exactly sure of a way to do this without function pointers...

As I remember, the PE format (EXE and DLL) doesn't allow an EXE to reference a DLL which references the EXE (staticly) or any other static reference.

The function pointers, though, allow you to make a DLL which allows the EXE to 1) change which functions get called at runtime and 2) allow you to have more than one EXE to use the DLL

The compiler expects WinMain (or main, or wmain, etc.). The PE format itself actually doesn't care what the entry-point is called.

I think you're wanting the EXE to reference (staticly) the functions in a DLL, while allowing the DLL to (staticly) reference functions in the EXE... this isn't allowed in the PE format (or probably any other executable format).

I'm only sure of the function pointer way, and I'm not sure if there really is another way... you'll have to ask somebody else (If there is another way, I don't know it)

Have to go now. :)

Share this post


Link to post
Share on other sites
I knew it! There is a proper way. If you define a function as __declspec(dllimport), it will expect the function be available in the executable or another dll, and compile without it.

Share this post


Link to post
Share on other sites
Yep, that should work.

You might also want to explore using delay-load, or something like that.

However, you might want to consider that needing to do this suggests that the coupling between the exe and the DLL is too high. It would be better to design them so that you don't need to do this.

Share this post


Link to post
Share on other sites
Quote:
Original post by jbizzler
Like, when you make a Win32 project, what exactly is expecting WinMain?


The compiler supplies an already compiled object file that contains a function that serves as the actual entry point to your executable. For GUI programs that function is WinMainCRTStartup, for console programs, mainCRTStartup.

These searches return a lot of information.

google: WinMainCRTStartup
google: mainCRTStartup

An exe can export functions like a dll, but outside of the operating system itself (i.e. ntoskrnl.exe), it's a rare thing.

If you have an exe that exports functions and you want to statically link it with a dll that would access those exported functions, I think you'd have to have the dll obtain those functions using GetProcAddress and passing GetModuleHandle(NULL) for the library handle. You wouldn't want to use LoadLibrary as the exe is already loaded.

But it would seem that the better approach would be to move the exported functions from the exe to the dll, then have the exe import them from the dll as needed. The dll would already have access to those functions and wouldn't need to load up pointers and so on.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!