0

I am trying to control the size and position of a UWP APP (Windows Mixed Reality Portal) via a sepate app. In my case, I am using a console app for simplicity. A Command script would also work for what I want to achieve.

I have tried Windows api such as MoveWindow,SetWindowPos but they do not work as expected and GetWindowRect returns a 0,0,0,0 rect. I can get the window handle but not change the size/position.

My reason for doing this is to send virtual mouse keys to the app in order to initialise the front position of the Windows Mixed Reality system. Sending the virtual keys are fine but I am having trouble automating shifting of the position of the uwp app itself.

#include <iostream>
#include <ShObjIdl.h>
#include <atlbase.h>
#include <tlhelp32.h>

BOOL CALLBACK EnumWindowsProcBack(HWND windowHandle, LPARAM lParam) {
    DWORD searchedProcessId = (DWORD)lParam;  // This is the process ID we search for (passed from BringToForeground as lParam)
    DWORD windowProcessId = 0;
    GetWindowThreadProcessId(windowHandle, &windowProcessId); // Get process ID of the window we just found
    if (searchedProcessId == windowProcessId) {  // Is it the process we care about?
        //std::cout << "moving window..\n";
        //bool s=MoveWindow(windowHandle, 0, 0, 1920, 1080, true);
        SetWindowPos(
            windowHandle,
            HWND_TOP,
            0,
            0,
            600,
            600,
            SWP_NOSIZE
        );
        return FALSE;  // Stop enumerating windows
    }
    return TRUE;  // Continue enumerating
}

void MoveWindowToFixedLocation(DWORD processId) {
    EnumWindows(&EnumWindowsProcBack, (LPARAM)processId);
}

HRESULT LaunchApp(LPCWSTR AUMID, DWORD &pid)
{
    HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
    if (FAILED(hr))
    {
        wprintf(L"LaunchApp %s: Failed to init COM. hr = 0x%08lx \n", AUMID, hr);
    }
    {
        CComPtr<IApplicationActivationManager> AppActivationMgr = nullptr;
        if (SUCCEEDED(hr))
        {
            hr = CoCreateInstance(CLSID_ApplicationActivationManager, nullptr,
                CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&AppActivationMgr));
            if (FAILED(hr))
            {
                wprintf(L"LaunchApp %s: Failed to create Application Activation Manager.hr = 0x%08lx \n", AUMID, hr);
            }
        }
        if (SUCCEEDED(hr))
        {
            //DWORD pid = 0;
            hr = AppActivationMgr->ActivateApplication(AUMID, nullptr, AO_NONE,
                &pid);
            if (FAILED(hr))
            {
                wprintf(L"LaunchApp %s: Failed to Activate App. hr = 0x%08lx \n", AUMID, hr);
            }
        }
    }
    CoUninitialize();
    return hr;
}


int main() {

    DWORD pid = 0;
    LaunchApp(L"Microsoft.Windows.HolographicFirstRun_cw5n1h2txyewy!App", pid);
    //cout << pid;
    MoveWindowToFixedLocation(pid);
}
  • 1
    C++ has no intrinsic concept of a window. So in order to move a window, you need to use the relevant system API, in this case the Windows API. Please rephrase your question in terms of why the Windows API is not working, rather than in terms of how to avoid the Windows API. – JaMiT May 16 '19 at 18:18
  • @JaMiT what do you mean? I am not trying to avoid the Windows API. I tried the typical api for wpf apps in general and it does not work. I have yet to find a UWP api that allows me to adjust UWP apps. As for why it is not working, I would guess that UWP does not allow access? – Jianwei Lee May 17 '19 at 07:31
  • You can [get the window's HWND](https://stackoverflow.com/q/34935077/17034). But doing this in another process is very unlikely to come to a good end. Not the least of this is that it is probably suspended. The UWP app needs to take care of this by itself. – Hans Passant May 17 '19 at 10:39
  • @JianweiLee It sounds like you don't want a Windows API solution when you state that they do not work (instead of asking why they do not work). Plus the phrase "native C++ code" in the title suggests you are looking for a solution that uses no API. – JaMiT May 17 '19 at 12:07
  • @JianweiLee Hmm... maybe it would be easier to grasp your intended question if you actually asked it? It occurred to me that the only question mark in your question is in a code comment explaining what an `if` condition means. So technically, there is not yet a question to answer unless one guesses. Also, an explicit question can help focus the answers. – JaMiT May 17 '19 at 12:21
  • @JaMiT edited the question for clarity – Jianwei Lee May 20 '19 at 05:34

1 Answers1

0

It's impossible. UWP app runs in own closed environment. A desktop application cannot sent it any signal.

Tania Chistyakova
  • 3,928
  • 6
  • 13