cannot convert parameter 1 from 'ATL::CString' to 'const wchar_t *' - visual-c++

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

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.

Error: no suitable conversion function from "std::string" to "char *" exists

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

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.

Convert a 'System::String ^' to 'const char *' in vc++

How can I convert a 'System::String ^' to 'const char *' in vc++?
My code:
String ^Result1= "C:/Users/Dev/Desktop/imag.jpg";
IplImage *img1 = cvLoadImage(Result1, 1);
if I do like above it will generate following error.
error C2664: 'cvLoadImage' : cannot convert parameter 1 from 'System::String ^' to 'const char *'
Please help me.
It's like this: How to convert from System::String* to Char* in Visual C++
System::String ^ str = "Hello world\n";
//method 1
pin_ptr<const wchar_t> str1 = PtrToStringChars(str);
wprintf(str1);
//method 2
char* str2 = (char*)Marshal::StringToHGlobalAnsi(str).ToPointer();
printf(str2);
Marshal::FreeHGlobal((IntPtr)str2);
//method 3
CString str3(str);
wprintf(str3);
//method 4
#if _MSC_VER > 1499 // Visual C++ 2008 only
marshal_context ^ context = gcnew marshal_context();
const char* str4 = context->marshal_as<const char*>(str);
puts(str4);
delete context;
#endif
This is work for me.
void MarshalString ( String ^ s, string& os ) {
using namespace Runtime::InteropServices;
const char* chars =
(const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
os = chars;
Marshal::FreeHGlobal(IntPtr((void*)chars));
}

converting String to double in vc++

Can anyone help me with a way of converting String to double in vc++?
I can't use atoi, since it converts char to double. But I am using istringstream.
std::istringstream stm;
double d;
String name = "32.67";
stm.str(name);
stm >>d;
It will give a compilation error:
error C2664: 'void std::basic_istringstream::str(const std::basic_string &)' :
cannot convert parameter 1 from 'System::String ^' to 'const std::basic_string &'
Please help with different solution or correct this.
std::stringstream str() accepts a std::string as an argument. You're passing it a System::String, wherever that comes from. Given the funky ^ symbol you must be using C++/CLI, using .NET strings.
Use std::string unless you are for some reason required to use the .NET library, in which case you need to either use .NET conversion functions, or convert to a std::string (or char* c-string and use the << operator).
As the other responder has suggestion, you are probably using C++/CLI. In that case:
String ^ name = "32.67";
double d;
d = Double::Parse(name);
Note that if the string cannot be parsed into a double, an exception will be thrown. Use Double::TryParse if you want to avoid that (it returns false if the string cannot be parsed).
I think it is very simple in vc++/CLR programming.
String ^name = "32.56";
String ^no = "56";
Double number_double = Convert::ToDouble(name); // convert String to double
Int number_int = Convert::ToInt32(no); // convert String to integer

Resources