3 Replies - 3486 Views - Last Post: 03 July 2012 - 10:22 PM

#1 machoolah  Icon User is offline

  • D.I.C Head

Reputation: -6
  • View blog
  • Posts: 87
  • Joined: 17-May 09

Questions about DLLs in C/C++

Posted 20 May 2012 - 03:18 PM

I have a few questions about DLLs I hope somebody could help me with:


1. What are DLLs good for?
2. Can you make DLLs using classic C++ (as opposed to C++/CLI)?
3. Can you make DLLs using C?
4. Is there any tutorial out there suitable for someone who has made command-line applications to learn creating DLLs?
Is This A Good Question/Topic? 0
  • +

Replies To: Questions about DLLs in C/C++

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,298
  • Joined: 28-March 11

Re: Questions about DLLs in C/C++

Posted 20 May 2012 - 03:46 PM

1a. Extending your program with Addins which are dlls
1b. Code injection
1c. Break your program up to make it easier to update, for a text editor, have all your parsing functions in a dll and when you need to fix something, just update the dll.
1d. and a bunch of other stuff.

2. I don't see why not.

3. Uh, yes and even Assembly.

4. Dlls are not much different than a regular exe, they just have a different entry point and export functions for use. You could search our forum, but here is a start:
http://www.dreaminco..._1&#entry176831
Was This Post Helpful? 0
  • +
  • -

#3 Cenron  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 13
  • Joined: 23-May 12

Re: Questions about DLLs in C/C++

Posted 01 July 2012 - 03:40 AM

View Postmachoolah, on 20 May 2012 - 03:18 PM, said:

I have a few questions about DLLs I hope somebody could help me with:


1. What are DLLs good for?
2. Can you make DLLs using classic C++ (as opposed to C++/CLI)?
3. Can you make DLLs using C?
4. Is there any tutorial out there suitable for someone who has made command-line applications to learn creating DLLs?



DLL's can be used to extend the functionality of a program by allowing an external library of functions that can be called.

You can create an interface into the DLL and as long as you maintain that interface, you can update the functionality of DLL without recompiling the main code.

So lets say for example your writing a game, you create a DLL that handles all the drawing of stuff to the screen ( for example take the following functions. )

DrawEngine.DLL
InitDrawEngine()
DrawSprite()
MoveSprite()
DestroyDrawEngine()



So then in your main program you import the DLL then create access to the functions

MyGame.exe
// This is pseudo code
import DrawEngine.DLL

// Dynamically Link the functions
FUNC_TEMPLATE InitDrawEngine = (FUNC_TEMPLATE)(DrawEngine.GetProcAddr(InitDrawEngine));
FUNC_TEMPLATE DrawSprite = (FUNC_TEMPLATE)(DrawEngine.GetProcAddr(DrawSprite));
FUNC_TEMPLATE MoveSprit) = (FUNC_TEMPLATE)(DrawEngine.GetProcAddr(MoveSprite));
FUNC_TEMPLATE DestroyDrawEngine = (FUNC_TEMPLATE)(DrawEngine.GetProcAddr(DestroyDrawEngine));

// Now call our functions since we linked them.
mainDisplay = InitDrawEngine();
DrawSprite( mainDisplay, drawCopper );
MoveSprite( mainDisplay, drawCopper, 20px, 100px );
DestroyDrawEngine( mainDisplay );



Now you can go back into your DLL project and modify the functionality of these function as much as you want, but as long as you dont change the function prototype then you wont have to modify and recompile your main program.


The other feature is you can create a DLL as if it was an EXE and when that DLL is loaded it will execute the code from start to finish. This option is usually used for code injection.

Here is a little guide with more detail.
http://www.flipcode....sing_DLLs.shtml

DLL's are very usful for big project like a game, because you can break up your code and only update the parts you want instead of having to recompile your entire project.
Was This Post Helpful? 2
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 833
  • View blog
  • Posts: 2,456
  • Joined: 20-March 10

Re: Questions about DLLs in C/C++

Posted 03 July 2012 - 10:22 PM

View Postmachoolah, on 20 May 2012 - 10:18 PM, said:

I have a few questions about DLLs I hope somebody could help me with:


1. What are DLLs good for?
2. Can you make DLLs using classic C++ (as opposed to C++/CLI)?
3. Can you make DLLs using C?
4. Is there any tutorial out there suitable for someone who has made command-line applications to learn creating DLLs?


1. This has been answered adequately.
2. Yes.
3. Yes.
4. Yes, here is one.

MSDN Dll Tutorial

Another older way of making the same Dll in above Tutorial in C++


main.h
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif


#ifdef __cplusplus
extern "C"
{
#endif
// Returns a + b
double DLL_EXPORT  Add(double a, double B)/>;
// Returns a-b
double DLL_EXPORT  Subtract(double a, double B)/>;
// Returns a*b
double DLL_EXPORT  Multiply(double a, double B)/>;

double DLL_EXPORT Divide(double a, double B)/>;
#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__






main.cpp

#include "main.h"

// a sample exported function
double DLL_EXPORT Add(double a, double B)/>
{
    return a + b;
}

double DLL_EXPORT Subtract(double a, double B)/>
{
    return a - b;
}

double DLL_EXPORT Multiply(double a, double B)/>
{
    return a * b;
}

double DLL_EXPORT Divide(double a, double B)/>
{
    if (b == 0)
        {

            MessageBox(NULL,"Error","2nd Parameter cannot be zero!",MB_OK);
            return 0;
        }
    else
        return a / b;


}


BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    switch (fdwReason)
    {
    case DLL_PROCESS_ATTACH:
        // attach to process
        // return FALSE to fail DLL load
        break;

    case DLL_PROCESS_DETACH:
        // detach from process
        break;

    case DLL_THREAD_ATTACH:
        // attach to thread
        break;

    case DLL_THREAD_DETACH:
        // detach from thread
        break;
    }
    return TRUE; // succesful
}




Thanks

Snoopy.

This post has been edited by snoopy11: 03 July 2012 - 10:26 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1