Query AD group members in C++ / MFC application / ADsOpenObject - c++

I'm trying to include a query of AD group members to an (ancient) C++ MFC application, and the only way I've figured out it could be done is using ADsOpenObject, which requires me to do #include <iads.h>, and that doesn't work well at all:
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\ATLMFC\INCLUDE\afxv_w32.h
(16): fatal error C1189: #error: WINDOWS.H already included. MFC apps must not #include <windows.h>
Am I just missing a #define that I need to add or is something else in the app causing this to go terribly wrong?
Or am I even trying to do this using totally wrong libraries?

The problem was just as simple as described in the comments -- changing inclusion order fixed the problem.

Related

Creating a Windows shortcut (shell link) in C++

I want to programmatically create a Windows shortcut (.lnk file) to a folder. To do this, I tried this code snippet. However, I get the compilation error C2371 'WebBrowser': redefinition; different basic types in C:\Program Files (x86)\Windows Kits\10\Include\10.0.22000.0\um\exdisp.h line 2367.
Is there a C++17 std::filesystem API for this? If not, how can I fix the compilation error from above? Even with cleaned up includes, the error persists:
#include <Windows.h>
#include <shlguid.h>
#include <shobjidl_core.h>
Using the mklink command yields:
The device does not support symbolic links.
So that doesn't work either, maybe because this is an external SSD.
What else could I try?
The first answer you linked to is the correct way. And it does not rely on WebBrowser at all. It will probably help to define some of the NO_XXX macros before including windows.h.
In my version of ShlGuid.h which is slightly older than yours, I see
#ifndef NO_SHDOCVW_GUIDS
#ifndef GUID_DEFS_ONLY
#include <exdisp.h>
#include <shldisp.h>
#endif
so you can use NO_SHDOCVW_GUIDS to excise the troublesome header that you weren't wanting anyway.
If you wanted support for WebBrowser and the kitchen sink, another fix might be moving #include <windows.h> after ShlGuid.h, as shown e.g. here: https://stackoverflow.com/a/73422757/103167
Links are something that differ a fair amount from filesystem to filesystem, so difficult to standardize. Shell links (.lnk files) are filesystem-independent, but only work on MS Windows (completely non-portable) which also is a strike against finding support in a standard API.

How to include Oleacc for GetProcessHandleFromHwnd (Visual C++)

I am using Visual C++ express 2008 and I want to call GetProcessHandleFromHwnd. I know that I need to include Oleacc which I did by coding
#include <Oleacc.h>
and I also added Oleacc.lib in the Configuration Properties / linker / input / Additional Dependencies. I keep getting the "error C3861: 'GetProcessHandleFromHwnd': identifier not found".
I searched on the net and it seems that I should not need to do anything else...
What I want to do is to get the process for the currently focused application. If there is another simpler way, I am open to suggestions!

Global variables in C++ and CriticalSections

