Error: no suitable conversion function from "std::string" to "char *" exists - visual-c++

basically I'm trying to secure some of the strings from my source but I keep getting this error on this line:
playerOptionsMenu->SetMenuTitle(base64_encode(reinterpret_cast<const unsigned char*>(playeroptionstitle.c_str()), playeroptionstitle.length()));
The error occurs on the base64_encode
Here is the definition of playeroptionstitle:
const std::string playeroptionstitle = "Player Options";
Here is the header for the base64_encode:
std::string base64_encode(unsigned char const* , unsigned int len);
The error I am getting is: Error: no suitable conversion function from "std::string" to "char *" exists

Related

Building String from UART Characters - Home Learning C

I'm working with a PIC and have successfully got a single character received over the UART, however I now need to capture a incoming sequence of characters, build up a string and perform a action after receiving a carrage return. I have some prior experience with PHP and thought things would be much easier than its turned out to be.
I've modified my simple code with the working UART functions to attempt to concatinate the received characters as follows and build up a string:
#include "mcc_generated_files/mcc.h"
#include "string.h"
unsigned char InChar;
char Temp[5];
char Command[32];
void UART_Demo_Command_INT(void)
{
if(eusartRxCount!=0)
{
InChar=EUSART_Read(); // read a byte for RX
strcat(Command,InChar); //concat Command with InChar, result in Command
printf = printf("Command String: %s \n", Command);
}
}
I'm receiving a number of errors:
UART_Demo.c:115:24: warning: incompatible integer to pointer conversion passing 'unsigned char' to parameter of type 'const char *' [-Wint-conversion]
strcat(Command,InChar); //concat Command with InChar, result in Command
^~~~~~
C:\Program Files (x86)\Microchip\xc8\v2.10\pic\include\c99\string.h:36:55: note: passing argument to parameter here
char *strcat (char *__restrict, const char *__restrict);
^
UART_Demo.c:116:16: error: non-object type 'int (const char *restrict, ...)' is not assignable
printf = printf("Command String: %s \n", Command);
~~~~~~ ^
1 warning and 1 error generated.
After spending a number of hours trying to fix this and getting nowhere I'm hoping some of you can help.
If anyone's interested the problem was because my received characters are just...characters, not a string which would normally have a termination \0 character at the end, therefore the strcat (Concatinate strings) function didn't work.
As a fix I just added \0 to the end of InChar then strcat worked.

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);

C2664 error stream.write unsigned char *

ostream &stream;
stream.write(SomeUnsignedCharStar, intSize);
error C2664 cannot convert parameter 1 from const unsigned char * to const char *
Is there an overload write for const unsigned char *?
I do not want to change SomeUnsignedCharStar because it is everywhere in the legacy code I inherited. This was compiled on VC6 with no complain. I am slowly upgrading the code to VS2003 and then VS2010 evantually.
What is the easiest and cleanest fix?
You can cast this without any issues. Strict aliasing allows casting pointers between unsigned and signed versions of the same type, as well as casting from any type to const char*, so you're safe here.

cannot convert parameter 1 from 'ATL::CString' to 'const wchar_t *'

For this line of code:
int currentSnapshotHeight = _wtoi(ExecuteExternalProgram(L"current.png"));
I got this error:
Error 1 error C2664: '_wtoi' : cannot convert parameter 1 from 'ATL::CString' to 'const wchar_t *'
How to fix?
Maybe this will work?
int currentSnapshotHeight = _wtoi(ExecuteExternalProgram(_T("current.png")));
Also check if Unicode setting of project are set as expected.
Try this:
int currentSnapshotHeight = _wtoi((wchar_t*)ExecuteExternalProgram(L"current.png").GetBuffer());

cant convert parameter from char[#] to LPWSTR

When I compile this code in Visual C++, I got the below error. Can help me solve this issue..
DWORD nBufferLength = MAX_PATH;
char szCurrentDirectory[MAX_PATH + 1];
GetCurrentDirectory(nBufferLength, szCurrentDirectory);
szCurrentDirectory[MAX_PATH +1 ] = '\0';
Error message:
Error 5 error C2664: 'GetCurrentDirectoryW' : cannot convert parameter 2 from 'char [261]' to 'LPWSTR' c:\car.cpp
Your program is configured to be compiled as unicode. Thats why GetCurrentDirectory is GetCurrentDirectoryW, which expects a LPWSTR (wchar_t*).
GetCurrentDirectoryW expects a wchar_t instead of char array. You can do this using TCHAR, which - like GetCurrentDirectory - depends on the unicode setting and always represents the appropriate character type.
Don't forget to prepend your '\0' with an L in order to make the char literal unicode, too!
It seems you have define UNICODE, _UNICODE compiler flags. In that case, you need to change the type of szCurrentDirectory from char to TCHAR.
Headers:
#include <iostream>
#include <fstream>
#include <direct.h>
#include <string.h>
#include <windows.h> //not sure
Function to get current directory:
std::string getCurrentDirectoryOnWindows()
{
const unsigned long maxDir = 260;
wchar_t currentDir[maxDir];
GetCurrentDirectory(maxDir, currentDir);
std::wstring ws(currentDir);
std::string current_dir(ws.begin(), ws.end());
return std::string(current_dir);
}
To call function:
std::string path = getCurrentDirectoryOnWindows(); //Output like: C:\Users\NameUser\Documents\Programming\MFC Program 5
To make dir (Folder) in current directory:
std::string FolderName = "NewFolder";
std::string Dir1 = getCurrentDirectoryOnWindows() + "\\" + FolderName;
_mkdir(Dir1.c_str());
This works for me in MFC C++.

Resources