2

I am developing a windows app in visual studio 2015 using C++. I need GetForegroundWindow() and GetWindowText() to return the app that is currently focusing on. However, it says GetForegroundWindow() and GetWindowText() are undefined even I've included "windows.h". I tried go to the definition of GetForegroundWindow(), it led me to "WinUser.h", so I included "WinUser.h" as well. But it still could not help. Here are my code:

#include "MainPage.xaml.h"
#include <windows.h>
#include <WinUser.h>
#include <winapifamily.h>
#include <string>
#include <stdio.h>
#include <iostream>

using namespace App1;
using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;
using std::cout;
using std::endl;

MainPage::MainPage()
{
InitializeComponent();
}

HWND currenthwnd, hwnd = NULL;
char wnd_title[1024];

void App1::MainPage::Focus_app_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
currenthwnd = GetForegroundWindow();
GetWindowText(hwnd, wnd_title, sizeof(wnd_title));
cout << wnd_title << endl;
}

Any ideas? Thanks in advance!

randomcat
  • 413
  • 1
  • 4
  • 16
  • 3
    You are writing a WinRT app, not a desktop app. You cannot use many of the legacy winapi functions, like GetForegroundWindow(). Nor are you permitted to interact with other processes like this, WinRT apps run in a sandbox that forbids this kind of interaction. That keeps the kind of user that downloads an app from the Store happy. High odds of course that you simply used the wrong project template to get started. – Hans Passant Nov 11 '16 at 08:42
  • @HansPassant I see! I was using the universal windows app template. Is there any way I could detect the app that is currently focused using this template? Or should I create a win32/Windows Forms/WPF application instead?Thanks!! – randomcat Nov 11 '16 at 20:48
  • I was not kidding. Your app isn't even running anymore when another app gets the foreground. – Hans Passant Nov 11 '16 at 23:21

1 Answers1

4

GetForegroundWindowand GetWindowText in the WinUser.h are declared inside #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP) macro block. So you could use them only for windows desktop applications.

Ari0nhh
  • 5,720
  • 3
  • 28
  • 33
  • Thanks!! I just tried to add the declaration and the errors are gone. But it threw new errors in App.xaml.obj saying "LINK2019: unresolved external symbol public: __cedcl ..", which I haven't see before. Do you know what's happening? Sorry I'm new to visual studio and windows. – randomcat Nov 11 '16 at 05:14
  • Of course it would. This API is unavailable for your target platform, so you simply could not use these methods. No way around it. – Ari0nhh Nov 11 '16 at 05:18
  • But my target platform is windows desktop as you said. If no way around, do you know any other methods I can use? Thanks! – randomcat Nov 11 '16 at 05:21
  • Check your project preprocessor defines. See if `WINAPI_FAMILY` macro is defined. – Ari0nhh Nov 11 '16 at 05:28
  • 2
    Do not try and work around this by resetting the `PARTITION` for a UWP app. You can fix the compilation and linking errors with some trivial work-arounds, but your runtime behaviour will be undefined. You also won't be able to submit to the Windows Store (even if you don't want to go to the Store, you should not feel good about an app that uses unsupported "features" and can stop working at any point in time). – Peter Torr - MSFT Nov 11 '16 at 20:18
  • @PeterTorr-MSFT Thanks!! Does it mean I create a win32/Windows Forms/WPF application instead of universal windows app? – randomcat Nov 11 '16 at 20:48
  • Yes, if you have a requirement to use those APIs. – Peter Torr - MSFT Nov 12 '16 at 17:00
  • @PeterTorr-MSFT Thanks!! – randomcat Nov 12 '16 at 20:36