Hello All,
I was trying to make dll to play with ctypes in python. I'm not good at C++ and I studied it lightly looong ago and only familiar with very simple thing. After search the net I found smo instructions but when I try to Build (with CodeBlocks), I get error:

C++ examples\dlltest.cpp|5|error C2447: '{' : missing function header (old-style formal list?)|
||=== Build finished: 1 errors, 0 warnings ===|

I don't know what is wrong :(
I want simple DLL that have two functions, one for adding numbers, and another for multiplying numbers. But I cannot get even the first function alone to work

Thanks for your help
dlltest.cpp

// dlltest.cpp
#define DLLEXPORT extern "C" _declspec(dllexport)
DLLEXPORT int sum(int a, int b);
{return c = a+b;}

dlltest.h

// dlltest.h
int sum(int, int);

Recommended Answers

All 6 Replies

To see how the _declspec(dllexport) and _declspec(dllimport) are supposed to be used, try creating a new 'dynamic link library' project in CodeBlocks. When you build the DLL project, remember to define the BUILD_DLL macro (do it via the project's build options).

There are a couple of errors, see below

DLLEXPORT int sum(int a, int b) ; // no semicolon needed here!
{return c = a+b;} // 'c' is not declared anywhere

Thanks Sir,
I'm going to change that and will be back

This is error I get
c:\users\apostle of jesus\documents\codeblock projects\testdll\main.h|4|fatal error C1083: Cannot open include file: 'windows.h': No such file or directory|
||=== Build finished: 1 errors, 0 warnings ===|


Also code blocks generates two files main.cpp and main.h----should I keep them?
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
}

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__

testdll.cpp

// dlltest.cpp
#define DLLEXPORT extern "C" _declspec(dllexport)
DLLEXPORT int sum(int a, int b)
{int c;
    return  c= a+b;}

dlltest.h

// dlltest.h
int sum(int, int);

IDE code Blocks
Compiler VC++ 2005 express ed
Windows Vista Home Premium

This is error I get
c:\users\apostle of jesus\documents\codeblock projects\testdll\main.h|4|fatal error C1083: Cannot open include file: 'windows.h': No such file or directory|
||=== Build finished: 1 errors, 0 warnings ===|

Appears that the include paths are not set up properly hence windows.h cannot be located. In case you have the gcc's compilers installed, try compiling the project with gcc instead of VS Express or configure the include/library paths so that VS Express finds the files.

Also code blocks generates two files main.cpp and main.h----should I keep them?

Well, those two files make up one working DLL project, keep them if you want.

I'll Change compiler
But Sometime I'll start Learning C++ and Many recommends VC++ over GCC. So No matter whether it is superior or not, I would like to know how to fix that Error

Thanks for your Good answers

I turned to GCC and this error came:
C:\Users\Apostle of Jesus\Documents\Codeblock Projects\testdll\testdll.cpp|3|error: expected constructor, destructor, or type conversion before '(' token|
||=== Build finished: 1 errors, 0 warnings ===|

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.