How do I show a Win32 MessageBox? - visual-studio

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'

You need:
MessageBox::Show("Hello World");
(Tested according to your instructions in Visual Studio 2005.)

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.

Related

C++ Program E0079 expected a type specifier

I have an c++ example project from an USB 3.0 Interface vendor called streamer application from cypress fx3. I wanted to get this to run first and see the potential behind the application but unfornately I'm getting a whole set of errors when building in Visual Studio 2017.
I get errors in the main file streamer.cpp showing me the errors:
Error (active) E0079 expected a type specifier Line 26
Error (active) E1986 an ordinary pointer to a C++/CLI ref class or interface >class is not allowed Line 28
in Code:
#include "stdafx.h"
#include <windows.h>
// windows.h includes WINGDI.h which
// defines GetObject as GetObjectA, breaking
// System::Resources::ResourceManager::GetObject.
// So, we undef here.
#undef GetObject
#include "Streamer.h"
#undef MessageBox
using namespace System::Windows::Forms;
using namespace Streams;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
try
{
Application::Run(new Form1()); // THIS IS LINE 26
}
catch (Exception *e) // THIS IS LINE 28
{
MessageBox::Show(e->StackTrace,e->Message);
}
return 0;
}
Form 1 is part of streamer.h . In streamer h the error amount exceedes 400.
Most often compiler tells me identifier expected (E0040) even for syntax like private and public. Then the "this" operator causes an error:
Error (active) E0258 'this' may only be used inside a nonstatic member >function
What I tried to get this running:
- Installing missing windows sdk version 8.1 via installation routine in windows system control
- Changing Common Language Runtime Support to /clr
- inluding all missing header-files, compiler is now finding these header files.
Seems to me that there is something missing in the source project. Can you push me in the right direction?
catch (Exception *e)
That is an unmanaged exception. You need to catch a managed exception:
catch (Exception^ e)

How to hide the console window in a Win32 project using Visual Studio 2010

I need to create an exe application with no console window or (any other window) during the start up of the application.
I tried the below for this:
Using Visual Studio 2010, created a Win32 Console Application as an Empty Project.
Added a header file "stdafx.h" to the project
Added a cpp file to the project and added the below code.
The project settings are visual stduio default.
#include "stdafx.h"
#include <windows.h>
#include "TlHelp32.h"
#include <iostream>
#include <string>
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
return 0;
}
The above code compiles good.
But if I change the Character Set to "Use Unicode Character Set", I am getting the following compilation error.
error C2731: 'WinMain' : function cannot be overloaded
I am building the application on a Windows 7 64 bit computer and Visual Studio Build platform as x64.
Thanks in advance for your help.
Yes, when you build with UNICODE in effect then the entrypoint takes a Unicode string for the command line argument. So that requires a different declaration:
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nShowCmd)
Or you can use #include <tchar.h> so it works either way, not much point to it these days:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
Create Windows service instead console app.
In Visual Studio, create a new "EMPTY Project". Add a new source file named "main.cpp". Use the following template (assumes you want to build with Unicode):
/************
main.cpp
************/
#define UNICODE 1
#include <windows.h>
int APIENTRY wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpCmdLine, int nShowCmd)
{
// Process Return Codes
const int SUCCESS=0, FAILURE=1;
return SUCCESS;
}

Documentation for writting your own dll for Rundll32.exe? [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
How to use Rundll32 to execute DLL Function?
Where can I find documentation (tutorials, books, etc.) to write my own dll's that can be run with rundll32.exe?
Here is the most basic Hello World sample I could come up with that will work with rundll.exe. Please follow along these steps:
Start a fresh WIN32 DLL project in Visual Studio (I used VS2010)
In dlllmain.cpp add:
// this shoud ideally go into the .h file I believe
__declspec( dllexport ) void CALLBACK EntryPoint(
HWND hwnd,
HINSTANCE hinst,
LPSTR lpszCmdLine,
int nCmdShow);
// our hello world function
void CALLBACK EntryPoint(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
{
int msgboxID = MessageBox(
NULL,
L"Hello World from Run32dll",
L"Hello World",
MB_ICONWARNING | MB_CANCELTRYCONTINUE | MB_DEFBUTTON2
);
switch (msgboxID)
{
case IDCANCEL:
// TODO: add code
break;
case IDTRYAGAIN:
// TODO: add code
break;
case IDCONTINUE:
// TODO: add code
break;
}
}
Add a module.def file to your project and edit the following snippet in it:
LIBRARY YourDll
EXPORTS
EntryPoint
Compile and then test from the commandline with
rundll32 YourDll.dll,EntryPoint
You should be greeted with a MessageBox with three buttons
I used the following url's to overcome C++ issues and EntryPoint not found in the early stages of my effort:
Rundll from user mricicle
How messagebox works
DependencyWalker
Exporting functions
Exporting with .def file
String literals

How to capture WM_SHOWWINDOW command in MFC

I am trying to do some action whenever dialog box is Shown. Its like we have modalless dialog, and we are hinding/showing the dialog on some button click. But we we need to perfomr some action whenever dialog is shown. I have added the WM_SHOWWINDOW message but control is not coming inside of OnShowWindow(BOOL bShow, UINT nStatus) function.
We are using ShowWindow(SW_HIDE) and ShowWindow(SW_SHOW) function to hide/show dialog box
Please suggest some pointer how to achieve that.
Thanks in advance
Mukesh
I tested this with notepad and Spy++ with the following code:
#include <Windows.h>
int CALLBACK WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
HWND hwnd = FindWindow(NULL, L"Untitled - Notepad");
ShowWindow( hwnd, SW_HIDE );
Sleep(4000);
ShowWindow( hwnd, SW_SHOW );
return ERROR_SUCCESS;
}
For hiding the window, you should be getting WM_SHOWWINDOW, WM_WINDOWPOSCHANGING, then finally WM_WINDOWPOSCHANGED.
For showing the window, the target did not receive WM_SHOWWINDOW, but still got WM_WINDOWPOSCHANGING and WM_WINDOWPOSCHANGED.
You could handle WM_WINDOWPOSCHANGED and check the flags in WINDOWPOS for SWP_HIDEWINDOW/SWP_SHOWWINDOW.

OpenGL ES Tutorial - 'Winmain': function cannot be overloaded

I'm trying to learn OpenGL ES with the "OpenGL ES Training Course" (An OpenGL ES tutorial). I use OPENGL-ES 1.1 WINDOWS PC EMULATION with visual studio 2010. I'm trying to compile the 'hello triangle' program and get an error:
'WinMain': function cannot be overloaded
EDIT: I have only one definition of WinMain in the project: The one in the 'hello triangle' source code (which I didn't write).
Could anyone tell me what's going on?
It sounds like you have two definitions of WinMain, or perhaps a prototype and a definition that disagree.
I had the problem too. It showed that I have overloaded the function:
My old text:
#include "windows.h"
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, long lpCmdLine, int nCmdShow)
{
}
and my new text:
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
With the new text it works
Try
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
// Your Code.
}
Instead Of
int WinMain(){
// Your Code.
}

Resources