I have this compilation error that I do not understand what is wrong. My Microsoft Visual Studio project is a Win32 project (not a console):
1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16 1>C:\Users\Soribo\Desktop\C++ Programming\Visual C++ Programming\KeyboardHook\Release\KeyboardHook.exe : fatal error LNK1120: 1 unresolved externals
EDIT: after creating #include "stdafx.h" as the first line, a compilation error:
1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16 1>C:\Users\Soribo\Desktop\C++ Programming\Visual C++ Programming\KeyboardHook\Release\KeyboardHook.exe : fatal error LNK1120: 1 unresolved externals
EDIT: hmm, I defined a WinMain function, right? see below code:
#include <windows.h> #include <cstdlib> #include "stdafx.h" using namespace std; static HHOOK keyboardHook; static HINSTANCE gInstance; // Functions List // LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam ); HHook ActivateKeyboardHook( HookProc hookProc, HINSTANCE hInstance ); bool DeactivateKeyboardHook( HHook keyboardHook ); int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) //int WINAPI WinMain( HINSTANCE gInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { WNDCLASSEX wc; HWND hwnd; MSG Msg; //Step 1: Registering the Window Class wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = gInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(DKGRAY_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = L"Custom Class"; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // if registration of main class fails if(!RegisterClassEx(&wc)) { MessageBox(NULL, L"Window Registration Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } // Step 2: Creating the Window hwnd = CreateWindowEx( WS_EX_CLIENTEDGE, L"Custom Class", L"App Name", WS_CAPTION|WS_MINIMIZEBOX|WS_VISIBLE|WS_OVERLAPPED|WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 600, 500, NULL, NULL, gInstance, NULL); if(hwnd == NULL) { MessageBox(NULL, L"Window Creation Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK); return 0; } ShowWindow(hwnd, nCmdShow); UpdateWindow(hwnd); // Step 3: The Message Loop while(GetMessage(&Msg, NULL, 0, 0) > 0) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.wParam; } LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam ) { /*if ( code < 0 ) { return CallNextHookEx( NULL, code, wParam, lParam ); }*/ switch ( wParam ) { case WM_KEYDOWN: { MessageBox( NULL, L"Notify", L"Key Down", MB_OK ); } break; case WM_KEYUP: { MessageBox( NULL, L"Notify", L"Key Up", MB_OK ); } break; case WM_SYSKEYDOWN: { MessageBox( NULL, L"Notify", L"Sys Key Down", MB_OK ); } break; case WM_SYSKEYUP: { MessageBox( NULL, L"Notify", L"Sys Key Up", MB_OK ); } break; default: { } break; } return CallNextHookEx( NULL, nCode, wParam, lParam ); } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: { keyboardHook = ActivateKeyboardHook( &LowLevelKeyboardProc, gInstance ); } break; case WM_COMMAND: { switch(LOWORD(wParam)) { default: break; } } break; case WM_CLOSE: { DeactivateKeyboardHook( keyboardHook ); DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: break; } return DefWindowProc(hwnd, msg, wParam, lParam); } HHOOK ActivateKeyboardHook( HookProc hookProc, HINSTANCE hInstance ) { return SetWindowsHookEx( WH_KEYBOARD_LL, hookProc, hInstance, 0 ); } bool DeactivateKeyboardHook( HHook keyboardHook ) { return UnhookWindowsHookEx( keyboardHook ); }