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.
Related
I'm trying to get the path of my executable on my windows machine.
std::string get_exe_path_dir()
{
wchar_t path[MAX_PATH];
GetModuleFileName( NULL, path, MAX_PATH );
PathRemoveFileSpec(path);
std::wstring ws(path);
std::string str(ws.begin(), ws.end());
return str.substr(0, str.find_last_of('/'));
}
But now I get following error message:
cannot convert argument 2 from 'wchar_t [260]' to 'LPSTR'
I tried casting but it didn't work.
You are using explicit wchar_t instead of TCHAR, so you should use explicit Unicode version GetModuleFileNameW and PathRemoveFileSpecW instead of GetModuleFileName and PathRemoveFileSpec.
This question already has answers here:
error: cannot convert 'const wchar_t [13]' to 'LPCSTR {aka const char*}' in assignment
(3 answers)
Closed 3 years ago.
I'm having trouble by compiling this code in Release mode (it's working perfectly in Debug mode)
#include "MemoryManagement.h"
#include "Windows.h"
#include "Colors.h"
#include <iostream>
#include <TlHelp32.h>
using namespace std;
MemoryManagement::MemoryManagement()
{
handle = NULL;
}
MemoryManagement::~MemoryManagement()
{
CloseHandle(handle);
}
DWORD MemoryManagement::getProcess(const char* proc)
{
HANDLE hProcessId = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
DWORD process = 0;
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof(pEntry);
SetConsoleColor(colors::blue_bright);
cout << "Searching game..." << endl;
while (!process)
{
do {
if (!strcmp(pEntry.szExeFile, proc))
{
process = pEntry.th32ProcessID;
CloseHandle(hProcessId);
handle = OpenProcess(PROCESS_ALL_ACCESS, false, process);
}
} while (Process32Next(hProcessId, &pEntry));
}
SetConsoleColor(colors::green_bright);
cout << "Game found!" << endl << endl;
return process;
}
uintptr_t MemoryManagement::getModule(DWORD procId, const char* modName)
{
HANDLE hModule = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procId);
MODULEENTRY32 mEntry;
mEntry.dwSize = sizeof(mEntry);
do
{
if (!strcmp(mEntry.szModule, modName))
{
CloseHandle(hModule);
return (DWORD)mEntry.hModule;
}
} while (Module32Next(hModule, &mEntry));
return 0;
}
DWORD MemoryManagement::getAddress(DWORD addr, std::vector<DWORD> vect)
{
for (unsigned int i = 0; i < vect.size(); i++)
{
ReadProcessMemory(handle, (BYTE*)addr, &addr, sizeof(addr), 0);
addr += vect[i];
}
return addr;
}
The error output is this one:
1>c:\users\kuhi\source\repos\cs\cs\memorymanagement.cpp(31): error C2664: 'int strcmp(const char *,const char *)': cannot convert argument 1 from 'WCHAR [260]' to 'const char *'
1>c:\users\kuhi\source\repos\cs\cs\memorymanagement.cpp(31): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\kuhi\source\repos\cs\cs\memorymanagement.cpp(54): error C2664: 'int strcmp(const char *,const char *)': cannot convert argument 1 from 'WCHAR [256]' to 'const char *'
1>c:\users\kuhi\source\repos\cs\cs\memorymanagement.cpp(54): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Done building project "CS.vcxproj" -- FAILED.
I see the problem is with strcmp, that I'm giving a wrong data type...
I searched around and didn't find a fix that match my situation...
Why it's working in Debug mode but not in Release mode?
I have to compare 2 strings that can change every time I open the program, so I don't get how to give the arguments as const
It does not make sense for this to work in debug mode; that must be a mis-observation.
WCHAR is a Microsoft alias for wchar_t (or unsigned short if needs be) (ref).
You have an array of these things.
Said array will never be compatible with const char*, because wchar_t and char are two different things. So, you cannot pass your array to functions that require const char*, like strcmp.
C (and, by extension, C++) also provides a wide-character version of strcmp that you can use: wcscmp.
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, ...)
I'm still rather new to C++ but I've ran into a problem I can't solve, this is my error message:
'HMODULE GetModuleHandleW(LPCWSTR)': cannot convert argument 1 from 'const char *' to 'LPCWSTR'
And this the line that is throwing the error:
ModuleHandle = (DWORD)GetModuleHandle(moduleName.c_str());
You are passing a char * to something that needs a wchar_t *. You'll have to either convert your stringtype to wchar_t *, for example using the MultiByteToWideChar function (https://msdn.microsoft.com/en-us/library/windows/desktop/dd319072%28v=vs.85%29.aspx), or you can use the non-wide version of GetModuleHandle by calling GetModuleHandleA() instead of GetModuleHandleW().
I'm trying to compile some old game SRC I found on the net here's the code
bool LoadFromINI(std::wstring const& strINIFileName = _T("./Local.ini"), char const* szDefaultLocale = "");
bool LoadFromINB(std::wstring const& strINBFileName, wchar_t const* szDefaultLocale = _T(""));
C:\...\...Code\Cel_Convert_Source\Cosmos\include\BM/LocalMgr.h(60): error C2440: 'default argument' : cannot convert from 'const char [1]' to 'const wchar_t *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>C:\...\...Code\Cel_Convert_Source\Cosmos\include\BM/LocalMgr.h(60): error C2548: 'LOCAL_MGR::CLocal::LoadFromINB' : missing default parameter for parameter 2
1>C:\...\...Code\Cel_Convert_Source\Cosmos\include\BM/LocalMgr.h(59): error C2440: 'default argument' : cannot convert from 'const char [12]' to 'const std::wstring &'
1> Reason: cannot convert from 'const char [12]' to 'const std::wstring'
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>C:\...\...Code\Cel_Convert_Source\Cosmos\include\BM/LocalMgr.h(103): fatal error C1903: unable to recover from previous error(s); stopping compilation
One more error:
Code:
_tcscpy_s(m_kDBName,30, (wchar_t const*)in_strDBName);
Output:
'errno_t strcpy_s(char *,rsize_t,const char *)' : cannot convert parameter 3 from 'const wchar_t *' to 'const char *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Change _T("blah") to L"blah".
_T is a macro that does nothing or adds an L.
Alternatively, compile project with the wchar option for _T and TCHAR.
The _T("str") expands to L"str" only if your project is compiled with the UNICODE preprocessor symbol defined. In your case, it seems it isn't, so _T() does nothing. Change the function declarations to
bool LoadFromINI(std::wstring const& strINIFileName = L"./Local.ini", wchar_t const* szDefaultLocale = "");
bool LoadFromINB(std::wstring const& strINBFileName, wchar_t const* szDefaultLocale = L"");
or if you really, really must support the _T() and TCHAR stuff, change them to
bool LoadFromINI(std::basic_string<TCHAR> const& strINIFileName = _T("./Local.ini"), TCHAR const* szDefaultLocale = "");
bool LoadFromINB(std::basic_string<TCHAR> const& strINBFileName, TCHAR const* szDefaultLocale = _T(""));
Now the first argument will be either std::string or std::wstring depending on whether UNICODE is defined.