Winrt Message Dialog not showing - c++

I am trying to display a Message Dialog in C++ (winrt) from a Desktop Windows App targeting Win 10 x64. The following code executes but the dialog is not shown. Return code from ShowAsync is good
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
Microsoft::WRL::Wrappers::RoInitializeWrapper init(RO_INIT_SINGLETHREADED);
Microsoft::WRL::ComPtr<ABI::Windows::UI::Popups::IMessageDialogFactory> messageDialogFactory;
Microsoft::WRL::Wrappers::HStringReference messageDialogFactoryId(RuntimeClass_Windows_UI_Popups_MessageDialog);
Windows::Foundation::GetActivationFactory(messageDialogFactoryId.Get(), messageDialogFactory.GetAddressOf());
Microsoft::WRL::ComPtr<ABI::Windows::UI::Popups::IMessageDialog> messageDialog;
Microsoft::WRL::ComPtr<ABI::Windows::Foundation::Collections::IVector<ABI::Windows::UI::Popups::IUICommand*>> uiCommands;
messageDialogFactory->CreateWithTitle(Microsoft::WRL::Wrappers::HStringReference(L"XXX").Get(), Microsoft::WRL::Wrappers::HStringReference(L"YYY").Get(), messageDialog.GetAddressOf());
messageDialog->get_Commands(uiCommands.GetAddressOf());
Microsoft::WRL::ComPtr<ABI::Windows::UI::Popups::IUICommandFactory> uiCommandFactory;
Microsoft::WRL::Wrappers::HStringReference commandFactoryId(RuntimeClass_Windows_UI_Popups_UICommand);
Windows::Foundation::GetActivationFactory(commandFactoryId.Get(), uiCommandFactory.GetAddressOf());
w::ComPtr<pu::IUICommand> button;
uiCommandFactory->CreateWithHandler(Microsoft::WRL::Wrappers::HStringReference(L"ZZZ").Get(), new ButtonHandler(), button.GetAddressOf());
uiCommands->Append(button.Get());
Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncOperation<ABI::Windows::UI::Popups::IUICommand*>> showOperation;
HRESULT hr = messageDialog->ShowAsync(showOperation.GetAddressOf());
MSG msg;
while( ::GetMessage(&msg, 0, 0, 0) > 0 )
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return 0;
}

Should have read documentation - API doesn't have the DualApiPartitionAttribute

Related

How to interpret docs on WinMain?

Please look at the WinMain docs.
int __clrcall WinMain(
[in] HINSTANCE hInstance,
[in] HINSTANCE hPrevInstance,
[in] LPSTR lpCmdLine,
[in] int nShowCmd
);
How should I interpret this? Is in attribute optional which is denoted by the square brackets? If so, what does it look like.
I tried to compile a simple application in Visual Studio with the following flags:
/clr /permissive- /Zc:twoPhase-
#include <Windows.h>
int __clrcall WinMain(
[in] HINSTANCE hInstance,
[in] HINSTANCE hPrevInstance,
[in] LPSTR lpCmdLine,
[in] int nShowCmd
) {
return 0;
}
But it won't compile and gives the following errors.
C2373 WinMain': redefinition; different type modifiers
C2337 'in': attribute not found
E0337 linkage specification is incompatible with previous "WinMain" (declared at line 1033) WinBase.h
E0147 declaration is incompatible with "int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)" (declared at WinBase.h)
The [in] decorations are just for documentation. You can ignore them and just implement WinMain like in the sample WinMain docs:
If building for Multibyte (a.k.a. ANSI):
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPSTR cmdline, int cmdshow)
{
return MessageBoxA(nullptr, "hello, world", "caption", 0);
}
If building for Unicode:
#include <Windows.h>
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hInstPrev, LPWSTR cmdline, int cmdshow)
{
return MessageBoxW(nullptr, L"hello, world", L"caption", 0);
}

Trying to create a desktop application in C++ on visual studio

