#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,nShowCmd)
{
MessageBox(NULL, L"Hello World!", L"Just another Hello World program!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
Working through a tutorial for getting started with c++ and can't get the first hello world example to compile.
Using visual studio express 2012. All I've done is create a new project and copy this into it replacing the tmain function.
please does anyone know how to resolve this?
In standard C++ program you must have main function so program knows where to start
int main()
{
return 0;
}
Maybe try another tutorial. This one seems good.
http://blogs.msdn.com/b/devschool/archive/2013/01/08/writing-your-first-c-program-using-visual-studio-2012.aspx
Related
I was just playing around with the message box when this error code stumbled upon me "LNK2019 unresolved external symbol_main referenced in function "int__cdecl invoke_main(void)" (?invoke_main##YAHXZ)" and then right after another came up "LNK1120 unresolved externals".
I tried messing around with the precompiled Header settings, Checking all of my #include things whatever they are called and I also tried re-writing the whole thing! but nothing happened :(
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args,
int ncmdshow)
{
MessageBoxA(NULL, "uwu", "This is a title belive it or not!",
MB_OK);
return 0;
}
The expected output was a console window and then a little window that says "uwu" but instead I got an error.
There are several ways of dealing with this. The simplest and most obvious is probably to just use main instead of Winmain:
#include <Windows.h>
int main()
{
MessageBoxA(NULL, "uwu", "This is a title believe it or not!", MB_OK);
}
I have been able to fine the WinMain function. Now I'm trying to make a win32 app:
#include <Windows.h>
int WINAPI WinMain() {
MessageBox(NULL,"Test", "Hello World", NULL);
return 0;
}
and then I get error C2731.
You haven't provided any parameters to the WinMain() method, and the error C2731 is saying that this method cannot be overloaded.
From MSDN:
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow);
Error C2664 is most likely your call to MessageBox, so try changing to something like:
MessageBox(NULL, L"Test", L"Hello World", NULL);
I figured it out I wasn't useint main.cpp as my main source code file and my WinMain needed to be switched to wWinMain
Code:
#include <tchar.h>
#include <windows.h>
typedef INT (WINAPI * lolMessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
lolMessageBoxA UchwytMessageBoxA;
UchwytMessageBoxA = lolMessageBoxA(GetProcAddress(GetModuleHandle("User32.dll"),"MessageBoxA"));
UchwytMessageBoxA(NULL,"TEST WINDOWS","TEXT 2", MB_OKCANCEL | MB_ICONWARNING);
return 0;
}
When i called MessageBoxA normally before making hook the hooking part worked good. But when i erased that part and leaved only the 'hooking' part it fails...
Solved:
#include <tchar.h>
#include <windows.h>
typedef INT (WINAPI * lolMessageBoxA)(HWND,LPCSTR,LPCSTR,UINT);
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
lolMessageBoxA UchwytMessageBoxA;
LoadLibraryA("User32.dll");
UchwytMessageBoxA = lolMessageBoxA(GetProcAddress(GetModuleHandle("User32.dll"),"MessageBoxA"));
UchwytMessageBoxA(NULL,"TEST WINDOWS","TEXT 2", MB_OKCANCEL | MB_ICONWARNING);
return 0;
}
As presented here, there's no reason for the process to have loaded user32.dll. So the call to GetModuleHandle will return NULL. Then the call to GetProcAddress will also fail and return NULL. And well, you can see what happens next.
When you called MessageBoxA directly, the linker would have produced an import table that forced the loader to load user32.dll. But when you removed the call to MessageBoxA, there was no need for it to do so.
You should replace the call to GetModuleHandle with a call to LoadLibrary, and so force the library to be loaded.
Finally, the real reason for you having asked this question is that your code neglects error checking. Don't do that. Check the values returned by the Win32 function calls. Had you done so you would have been able to work this out for yourself.
I have a project which causes two assertion failures in succession, AFTER the 'main'-function is completed. This is very problematic because it doesn't show me the piece of code causing the problem.
I was able to narrow the problem down somewhat. With an empty main-function:
int main(int argc,char* argv[])
{
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
return main(__argc,__argv);
}
everything is working just fine. However as soon as I use any class or function from a specific dll (which is part of the project), the problem starts occurring:
int main(int argc,char* argv[])
{
Color col(255,0,0,255);
col.r += 1;
int r = HeapValidate(GetProcessHeap(),0,nullptr);
std::cout<<r<<std::endl;
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
return main(__argc,__argv);
}
the 'Color' class is part of that dll, but it's not the cause of the issue. 'HeapValidate' returns 1, which means the heap is valid according to the documentation.
The dll in question contains hundreds of thousands lines of code, which makes debugging even more difficult.
On a similar question, I found the suggestion to use WinDBG, but I'm not sure what to do with the result:
http://pastebin.com/zV27b51Z
It confirms that the issue is some sort of memory corruption, but I still don't know where it originates from.
Is there any way to reliably find the origin, or am I stuck using trial and error?
My OS is Windows 8.1, I'm using Visual Studio 2013.
Run the application under debugger and press Retry button in assertion messagebox. DebugBreak will be called and the debugger will take coltrol.
For the lack of the documentation on the Internet about this technologie, can someone please just explain the architecture of an ATL project (Service EXE) ?
And, give a full example of the main methods defined by Microsoft able to be written xxx.cpp item ?
An example of response to the second question:
extern "C" int WINAPI _tWinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/,
LPTSTR /*lpCmdLine*/, int nShowCmd)
{
return _AtlModule.WinMain(nShowCmd);
}
void CATLProject6Module::ServiceMain(DWORD dwArgc, LPTSTR* lpszArgv)
{
// I can do this this and this
CAtlServiceModuleT<CATLProject6Module, 100>::ServiceMain(dwArgc, lpszArgv);
}
//Run method()...
//Handler method()...
//...
This topic may help anyone interested in developing his first Active Template Library application (Service EXE) !
Thanks a lot !!