CString to const char * _Source in function strcpy_s - visual-c++

I have code:
char buff[50000];
CString params = "sss" ;
strcpy_s(buff,params);
Why strcpy_s accepts second param that is CString? According to my understanding second param should be of type const char * _Source

CString has a LPCTSTR conversion operator. The compiler calls this to convert your CString to the required const char *. So what the compiler does is:
strcpy_s(buff, (const char*)params);
which is equivalent to
strcpy_s(buff, (LPCTSTR)params);
which is like
strcpy_s(buff, params.operator LPCTSTR());
This only works if you are not compiling for Unicode. On Unicode LPCTSTR is not const char * but const WCHAR *, so this would not work.

Related

How to convert string to const char[] in c++

I am working on a C++ project for a Vex Robot, I am using a function that takes a const char[] but for showing an integer, there is no such function that converts to const char[], so is there a way to do that. All the search results showed how to convert to const char* and by the way "string" is of type const char[].
EDIT:
I also need to covert the string to concatenate tow strings (but ended up just coverting both strings to std::string and then back to const char[])
With std::string there is the function c_str() (with the synonym data() since C++11) to get a pointer to the underlying null-terminated character array:
std::string const myString{"stackoverflow"};
const char* cstr = myString.c_str();
const char* data = myString.data();

creating LPCTSTR conversion operator

Trying to create LPCTSTR conversion operator for my class:
AuthData::operator LPCTSTR() const
{
const char* k = "aaaa";
return k;
}
Error: return value type does not match the function type
Why it don't match?
If Unicode Character Set is enabled, LPCTSTR type is const wchar_t *.
#Edit
You can read about these types here:
https://softwareengineering.stackexchange.com/a/194768
In a Multi-Byte project, LPCTSTR is defined as LPCSTR, which is CONST CHAR*, so your code should compile (if it is in fact non-Unicode).
NEVER mix TCHARs with chars! At least - not without a conversion. Your declaration practically implies that TCHAR is defined as char, so why would you suggest to the readers that you are TCHAR-aware (since you are not)?

Argument mismatch in SecureZeroMemory in C++

char *chBuff = new char[nBufferSize];
::SecureZeroMemory(chBuff, sizeof(chBuff));
I used the above code but get the following error
Wrong sizeof argument (SIZEOF_MISMATCH)
suspicious_sizeof: Passing argument chBuff of type char * and argument 4UL /* sizeof (chBuff) */ to function RtlSecureZeroMemory is suspicious.
Should I typecast? If so how?
syntax of SecureZeroMemory:
PVOID SecureZeroMemory(
_In_ PVOID ptr,
_In_ SIZE_T cnt
);
sizeof(char *) will provide you size of a pointer, instead, use the following:
char *chBuff = new char[nBufferSize];
::SecureZeroMemory(chBuff, sizeof(char)*nBufferSize);

How to convert unsigned char to LPCTSTR in visual c++?

BYTE name[1000];
In my visual c++ project there is a variable defined name with the BYTE data type. If i am not wrong then BYTE is equivalent to unsigned char. Now i want to convert this unsigned char * to LPCTSTR.
How should i do that?
LPCTSTR is defined as either char const* or wchar_t const* based on whether UNICODE is defined or not.
If UNICODE is defined, then you need to convert the multi-byte string to a wide-char string using MultiByteToWideChar.
If UNICODE is not defined, a simple cast will suffice: static_cast< char const* >( name ).
This assumes that name is a null-terminated c-string, in which case defining it BYTE would make no sense. You should use CHAR or TCHAR, based on how are you operating on name.
You can also assign 'name' variable to CString object directly like:
CString strName = name;
And then you can call CString's GetBuffer() or even preferably GetString() method which is more better to get LPCTSTR. The advantage is CString class will perform any conversions required automatically for you. No need to worry about Unicode settings.
LPCTSTR pszName = strName.GetString();

Conversion of CString to float

Some body help me regarding to the following problem
strFixFactorSide = _T("0.5");
dFixFactorSide = atof((const char *)(LPCTSTR)strFixFactorSide);
"dFixFactorSide" takes value as 0.0000;
How I will get correct value?
Use _tstof() instead of atof(), and cast CString to LPCTSTR, and leave it as such, instead of trying to get it to const char *. Forget about const char * (LPCSTR) while you're working with unicode and use only const _TCHAR * (LPCTSTR).
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
CString s1 = _T("123.4");
CString s2 = _T("567.8");
double v1 = _tstof((LPCTSTR)s1);
double v2 = _tstof((LPCTSTR)s2);
_tprintf(_T("%.3f"), v1 + v2);
return nRetCode;
}
and running this correctly gives the expected answer.
I think your CString strFixFactorSide is a Unicode (UTF-16) string.
If it is, the cast (const char *) only changes the pointer type, but the string it points to still remains Unicode.
atof() doesn't work with Unicode strings. If you shove L"0.5" into it, it will fetch bytes 0x30 ('0') and 0x00 (also part of UTF-16 '0'), treat that as a NUL-terminated ASCII string "0" and convert it to 0.0.
If CString strFixFactorSide is a Unicode string, you need to either first convert it to an ASCII string and then apply atof() or use a function capable of converting Unicode strings to numbers. _wtof() can be used for Unicode strings.

Resources