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!
Related
I have searched on this site for an answer and the top answer did not work for me, I keep getting this error. I recently (not sure if I did it successfully) imported my project, that has no errors, from my desktop to my new laptop. Whenever I try to run, I get this error in multiple files, using only one as an example :
error C2664: 'DWORD CHackProcess::GetModuleNamePointer(LPSTR,DWORD)': cannot convert argument 1 from 'const char [11]' to 'LPSTR'
This is on these lines:
while (__dwordClient == 0x0) __dwordClient = GetModuleNamePointer("client.dll", __gameProcess.th32ProcessID);
while (__dwordEngine == 0x0) __dwordEngine = GetModuleNamePointer("engine.dll"6, __gameProcess.th32ProcessID);
while (__dwordVGui == 0x0) __dwordVGui = GetModuleNamePointer("vguimatsurface.dll", __gameProcess.th32ProcessID);
MSDN states the following definition for LPSTR:
typedef char* PSTR, *LPSTR;
This means that it is a non const expression. The string you're passing is constant.
You just have to pass a non constant string as the first argument.
EDIT:
It could be translated to the following:
char engineModuleName[] = "engine.dll";
GetModuleNamePointer(engineModuleName, __gameProcess.th32ProcessID);
I know this has already been discussed in several questions on SO, but none of those solutions have worked for me.
I start with a char* because this is for a DLL that will be called from VBA, and char* is necessary for VBA to pass a string to the DLL.
I need to return a LPCWSTR because that's the input parameter for the API function I'm trying to call, and I can't enable casting by switching from Unicode to multi-byte character set in the Properties window, because the API has this code:
#if !defined(UNICODE) && !defined(NOUNICODE)
#error UNICODE is not defined. UNICODE must be defined for correct API arguments.
#endif
I tried this:
LPCWSTR convertCharArrayToLPCWSTR(char* charArray)
{
const char* cs=charArray;
wchar_t filename[4096] = {0};
MultiByteToWideChar(0, 0, cs[1], strlen(cs[1]), filename, strlen(cs[1]));
}
which gave these errors:
error C2664: 'strlen' : cannot convert parameter 1 from 'const char' to 'const char *'
error C2664: 'MultiByteToWideChar' : cannot convert parameter 3 from 'const char' to 'LPCCH'
I tried this (same function header), loosely adapted from this post:
size_t retVal;
const char * cs = charArray;
size_t length=strlen(cs);
wchar_t * buf = new wchar_t[length](); // value-initialize to 0 (see below)
size_t wn = mbsrtowcs_s(&retVal,buf,20, &cs, length + 1, NULL);
return buf;
This compiled ok, but when I passed it an example string of "xyz.xlsx", mbsrtowcs_s() set buf to an empty string: L""
So, how do I make this conversion?
Following Hans Passant's advice regarding pointers to local variables, I worked out this approach, which seems to work well:
wchar_t *convertCharArrayToLPCWSTR(const char* charArray)
{
wchar_t* wString=new wchar_t[4096];
MultiByteToWideChar(CP_ACP, 0, charArray, -1, wString, 4096);
return wString;
}
I'm aware that the use of new requires memory management, which I perform in the function that calls this one.
Since cs is a const char*, cs[1] is a const char. C++ won't convert it to a pointer for you, because in most cases that doesn't make sense.
You could instead say &cs[1] or cs+1 if the intent is to skip the first char. (That's what you're doing when you pass a pointer to the 1th element; in C++, indexes start at 0.) If the intent is to pass the whole string, then just pass cs.
I'm trying to build a function to assign VARIANTARG types to a DISPPARAMS structure, and I'm having some issues figuring out how to check the actual type of the arg.
myFunc(int count, const BYTE* types, ...)
{
DISPPARAMS dParams;
//Initialization stuff goes here
for(int x = 0;x < count;x++)
{
BYTE vt = types[0];
dParams.rgvarg[x].vt = vt;
if(vt == VTS_I4)
{
dParams.rgvarg[x].lVal = ...;
}
}
}
BYTE params[] = {VTS_I4};
myFunc(1, params, 123);
When I compile this, I get two errors at the if statement:
error C2446: '==' : no conversion from 'const char *' to 'int'
error C2040: '==' : 'int' differs in levels of indirection from 'const char [2]'
So I'm fairly confused here: why can I assign a BYTE value from the VTS_I4, but I can't compare it later? If I cast VTS_I4 to a BYTE then I can compare it, but it has a different value so the if statement isn't triggered. The other thing I can do since VTS_I4 is defined in afxdisp.h as "\x03" is check if(vt == 0x03), and that works fine, but has to be wrong somehow.
Another way to ask this question might be why I can do this:
BYTE a[] = VTS_I4;
but not this:
BYTE b = VTS_I4; //'initializing' : cannot convert from 'const char [2]' to 'BYTE'
Thanks
As mentioned in the comments, the problem was that I was using the wrong constant; I needed to use VT_I4 instead of VTS_I4.
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()
I have the below program written in C++:
#include <iostream>
using namespace std;
int main()
{
int age[5];
char name[5][10];
age[0]=10;
age[1]=20;
age[2]=30;
age[3]=25;
age[4]=40;
name[0]="abc";
name[1]="abc";
name[2]="abc";
name[3]="abc";
name[4]="abc";
cout<<name[0]<<" is "<<age[0]<<"years old";
cout<<"\n";
cout<<name[1]<<" is "<<age[1]<<"years old";
cout<<"\n";
cout<<name[2]<<" is "<<age[2]<<"years old";
cout<<"\n";
cout<<name[3]<<" is "<<age[3]<<"years old";
cout<<"\n";
cout<<name[4]<<" is "<<age[4]<<"years old";
cout<<"\n\n";
system("PAUSE");
}
When I compile and run it, I get these errors:
error C2440: '=' : cannot convert
from 'const char [3]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
error C2440: '=' : cannot convert
from 'const char [2]' to 'char [10]'
There is no context in which this conversion is possible
I am running MSVC 2008 under Windows 7. I have tried many possible solutions but I failed in fixing this. Any help would be appreciated,
You are treating the name array as if it was defined thus:
char *name[5];
So either define it that way, or use the following code to populate it:
strcpy(name[0], "abc");
strcpy(name[1], "abc");
strcpy(name[2], "abc");
strcpy(name[3], "abc");
strcpy(name[4], "abc");
I prefer the former choice. The point being you are trying to assign a char * to a char [] which is what strcpy is for. Given you are manipulating initialized C strings in this case anyway, you might as well deal with char * throughout the code.
You should use std::string for this purpose. The use of char* and char[] to represent strings is deprecated in C++ for many good reasons.
Given the program snippet, name can be initialized at the declaration itself.
char name[5][10] = { "abc", "abc", "abc", "abc", "abc" } ;
// ^ index 5 is not necessary. char name[][10] = { .. } would also suffice.
Specified the length of each row is 10 but only using first 3 indexes of it. Every 3rd index ( i.e., 4th element in the array ) is automatically added with a '\0'.
Initialization can be done in case of age array too.
You can use also std::string name[10] instead of 2d char's array. In this case only you can assign new values to the strings through operator '='.
Otherwise you should to use array of char* and use strcpy() function for assignment.