How to get system paths of windows? - c++

I am trying to get all the system paths of windows. When reading this it says to use KNOWNFOLDERID. So I followed the example from here. When try to use that example I get a compilation error.
Test.cpp:
#include <tchar.h>
#include <sysinfoapi.h>
#include <Shlobj.h>
#include <combaseapi.h>
#include <WTypesbase.h>
#include <winnt.h>
int main() {
IKnownFolderManager *pManager;
HRESULT hr = CoCreateInstance(
CLSID_KnownFolderManager,
NULL, CLSCTX_INPROC_SERVER,
IID_PPV_ARGS(&pManager)
);
}
This is what I have so far. The error I get is
error:
invalid static_cast from type 'IKnownFolderManager*' to type
'IUnknown*'
static_cast<IUnknown *> (*pp);
I am compiling from command line in windows 10 as: g++ test.cpp

IKnownFolderManager requires <Shobjidl.h>. Without that include, the compiler doesn't know how to cast IKnownFolderManager to IUnknown.

Related

AnsiString does not work (AnsiString identifier is not defined)

Here's the code:
AnsiString path = "BrowserBot.exe";
ShellExecute(0, TEXT("open"), path.c_str(), TEXT("-parametr"), 0, SW_SHOW);
Writes an error that the AnsiString identifier is not defined. I don't know what the problem is.
All connected libraries:
#include <iostream>
#include <conio.h>
#include <Windows.h>
#include <fstream>
#include <sstream>
AnsiString is a string class that is specific to the C++Builder compiler. If you are using that compiler, make sure you are compiling your project with C++Builder's VCL (Visual Component Library) or FMX (FireMonkey) framework enabled, and that you have a corresponding #include <vcl.h> or #include <fmx.h> statement in your C++ code.
Otherwise, if you are using any other compiler, you should use the standard C++ std::string class instead (which can also be used in C++Builder), eg:
#include <string>
std::string path = "BrowserBot.exe";
ShellExecuteA(0, "open", path.c_str(), "-parametr", 0, SW_SHOW);

Unable to call `SetWallpaper()` due to error LNK2019

Intro
First of all, I would like to say that I have read through the previous answers for this type of question, including this excellently written one.
However, I do not understand enough about C++ to be able to use the more "advanced" fixes.
I have ensured that the right type of console has been selected (Console (/SUBSYSTEM:CONSOLE) for those interested), and have the required imports with the possible exception of an IDL mentioned somewhere (that falls into the lack of understanding category).
If this is a duplicate, I would be more than happy to use the post I duplicated, but I have not been able to find anything that can help someone of my skill level.
Technical Information
IDE: Visual Studio
Platform: Windows
Code
headers.h
#pragma once
#include <stdio.h>
#include <iostream>
#include <string>
#include <windows.h>
#include <Shobjidl.h>
#include <time.h>
#include <stdlib.h>
#include <tchar.h>
main.cpp
#include "headers.h"
using namespace std;
int main() {
string x = "C://Users/student/Desktop/i-should-buy-a-boat.jpg";
x.c_str();
wstring tempx = std::wstring(x.begin(), x.end());
LPCWSTR sw = tempx.c_str();
HRESULT SetWallpaper(
LPCWSTR monitorID,
LPCWSTR wallpaper
);
SetWallpaper(NULL, sw);
}
SetWallpaper() is not a standalone function exported by the Win32 API. It is a method of the IDesktopWallpaper interface (see here).
So you need to use code that is more like this instead:
#include "headers.h"
int main()
{
std::wstring x = L"C:\\Users\\student\\Desktop\\i-should-buy-a-boat.jpg";
CoInitialize(NULL);
IDesktopWallpaper *p;
if (SUCCEEDED(CoCreateInstance(__uuidof(DesktopWallpaper), 0, CLSCTX_LOCAL_SERVER, __uuidof(IDesktopWallpaper), (void**)&p)))
{
p->SetWallpaper(NULL, x.c_str());
p->Release();
}
CoUninitialize();
return 0;
}

C++ TlHelp32.h not working?

