'GetProcessIdOfThread': identifier not found - c++

Here is my codes in stdafx.h :
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#pragma once
#define _WIN32_WINNT 0x0502
#include "winsock2.h"
#include "windows.h"
#include "stdio.h"
#include "Iphlpapi.h"
#include <psapi.h>
#include "Ntsecapi.h"
#include "txdtc.h"
#include "xolehlp.h"
#include <iostream>
#include <tchar.h>
// TODO: reference additional headers your program requires here
As you see i have included "windows.h"
And here is main codes :
#include "stdafx.h"
...
if (hThread && dwRpcssPid == GetProcessIdOfThread(hThread))
...
My errors are :
'GetProcessIdOfThread': identifier not found
IntelliSense: identifier "GetProcessIdOfThread" is undefined
How can i fix these errors?

The function is not available with _WIN32_WINNT values less than 0x0600 AKA _WIN32_WINNT_VISTA. If you change your code this way, you will get it working:
//#define _WIN32_WINNT 0x0502
#define _WIN32_WINNT 0x0600
The function is available since Vista, to target Vista+ you should have this value defined respectively.
To target latest versions of API with current SDK, you can simply include SDKDDKVer.h and those values will be defined for you/
//#define _WIN32_WINNT 0x0502
#include <SDKDDKVer.h>
See also:
What is _WIN32_WINNT and how does it work?

GetProcessIdOfThread's platform requirements states:
Windows Vista [desktop apps only]
Windows Server 2003 [desktop apps only]
And the header requirements states:
Processthreadsapi.h on Windows 8 and Windows Server 2012
So:
Make sure your windows SDK is up-to-date
Make sure you have specified your platform requirements properly.
Make sure you're including the right header file.

If you are using windows 8 you need to include : Processthreadsapi.h
See the MSDN references in the header section.

Related

C2065 'cout': undeclared identifier [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
So I'm a complete noob to C++ and I'm trying to make a simple "Hello world" program for an assignment. My code is below:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout<<"hello world!";
return 0;
}
For some reason, VS2017 is throwing an error at cout, saying it's undefined. I've done some reading on old posts about this and added in #include "stdafx.h to see if that would solve it as per old advice, but it continues to give me the error. Any ideas?
EDIT:
Again a complete noob but there are multiple version of stdafx.h that come up when I search for it, here's what looks like the "main" one:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently,
// but are changed infrequently
#pragma once
#ifndef STRICT
#define STRICT
#endif
#include "targetver.h"
[!if SERVICE_APP]
#define _ATL_FREE_THREADED
[!else]
#define _ATL_APARTMENT_THREADED
[!endif]
#define _ATL_NO_AUTOMATIC_NAMESPACE
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
[!if PREVIEW_HANDLER || THUMBNAIL_HANDLER || SEARCH_HANDLER]
#ifdef _MANAGED
#error File type handlers cannot be built as managed assemblies. Set the Common Language Runtime options to no CLR support in project properties.
#endif
#ifndef _UNICODE
#error File type handlers must be built Unicode. Set the Character Set option to Unicode in project properties.
#endif
#define SHARED_HANDLERS
[!endif]
[!if SUPPORT_MFC]
#include <afxwin.h>
#include <afxext.h>
#include <afxole.h>
#include <afxodlgs.h>
#include <afxrich.h>
#include <afxhtml.h>
#include <afxcview.h>
#include <afxwinappex.h>
#include <afxframewndex.h>
#include <afxmdiframewndex.h>
#ifndef _AFX_NO_OLE_SUPPORT
#include <afxdisp.h> // MFC Automation classes
#endif // _AFX_NO_OLE_SUPPORT
[!endif]
[!if SUPPORT_COMPLUS]
#include <comsvcs.h>
[!endif]
#define ATL_NO_ASSERT_ON_DESTROY_NONEXISTENT_WINDOW
#include "resource.h"
#include <atlbase.h>
#include <atlcom.h>
#include <atlctl.h>
error C2065: 'cout': undeclared identifier is a result of the absence of #include <iostream>. The first cause might be stdafx.h content. The one you provided, I'm not so sure how it is related to your main.cpp/project. Let's start from a fresh project: ...VS2017 IDE: Create new project, ConsoleApplication project-type, & replace the main() function with yours.
A VS2017 IDE (15.8.2) fresh ConsoleApplication project: ConsoleApplication1
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "hello world!";
return 0;
}
stdafx.h: (Generated by the IDE)
// 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"
// TODO: reference additional headers your program requires here
stdafx.cpp: (Generated by the IDE)
// stdafx.cpp : source file that includes just the standard includes
// ConsoleApplication1.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
targetver.h: (Generated by the IDE)
#pragma once
// Including SDKDDKVer.h defines the highest available Windows platform.
// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
#include <SDKDDKVer.h>
** This code runs perfectly. **
--
"there are multiple versions of stdafx.h that come up when I search for it" - What do you mean? In your project? On the internet? You can't just take one stdafx.h from the internet. stdafx.h content is tailored per project, not universal. What I provided above, for example, is the IDE default new ConsoleApplication project stdafx.h. You may add to the file according to your project needs.

