Jump to content
  • Advertisement
Sign in to follow this  
Dookie

How do I make a DLL for use in my game?

This topic is 4834 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

Hey guys. I've finally tried making a DLL, but am having no luck. Here's how I've done it: 1) Created a new VisualCPP project, DLL. 2) Wrote some code. 3) Compiled a DLL using 'release' compile option. 4) Dropped my DLL into anther existing VisualCPP project folder. 5) Made reference to a function contained in my DLL. 6) Got linker errors when I tried to compile the project. I can compile my DLL just fine, but I can't use it. I don't even know if my DLL is functional... Here's some demo code I used for my DLL just to see if I could get something working:
// atomdll.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

// Prototypes for exportation (use in other programs)
__declspec(dllexport) int Stoopid();


BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}


int Stoopid()
{
	// Something goes here...
	return 69;
}
Most of the above code is what Visual Studio wrote when it created the project, and I wrote the function 'Stoopid()'. Now, if I move my 'atomdll.dll' to another project's folder and then try to make reference to the function 'Stoopid()' within that project, I get linker errors when I compile. I use '__declspec(dllimport) int Stoopid();' as a function prototype at the beginning of my code, and simply make a call to 'Stoopid()' somewhere in my program. I'm sure why it isn't working is an amazingly silly mistake, but I've never written a DLL before. Can anyone help me get Stoopid() to work? If I can figure THAT out, I might be able to figure the rest out on my own. Thanks in advance for the help!

Share this post


Link to post
Share on other sites
Advertisement
Sure thing, it's actually quite simple :)
When your DLL project is compiled, two files should be produced: a .dll file AND a .lib file.
Add the .lib file to your new project's files to link.

If your DLL project doesn't create a .lib file, it's because it doesn't know which functions should be part of the dll -- add __declspec(dllexport) to your Stoopid function declaration.

Share this post


Link to post
Share on other sites
You need to link with your dll to get build time linking. To load it dynamically at runtime you need to have your project be able to see the dll's .h file so it can compile.

Then you need to call load library on it and getprocaddress to get a pointer to the method you want to call.

Cheers
Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by chollida1
You need to link with your dll to get build time linking.

I presume you mean link with the associated .lib file...

Quote:
To load it dynamically at runtime you need to have your project be able to see the dll's .h file so it can compile. Then you need to call load library on it and getprocaddress to get a pointer to the method you want to call.

Actually, no, you don't need any sort of header file or other data definition for dynamic loading; GetProcAddress is enough.

Ta,
Twilight Dragon

Share this post


Link to post
Share on other sites
Thanks for the quick reply, TDragon! I didn't include the library, and that's why I got the linker errors. With your help I think I'm closer to getting this thing to work, but now I get this error messagebox whenever I try to run my test program that makes a call to 'Stoopid()':

The application failed to initialize properly (0xc000007b). Click on OK to terminate the application.

And in the 'Debug' window, it says 'Debugger:: An unhandled non-continuable exception was thrown during process load'. Dang!

If I take out 'Stoopid()', the program runs fine.

I'm sure I'm leaving another simple thing out... Would you help me on this one too, please? Thanks!

Share this post


Link to post
Share on other sites
Never mind, I'm a tard. I found where I accidently left a copy of my DLL in BOTH the root folder of my project AND in the 'Debug' folder (and they were both different versions of the same DLL). D'OH! It seems to work now... Thanks for the help guys, I really appreciate it!

Share this post


Link to post
Share on other sites
Quote:
Original post by TDragon
Quote:
Original post by chollida1
You need to link with your dll to get build time linking.

I presume you mean link with the associated .lib file...

Quote:
To load it dynamically at runtime you need to have your project be able to see the dll's .h file so it can compile. Then you need to call load library on it and getprocaddress to get a pointer to the method you want to call.

Actually, no, you don't need any sort of header file or other data definition for dynamic loading; GetProcAddress is enough.

Ta,
Twilight Dragon


Not necessarily. What if you have defined some specific types in your .h file. Say a class or structure that a method in your dll returns. How will your app know about it

Say
class A
{
int a;
};

and in your app.

A myA = FromDllMethod();

myA.a = 10;

How can the compiler figure this out without the .h???

Cheers
Chris

Share this post


Link to post
Share on other sites
Then you could indeed, create a shared header file with that definition in it; or you could just add it to the new app's code.

But notice that I did say, "Actually, no, you don't need any sort of header file or other data definition for dynamic loading; GetProcAddress is enough."

Because you don't NEED it for dynamic loading in most cases. Need is the key word. You don't always need to include a specific header.

Okay, I've probably beaten the issue to death.

Love luck 'n' lollipops,
Twilight Dragon

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!