I'm trying to program a desktop application that will eventually communicate with an arduino (which has not come in yet) to control LED lights around my room and PC. I am on microsoft's page on how to create a desktop application and have copied 99% of this program off them so I could play around with it and see what it does. However I cannot even run the thing because of 2 issues.
When I try to use WndProc it says "function definition for WndProc not found and,
LRESULT CALLBACK WndProc(
_In_ HWND hWnd,
_In_ UINT message,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
When I try to use WinMain it says "Inconsistent annotation for WinMain: this instance has no annotations"
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
Here is the full source code:
#include <windows.h>
#include <tchar.h>
#include <stdlib.h>
#include <string.h>
static TCHAR szWindowClass[] = _T("Colour Control");
static TCHAR szTitle[] = _T("Colour Control 1.0");
HINSTANCE hInst;
int CALLBACK WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
);
LRESULT CALLBACK WndProc(
_In_ HWND hWnd,
_In_ UINT message,
_In_ WPARAM wParam,
_In_ LPARAM lParam
);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);
if (!RegisterClassEx(&wcex))
{
MessageBox(NULL,
_T("Call to RegisterClassEx failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
// The parameters to CreateWindow explained:
// szWindowClass: the name of the application
// szTitle: the text that appears in the title bar
// WS_OVERLAPPEDWINDOW: the type of window to create
// CW_USEDEFAULT, CW_USEDEFAULT: initial position (x, y)
// 500, 100: initial size (width, length)
// NULL: the parent of this window
// NULL: this application dows not have a menu bar
// hInstance: the first parameter from WinMain
// NULL: not used in this application
HWND hWnd = CreateWindow(
szWindowClass,
szTitle,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
500, 100,
NULL,
NULL,
hInstance,
NULL
);
if (!hWnd)
{
MessageBox(NULL,
_T("Call to CreateWindow failed!"),
_T("Windows Desktop Guided Tour"),
NULL);
return 1;
}
// Store instance handle in our global variable
hInst = hInstance;
// The parameters to ShowWindow explained:
// hWnd: the value returned from CreateWindow
// nCmdShow: the fourth parameter from WinMain
ShowWindow(hWnd,
nCmdShow);
UpdateWindow(hWnd);
// Main message loop:
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
why are you defining WinMain and WndProc in your .cpp file. You should not do that instead you have to provide only definition of these functions. Like for WindowsProc
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
//Code
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
You have to delete deceleration of WndProc and WinMain.

[c++ windows]capturing mouse click event from a window that isn't mine

I want to detect when the mouse is pressed on my wallpaper.
So I got the wallpaper handle and now I'm trying to add a message loop to it, but its not working for some reason.
Here's my code so far:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
HWND hWallPaper = getWallPaperHWND();
if (hWallPaper != NULL)
{
MSG msg;
while (GetMessage(&msg, hWallPaper, 0, 0))
{
MessageBox(NULL, "msg", "got message", MB_OK);
}
}
else
MessageBox(NULL,"Window wasn't found","window not found",MB_OK);
return 0;
}
Why isn't it showing a message box when I fire an even on the wallpaper, like when I click on it or even just move the mouse?

C++ run a function in async and not block the ui

I have a small app that checks the dotnet framework, if it is not installed it will install it
Now when the application starts i want to popup a gif image with something like loading and in background check the framework and install.
the catch here is that it can not have any prerequisite to run the application
here is what i have till now
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
int exitCode = -1;
showPic(hInstance);
MessageBox(0L, L"Dotnet will installed", L"Alert", 0);
auto fut = std::async(std::launch::async, DoWork, hInstance, lpCmdLine);
fut.wait();
exitCode = fut.get();
return exitCode;
}
showPic()
void showPic(HINSTANCE hInstance)
{
loadImage(hInstance);
// create window
wnd = createWindow(hInstance);
SetWindowLong(wnd, GWL_STYLE, 0);
ShowWindow(wnd, SW_SHOW);
}
loadImage(HINSTANCE hInstance)
void loadImage(HINSTANCE hInstance)
{
imageDC = CreateCompatibleDC(NULL);
imageBmp = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
imageBmpOld = (HBITMAP)SelectObject(imageDC, imageBmp);
}
Now what is happening here is that the if i dont show the messagebox the picture does not load in the window, and still the window goes into not responding mode also i could not get it work with gif, only with bmp images
any help is appriciated
now since i wait for fut it is obvious that it will block the ui until it has the value, what is the workaround for that
This should be simple. Create the window, show it, call the thread, go to main message loop. When thread is done it will destroy the window.
struct T_data {
HWND hWnd;
HINSTANCE hInstance;
LPTSTR cmdline;
int exitCode;
};
DWORD WINAPI taking_too_long(LPVOID p) {
Sleep(2000); //wait at least 2 seconds!
T_data* data = reinterpret_cast<T_data*>(p);
auto fut = std::async(std::launch::async, DoWork, data->hInstance, data->lpCmdLine);
fut.wait();
data->exitCode = fut.get();
//make sure the window handles IDM_EXIT to close itself
PostMessage(data->hWnd, WM_COMMAND, IDM_EXIT, 0);
}
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR lpCmdLine, int) {
T_data data;
data.exitCode = -1;
data.hWnd = hWnd;
data.hInstance = hInstance;
data.cmdline = lpCmdLine;
data.hWnd = showPic(hInstance);
CreateThread(NULL, 0, taking_too_long, &data, 0, NULL);
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return data.exitCode;
}

how do i fix a "identifier is undefined" error in a simple C++ application?

I have searched everywhere, but to no avail. Could someone tell me what's wrong with this code? It is giving me the "Error: Identifier "XXX" Is Undefined"
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
MyRegisterClass(hInstance);
if(!InitInstance (hInstance, nCmdShow))
return false;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
There are no MyRegisterClass nor InitInstance global functions in Windows.h