error C2664: cannot convert from 'LPTSTR []' to 'LPCTSTR *' - c++

I use Visual Studio 2013 and I get the following error:
error C2664: 'DWORD Options(int,LPCTSTR *,LPCTSTR,...)' : cannot convert argument 2 from 'LPTSTR []' to 'LPCTSTR *' 54 1 ConsoleApplication3
This is the code:
DWORD Options(int argc, LPCTSTR argv[], LPCTSTR OptStr, ...){
// Code
}
int _tmain(int argc, LPTSTR argv[]){
iFirstFile = Options(argc, argv, _T("s"), &dashS, NULL);
// Code
}
Does anyone know how to fix it?
And explain why this error does occur?

"And explain why this error does occur?"
The reason behind this error can be found here:
an implicit conversion "... would let you silently and accidentally modify a const object without a cast..."
"Does anyone know how to fix it?"
LPCTSTR argv[] is not a constant object, but an array of constant strings. The array itself may be modified (argv[0] = 0;).
Since the advice in the link above is to avoid casting ("...please do not pointer-cast your way around that compile-time error message..."), the simplest solution is to change the signature of Options (notice the added const):
DWORD Options(int argc, const LPCTSTR argv[], LPCTSTR OptStr, ...)

Related

C++ can't compile code using PROCESSENTRY32

I'm trying to compile simple lines of code but I'm getting C2664 Error code.
#include <TlHelp32.h>
PROCESSENTRY32 pe32 = { 0 };
if (wcscmp(pe32.something, something) == 0)
Error:
int wcscmp(const wchar_t *,const wchar_t *)': cannot convert argument 1 from 'CHAR [260]' to 'const wchar_t
The definition of wcscmp() is:
_Check_return_
_ACRTIMP int __cdecl wcscmp(
_In_z_ wchar_t const* _String1,
_In_z_ wchar_t const* _String2
);
I can't use PROCESSENTRY32W because then Process32First() breaks because it needs PROCESSENTRY32.
How could I change this to make it compilable?
Use PROCESSENTRY32W instead of PROCESSENTRY32.
Use Process32FirstW instead of Process32First.

static_cast from 'FARPROC' is not allowed

I'm trying to get rid of a warning in my code, but I'm in way over my head here...
The original code row looks like this:
FeasaCom_Open=(tFeasaCom_Open)(GetProcAddress(static_cast<HMODULE>(hFeasaComDLL), "FeasaCom_Open"));
The warning that's thrown is:
warning: use of old-style cast
I've tried to fix this by re-writing it in this way:
FeasaCom_Open=static_cast<tFeasaCom_Open>(GetProcAddress(static_cast<HMODULE>(hFeasaComDLL), "FeasaCom_Open"));
But then I get the warning:
error: static_cast from 'FARPROC' (aka 'int (*)() __attribute__((stdcall))') to 'tFeasaCom_Open' (aka 'int (*)(int, char *) __attribute__((stdcall))') is not allowed
The definition of FeasaCom_Open is:
typedef int (__stdcall *tFeasaCom_Open)(int CommPort, char * Baudrate);
And declaration is:
tFeasaCom_Open FeasaCom_Open;
I probably don't need to tell you this, but declaration of GetProcAddress looks like this:
WINBASEAPI FARPROC WINAPI GetProcAddress (HMODULE hModule, LPCSTR lpProcName);
And FARPROC:
typedef int (WINAPI *FARPROC) ();
I've tried to fix this, but I don't really know what I'm doing here... I'm using Qt 5.9.6 with MinGW 5.3.0 32-bit, if that's any help.

Arguments from commandline to MessageBox in pure winapi (C++ without standard libraries)

I was trying to make a pure WinApi program that simply takes arguments and puts the first one into MessageBoxW. However, there is a problem, as the arguments passed are put into LPWSTR array, but MessageBoxW takes in LPCWSTR. This givesme an error, and I tried everything, using reinterpret_cast, const_cast, standard C conversion, but nothing works.
Here's the code:
#include <Windows.h>
int main()
{
LPWSTR *argv;
int argc;
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
LPWSTR lol = argv[1];
MessageBoxW(NULL, lol, "Test", 0);
ExitProcess(0);
}
The result is:
main.cpp(11): error C2664: 'int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)': cannot convert argument 3 from 'const char [5]' to 'LPCWSTR'
main.cpp(11): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Program compiled in Visual Studio 2015 using commandline:
cl /Os /GS- /Oi- primetest.cpp /link /fixed /nodefaultlib /safeseh:no /filealign:512 /entry:main /subsystem:console /MERGE:.rdata=.text kernel32.lib shell32.lib user32.lib
Those parameters allow for a small output file of 2kb, but this has nothing to do with the type conversion.
Also, the program compiles without any errors or warnings, and works, if a string literal is placed instead of "lol" variable in MessageBoxW.
#include <Windows.h>
int main()
{
LPCWSTR *argv;
int argc;
argv = CommandLineToArgvW(GetCommandLineW(),
&argc);
LPCWSTR lol = argv[1];
MessageBoxW(NULL, lol, L"Test", NULL);
ExitProcess(0);
return 0;
}

C++ error C2664 with CStrings

I have some old C++ file which I know used to compile. I have created a new install of Visual C++ version 6.
I am getting lots of compile errors with CStrings about not being able to convert to const char *
Here's an example.
CString dogs = "test";
writeoutfile(dogs, 1);
void Crender::writeoutfile(CString data, long data_size) {}
I get this error:
error C2664: 'void __thiscall Crender::writeoutfile(const char *,long)' : cannot convert parameter 1 from 'class CString' to 'const char *'
Is there some way I can get round this?
You have to get the raw pointer to the char field. This can be done with
CString::GetBuffer()
so you could call
writeoutfile(dogs.GetBuffer(), 1);
CString should convert to const char*. Is it a Unicode build? That's the only explanation I can think of.
GetBuffer() is for getting a writeable pointer to the data contained inside CString. Don't do that!

Getting a compilation error in VS2010 by following the first hello world example

i just started learning MFC..found a tutorial here http://bit.ly/j2uhHO ..just tried the same thing in VS2010 but getting a compilation error in this code..
void CChildView::OnPaint()
{
CPaintDC dc(this); // device context for painting
dc.TextOut(0, 0, "Hello, world!");
// TODO: Add your message handler code here
// Do not call CWnd::OnPaint() for painting messages
}
And the error is:
error C2664: 'BOOL CDC::TextOutW(int,int,const CString &)' : cannot convert parameter 3 from 'const char [14]' to 'const CString &'
Can anyone solve this and suggest some mfc tutorials please..thank u..
The error tells you whats exactly wrong.
error C2664: 'BOOL CDC::TextOutW(int,int,const CString &)' : cannot convert parameter 3 from 'const char [14]' to 'const CString &'
TextOutW() is expecting const CString & as the third parameter and you are passing const char [14]
You need to do:
dc.TextOut(0, 0, L"Hello, world!");
Which passes the third argument in the format desired by the function.
For MFC resources to refer, you see this.
The problem is that Windows by default uses wide characters wchar_t for texts. You would need
dc.TextOut(0, 0, L"Hello, world!");