TCHAR* to LPCSTR - Linker Error - c++

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.

Related

VS2017 C++ compiler error C2664 can not convert argument

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.

C++ windows code links in 64bit but not in 32bit

I have code written in C++ on windows.
My code compiles and links when I compile it as x64 but not when I change the build configuration to x86.
The failure is a linking error.
I'm using the function RtlIsNameInExpression from ntdll.
When I compile it in 32bit mode I get a linkage error (LNK2019) of unresolved external.
Any ideas why this might happen?
10x
first of all - how you declare function and which symbol can not found linker ?
declaration must be
extern "C" NTSYSAPI BOOLEAN NTAPI RtlIsNameInExpression(
_In_ PCUNICODE_STRING Expression,
_In_ PCUNICODE_STRING Name,
_In_ BOOLEAN IgnoreCase,
_In_opt_ PWCH UpcaseTable
);
i can guess that you miss NTAPI i.e __stdacall keyword if you copy-paste from here. for x64 exist only one calling convention, but for x86 exist different between __stdcall and __cdecl for example. this can explain why this found in x64 but not found in x86
what error give you linker (not compiler !) ? unresolved external symbol __imp__RtlIsNameInExpression ? (if yes you really forget __stdcall set) or __imp__RtlIsNameInExpression#16 ? in this case you declare function correct, but your ntdll.lib not containing this symbol. (may be you use old ntdll.lib
for xp ? ) simply search __imp__RtlIsNameInExpression#16 string as is in ntdll[p].lib - are it found ? if not you have old (xp) version of ntdll i guess.
The answer is in the online documentation for that function:
This function has no associated header file. The associated import library, Ntdll.lib, is available in the Microsoft Windows Driver Kit (WDK). You can also call this function using the LoadLibrary and GetProcAddress functions to dynamically link to Ntdll.dll.
If you can't add the ntdll.lib file from the WDK to your link command, then you need to use the LoadLibrary-GetProcAddress approach.
Also from the same section of documentation:
The functions and structures in Winternl.h are internal to the operating system and subject to change from one release of Windows to the next, and possibly even between service packs for each release. To maintain the compatibility of your application, you should use the equivalent public functions instead. Further information is available in the header file, Winternl.h, and the documentation for each function.

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.

How to define/use CBaseVideoRenderer-constructor

I want to use the C++-Class CBaseVideoRenderer. Which file do I need to include to get its constructor?
I have included renbase.h, but there's only the declaration. I have included strmbase.h and any other errors regarding this class vanished but the one for the missing constructor.
renbase.cpp seems to have a constructor but I cannot just include it without errors.
So, do you know which files I need?
Im Using embarcadero's C++-Builder from XE2 16.
EDIT: I want to use this class:
CBitmapRenderer::CBitmapRenderer(
TCHAR *pName,
LPUNKNOWN pUnk,
HRESULT *phr
)
: CBaseVideoRenderer(CLSID_BitmapRenderer,pName,pUnk,phr)
, m_InputPin(NAME("Video Pin"),this,&m_InterfaceLock,phr,L"Input")
{...
With this code:
CBitmapRenderer *m_pSnapshotter = new CBitmapRenderer( _T("Bitmap renderer"), NULL, &hr );
Which leads to an Link-error:
[ILINK32 Fehler] Error: Nicht auflösbares externes 'CBaseVideoRenderer::CBaseVideoRenderer(_GUID&, wchar_t *, IUnknown *, long *)' referenziert von C:\USERS\JULIAN\DESKTOP\PROGRAMM\WIN32\DEBUG\TESTRAUSCHEN.OBJ
That I know comes from a missing definition for the constructor.
EDIT2:
I have now included all files within baseClasses-direcory and the compiling is sucessful(without errors). However, when I try to run the application, I get the message "strmbase.dll" is missing. Install this program again"
But I never had a strmbasd.dll. I have a strmbasd.lib. That's the result when trying to compile the baseclasses-project with visual studio in debug-Mode.
Does anybody knows where to get this dll or how to use the lib instead?
Regards,
Julian

Removing console window for Glut/FreeGlut/GLFW?

Under Visual C++, I have played around with Glut/FreeGlut/GLFW. It seems that everyone of these projects adds a CMD window by default. I tried removing it going under:
Properties->C/C++->Preprocessor->Preprocessor
Definitions
From here, I remove the _CONSOLE and replace it with _WINDOWS
Then I went under:
Properties->Linker->System->SubSystem
And I set the option to Windows (/SUBSYSTEM:WINDOWS)
Then when I try compiling under GLFW, I get the following building errors:
Error 1 error LNK2001: unresolved
external symbol _WinMain#16
MSVCRT.lib
Error 2 fatal error LNK1120: 1
unresolved externals glfwWindow.exe
Is it possible to remove the console window?
Under the linker options, set your entry point to mainCRTStartup . This function does the necessary setup of the MS libc and then calls main.
Most linkers support options that automatically remove the console startup code.
I think on GCC it's called -mwindows
My project just has a main, (no WinMain) and to disable console, I just set Linker->System->SubSystem to "Windows (/SUBSYSTEM:WINDOWS)" instead of "Console (/SUBSYSTEM:CONSOLE)" and the console goes away.
You don't need to mess with the Preprocessor Definitions to remove the console window.
I know my answer is a few years late, but I hope it helps.
To get rid of the console using cmake, the link flags can be set as follows:
set_target_properties(exe_name PROPERTIES
LINK_FLAGS "/ENTRY:mainCRTStartup /SUBSYSTEM:WINDOWS")
Non-console Windows applications use the WinMain() entry point convention. Your Glut examples probably use the standard C main() convention.
If you want a quick fix just for the demo app, the WinAPI function FreeConsole() might help.
MSDN: http://msdn.microsoft.com/en-us/library/ms683150(v=vs.85).aspx
You need to write a WinMain entry point and copy your existing code (from main):
int CALLBACK WinMain(
__in HINSTANCE hInstance,
__in HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nCmdShow
){
// ...
}
If you create a new project as a console application, it will always run as such. You have to create a new GUI project if you want to run it in an actual window, otherwise the correct headers and libraries will not be included.
Also the WinMain function that's required will be included for you in the resulting template files.
When I've gotten an error like that I was able to fix it by entering that following text in the linker, section Advance, option Entry Point the following:
main