I've included the TlHelp32.h header properly though I keep getting the message:
"Error: identifier "CreateToolhelp32Snapshot" is undefined"
when attempting to use CreateToolhelp32Snapshot. When I used the "peek definition" feature in VS I found that there are errors within this header where in certain areas it says:
"Error expected a ';'"
Any ideas how to fix this?
#include "stdafx.h"
#include <TlHelp32.h>
#include <Windows.h>
#include <iostream>
using namespace std;
class Functions{
public:
void playerHealthPrinter(){
HANDLE hProcess;
DWORD dwPID, dwProtection, dwCaveAddress;
BOOL bPOn, bIOn, bProt;
HANDLE hPID = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
Never include Win32 headers before <windows.h>. The following should work, instead:
#include <Windows.h>
#include <TlHelp32.h> // <-- include *after* windows.h
If it still doesn't work then please post an MCVE including the relevant headers and version of VS.

error: 'WlanScan' was not declared in this scope

I have been playing around with wlanapi in windows. I did not have any problem compiling or running until I tried using the function WlanScan. Then I was unable to compile due to "WlanScan" not being declared in the scope. I wrote a really short program illustrating this using two functions: WlanOpenHandle that works and WlanScan which doesn't.
#include <windows.h>
#include <wlanapi.h>
int main()
{
HANDLE hClient;
WlanOpenHandle(2, 0, 0, &hClient);
WlanScan(hClient, 0, 0, 0, 0);
}
Compiling that single file like this:
g++ main.cpp -lwlanapi
Results in this error:
main.cpp: In function 'int main()':
main.cpp:9:30: error: 'WlanScan' was not declared in this scope
WlanScan(hClient, 0, 0, 0, 0);
^
What could be the cause of this? I've been able to use a handful of functions from the wlanapi. I am on Windows 7 compiling with minGW.
EDIT:
In accordance to what u/ IInspectable said, I changed the command used to compile to:
g++ -D_WIN32_WINNT=_WIN32_WINNT_WIN7 main.cpp -lwlanapi
And it worked!
It looks like someone else has had this problem before:
How To Compile C++ Code that has a 'wlanapi.h' and 'windows.h' dependency
The recommended solution is to just put it into Visual Studio and compile it using that; MinGW probably isn't able to find the library.
Using VS2010 I created a VC++ console application (with a precompiled header), and I was able to get the following to compile without any errors:
// wlanapi_Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hClient;
WlanOpenHandle(2, 0, 0, &hClient);
WlanScan(hClient, 0, 0, 0, 0);
}
And here's my precompiled header:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
// TODO: reference additional headers your program requires here
#include <windows.h>
#include <wlanapi.h>
#pragma comment(lib, "wlanapi.lib")

GLFW3 error: 'glfwGetWin32Window' was not declared in this scope

I was looking for a native API access in GLFW3 documentation to get HWND but it's not in my GLFW/glfw3.h file. Is there any #define's to be able to find it by compiler? I can't also find it manually in the file itself using text-finder, so how can I get it?
PS. I can't tag glfw3.
Edit:
Code:
#define GLFW_INCLUDE_GLU
#define GLFW_EXPOSE_NATIVE_WGL
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GL/GLFW/glfw3native.h>
#include <GL/GLFW/glfw3.h>
#include <ctime>
#include <cstdlib>
...
int main()
{
//glfw setup
...
//bla bla bla
...
//all I want to do is to call this one
ScreenToClient( glfwGetWin32Window(window), &point);
}
After getting confused by your problems I tried it my self and I think the include order is your problem. A minimal code example that mimics on Linux what you try to do on Windows compiles and works as intended:
#define GLFW_EXPOSE_NATIVE_X11
#define GLFW_EXPOSE_NATIVE_GLX
#include <GLFW/glfw3.h>
#include <GLFW/glfw3native.h>
int main (int argc, char ** argv)
{
glfwInit();
GLFWwindow* window = glfwCreateWindow (256, 256, "GLFW", nullptr, nullptr);
glfwGetX11Window(window);
glfwTerminate();
return 0;
}
EDIT: Added the incovation of glfwTerminate() for proper clean-up. Please note, of course there should be appropriate error checking taking place, but for the purpose of demonstrating a minimal example, the above is sufficient.