How to receive simplest Windows message on UWP XAML MVVM app? - windows

My big-picture problem:
I need to send a signal from a Windows 10 desktop app (or a service, really) to a UWP XAML MVVM app on the same OS instance / machine.
I was using named semaphores in the global namespace, but these don't work at all on UWP (by design, for security reasons maybe). No joy.
I tried UWP sockets, and this works with the UWP as listener only if the client is on a remote machine. Is this a security design decision, too? No idea. Exempting the app from loopback restriction does not help, since that only applies if the UWP app is the client making the request. No joy.
OK, so I'm left with sending a Windows message to the specific window on the OS...
My test app is the AdventureWorks sample for UWP on GitHub. How do I get this to handle a Windows message sent from a different process? I'm stumped.
Below is my test client code.
QUESTION:
How do I handle this message in AdventureWorks? It's important to keep code changes to a minimum, but currently I'm stuck and have no idea to proceed. (It seems to be a tightly-held secret...)
Please help! Sample code please.
class Program
{
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, String lpWindowName);
[DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern uint RegisterWindowMessage(string lpString);
[DllImport("kernel32.dll")]
static extern uint GetLastError();
static void Main(string[] args)
{
const string lpClassName = "ApplicationFrameWindow";
const string lpWindowName = "AdventureWorks.Shopper";
IntPtr hwnd = FindWindow(lpClassName, lpWindowName);
uint messageId = RegisterWindowMessage("MetaAutomationMessage");
int sendMessageResult = SendMessage(hwnd, messageId, IntPtr.Zero, IntPtr.Zero);
Console.WriteLine(string.Format("Message result is '{0}', ", sendMessageResult));
uint lastError = GetLastError();
Console.WriteLine(string.Format("GetLastError result is '{0}', ", lastError));
}
}

Windows 10 UWP App service communication can be one of the options to send set of key-values between UWP apps.
Reference:
https://msdn.microsoft.com/en-us/windows/uwp/launch-resume/how-to-create-and-consume-an-app-service

