Visual Studio: how to get output in "Win32" project
c++, visual-studio-2010
Solution
In you console project the main function has to be called main, not WinMain.
#include <iostream>
#include <windows.h>
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lparam)
{
LPTSTR title = 0;
GetWindowText(hwnd, title, sizeof(title));
std::cout << "Window Name: " << std::endl;
return true;
}
int main(int argc, char* argv[]) // SUBSYSTEM:CONSOLE
//int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine, int nCmdShow) // SUBSYSTEM:WINDOWS
{
EnumWindows(EnumWindowsProc, NULL);
std::cin.get();
return 0;
}
This will work for a Win32 console application project. It's exactly your code but I changed the signature of the WinMain function to be the standard main.
To know which of the two you should use, check the following setting: `Properties -> Linker -> System -> SubSystem`, if this is set to `Console (/SUBSYSTEM:CONSOLE)` it will expect the standard main signature, if it is set to `Windows (/SUBSYSTEM:WINDOWS)` it will expect the WinMain one.
Problem
I wanted to write a simple program that would find all open windows and display their window names; however, in Visual Studio under a "Win32 Project", the console isn't there to output anything by the "cout" function. But if I were to attempt to put the code under a "Win32 Console" project, I get errors about external dependencies (so I'm assuming I just can't do that?). So is there any way I can add the console to a "Win32" project? The code in question (unfinished, of course): ``` // ConsoleApplication2.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <windows.h> using namespace std; BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prev, LPSTR lpCmdLine, int nCmdShow){ EnumWindows(EnumWindowsProc, NULL); return 0; } BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lparam){ LPTSTR title = 0; GetWindowText(hwnd, title, sizeof(title)); cout << "Window Name: " << endl; return TRUE; } ``` Last but not least, the erros I get when I attempt to run the code in a "Win32 console" project. ``` Error 2 error LNK1120: 1 unresolved externals c:\users\justin\documents\visual studio 2012\Projects\ConsoleApplication5\Debug\ConsoleApplication5.exe 1 1 ConsoleApplication5 Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup c:\Users\Justin\documents\visual studio 2012\Projects\ConsoleApplication5\ConsoleApplication5\MSVCRTD.lib(crtexe.obj) ConsoleApplication5 ```