I have problems with dll file

Feb 7, 2009 at 9:23pm
Hello I would need some help with dll files I am trying to make dll file that display two msgbox and then create console window.

I made my project with code::blocks

in main.cpp file I have

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
28
29
30
31
32
33
34
#include "main.h"

// a sample exported function
int DLL_EXPORT MsgBox(int x = 0){
    MessageBoxA(0, "Nice Text", "DLL Message", MB_OK | MB_ICONINFORMATION);
    MessageBoxA(0, "Stupid Text", "DLL Message", MB_OK | MB_ICONINFORMATION);
    cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
    cin.get();
    return x;
}

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
}


and in main.h file I have
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
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

/*  To use this exported function of dll, include this header
 *  in your project.
 */

    #define DLL_EXPORT __declspec(dllexport)


#ifdef __cplusplus
extern "C"
{
#endif

int DLL_EXPORT MsgBox(int x);
int DLL_EXPORT cout(int x);
int DLL_EXPORT cin(int x);

#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__ 


I get error
1
2
3
4
Z:\My Documents\11-2008\dllfail\dllfail\main.cpp||In function `int MsgBox(int)':|
Z:\My Documents\11-2008\dllfail\dllfail\main.cpp|7|error: invalid operands of types `int ()(int)' and `const char[43]' to binary `operator<<'|
Z:\My Documents\11-2008\dllfail\dllfail\main.cpp|8|error: request for member `get' in `cin', which is of non-class type `int ()(int)'|
||=== Build finished: 2 errors, 0 warnings ===| 


Could anyone say me please what I have wrong here?
Thank you
Feb 7, 2009 at 9:31pm
'cout' and 'cin' refer to functions you declared in main.h, lines 19 and 20, not to the standard objects. Include iostream.
Feb 11, 2009 at 10:08pm
I added #Include <iostream> at the beginning of main.h, but now I have problems with cin.get();.

Is there possible to have standard dll template so all I have to do is copy functions in main file and compile it?

I just asking because I am beginner at C++ and I actually know nothing about it :S


I also need dll file to empty given directory (delete all files in it) and I want give directory name as parameter.

thanks for readin my terrible englis :)
Feb 11, 2009 at 10:34pm
Don't name you functions cin, cout etc. These names are already taken by the standard IOStream objects.

Your compiler is having a hissy because it cannot tell the difference between which one you want to use.
Feb 11, 2009 at 10:42pm
ok now I changed code, but and I am able to compile it, but I dont see cmd window.

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
28
29
30
31
32
33
34
35
36
#include "main.h"
#include <iostream>
using namespace std;

// a sample exported function
int DLL_EXPORT MsgBox(int x = 0){
    MessageBoxA(0, "Nice Text", "DLL Message", MB_OK | MB_ICONINFORMATION);
    MessageBoxA(0, "Stupid Text", "DLL Message", MB_OK | MB_ICONINFORMATION);
//    cin.get();
  cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
  cin.get();
    return x;
}

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
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef __MAIN_H__
#define __MAIN_H__

#include <windows.h>

//#include <iostream>


/*  To use this exported function of dll, include this header
 *  in your project.
 */

    #define DLL_EXPORT __declspec(dllexport)


#ifdef __cplusplus
extern "C"
{
#endif

int DLL_EXPORT MsgBox(int x);
//int DLL_EXPORT cin(int x);
//int DLL_EXPORT cout(int x);


#ifdef __cplusplus
}
#endif

#endif // __MAIN_H__
Feb 11, 2009 at 10:47pm
WinMain() doesn't create a command window. It's a GUI orientated entry-point into your application.

You will need to manually create a new command window if you want to use WinMain(), or swap to int main().
Topic archived. No new replies allowed.