C++ SetDlgItemText Error - c++

i am new to C++ and am trying to set an Editbox text with a Int.
Using this code
int MyInt = 100;
SetDlgItemText(MyWindow, EditKills, MyInt);
I am getting this error below
In function 'BOOL DialogProc(HWND__*, UINT, WPARAM, LPARAM)':|
invalid conversion from 'int' to 'const CHAR*'|
Update Solved.
I used this code if anyone else has this problem and needs help.
int MyInt = 100;
SetDlgItemInt(MyWindow, EditKills, MyInt, TRUE);

The answer is the same as the answer to your question Invalid conversion from 'DWORD' to 'const char *'
C++ does not automatically convert any type to a string. You'll have to do that manually with sprintf.
case CSETimer:
int GotValue;
ReadProcessMemory(hProcess, (void*)(MYBASE + 0x728), &GotValue, 4, NULL);
char GotValueStr[10];
sprintf(GotValueStr, "%d", GotValue);
SetDlgItemText(MyWindow, EditKills, GotValueStr); return TRUE;

As per your code the variable GotValue is int . You need to convert into to LPCTSTR and pass it as the third argument of your SetDlgItemText function
char szBuf[MAX_PATH]={0};
_snprintf(szBuf, MAX_PATH-1,"%d",GotValue);
or use itoa_s
or
std::ostringstream oss;
oss <<GotValue;
std::string s(oss.str());
s.c_str()

Related

error: cannot convert 'const CHAR**' {aka 'const char**'} to 'LPCSTR' {aka 'const char*'} in C++

I have a big chunk of code that I am debugging, and I am stuck trying to figure out why I get the following error when I try to build the project:
"error: cannot convert 'const CHAR**' {aka 'const char**'} to 'LPCSTR'
{aka 'const char*'}"
The issue is with the wsKey parameter. SHRegGetValue is a function defined in the Shlwapi.h header, but I don't know how to fix this and I am inexperienced with the Windows API. Please let me know if there is a solution.
LPCSTR wsKey[MAX_PATH] = {"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"};
WCHAR wsValue[MAX_PATH] = L"ProxyEnable";
DWORD dwValue = (DWORD)FALSE;
DWORD dwSize = sizeof(dwValue);
LONG nStatus = SHRegGetValue(HKEY_CURRENT_USER, wsKey, wsValue, SRRF_RT_DWORD, NULL, &dwValue, &dwSize);
The line:
LPCSTR wsKey[MAX_PATH] = {"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"};
declares wsKey to be an array of MAX_PATH character pointers, the first of which points to the given string literal. If this is really what you want, then the second argument to your SHRegGetValue call should be that first element: wsKey[0].
However, what is more likely, is that you need wsKey to be an array of MAX_PATH characters – not pointers. Like this:
const CHAR wsKey[MAX_PATH] = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"; // Note the removal of the enclosing {...} braces!
You also have an error with the third parameter to SHRegGetValue: this should be a char (or CHAR) string, not a WCHAR string (you are mixing up the ANSI and Unicode versions of the call). Declare wsValue like this, to use the ANSI version:
CHAR wsValue[MAX_PATH] = "ProxyEnable";
Alternatively, if you intend to use the wide-character (Unicode) version, then you need to change your wsKey to a wide-character string. (This seems more likely, given the variable names.)
const WCHAR wsKey[MAX_PATH] = L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
WCHAR wsValue[MAX_PATH] = L"ProxyEnable";
Isn't your question answered in your title: const char** is not const char*. Why you expect that it should work?
I assume it's a copy & paste error. You should write:
LPCSTR wsKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
LPCSTR is an alias to const char *

C++ how to convert from char * to unsigned char * in function call?

When building my C++ application the build fails at this line of code
if (!PyTuple_GetByte(poArgs, 0, &SourceCell.window_type))
with this error
error C2664: 'PyTuple_GetByte' : cannot convert parameter 3 from
'char *' to 'unsigned char *'
This is the called function:
bool PyTuple_GetByte(PyObject* poArgs, int pos, unsigned char* ret);
The third parameter &SourceCell.window_type is type char.
Is there a way to convert/cast the parameter inside the function call like
if (!PyTuple_GetByte(poArgs, 0, reinterpret_cast<unsigned char*>(&SourceCell.window_type)))
or do I have to deal with it in another way?
From the error, the signature of the PyTuple_GetByte function was expecting a third parameter of type unsigned char*, but you passed a variable of type char* at its invocation. I think you have two options here.
You can change the signature of function PyTuple_GetByte to expect a char* parameter.
You need to convert your input variable from type char* to type unsigned char*, before you can pass it into PyTuple_GetByte.
The conversion is normally like this:
unsigned char* convert_var = reinterpret_cast<unsigned char*>(&SourceCell.window_type); // (c++ way)
or
unsigned char* convert_var = (unsigned char*)(&SourceCell.window_type); // (c way)