I have several global critical sections that need visibility across 3 or more classes defined in 3 or more .cpp files. They're defined in an h file as:
GlobalCS.h
#pragma once
#include "stdafx.h"
extern CRITICAL_SECTION g_cs1;
extern CRITICAL_SECTION g_cs2;
etc...
In a GlobalCS.cpp they are defined
#include "stdafx.h"
#include "GlobalCS.h"
CRITICAL_SECTION g_cs1;
CRITICAL_SECTION g_cs2;
Then in the cpp for the classes they need to work in they're included as
#include "stdafx.h"
#include "GlobalCS.h"
The linker is complaining giving unresolved external in the files where the critical sections are being used. I didn't expect this since the variables are defined as extern. What am I doing wrong or how do I get around this?
error LNK2001: unresolved external symbol "struct _RTL_CRITICAL_SECTION g_CS_SymbolStrPlotAccess" (?g_CS_SymbolStrPlotAccess##3U_RTL_CRITICAL_SECTION##A)
Not sure why you are getting the error. I tried the same thing in Visual Studio and in _tmain function I wrote the following:
int _tmain(int argc, _TCHAR* argv[])
{
//::g_cs1;
ZeroMemory(&g_cs1, sizeof(::g_cs1));
return 0;
}
And it built with no issues whatsoever.
Thank you all for your help. It always is helpful to have sanity checks. The issue was once again Visual Studio setup. Visual Studio will generate link errors if it doesn't like the way you have files added to your project. This is the second bug I've encountered that generated a link error based on the way the project was configured. If it's that important VS should either prevent you from modifying a project in a harmful way or provide a proper error message.
Anyway, the error is the same as this one:
LNK2019 Error under Visual Studio 2010
I had the GlobalCS.h and GlobalCS.cpp in the source directory. I prefer it this way because I find it makes finding files and code faster and in a large c++ project, just moving around the code base is a significant time waster. So much time could be saved writing c++ code if the IDE was designed to help find code faster. 2012 is A LOT better than 2010 so I'll give MSFT that but there could be a lot more features to that end (afterall VS has been around for almost 2 decades now)these types of persistent problems just get in the way of development. When I moved GlobalCS.h to the Header folder and cleaned the project and rebuilt, everything compiled as expected. The other similar error VS will throw at you is when the .h file is in the code directory (so #includes work) but not in the project. I got the same error messages when that happened and it took a good couple days to figure that one out. In a small project, it might not be as problematic but in big solution with multiple projects and dozens of files, it can be problematic.

how to set Windows file permissions in Qt/C++

In my Qt 4.7.4 x64 C++ app I'm building in Qt Creator 2.5.0, I want to give full access to everyone for a file. I'm using QFile::setPermissions, which I believe works fine for Mac and Linux, but it doesn't work for Windows. According to Qt setPermissions not setting permisions, I should use
SetNamedSecurityInfoA("C:\file.txt", SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL, NULL, NULL);
But I don't know what to #include to make it work. I tried:
#ifdef Q_WS_WIN
#include "Windows.h"
#endif
based on what I found here. But when I compile, I get C3861: 'SetNamedSecurityInfo': identifier not found (among some other new errors).
When I mouse over my #include "Windows.h", I get the tooltip: C:\Program Files\Microsoft SDKs\Windows\v7.0\include\Windows.h, and I can press F2 and jump to that file. We have other files in our project that include that same Windows.h, and they compile fine.
How do I set file permissions on Windows for everyone to read/write? If SetNamedSecurityInfo is what I want (I guess SetNamedSecurityInfoW in my case since my users may be running OS's in any language), what do I #include to be able to use it? Better yet, how do I figure out what to #include, so I know for next time I need to use the Windows API?
SetNamedSecurityInfo
Header
Aclapi.h
Library
Advapi32.lib
DLL
Advapi32.dll
A quick search on Google would probably have had this page among it top hits. There you can see which header file and library you need.

C++ Problems with #include <anyfile.h> when trying to compile a gstreamer app

I didn't find anything like this when I searched for it. I'm trying to make a simple gstreamer app based on code I found in another stack overflow thread. Whenever I try to compile it by going to the directory with command prompt and entering cl cppgstreamer.cc. Initially, the only include was #include <gstreamermm.h>, which gives me
cppgstreamer.cc(1) : fatal error C1083: Cannot open include file: 'gstreamermm.h
': No such file or directory
Even when I put cppgstreamer.cc into the same directory as gstreamermm.h. I then tried changing the include to
#include "C:\Users\MY_NAME\Documents\gstreamer c++\gstreamermm-0.10.10.2\gstreamer\gstreamermm.h"
with MY_NAME being replaced by my name.
This seems to work, but then it tries to do the includes in the gstreamermm.h, and they are in #include <file.h> form, so I get another fatal error C1083,except this time with init.h, or when I changed that, error.h. And they have dependencies. Is there a way that I can get my compiler to like the #include <file.h> syntax? I really don't want to go through the file and change every #include <file.h> to #include "file.h".
Sorry if this is a dumb question. I'm new to C++, although I've worked with C in the past.
I am using Microsoft Visual C++ Express 2010. Any help would be appreciated. Thanks!
You need to add the directory containing gstreamermm.h to include path. I don't have visual c++ here to check, but it's in somewhere in project properties under C/C++ .