Create a DLL -- Need Your "Okay"

Hello everyone!

It's good to back in the Windows Programming forum. I read a couple of tutorials about creating a DLL in C and it all looks to me as template at this point. Following my question about calling an application from another, found here http://www.cplusplus.com/forum/general/77570/, I would be grateful if someone would tell me whether this would successfully work or not, I'm using gcc


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "main.h"



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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>
#include "h1.h"

#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT __declspec(dllimport)
#endif

void DLL_EXPORT perff(double * pd)
{
     // function code goes here | uses functions defined in h1.h
}

#endif // __MAIN_H__ 

Last edited on
For C, it's mostly ok.

C++ has name mangling, so you'd need to declare the function as extern "C" in a C++ program.

You should not declare functions in header files. You can put the prototype there, but not the function. That should go in a .c file, for C.

If you're working with a number of DLL's, then the name DLL_EXPORT will clash, so you'd chose a more targeted name like SAMPLE1_DLL_EXPORT.
i'm sorry for this, it's not an reply.i'm just a newbie to cpp programming can u please tell how and where do you program the .DLL files and how to use them in a live program.
please ignore the reply if it seems violating the forum rules but please don't ban me.
Thanking you in advance.
@ ankit2313: Please make your own thread, it makes it easier for people with similar questions to find it. Have you tried looking it up on Google? Are you stuck at some particular point? What do you want the DLL to do?
@kbw

I wrap extern "C" around void DLL_EXPORT perff(double * pd) in main.h and around BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) in main.c?


I can jam whatever I want in the .c file right, i.e. whatever functions or variables perff() uses can go in main.c too, there's no harm in doing that?

I think I need to #include <windows.h> also?
Last edited on
Topic archived. No new replies allowed.