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.
Related
argument of type "char *" is incompatible with parameter of type "LPCWSTR"
'int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)': unable to convert an argument 2 of 'char [255]' to 'LPCWSTR'
type conversion': conversion from 'unsigned int' to larger size 'LPVOID'
The code I'm using is this
char buffer[255] = { 0 };
unsigned addr = 0xb50798;
if (ReadProcessMemory(hproc, (LPVOID)addr, &buffer, sizeof(buffer), NULL))
{
MessageBoxW(hwnd, buffer, NULL, NULL);
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.
I have the following line of code:
(total_time== 0xAAAABBBB) ? ("") : _ui64toa_s(total_time, myArray, 1024, 10);
As the title suggests, I receive this error:
error C2446: ':' : no conversion from 'errno_t' to 'const char *'
I am unsure how to convert errno_t to const char *.
I tried to solve it by writing
const char *n = _ui64toa_s(total_time, myArray, 1024, 10);
only to see this:
error C2440: 'initializing' : cannot convert from 'errno_t' to 'const char *'
_ui64toa_s does not return a string like _ui64toa does.
Instead it returns an errno_t which is defined as int.
errno_t e = _ui64toa_s(total_time, myArray, 1024, 10);
But since you're using C++ I recommend you use std::to_string instead.
std::to_string(total_time).c_str();
You might also want to take a look at this question.
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.
I have made a test case to show the problems I am running into. Please forgive me my ignorance on the issues of Deferred libraries and pointer casting. The only library included in the deferred.lib.
#include <deferred/deferred.h>
using namespace deferred;
SafePtr<Deferred> recordTime(int time)
{
SafePtr<Deferred> d = createDeferred();
SafePtr<CallbackData> p = new PointerCBD< char>( 0 );
d->execute(p);
return d;
}
int main(int argc, char* argv[])
{
while(1)
{
SafePtr<Deferred> d = recordTime(1000);
d->waitHereForCompletion();
char* c = dynamic_pointer_cast<char>(d->endResult());
}
return 0;
}
When I try and compile in Windows XP with VS2008 I get:
Error C2440: 'initializing' : cannot convert from
'deferred:SafePtr' to 'char *' with
[T=char]
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called.
I have tried this command to return a pointer, rather than a SafePtr:
ManagerTimings* t = dynamic_pointer_cast<ManagerTimings>(d->endResult()).get();
Then I get this error:
Error C2664: 'deferred::intrusive_ptr_release' : cannot convert
parameter 1 from 'char *' to
'deferred:ReferenceCountable *'
I have tried this command:
ManagerTimings* t = dynamic_pointer_cast<ManagerTimings>(d->endResult().get());
Then I get this error:
Error C2784: 'deferred::SafePtr deferred::dynamic_pointer_cast(const deferred::SafePtr< U > &)' : could not deduce template argument for 'const deferred::SafePtr< U > &' from 'deferred::CallbackData *'
Try this:
ManagerTimings* t = dynamic_pointer_cast<ManagerTimings>(d->endResult().get());
if you want to get an "unsafe" pointer, or probably this:
SafePtr<ManagerTimings> t= dynamic_pointer_cast<ManagerTimings>(d->endResult());
to get a safe ManagerTimings pointer. I don't know what library you are using, but I suspect that dynamic_pointer_cast can convert a SafePtr to another SafePtr. Or it just converts pointers.
&*d->endResult()
I think from this code endResult is the SafePtr you're having trouble with.