Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I compiled this short "Hello World" program in Visual Studio 2017 and failed.
I used console type of project:

#include <afxwin.h>

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
	::MessageBox(NULL, _T("Hello World"), _T("MFC Programming"), MB_OK);
}


What I have tried:

after changed to Windows type with MFC static linked configuration, it still failed.
I tried all kinds of way and did not make it to work.

I can not find the right configuration for this short program in Visual Studio 2017.
Posted
Updated 13-Jul-20 0:26am
Comments
Garth J Lancaster 12-Jul-20 0:53am    
"and failed." & "it still failed." you really need to use Improve question and include how it failed and what the errors were - as succinctly as possible, but more information that you have provided so far - it could be an easy or hard 'fix'

You are correct in that a console program type of project is wrong, it doesnt include enough of the framework(s), most noteably MFC that you need, so I'd have expected a better result with "MFC static linked" .. dont lose heart, it does get easier .. likely, you could start with a Dialog based Project .. this may help https://www.youtube.com/watch?v=WHjJr6Va4cM ..
Richard MacCutchan 12-Jul-20 5:46am    
I do not think there is enough code in that sample to make a program. However, since you have not told us what happens when you build and/or run it we cannot offer suggestions.

Also, that is not a Console project, it is a Windows MFC application.

I posted a comment that because of the Redis cache issues hasnt yet re-appeared .. anyway, In VS2019, I generated a 'Windows Desktop Application', copied your one line thus
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPWSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.
    ::MessageBox(NULL, _T("Hello World"), _T("MFC Programming"), MB_OK);

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_CPQATEST1, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);
compiled and ran, no issues - I have VS2012 somewhere, Im not sure what the equivalent project is, they keep moving all the options around :-(

Update 1 : In VS2012 I generated a simple Dialog based MFC app, and put the ::MessageBox line in the OnInitDialog() code, and it ran there as well ..
Update 2 : In VS2012, I went New Project -> Win32 Console Application -> Next -> Windows Application, put the line
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	::MessageBox(NULL, _T("Hello World"), _T("MFC Programming"), MB_OK);
and again no issues.

FWIW (and iirc), ::MessageBox is a Win32 API function, NOT a MFC Class/Construct
 
Share this answer
 
v3
Comments
Southmountain 13-Jul-20 21:10pm    
you are absolutely right. yours is the answer I am looking for.
thanks a million!
Quote:
I compiled this short "Hello World" program in Visual Studio 2017 and failed.
I used console type of project:

You can't use Windows code in a console program.
Quote:
after changed to Windows type with MFC static linked configuration, it still failed.

Changing a project type to something else is tricky and prone to errors. it's not worth going down that route and figuring out what to do, especially since you will likely never need that knowledge again.

My suggestion: create a completely new Windows project from scratch, in a different project directory. That will give you all the code you need, except for that MessageBox.

If you can't get that to run, come back and report with full details what is going wrong and what error message(s) you are getting.
 
Share this answer
 
Comments
Southmountain 13-Jul-20 21:14pm    
thanks for your time. I did not list all error messages because I tried all kinds of ways to make it work and message varied...
Stefan_Lang 14-Jul-20 4:47am    
Normally, just posting the state of the code at one point and the very first error message you get for that specific code should be enough for us to get a hunch what is going wrong.

However, if you messed with your project settings it's getting a lot harder for us to figure out what's happening. Therefore, to save you and all of us a lot of headaches, please go by my advice, throw your project files away, and create a new project with correct settings. Visual Studio will create most of the files and code that you need, and if something doesn't work, we'll have a much better chance to help you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900