Intel TBB - 'InitializeCriticalSectionEx': identifier not found compiler error

I have a VS (C++) project that relies on OpenCV and TBB, so I created property sheets for each library and included them in the project. Everything worked fine and the code compiled.
Yesterday, I have started using vcpkg package manager. I installed OpenCV and TBB via vcpkg and everything seemed to work. I created an empty project, included the headers of both and tested if the new compiled libraries work. After verifying that, I went back to my main project and removed the property sheets, so I can use the libraries from vcpkg. I did not change the code in any way since the last successful compilation.
But when I try to compile the code now I get this error two times (in main.cpp and in a submodule)
tbb\critical_section.h(53): error C3861: 'InitializeCriticalSectionEx': identifier not found
Does anybody know what is going on here or why this error occurs?
Update
I found the error myself. I'm adding the poco-libraries tag, because it's actually a conflict between TBB and Poco.
I found the source of the problem and it has actually nothing to do with TBB but with the Poco library.
Consider the minimum example:
#include <Poco/Poco.h>
#include <tbb/tbb.h>
void main()
{
}
This will throw an compiler error.
Tracing down the path
When including tbb.h, critical_section.h is included in line 51 of tbb.h. However, ciritcal_section.hpp includes machine/winwdows_api.h which looks like this (unnecessary stuff is cut out):
tbb/machine/winwdows_api.h:
#if _WIN32 || _WIN64
#include <windows.h>
#if _WIN32_WINNT < 0x0600
#define InitializeCriticalSectionEx inlineInitializeCriticalSectionEx
inline BOOL WINAPI inlineInitializeCriticalSectionEx( LPCRITICAL_SECTION lpCriticalSection, DWORD dwSpinCount, DWORD )
{
return InitializeCriticalSectionAndSpinCount( lpCriticalSection, dwSpinCount );
}
#endif
As you can see, windows.h is included before the check of the _WIN32_WINNT macro. This macro is defined in sdkddkver.h (which is included in windows.h), iff it's not already defined (in my case it's set to Win10):
sdkddkver.h:
#if !defined(_WIN32_WINNT) && !defined(_CHICAGO_)
#define _WIN32_WINNT 0x0A00
#endif
In windows.h, the _WIN32_WINNT macro controls which version of the windows header files are actually included. If _WIN32_WINNT is set to an earlier version than Windows Vista, the function InitializeCriticalSectionEx is not defined.
This issue is catched by machine/winwdows_api.h (as you can see in the code block of that file) by simply defining a macro InitializeCriticalSectionEx that calls an appropriate alternative function.
So far so good.
The problem
The root of all evil lies in Poco/UnWindows.h of the Poco library. When including a poco header, at some point UnWindows.h will be included.
Poco/UnWindows.h (shortened):
#if defined(_WIN32_WINNT)
#if (_WIN32_WINNT < 0x0501)
#error Unsupported Windows version.
#endif
#elif defined(NTDDI_VERSION)
#if (NTDDI_VERSION < 0x05010100)
#error Unsupported Windows version.
#endif
#elif !defined(_WIN32_WINNT)
#define _WIN32_WINNT 0x0501
#define NTDDI_VERSION 0x05010100
#endif
#endif
#include <windows.h>
The preprocessor checks, if _WIN32_WINNT is already defined, and if not, sets it to 0x0501 which is Windows XP. After that, windows.h is included. In the previous chapter I mentioned that _WIN32_WINNT controls which version of the windows header files are actually included.
Now imagine, the very first include in our project is a header from Poco. This means, that _WIN32_WINNT will be set to Windows XP and windows.h will include the windows headers of Windows XP (which imo is already a bad sign).
But don't worry, it gets worse.
If we trace the include hierarchy one level up, we reach Poco/Platform_WIN32.h.
Poco/Platform_WIN32.h (shortened):
#include "Poco/UnWindows.h"
...
#if defined (_WIN32_WINNT_WINBLUE)
#ifdef _WIN32_WINNT
#undef _WIN32_WINNT
#endif
#define _WIN32_WINNT _WIN32_WINNT_WINBLUE
...
Funny, isn't it? First, it includes UnWindows.h, which sets _WIN32_WINNT and causes Windows XP headers to be included, and next it redefines _WIN32_WINNT to be Windows 8.1. I have no clue why it does that, maybe there is a good reason, idk.
If we now look at the minimum example at the very top we see that Poco is included before TBB. What now happens is:
Include Poco headers
Set _WIN32_WINNT to Windows XP
Include windows headers (Windows XP version, because of 2)
Reset _WIN32_WINNT to Windows 8.1
Include TBB headers (windows headers are already included, so TBB doesn't need to include them again in tbb/windows_api.h)
TBB checks the windows version via _WIN32_WINNT and recognizes Windows 8.1 (as set by Poco)
TBB thinks InitializeCriticalSectionEx is defined, because the Windows version is 8.1 (or is it? Poco says: get rekt) and InitializeCriticalSectionEx is defined since Windows Vista.
Unfortunately Poco ensured that the Windows XP headers are loaded, so compiler says: no.
The solution
Either include windows.h yourself beforehand, or set _WIN32_WINNT yourself beforehand:
#define _WIN32_WINNT 0x0A00 // either this
#include <Windows.h> // or this
#include <Poco/Poco.h>
#include <tbb/tbb.h>
void main()
{
}
Maybe someone of the Poco contributors can clarify some things here. The Poco version is 1.8.1-1 built with x64 (via vcpkg).
Update
Poco is on the issue. Updates can be found here.

GetAncestor: Undeclared identifier

I'm using Visual Studio 6 for backward compatibility reasons and I need
to use GetAncestor() like this to create a dll :
HWND ancestorhandle = GetAncestor(myvar, 2);
I've included these header files:
#include <windows.h>
#include "stdafx.h"
#include "offlinelib.h"
#include <stdio.h>
As per the documentation:
GetAncestor function
But while compiling, I get an error:
GetAncestor: Undeclared identifier
What am I doing wrong?
Try moving the #include "stdafx.h" (precompiled header) up, making it the first include line.
Solved by adding
#ifdef WINVER
#undef WINVER
#endif
#define WINVER 0x500
on top of stdafx.h

Error while building OpenCV :: MonitorFromRect was not declared in this scope

I was trying to build OpenCV version 2.4.8 to use it with CodeBlocks and MinGw. I followed the instructions from here. But I got the following error. I have no clue how to solve it. I didn't find anything useful by searching in the net.
This is also not solving.
I don't want to mess with the openCV code, I intend to use OpenCV in my project and this is first time I am using it.
[ 26%] Built target pch_Generate_opencv_highgui
[ 26%] Building CXX object modules/highgui/CMakeFiles/opencv_highgui.dir/src/window_w32.cpp.obj
C:\Program Files (x86)\opencv\sources\modules\highgui\src\window_w32.cpp: In function 'void cvSetModeWindow_W32(const char*, double)':
C:\Program Files (x86)\opencv\sources\modules\highgui\src\window_w32.cpp:477: error: 'MonitorFromRect' was not declared in this scope
C:\Program Files (x86)\opencv\sources\modules\highgui\src\window_w32.cpp: In function 'LRESULT MainWindowProc(HWND__*, UINT, WPARAM, LPARAM)':
C:\Program Files (x86)\opencv\sources\modules\highgui\src\window_w32.cpp:1355: error: 'MonitorFromRect' was not declared in this scope
mingw32-make.exe[2]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/src/window_w32.cpp.obj] Error 1
mingw32-make.exe[1]: *** [modules/highgui/CMakeFiles/opencv_highgui.dir/all] Error 2
mingw32-make.exe: *** [all] Error 2
I tried to manually include the prototype of the function in the file, but then it comes to linking error.
will anyone please tell me what may have gone wrong here? How can I solve it?
It seems All the changes from recent commit is not reflected in your check out. To resolve the problems, make the following changes:
In modules/highgui/src/precomp.hpp, add the + marked line:
#if defined WIN32 || defined WINCE
+ #if !defined _WIN32_WINNT
+ #ifdef HAVE_MSMF
+ #define _WIN32_WINNT 0x0600 // Windows Vista
+ #else
+ #define _WIN32_WINNT 0x0500 // Windows 2000
+ #endif
+ #endif
+
#include <windows.h>
And in modules/highgui/src/window_w32.cpp, remove the - marked lines:
#if defined WIN32 || defined _WIN32
-#define COMPILE_MULTIMON_STUBS // Required for multi-monitor support
-#ifndef _MULTIMON_USE_SECURE_CRT
-# define _MULTIMON_USE_SECURE_CRT 0 // some MinGW platforms have no strncpy_s
-#endif
-
-#if defined SM_CMONITORS && !defined MONITOR_DEFAULTTONEAREST
-# define MONITOR_DEFAULTTONULL 0x00000000
-# define MONITOR_DEFAULTTOPRIMARY 0x00000001
-# define MONITOR_DEFAULTTONEAREST 0x00000002
-# define MONITORINFOF_PRIMARY 0x00000001
-#endif
-#ifndef __inout
-# define __inout
-#endif
-
#ifdef __GNUC__
# pragma GCC diagnostic ignored "-Wmissing-declarations"
#endif
#include <commctrl.h>
-#include <winuser.h>
#include <stdlib.h>
#include <string.h>
This will solve the build error.
I had the same problem when building OpenCV 3.0.0 RC1 with mingw32 and the TBB library enabled.
The fix from Rajdhar is already included in the precomp.h file. However, due when building OpenCV with the TBB library, the extra includes trigger the same problem again.
I provisionally solved the issue by moving the definition of _WIN32_WINNT indicated by Rajdhar to an earlier point in the file, before the opencv/core includes:
#ifndef __HIGHGUI_H_
#define __HIGHGUI_H_
#include "opencv2/highgui.hpp"
// MOVED UP
#if defined WIN32 || defined WINCE
#if !defined _WIN32_WINNT
#ifdef HAVE_MSMF
#define _WIN32_WINNT 0x0600 // Windows Vista
#else
#define _WIN32_WINNT 0x0500 // Windows 2000
#endif
#endif
#include <windows.h>
#undef small
#undef min
#undef max
#undef abs
#endif
// END MOVED
#include "opencv2/core/utility.hpp"
#include "opencv2/core/private.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgcodecs/imgcodecs_c.h"
#include "opencv2/highgui/highgui_c.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
#include <assert.h>
// MOVED FROM HERE
#ifdef HAVE_TEGRA_OPTIMIZATION
#include "opencv2/highgui/highgui_tegra.hpp"
#endif
I've had exactly the same problem, and after a quick glance at the file winuser.h, I knew what's going on and added necessary macros to CFLAGS and CXXFLAGS in the command line:
CFLAGS=-D_WIN32_WINNT=0x0500 CXXFLAGS=-D_WIN32_WINNT=0x0500 make
However, the problem was still unsolved. Adding VERBOSE=1 showed that the custom CFLAGS and CXXFLAGS did not take effect at all. It was wierd and I think it should have something to do with my environment, though, i still could not figure it out. Anyway, #Rajdhar 's answer solved my problem, thanks.