C++ Error to Read byte line

I'm trying to make the program read the text file and use the line for function, but I get an error!
5 IntelliSense: operand types are incompatible ("BYTE" and "char *")
Error 1 error C2446: '==' : no conversion from 'char *' to 'int'
Error 2 error C2040: '==' : 'int' differs in levels of indirection from 'char [260]'
my code:
char* ReadINI(char* szSection, char* szKey, const char* szDefaultValue)
{
char* szResult = new char[255];
memset(szResult, 0x00, 255);
GetPrivateProfileString(szSection, szKey, szDefaultValue, szResult, 255, ".\\Config.ini");
return szResult;
}
int main (Classdata* Cdata)
{
BYTE ByteID = Cdata->ByteType;
static char ReadByte[MAX_PATH];
sprintf(ReadByte, "%s", ReadINI("CONFIG", "Key", "0"));
if (ByteID == ReadByte)
{
printf("Byte Value: %p", ReadByte);
}
}
Firstly, you are comparing ByteId (an unsigned char `) with ReadByte (a char*) which is unlikely to be meaningful.
On an unrelated note, szResult is created with new but never deleted in main.
Couldn't be much clearer ByteId is a BYTE, ReadByte is a char[260]. You cannot compare integers and arrays. Perhaps (only a guess) you meant ByteId == ReadByte[0].
Plus you have a memory leak in ReadIni, and ReadByte is declared static for no good reason I can see, and your declaration of main is not legal.
I used another type of function to read the file! Sorry for the bad explanation is that I'm working on a private project! thanks for the help everyone
fuction:
UINT value = GetPrivateProfileInt("Section", "Key", DEFAULT_VALUE, "program.ini");
thx for all!

adding a value in to the http request from a long to char

I am trying to to add value in to the http request and getting errors when I add a long into the path.
long test1, test2;
unsigned long age;
numdata=inet.httpGET("test.com", 80, '/system/get.php?value1='+test1+'&value2='+test2, msg, 50);
error: invalid conversion from 'long int' to 'const char*'
And I have tried the following and getting an error.
const char getRequest = '/system/get.php?value1='+test1+'&value2='+test2;
numdata=inet.httpGET("test.com", 80, getRequest, msg, 50);
And am getting the following error
error: invalid conversion from 'char' to 'const char*'
If would be better to use a ostringstream for this
#include <sstream>
std::ostringstream ss;
ss << "/system/get.php?value1=" << test1 << "&value2=" << test2;
then you can get at the std::string from the string stream using
ss.str();
Whatever you choose you should use " instead of single quotes when dealing with a char array. Use only single quotes when dealing with a single char variable.
What you are currently doing here
const char getRequest = '/system/get.php?value1='+test1+'&value2='+test2;
is declaring a const char - that is a single constant character. This is not the same as an array of char.

Convert C++ string variable to long

I have a variable:
string item;
It gets initialized at run-time. I need to convert it to long. How to do it? I have tried atol() and strtol() but I always get following error for strtol() and atol() respectively:
cannot convert 'std::string' to 'const char*' for argument '1' to 'long int strtol(const char*, char**, int)'
cannot convert 'std::string' to 'const char*' for argument '1' to 'long int atol(const char*)'
c++11:
long l = std::stol(item);
http://en.cppreference.com/w/cpp/string/basic_string/stol
C++98:
char * pEnd;.
long l = std::strtol(item.c_str(),&pEnd,10);
http://en.cppreference.com/w/cpp/string/byte/strtol
Try like this:
long i = atol(item.c_str());
Use a string stream.
#include <sstream>
// code...
std::string text;
std::stringstream buffer(text);
long var;
buffer >> var;
Use std::stol < characters to fill space >
If you don't have access to C++11, and you can use the boost library, you can consider this option:
long l = boost::lexical_cast< long >( item );