I was going through the forger's win32 API tutorial, and I thought "hey, I'll be clever and put all this stuff into special functions", well it doesn't work and I think knowing why would help with my C skills.
Main.cpp
#define WIN32_LEAN_AND_MEAN //Cuts the crap
//Includes//////////////////////////////////////////////////////////////////////
#include "Functions.h"
//WinMain///////////////////////////////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG Msg;
CreateMyWindClass();
CreateMyWindow();
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Functions.cpp
//FunctionDefinitions///////////////////////////////////////////////////////////
//Main Window Procedure/////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
{
DestroyWindow(hwnd);
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
//CreateWindClass///////////////////////////////////////////////////////////////
int CreateMyWindClass()
{
//wc is static, so there's no need for a return value, just use wc
static WNDCLASSEX wc;
extern HINSTANCE hInstance;
extern char g_szClassName[];
//Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
return 1;
}
int CreateMyWindow()
{
extern HINSTANCE hInstance;
extern int nCmdShow;
extern char g_szClassName[];
static HWND hwnd;
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 1;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
}
and Functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
////////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include "Functions.cpp"
//Consts////////////////////////////////////////////////////////////////////////
const char g_szClassName[] = "myWindowClass";
//FunctionDeclarations//////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
int CreateMyWindClass();
int CreateMyWindow();
#endif
the linker says:
Functions.o(.text+0x0):Functions.cpp: multiple definition of `_Z7WndProcP6HWND__jjl@16'
main.o(.text+0x0):main.cpp: first defined here
Functions.o(.text+0x80):Functions.cpp: multiple definition of `CreateMyWindClass()'
main.o(.text+0x80):main.cpp: first defined here
Functions.o(.text+0x192):Functions.cpp: multiple definition of `CreateMyWindow()'
main.o(.text+0x192):main.cpp: first defined here
main.o(.text+0xb9):main.cpp: undefined reference to `hInstance'
main.o(.text+0x19c):main.cpp: undefined reference to `hInstance'
main.o(.text+0x1ff):main.cpp: undefined reference to `nCmdShow'
Functions.o(.text+0xb9):Functions.cpp: undefined reference to `hInstance'
Functions.o(.text+0x19c):Functions.cpp: undefined reference to `hInstance'
Functions.o(.text+0x1ff):Functions.cpp: undefined reference to `nCmdShow'
I don't redefine anythng at all, and I don't know where my undefined references are coming from. I thought I defined everything.
And yes, I chose Win32 GUI project, not console.
[Edited by - Drakkcon on August 3, 2004 1:00:20 AM]