error C1189 after installing Visual Studio 2010

I installed VS2010 after a drive crash, prior I had VS2005 and everything was fine.
Now on compiling a C++ app that was fine previously I am seeing a couple of errors which I just cannot figure out.
Error 1 error C1189: #error : This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x0501 or higher is recommended. C:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\include\atlcore.h 35 1 BIOXGINA
#ifndef __ATLCORE_H__
#define __ATLCORE_H__
#pragma once
#ifdef _ATL_ALL_WARNINGS
#pragma warning( push )
#endif
#pragma warning(disable: 4786) // identifier was truncated in the debug information
#pragma warning(disable: 4127) // constant expression
#include <atldef.h>
#include <windows.h>
#include <ole2.h>
#include <limits.h>
#include <tchar.h>
#include <mbstring.h>
#include <atlchecked.h>
#include <atlsimpcoll.h>
34. #if _WIN32_WINNT < 0x0403
35. #error This file requires _WIN32_WINNT to be #defined at least to 0x0403. Value 0x0501 or higher is recommended.
36. #endif
#pragma pack(push,_ATL_PACKING)
namespace ATL
{
/////////////////////////////////////////////////////////////////////////////
// Verify that a null-terminated string points to valid memory
inline BOOL AtlIsValidString(
_In_z_count_(nMaxLength) LPCWSTR psz,
_In_ size_t nMaxLength = INT_MAX)
{
(nMaxLength);
return (psz != NULL);
}
If I comment out the above lines, I then get
error C3861 Identifier not found on line 111 below.
I presume I'm only getting this because I commented the above lines ?
HRESULT Init() throw()
{
HRESULT hRes = S_OK;
111. if (!InitializeCriticalSectionAndSpinCount(&m_sec, 0))
{
hRes = HRESULT_FROM_WIN32(GetLastError());
}
return hRes;
}
I would appreciate any assistance on this. Don't really want to reinstall 2005.
This Microsoft Connect issue has this potential solution:
edit file "stdafx.h" and change the value defined for _WIN32_WINNT and WINVER to 0x0502.
More discussion here about this error on the MSDN C++ forum: Problem with older VC Solution.
Your project targets a Windows version that is no longer supported by the newer compiler (or anything else).
You have to select a minimum target version that is Windows XP ("Windows 5") or later.
Thank you both for the replies.
I managed to get rid of the error message as follows. The Context.h looked liked this.
#pragma once
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <winwlx.h>
#include <ObjBase.h>
#include <comdef.h>
#include <atlbase.h>
extern CComModule _Module;
#include <atlcom.h>
#include <vector>
I moved the #define _WIN32_WINNT 0x0400 to then end after all the includes, and it compiled Ok. Odd, but it worked.
I will however alter it to 0x0502 as suggested.
thanks
Project Property –> Configuration Properties –> C/C++ -> Command Line ->Additional Options
:add this code
/D “_WIN32_WINNT=0×0501”
if in Windows server 2003,0×0501chagnes to 0×0502;
if in Windows 7, 0×0501chagnes to 0×0601
works well