User forums > Using Code::Blocks
writing a dll
arturapps:
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 !
Seronis:
"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.
Seronis:
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;
}
--- End code ---
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.
arturapps:
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" ?
Seronis:
--- Quote from: arturapps 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" ?
--- End quote ---
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
}
--- End code ---
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__
--- End code ---
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]
Navigation
[0] Message Index
[#] Next page
Go to full version