"GetForegroundWindow: identifier not found" in visual studio 2015 - winapi

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!

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.

Related

WinRT too many errors in base.h

I am using Visual Studio and trying to compile simple WinRT example:
pch.h:
// pch.h
#pragma once
#include <Windows.h>
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Web.Syndication.h>
#include <iostream>
ConsoleApplication2.h:
#include "pch.h"
using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Web::Syndication;
int main()
{
winrt::init_apartment();
Uri rssFeedUri{ L"https://blogs.windows.com/feed" };
SyndicationClient syndicationClient;
SyndicationFeed syndicationFeed = syndicationClient.RetrieveFeedAsync(rssFeedUri).get();
for (const SyndicationItem syndicationItem : syndicationFeed.Items())
{
winrt::hstring titleAsHstring = syndicationItem.Title().Text();
std::wcout << titleAsHstring.c_str() << std::endl;
}
}
I get "too many errors" in winrt\base.h library file.
What I do wrong and how to compile this simple example?
I guess i found solution.
Visual Studio Menu -> Project -> Manage NuGet Packages -> Browse
install Microsoft.Windows.CppWinRT
Rebuild.

How do I change visibility settings in Visual Studio 2017 using C++

I'm trying to make a button visible then invisible with a button click, but I am new to windows coding so any help would be helpful.
#include "pch.h"
#include "MainPage.xaml.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;
// The Blank Page item template is documented at
https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
int blockbase = 1 ;
MainPage::MainPage()
{
InitializeComponent();
}
void App1::MainPage::Button_Click(Platform::Object^ sender,
Windows::UI::Xaml::RoutedEventArgs^ e)
{
greetingOutput->Text = "Hello, " + nameInput->Text + "! \n Welcome to the
worlds worst quiz show!!! \n Here is the one and only question: What
director directed the classic film Battleship Potempkin?";
AnswerChoiceA-> Visibility = "Visible" ;
}
Currently I am getting errors C2664 and E1767
C2664:'void Windows::UI::Xaml::UIElement::Visibility::set(Windows::UI::Xaml::Visibility)': cannot convert argument 1 from 'const char [8]' to 'Windows::UI::Xaml::Visibility'
E1767: function "Windows::UI::Xaml::UIElement::Visibility::set" cannot be called with the given argument list

How to wrap a c++ class with an opencv object with c++/cli

I would like to wrap a complex c++ class which used opencv object with c++/cli for using in c# and vb. I'm using Visual Studio 2015. I'm using the x64 versions of of the OpenCV library and compiling also to x64.
Following is the header.h file of a small test class, just as an example:
#pragma once
#include <vector>
#include "opencv2/opencv.hpp"
using namespace std;
class compC
{
public:
compC(int * pInt, int arrSize);
int sumArray();
private:
vector<int> vec;
cv::Mat img;
};
This is going to be compiled as a win32-application to a .lib file without any problems.
The example wrapper class (wrapper.h) would look like:
#pragma once
#include "../ConsoleApplication1/header.h"
#include "../ConsoleApplication1/body.cpp"
using namespace System;
namespace WrapperLibrary {
public ref class WrapperClass
{
public:
WrapperClass(int * pInt, int arraySize);
int getSum();
private:
compC * pcC;
int sum;
};
}
The second class would be compiled as a dll with /clr (as c++/cli project).
First I got an error that opencv.hpp could not be find and had to add the include directory of opencv in to the wrapper project. But I cannot compile it and get a lot of errors, mainly pointing to linking problems "unresolved external problems ..." in connection with the opencv-mat object.
If I remove the lines
#include "opencv2/opencv.hpp"
and
cv::Mat img;
in the test class (header.h) all will be fine.
What did I made wrong? How can I wrap those c++ class with c++/cli?

Error while trying to use strings while working with windows application forms

When creating a new class while working in a project that is based around windows application forms. I have a problem where string becomes unusable. I get errors the say things like
"a member of a managed class cannot be of a non-managed class type"
"IntelliSense: a function type involving a generic parameter cannot have an ellipsis parameter"
"IntelliSense: linkage specification is incompatible with previous "bsearch_s"(declared at line 426)"
Person.h
#pragma once
#include <string>
using namespace std;
ref class Person
{
public:
Person(void);
string name;
};
Person.cpp
#include "stdafx.h"
#include "Person.h"
#include <string>
using namespace std;
Person::Person(void)
{
name = "Bob";
}
If someone has a solution to this or a work around that isn't creating my own string class I would love to hear it as this has been bothering me for days.
String header in CLI C++ is already included in the System namespace.
String works as a pointer. Using the cap ^ handler
//This works:
#include "stdafx.h"
using namespace System;
void function()
{
String^ simpleStr = "Bob";
}

Visual C++ failure to include

I have two mutually dependent header files: BarP.h and addNewProductForm.h, built using Microsoft CLR components, which look something like this (they are way too long to include intact):
BarP.h:
#pragma once
#include "addNewProductForm.h"
#include "editBarOptionsForm.h"
#include "editDecayParamsForm.h"
#include "editExistingItemForm.h"
#include <math.h>
#include <string>
#include <iostream>
#include <fstream>
#include <msclr\marshal.h>
namespace BarPricer3 {
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace msclr::interop;
ref struct productDat{...};
productDat^ createProduct(String^ name,double firstPrice,double lastPrice,double lastDemand){...};
public ref class BarP{
...
private: System::Void createNewProductForm(...){
BarPricer3::addNewProductForm^ newProductForm = gcnew addNewProductForm;
newProductForm->ShowDialog(this);
}
}
addNewProductForm.h
#pragma once
#include "BarP.h"
#include <fstream>
namespace BarPricer3 {
using namespace std;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class addNewProductForm{
...
private: System::Void createNewProduct(...){
productDat^ theNewP = createProduct(this->name->Text,Convert::ToDouble(this->firstPrice->Text),Convert::ToDouble(this->lastPrice->Text),Convert::ToDouble(this->lastDemand->Text));
...
}
}
When I try to compile, I get the following errors:
Error 8 error C2039: 'addNewProductForm' : is not a member of 'BarPricer3' d:...\BarP.h 690 (line 32 in my code)
Error 15 error C2065: 'productDat' : undeclared identifier d:...\addNewProductForm.h 181 (line 19 in my code)
Can I get any advice about what's happening here?
You have a circular inclusion in the header files, and that, combined with the #pragma once directive, is what yields the error. You need to remove one of the includes (or both) and replace them with a forward declaration (might want to google this).
You'll probably need to separate the implementations to a different file, because you use the types inside the headers.
Your problem is that addNewProductForm.h uses productDat which is defined in BarP.h, and that BarP.h uses addNewProductForm which is defined in addNewProductForm.h. (These are the classes that will need to be forward-declared).

Resources