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

## 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 on other sites
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.

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 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 on other sites
Quote:

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.

Ta,
Twilight Dragon

##### 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 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 on other sites
Quote:
Original post by TDragon
Quote:

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.

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;
};

A myA = FromDllMethod();

myA.a = 10;

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

Cheers
Chris

##### 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

1. 1
Rutin
38
2. 2
3. 3
4. 4
5. 5

• 11
• 12
• 14
• 9
• 9
• ### Forum Statistics

• Total Topics
633350
• Total Posts
3011467
• ### Who's Online (See full list)

There are no registered users currently online

×