VS2017 C++ compiler error C2664 can not convert argument - c++

Using VS2017 I compile the code below using the unicode character set
STDMETHODIMP Load(LPCOLESTR lpwszFileName, const AM_MEDIA_TYPE *pmt) {
TCHAR *szExtension = PathFindExtension(lpwszFileName);
and I get the following error
error C2664: 'LPSTR PathFindExtensionA(LPCSTR)': cannot convert argument 1 from 'LPCOLESTR' to 'LPCSTR'
The same code under VS2008 compiles just fine. What seems to be to problem here and why the compiler chooses the ANSI version of the PathFindExtenstion instead of the unicode one ?

The problem was that the VS2017 variable
%(PreprocessorDefinitions)
was missing from Preprocessor Definitions. Now the definers /D _UNICODE and /D UNICODE are added correctly to the compiler parameter's list.

Related

Visual Studio 2010 Arduino cpp Error: argument of type "char *" is incompatible with parameter of type "LPCWSTR"

I'm trying to set up an arduino uno for serial port communication with a C++ program in visual studio 2010. I'm working from the code found here: http://playground.arduino.cc/Interfacing/CPPWindows
Unfortunately, the .cpp file gives me the following message for line 9 for the variable 'portName':
Error: argument of type "char *" is incompatible with parameter of type "LPCWSTR"
I don't understand this error message, and have tried a few different things to fix it. Any help would be greatly appreciated!
Given the code link in your question, it seems the problem is here:
Serial::Serial(char *portName)
{
...
this->hSerial = CreateFile(portName, // <--- ERROR
CreateFile is a Win32 API that expects an LPCTSTR as first string parameter .
LPCTSTR is a Win32 typedef, which is expanded to:
const char* in ANSI/MBCS builds
const wchar_t* in Unicode builds (which have been the default since VS2005)
Since you are using VS2010, probably you are in the default Unicode build mode.
Actually, there is no "physical" CreateFile API exposed, but there are two distinct functions: CreateFileA and CreateFileW. The former takes a const char* input string, the latter takes a const wchar_t*.
In Unicode builds, CreateFile is a preprocessor macro expanded to CreateFileW; in ANSI/MBCS builds, CreateFile is expanded to CreateFileA.
So, if you are in Unicode build mode, your CreateFile call is expanded to CreateFileW(const wchar_t*, ...). Since portName is defined as a char*, there is a mismatch between wchar_t* and char*, and you get a compiler error.
To fix that, you have some options.
For example, you could be explicit in your code, and just call CreateFileA() instead of CreateFile(). In this way, you will be using the ANSI/MBCS version of the function (i.e., the one taking a const char*), independently from the actual ANSI/MBCS/Unicode settings in Visual Studio.
Another option would be to change your current build settings from the default Unicode mode to ANSI/MBCS. To do that, you can follow the path:
Project Properties | Configuration Properties | General | Character Set
and select "Use Multi-Byte Character Set", as showed in the following screenshot:
Your settings in Visual Studio are probably set to Unicode but the code you're compiling expects ASCII.
Go to Project Properties -> Configuration Properties -> General -> Character Set and choose "Use Multi-Byte Character Set".
-Surenthar
Your settings in Visual Studio are probably set to Unicode but the code you're compiling expects ASCII.
Go to Project Properties -> Configuration Properties -> General -> Character Set and choose "Use Multi-Byte Character Set".
You should also remove UNICODE or _UNICODE from C++ -> Preprocessor -> Preprocessor definitions, if they are defined there.
This will make your code call the ASCII versions of the Windows API functions, which accept char strings.

TCHAR* to LPCSTR - Linker Error

I am currently porting some code from Visual Studio to Mingw.This code is from an open source library.I came across this constructor in which a TCHAR* is being passed to to LPCSTR .I simply used the basic Cstyle cast for conversion.In visual Studio I did not need a cast and the application built fine. However I get a linker error in Mingw GCC if I do not place a cast.
This is the constrcutor
CAsyncReader::CAsyncReader(
TCHAR *pName,
LPUNKNOWN pUnk,
CAsyncStream *pStream,
HRESULT *phr)
: CBaseFilter(
LPCSTR(pName), // Is this cast ok ?
pUnk,
&m_csFilter,
CLSID_AsyncSample,
NULL
),
m_OutputPin(
phr,
this,
&m_Io,
&m_csFilter),
m_Io(pStream)
{
}
And this is the constructor of the parent class CBaseFilter
CBaseFilter(
__in_opt LPCTSTR pName, // Object description
__in_opt LPUNKNOWN pUnk, // IUnknown of delegating object
__in CCritSec *pLock, // Object who maintains lock
REFCLSID clsid, // The clsid to be used to serialize this filter
__inout HRESULT *phr); // General OLE return code
}
Kindly let me know if this works.
Update:
Since the application built fine in VS and is giving me a linker error in Mingw GCC my question is are there any flags that I might be missing in my GCC project or does VS does this implicitly ? Both the projects have UNICODE enabled so I am a totally confused with the linker error. This is the linker error I am getting in Mingw GCC and goes away with the cast (which now I am certain is very WRONG judging from the comments).
undefined reference to `CBaseFilter::CBaseFilter(wchar_t const*, IUnknown*, CCritSec*, _GUID const&, long*)'
collect2.exe: error: ld returned 1 exit status
First you need UNICODE and _UNICODE defines. Check MSDN:
Be careful: Some headers use the preprocessor symbol UNICODE, others
use _UNICODE with an underscore prefix. Always define both symbols.
Visual C++ sets them both by default when you create a new project.

No way to solve this without modifying Microsoft header?

Trying to compile this old VC++ 6.0 program in VC++ 2010. This ATL/WTL stuff is giving me lots of problems. I downloaded and have linked to the latest WTL (as far as I know) wtl71.
I am getting compile errors in atlmisc.h:
atlmisc.h(1159): error C2440: 'return' : cannot convert from 'const char *' to 'TCHAR *'
I've searched the 'net, and the answers that come up call for modifying the stock MS atlmisc.h file!
Am I missing something here? What do I need to do to get this to compile?
Most of the time I've seen that error, it's because I've been trying to build a Unicode application. In a Unicode application, the TCHAR* is a short* or wchar_t* rather than a char*.
I'd suggest checking your project settings and making sure that in the project properties, General > Character Set is set to Use Multi-Byte Character Set.

UTF-8 locale in Visual C++ 2010

I am trying to read a UTF-8 text file in Visual C++ 2010 using only the standard library and not Boost or Windows APIs. I define the locale as:
std::locale utf8_locale(std::locale(), new std::codecvt_utf8<wchar_t>);
but this results in the following compiler error:
error C2661: 'std::locale::facet::operator new' : no overloaded function takes 3 arguments
error C2664: 'std::locale::locale(const char *,std::locale::category)' : cannot convert parameter 1 from 'std::locale' to 'const char *'
The error is occures in debug mode when the code is used in the file that micrsoft Visual c++ provided below macro is placed.
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
to get rid of this error the #define new DEBUG_NEW should be commented out or the code should be implemented in another file which does not have the above macro.
This bug for Visual c++ 2010 is mentioned here http://connect.microsoft.com/VisualStudio/feedback/details/683483/mfc-c-fails-to-compile-use-of-codecvt-utf8-in-debug-configuration
FYI, another work around is to just write
std::locale utf8(std::locale(), ::new std::codecvt_utf8<wchar_t>);
That will force the compiler to use the global new, instead of the locale new

Compilation error when calling _tcsstr and assigning to a wchar_t*

I am getting a compilation error when trying to build a C++ project which previously worked.
The code follows:
const wchar_t* pdest;
pdest = _tcsstr(ConnStr, Name);
The error follows:
Error 10 error C2440: '=' : cannot convert from 'const char *' to 'const wchar_t
I'm using Visual Studio 2008. The error message explains the problem well, but I know this program used to compile, what am I doing wrong?
Your code is dangerous. _tcsstr is a TCHAR macro, so it's definition can change depending on whether or not UNICODE is defined. wchar_t is fixed. The error you're seeing is due to this exact problem - the environment is using the single-byte version of _tcsstr (likely becasue UNICODE is not defined).
Don't just define UNICODE. Fix the code first. Either use TCHAR macros for both, or the wide character functions.
_tcsstr is for use with TCHAR. Depending on compile settings, this is either char or wchar_t.
So either use TCHAR, or wcsstr
That should fix this issue:
Property -> Configuration Properties -> General -> Character Set : Use multi-Byte Character Set.