0

So I'm currently stuck trying to get the windows api to show up (language is c++), when I try to run the program I get the following errors. (though I am getting now error showing up in regards to the red underline). I am currently using visual studio community as my IDE.

"unresolved external symbol _main reference in function "int _cdel invoke(main(void))"

"1 resolved externals"

I've already checked online, and tried it both as a win 32 console program and a win 32 project (which some have listed as a way to resolve the program.) However no results. I have no idea what the error could be. (also for reference I am using the following tutorial as a starting base https://www.youtube.com/watch?v=012pFrYE5_k, but am not using the open GL library as all I am trying to accomplish is make a very simplified template of a win32 window). Any Idea's?

Code: main.h

#pragma once
#pragma once
#include<Windows.h>
#include<tchar.h>
#include<iostream>
#define WINDOW_WIDTH = 800 //remember to leave off the ; at the end of define sets(currently not being used
#define WINDOW_HEIGHT = 600
HWND hWnd;

Code: main.cpp

#include "main.h" //remember <> means library, "" means within your project. 
// long results, CALL BACK, windows procedure, basically the basics of what to do. UINT (unsigned INT)
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
    switch (msg) {
    case WM_CREATE:
        break;
    case WM_DESTROY:
    case WM_QUIT:
    case WM_CLOSE:
        PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hWnd, msg, wParam, lParam);
}
/*
remember that this is a run time instance, and is in essence the same as Main (char[args]) {}
returning 0 ends the operation of the win32 library.
*/
int WINAPI winMain(HINSTANCE hInstance, HINSTANCE hPrevious, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASS wc; //default instance for the scope of the window (new window named WC for WINDOW CLASS)
    MSG msg;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH); //default background color, set to stock light gray
    wc.hCursor = LoadCursor(hInstance, IDC_ARROW); //default cursor
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = _T("NAME");
    wc.lpszMenuName = NULL;
    wc.style = CS_VREDRAW | CS_HREDRAW;  //windows style in bit form, 0X00 hex if you were to print them
    if (!RegisterClass(&wc)) //pointer the to the window, if it doesn't exist call this simple error handle
    {
        MessageBox(NULL, _T("error: cannot reg window class"), _T("error"), MB_OK);
        return 0; // kills 
    }
    hWnd = CreateWindow(L"NAME", //reference of the object already defined as wc
        L"Window Title", //title
        WS_OVERLAPPEDWINDOW, //basic window style
        0, //x start,
        0, //y start,
        800,
        600,       //set all the dimensions to default value
        NULL,                //no parent window
        NULL,                //no menu
        hInstance,
        NULL);
    if (!hWnd)
    {
        MessageBox(NULL, L"ERROR: cannot create window", L"ERROR!", MB_OK);
        return 0;
    }
    while (1)
    {
        while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE)) {
            if (GetMessage(&msg, NULL, 0, 0))
            {
                break;
            }
        }
        DispatchMessage(&msg);
        TranslateMessage(&msg);
    }
    return(int)msg.wParam;
}
Metric
  • 88
  • 1
  • 11
  • Your message loop is rather odd. Use the standard loop. Why write `#pragma once` twice? Declaring `hWnd` in the header file is going to comeback to haunt you. Don't do that. You'll have multiple instances which is not what you want. You don't want to use `tchar` these days. You are not supporting Windows 98. – David Heffernan Jun 22 '16 at 16:18
  • TranslateMessage() goes before DispatchMessage(), not after. – andlabs Jun 22 '16 at 17:10
  • That's pretty broken code, I'm not going to point out all that's wrong. To get your immediate issue resolved do the following: In the project settings, go to *General* and select *"Use Unicode Character Set"*. On the Linker settings tab, choose *System* and change the subsystem to *"/SUBSYSTEM:WINDOWS"*. Replace your function name `winMain` with `wWinMain`. That'll link your executable. It'll break horribly at runtime, though. – IInspectable Jun 22 '16 at 17:24
  • Thanks, that at least got it working, I know the code was pretty terrabad it was just the first example I've worked through for win32 that I could find. – Metric Jun 22 '16 at 19:36
  • *Strongly* favor using the Win32 Project template to get you up and going. It ensures that you will not make these basic mistakes. Furthermore, I would recommend skipping the YouTube videos and purchasing [a used copy of Petzold's 5th Edition](https://www.amazon.com/Programming-Windows%C2%AE-Fifth-Developer-Reference/dp/157231995X) if you want to learn Windows programming. – Cody Gray - on strike Jun 23 '16 at 14:19
  • Might not be a bad idea to get some sort of reference. Plus as it is out of date a bit, the knowledge would mostly be the rules rather then the current (constantly) changing aplication. Thanks, looks like a good text, I'll look into it. – Metric Jun 23 '16 at 15:01

1 Answers1

2

In main.cpp, you need to capitalize the function name: int WINAPI WinMain

tsandy
  • 911
  • 4
  • 10