Search code examples
c++comatl

Getting compile error in atlbase 'GetThreadLocale': identifier not found


I am trying to build some COM code from this article - https://www.codeproject.com/Articles/9190/Understanding-The-COM-Single-Threaded-Apartment-Pa I am trying to use code in \SimpleCOMObject2 folder. I am not sure how to use legacy dsw project files so I have created new project in VS 2022 and added all source files to it.

It is giving weird error -

1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.36.32532\atlmfc\include\atlbase.h(3462,15): error C3861: 'GetThreadLocale': identifier not found
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.36.32532\atlmfc\include\atlbase.h(3462,30): message : 'GetThreadLocale': function declaration must be available as none of the arguments depend on a template parameter
1>C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.36.32532\atlmfc\include\atlbase.h(3496,2): message : see reference to class template instantiation 'ATL::CAtlDllModuleT<T>' being compiled

Project contains following files that I think are creating problem.

stdafx.h

// stdafx.h : include file for standard system include files,
//      or project specific include files that are used frequently,
//      but are changed infrequently

#if !defined(AFX_STDAFX_H__E44A71C1_F36D_4C5D_8FA0_7FCCA7BD0E95__INCLUDED_)
#define AFX_STDAFX_H__E44A71C1_F36D_4C5D_8FA0_7FCCA7BD0E95__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define STRICT
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
#define _ATL_APARTMENT_THREADED

#include <Windows.h>
#include <winnls.h>
#include <atlbase.h>
//You may derive a class from CComModule and use it if you want to override
//something, but do not change the name of _Module
extern CComModule _Module;
#include <atlcom.h>

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__E44A71C1_F36D_4C5D_8FA0_7FCCA7BD0E95__INCLUDED)

stdafx.cpp

#include "stdafx.h"

#ifdef _ATL_STATIC_REGISTRY
#include <statreg.h>
#include <statreg.cpp>
#endif

SimpleCOMObject2.cpp

#include "stdafx.h"
#include <initguid.h>
#include "SimpleCOMObject2.h"

#include "SimpleCOMObject2_impl.h"

CComModule _Module;

...

STDAPI DllCanUnloadNow(void)
{
    return (_Module.GetLockCount() == 0) ? S_OK : S_FALSE;
}

...

Solution

  • In stdafx.h. Remove this line:

    #define _WIN32_WINNT 0x0400
    

    The default value of 0x0A00 will get picked up naturally after you do that. And then GetThreadLocale will not be excluded by the preprocessor when winnls.h is included.