views:

511

answers:

2

I'm trying to make a pop up message box with "Hello World" written on it. I started off with File>New Project>Visual C++>CLR>Windows Form Application Then I dragged a button from the toolbox onto the form, double clicked it entered

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
MessageBox("Hello World");
}

then I compiled... but I got an error message saying

error C2440: '' : cannot convert from 'const char [12]' to 'System::Windows::Forms::MessageBox'

+5  A: 

You need:

MessageBox::Show("Hello World");

(Tested according to your instructions in Visual Studio 2005.)

RichieHindle
Yay!!! it works!!! thank you richie!!
then accept his answer!
Saif Khan
+1  A: 

I'm not sure what your ultimate goals are, but the subject line mentioned a "Windows Application in C" -- you've created a C++/CLI application, which isn't really the same thing.

C++/CLI is Microsoft's attempt to create a C++ dialect closer to the .NET runtime.

If you want to build a C program, start with a Visual C++ -> Win 32 Project.

In the generated code, in the _tWinMain function, add a call to the native MessageBox function:

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    MessageBox(NULL, _T("Hello world!"), _T("My program"), MB_OK);

// ...
}

That should get you started.

Kim Gräsman
Im trying to learn theForger's Win32 API Programming Tutorialbut I don't even know how to get started... I've been having trouble completing their first assignemnt =(
OK, then you shouldn't be using the CLR project types, but rather the native Win32 projects.Try starting here: http://winprog.org/tutorial/start.html and start from a Visual C++ -> Win32 -> Win32 Project. It's probably best for you to begin with an Empty project (checkbox on page 2 of the project wizard.)
Kim Gräsman