Author Topic: writing a dll  (Read 5408 times)

Offline arturapps

  • Single posting newcomer
  • *
  • Posts: 4
writing a dll
« on: March 14, 2008, 01:13:53 pm »
Hi people !

I´m working on a project that must have a dll module. So , I used Codeblocks´s dll project template to start my dll project. Well, I noticed that codeblocks have generated main.cpp with some exported simbols as an example for me. So far so good , but would not have to exist a .h file  there ? How can I write a .h ( header) file in order to make other modules to include the exported simbols?

Thanks a lot !

Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Re: writing a dll
« Reply #1 on: March 14, 2008, 01:42:40 pm »
"File" menu -> "New" submenu -> "File..." menu option

If you need a new file.  But the project wizard for dll already gave you a file to use.  IE:  main.h

It even has the little bit of #define magic required so that the header can be used in both building the dll and in importing it.

Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Re: writing a dll
« Reply #2 on: March 14, 2008, 02:01:56 pm »
As an example in a new workspace I created a dll project just now named  'NewDLL' and built it with no problems.  Then created another console app project named UseDLL where the main.cpp consisted solely of:

Code
#include <iostream>
#include <main.h>

using namespace std;

int main(int argc, char ** argv)
{
    cout << "Hello world!" << endl;
    SomeFunction("Goodbye World...");
    return 0;
}

I set my include path to point to the NewDLL project so that the same main.h was being used and made sure that  'newdll' was in the Library list (along with proper search path for lib).  As expected the console window printed off Hello World and a box popped up saying goodbye.  This is a trivial example but i think thoroughly shows what you need.

Offline arturapps

  • Single posting newcomer
  • *
  • Posts: 4
Re: writing a dll
« Reply #3 on: March 14, 2008, 02:03:14 pm »
Thanks!
It´s very odd , because Codeblocks has not generated main.h ( I´m sure of that) . Instead , It put the
"define magic " on the main.cpp file . Should I create a main.h file myself and than copy the "define magic there" ?


Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Re: writing a dll
« Reply #4 on: March 14, 2008, 02:15:19 pm »
Thanks!
It´s very odd , because Codeblocks has not generated main.h ( I´m sure of that) . Instead , It put the
"define magic " on the main.cpp file . Should I create a main.h file myself and than copy the "define magic there" ?

Well I'm currently using C::B 8.02 vista install downloaded last week.  The  DLL Project wizard in my copy creates both a main.cpp and a  main.h.   Two files look like:

file:main.cpp
Code
#include "main.h"

// a sample exported function
void DLL_EXPORT SomeFunction(const LPCSTR sometext)
{
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION);
}

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
}


file:  main.h
Code
#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

void DLL_EXPORT SomeFunction(const LPCSTR sometext);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__


In all honesty you dont ned a .h file at all.  Its just a convienence factor.  What you DO need is in the DLL project itself your functions need defined as
void __declspec(dllexport) myFunction();

and in the project using those functions you need a declaration in the form of
void __declspec(dllimport) myFunction();


The way the DLL Project works is that it defines DLL_BUILD as a project specific define making the header file define 'DLL_EXPORT' appropriately with export linkage in the dll and with import linkage in any project needing the dll.

[attachment deleted by admin]
« Last Edit: March 14, 2008, 02:32:57 pm by Seronis »

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: writing a dll
« Reply #5 on: March 14, 2008, 03:36:28 pm »
Quote
In all honesty you dont ned a .h file at all.  Its just a convienence factor.  What you DO need is in the DLL project itself your functions need defined as
void __declspec(dllexport) myFunction();

and in the project using those functions you need a declaration in the form of
void __declspec(dllimport) myFunction();
And that is what this header does. In fact, dllexport is the only thing needed to build a DLL at all. DllMain() is entirely optional and useless (I even consider it harmful), and dllimport is only a compiler optimisation.

What you do need, however, is a consistent way to declare your functions and variables for both the library and a program using the library -- at least, if you plan to do anything more serious than "Hello DLL".
This is what the macro is about, it adds dllexport where it's needed, and only there, and leaves everything else intact and in sync.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Re: writing a dll
« Reply #6 on: March 14, 2008, 03:40:32 pm »
Few questions:

1. How do you consider dllmain() to be harmful? (as compared to just being used/not used)

2. dllimport is -only- an optimization?

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: writing a dll
« Reply #7 on: March 14, 2008, 05:11:34 pm »
1. Read MSDN's description. :)

2. It allows the compiler to eleminate a thunk in the DLL, so using that attribute effectively saves one pointer worth of memory per exported function plus some (insignificant) setup. If you omit it alltogether, nothing evil will happen, your program will work the same.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline arturapps

  • Single posting newcomer
  • *
  • Posts: 4
Re: writing a dll
« Reply #8 on: March 14, 2008, 06:18:32 pm »
Thanks people!

I´m going to write a header for using with my dlls wright now !

Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Re: writing a dll
« Reply #9 on: March 15, 2008, 04:49:36 am »
1. Read MSDN's description. :)

Well I already consider msdn to be harmful,  but i'll read the description later anyways =-)