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