The UWP app is sandboxed, so you can't send or receive messages, etc. by design to a Win32 app.
There are somewhat potentially less desirable options like enabling loopback communications over a local socket: https://stackoverflow.com/a/33263253/95190 (which maybe is what you tried -- it's not clear).
However a UWP app may not be the best platform choice today if you need to communicate from a locally installed service to your Universal app.
There are some ways of launching an app and providing some data as part of the launch, but a Win32 service should not launch interactive user processes (so, it wouldn't be a good way to pass data either).
You may want to consider building your app as a WPF app if communication with a Win32 app/service is needed.

Related

How to install hook procedure that monitors keystroke messages (WH_KEYBOARD_DLL or WH_KEYBOARD) using Easyhook C++

I'm using Easyhook to hook keystrokes, but I don't understand how Easyhook works. How to install a hook to hook KeyboardProc (callback function).
This is API of the LhInstallHook:
static void LhInstallHook(
IntPtr InEntryPoint,
IntPtr InHookProc,
IntPtr InCallback,
IntPtr OutHandle
)

how to sign out from windows 10 program in uwp?

how to log off(sign out) windows 10 using uwp?
I have do in win-forms but codes are not working in uwp.
[DllImport("user32.dll")]
public static extern int ExitWindowsEx(int operationFlag, int rationReason);
private void button_Click(object sender, RoutedEventArgs e)
{
//Application.Current.Exit();
ExitWindowsEx(0, 0);
}
In non-UWP programme You can create a new process and run
%windir%\System32\shutdown.exe /l /t 0
But this is not available to UWP programmes. Check out the Stack Overflow link here for another take on this problem. But it looks like you are out of luck.
As i see your code above , you are actually closing the application. In UWP , Application works on states like Activated, Running, Suspended or Exited
If you want to log out from the application , it means that you would like to wipe of all user data and his Navigation History.
Make A function that clears the NavigationTrace and once the NavigationEntry is cleared , navigate the user to the login screen/home page

ExitWindowsEx windows 7 shutdown does not work

I am trying to get a Windows 7 machine to reboot from a C# WPF application. To this extent I've added the following code (I used an enum, but to keep the code short I'm just inserting the constants here):
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
public static void Reboot() {
ExitWindowsEx(0x02, 0x0)
}
On Windows 7 machines this does absolutely nothing (for me anyway). Changing the 0x2 (reboot) to 0x0 (logoff) does make the code logoff the current user, but the Reboot code doesn't seem to work.
Using the GetLastError API call didn't do much either. It just says something about the function having completed successfully.
For now I just cope by calling the shutdown command with /r /f, but I'd prefer to be able to call a Windows API directly from my application, so any help would be greatly appreciated.
You are not checking for errors correctly. Only check if the function returns false, do not pinvoke GetLastError(), use Marshal.GetLastWin32Error() instead. Best way:
public static void Reboot() {
if (!ExitWindowsEx(0x02, 0x0)) {
throw new System.ComponentModel.Win32Exception();
}
}
With high odds that you'll find out that you don't have enough privileges to reboot the machine. AdjustTokenPrivileges required, check the MSDN article.

AXUIElement available in MonoMac?

I'd like to be able to use the Cocoa Accessibility API in a MonoMac application, but I can't find any references to it in the MonoMac documentation. Has AXUIElement.h been mapped yet?
I couldn't find anything on it either.
Some parts can be implemented with DllImport. For example, I did the following:
public const string AccessibilityLibrary = "/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices";
[DllImport (AccessibilityLibrary)]
extern static bool AXAPIEnabled();

CreateDesktop and DDE

I am setting up a scratch desktop to run another application in a "silent mode" - the other app is noisy and throws all sorts of windows around while it processes.
I have used the info here: CreateDesktop() with vista and UAC on (C, windows)
and CreateDesktop works - I can create the other desktop, I can launch the application into the other desktop (I see it launching in task manager) - but when I try to interact with the app via DDE, the DdeConnect call hangs until it times out.
And here's how I'm calling CreateDesktop:
LPSTR desktopName = "MYDESKTOPNAME";
HDESK hDesk = CreateDesktop(desktopName , NULL, NULL, 0, DESKTOP_SWITCHDESKTOP|
DESKTOP_WRITEOBJECTS|
DESKTOP_READOBJECTS|
DESKTOP_ENUMERATE|
DESKTOP_CREATEWINDOW|
DESKTOP_CREATEMENU, NULL);
Here is CreateProcess to actually launch the app into the new desktop:
STARTUPINFO startupInfo;
GetStartupInfo(&startupInfo);
startupInfo.lpDesktop = desktopName;
PROCESS_INFORMATION procInfo;
memset(&procInfo, 0, sizeof(procInfo));
if (CreateProcess(NULL, exePath, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &startupInfo, &procInfo)){
WaitForInputIdle(procInfo.hProcess, INFINITE);
CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);
}
If it matters, the call to DdeInitialize:
DWORD afCmd = APPCLASS_STANDARD | APPCMD_CLIENTONLY | CBF_SKIP_ALLNOTIFICATIONS;
UINT rslt = ::DdeInitialize(&ddeInst, NULL, afCmd, 0);
Here is the DdeConnect call (the hsz* parameters, etc... are all fine - if I launch the app into the regular desktop, the calls all work perfectly).
hConv = ::DdeConnect(ddeInst,
hszService,
hszTopic,
NULL);
This call just hangs for ~60 seconds.
Is this a security issue of some sort? i.e. the windows messages aren't passing between desktops? Or does anyone have any suggestion on how to troubleshoot this further?
The documentation for CreateDesktop contains a cross-reference to the Desktops topic, which says
Window messages can be sent only between processes that are on the same desktop.
The overview topics are important. They provide background information to help you understand the feature.
Raymond explains why the messages don't get through. In order to solve the problem, assuming you continue with the separate desktop, you will simply need to run the process that performs the DDE in the same desktop as the target app. If you need to communicate between your process on the main desktop and the target process then you will need to use some other form of IPC.

Resources