Inferno Web Development
  • Home
  • Forums
  • About
  • Links
  • Contact

Tutorials List

  • C++ Tutorials

Articles

  • Java
  • PHP
  • Photoshop
  • C#
  • C++ Qt GUI
  • C++ Win32 API
  • C++
  • MASM32
  • General News
  • JavaScript
  • Web Development
  • Windows Tweaks

Popular Tutorials

All time:

  • Website Header with 3D Fold Up / Lift Effect
  • C++ Win32 API Tutorial
  • Perfect C++ String Explode Split
  • Simple C++ DLL Loading a Message Box
  • Simple C++ Pointers and References

Related Articles

  • C++ Win32 API Simple GUI Wrapper
  • C++ Win32 API Tutorial
  • Winsock Client Tutorial
  • Winsock Server Tutorial

Programming Tutorials

Home

Simple C++ DLL Loading a Message Box

Submitted by Baran Ornarli on Mon, 11/17/2008 - 07:02.
  • C++ Win32 API

Table of Contents

  1. YouTube Tutorial
  2. DLL Project
  3. DLL Header File
  4. Main DLL Program
  5. Main C++ Program

This tutorial shows you how to create a C++ Win32 DLL (Dynamic Link Library) which you can then use to call functions from that DLL in your main C++ program. I will be calling a MessageBox from the DLL and load it inside my main C++ program using LoadLibrary and GetProcAddress. I will also exchange integers between the DLL and the main program.

YouTube Tutorial

I also have a video tutorial for this on youtube:

DLL Project

I first start by creating our DLL, using CodeBlocks you can create a new project as a Dynamic Link Library (DLL). It may be different in other IDEs or editors. Somewhere you have to specify it's a DLL project.

DLL Dynamic Link Library

Create 2 files, one C++ file (.cpp or .cxx) and one header file (.h).

DLL Header File

This will be our DLL header file:

#ifndef __MAIN_H__
#define __MAIN_H__
 
#include <windows.h>
 
/*  To use this exported function of dll, include this header
 *  in your project.
 */
 
#define DLL_EXPORT __declspec(dllexport)
 
 
#ifdef __cplusplus
extern "C"
{
#endif
 
int DLL_EXPORT MsgBox(int x);
 
#ifdef __cplusplus
}
#endif
 
#endif // __MAIN_H__

Basically, there is a definition called __MAIN_H__ which will allow our header file to only be called once, because if it is not defined, I define it and include the code, otherwise I don't repeat the same code.

I include windows.h which has our C++ Win32 API functions and then define DLL_EXPORT as __declspec(dllexport) which just means that the function can be exporting via DLL.

Then I check if it's a C++ compile or a C compile, if it is C++ the preprocessor will use extern "C" so that it works in C programs as well.

Then I define an int DLL_EXPORT function called MsgBox with a parameter (int x). Thus, it will return an int and take an int as an argument.

Main DLL Program

This is our main.cpp file. Our header file was called main.h.

#include "main.h"
 
// a sample exported function
int DLL_EXPORT MsgBox(int x = 0){
    MessageBox(0, "Join us at Inferno Dev!", "DLL Message", MB_OK | MB_ICONINFORMATION);
    return x;
}
 
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; // successful
}

I have included the same function in the header file, with integer parameter and return value as a DLL_EXPORT type.

The default value for x is 0.

Then MessageBox which is a Win32 API function will display a message box with no owner (the first parameter), the message is then written, then the title of the message box, and finally MB_OK which means the messagebox will display an OK button, and MB_ICONINFORMATION which means messagebox will have an ICONINFORMATION icon.

Then I return the integer.

DllMain function is the standard main function for all DLLs. I wasn't doing anything special so I didn't put any real code here.

Now compile this dll as "InfernoDevelopment.dll" (or name your project as InfernoDevelopment) because that is the DLL we call in the code below.

Main C++ Program

This will be our main program that will call our DLL function and load our DLL as well.

#include <iostream>
#include <windows.h>
 
using namespace std;
 
typedef int (*MsgFunction)(int);
 
HINSTANCE hinstDLL;
 
int main(){
  MsgFunction MsgBox(0);
  hinstDLL = LoadLibrary("InfernoDevelopment.dll");
  if(hinstDLL != 0){
      MsgBox = (MsgFunction)GetProcAddress(hinstDLL, "MsgBox");
  }
  if(MsgBox == 0)cout << "MsgBox is NULL\n";
  int x = MsgBox(5);
  if(x == 5){
    cout << "Message displayed!\n";
  }
  FreeLibrary(hinstDLL);
  return 0;
}

I declare my includes, iostream for cout and cin. Windows.h for LoadLibrary, GetProcAddress, and FreeLibrary.

I typedef (define a type) an int returning (*MsgFunction) (the name of the type) and an integer argument. This is my definition for the function I am about to call.

Then I declare MsgFunction type called MsgBox, set it equal to zero so that I can check if the function was actually loaded properly.

HINSTANCE hinstDLL is our Win32 instance of that DLL. I set LoadLibrary("InfernoDevelopment.dll") as hinstDLL, calling our DLL file.

Note: DLL should be in the SAME folder as this exe.

I check if hinstDLL is not 0, and is working. Then I cast the GetProcAddress(hinstDLL, "MsgBox"); as a (MsgFunction) type and set that to MsgBox. The parameter in GetProcAddress will take the instance and find the process address where there is a function called "MsgBox" in the DLL.

Finally we call the function and check if we got the exact same integer as a return or a zero. I also checked if MsgBox was 0, and if so, an error will be displayed noting that the MsgBox wasn't properly loaded by GetProcAddress.

Then I FreeLibrary(hinstDLL) to avoid memory leaks and to let the DLL go, because we already called our function.

If you have any problems, please visit our forums.


5
Average: 5 (1 vote)
»
  • Share Tutorial

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <img> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h4> <h2>
  • Lines and paragraphs break automatically.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • You can use BBCode tags in the text. URLs will automatically be converted to links.
  • Table of contents based on the <h*> tags

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
                .oooooo.       ooooooooo.              oooooo   oooo   .ooooo.   
d8P' `Y8b `888 `Y88. `888. .8' 888' `Y88.
oooo ooo 888 888 888 .d88' .oooo.o `888. .8' 888 888
`88b..8P' 888 888 888ooo88P' d88( "8 `888.8' `Vbood888
Y888' 888 888 888 `"Y88b. `888' 888'
.o8"'88b `88b d88b 888 o. )88b 888 .88P'
o88' 888o `Y8bood8P'Ybd' o888o 8""888P' o888o .oP'


Enter the code depicted in ASCII art style.

Navigation

  • Home
  • Forums
  • Image Gallery
  • Links
  • About
  • Contact

Why Register? Contribute articles and tutorials to ID, earn titles, learn from ID programming projects, and advertise your blog in our forums.

  • Forum Community
  • Register Now
  • Write an Article


Copyright © Inferno Development 2008. All Rights Reserved.