Resolving the 'WinMain@16' Link Error for Console

Hello everyone!

Here's the basic template of the DLL I've been trying to create: http://www.cplusplus.com/forum/windows/77582/


I Googled out this error and it seems that the main I'm using is for a Window application but there's no clear answer, they just go, "Oh you know, it's because you're using a console application".

Now, do I just replace this line
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
by this
int main()?

Also, I want to insist that whatever alternative causes the least security gap, this is why I'm posting the question here...

Thanks!
Last edited on
I dont know but that was the same error I got when I wrote int nain() instead of int main()
Windows programs have two entry points depending on a linker setting.

If the app is linked as a Console app, it expects to have main() as the entry point.

If the app is linked as a Windows app, it expects to have WinMain as the entry point.

You can view/change this setting in Visual Studio by looking at Properties->Linker->System.
I want to create a DLL, what options do I have to set (I'm using Code::Blocks, but you can point directions for MSVC as well). In other words, how do I write the entry point of the code to be compiled as a DLL?
If you're building a DLL, the linker should not be looking for a program entry point. It sounds like your project is set up to build an app rather than a lib.
So there shouldn't be main() or WinMain() or whatsoever? This is weird because the template comes with WinMain().

EDIT: I meant DLLMain(), this is the main I'm using!
Last edited on
A DLL's entry point is DLLMain(), I mean it gets called by the loader.

But a main or WinMain will never be called in a DLL, nothing expects them to be there and they shouldn't be there. If the linker is complaining that one these is missing, the linker is misconfigured and is trying to link a program.
The correct entry point for most CRT implementations is DllMain, not DLLMain.
Here is a code summary:


function2.c
1
2
3
4
5
6
7
// ... includes
// ... defines
// ... function definitions

// ... function2()
// ... function3()
// ... function4() 




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
#ifndef _MAIN_H_
#define _MAIN_H_

// ... more includes

#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C"
{
#endif
	
	// function header
        double function1(double * argv);

#ifdef __cplusplus
}
#endif

#endif /* _MAIN_H_ */ 



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
32
33
34
35
36
#include "main.h"

// ... more function definitions
						   
// function1() definition

// !! function1() used to be main(), but I changed a lot of things so that it 
// becomes a function on its own




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
}
It's not your code that's the problem, it's your build settings; specifically, your linker settings.
@kbw

KUDOS! I built my first ever DLL ^^
Topic archived. No new replies allowed.