Thread: Need a little help

  1. #1
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335

    Need a little help

    I'm just trying to get the Hello World program to display in a Win32 App (I think that's what I picked )
    Code:
    // New Test1.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include "stdio.h" 
    #include "iostream.h"
    #include "conio.h"
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    	cout<<"Hello World!";
    	getch();
    	return 0;
    }
    It compiles fine, but when I try and run the program it does do anything.

    MSVC++ Intro Version

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want a console in a win32 application (as oposed to a console application) you'll have to make your own -

    Code:
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    
    	LPCSTR output= "Hello World!";
    	LPCSTR terminate = "Press any key to continue...";
    	DWORD written;
    	DWORD read;
    	INPUT_RECORD ir;
    	
    	AllocConsole();
    	WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),
    		output,strlen(output),&written,NULL);
    
    	WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),
    		"\n",1,&written,NULL);
    
    	WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE),
    		terminate,strlen(terminate),&written,NULL);
    	
    	for(;; )
    	{
    	ReadConsoleInput(GetStdHandle(STD_INPUT_HANDLE),&ir,1,&read);
    	if (ir.EventType==KEY_EVENT)
    		break;
    	}
    
    	FreeConsole();
    
    	return 0;
    }

    Alternatively, select console app from the menu.
    zen

  3. #3
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Well that's what MSVC++ started me with. Then it had a spot that said code here, so I did and it won't do anything when I want it to run.

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    61
    Replace

    cout<<"Hello World!";
    getch();

    with

    MessageBox(NULL, "Hello World!", "My Program", MB_OK | MB_ICONINFORMATION);

    you can't use functions like cout or getch(); in a windows program!

  5. #5
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    Really!?! LOL, I never knew that.

    Thanks man, that really helps!

  6. #6
    Registered User Fool's Avatar
    Join Date
    Aug 2001
    Posts
    335
    So what kind of Windows thing was that? A Win32 App? Here's the code for anyone who wants to answer the question
    Code:
    // New Test1.cpp : Defines the entry point for the application.
    //
    
    #include "stdafx.h"
    #include "stdio.h" 
    #include "iostream.h"
    #include "conio.h"
    
    int APIENTRY WinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPSTR     lpCmdLine,
                         int       nCmdShow)
    {
    	MessageBox(NULL, "Hello World!", "My Program", MB_OK | MB_ICONINFORMATION);
    }

Popular pages Recent additions subscribe to a feed