cannot convert parameter 1 from 'char *' to 'System::String ^'  
Author Message
AliiZ





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

Hello!

I'm trying to read a variable from a ini file and put this data into a input box.

Here's my code:

char* lpstrINIFile = "inca.ini";
char* lpstrBuffer = new char[255+1];

GetPrivateProfileString( LPCWSTR("ADR"), LPCWSTR("ADRxPos"), NULL, LPWSTR(lpstrBuffer), 255, LPCWSTR(lpstrINIFile));

ADRxPos->Text = lpstrBuffer;

ADRxPos is the name of the input box, but somehow i get this error on compiling:
error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'char *' to 'System::String ^'

I've been googling for a solution for the last couple of hours, but can't find anything on how to fix this.

 #edit: Sorry for the doublepost, my browser was acting a bit weird.



Visual C++4  
 
 
Sahir Shah





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

You cannot use a char* where a String object is expected. You need to convert it. 

  ADRxPos->Text =  gcnew String(lpstrBuffer);


 
 
AliiZ





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

You cannot use a char* where a String object is expected. You need to convert it.

ADRxPos->Text = gcnew String(lpstrBuffer);

Ok it compiles now, so that helped a lot. But, the variable is empty.
The Code above still looks the same.

Here's the INI file:

[ADR]
ADRxPos=testXpos
ADRyPos=testYpos
ADRzPos=testZpos


 
 
Sahir Shah





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

But, the variable is empty.
The Code above still looks the same.
Here's the INI file

By that I presume you are asking why the call to GetPrivateProfileString failed. It may be because you haven't given the full path to the file i.e. char* lpstrINIFile = "inca.ini"


 
 
AliiZ





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

Still nothing =(

char* lpstrINIFile = "c:\inca.ini";

char* lpstrBuffer = new char[255+1];

GetPrivateProfileString( LPCWSTR("ADR"), LPCWSTR("ADRxPos"), NULL, LPWSTR(lpstrBuffer), 255, LPCWSTR(lpstrINIFile));

ADRxPos->Text = gcnew String(lpstrBuffer);


 
 
Simple Samples





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

If "c:\inca.ini" matches your source code exactly, then you still do not have a valid path. In C/C++, we must use two backslashes. You probably know that and it is easily overlooked.

Since GetPrivateProfileString does not return an indication of error such as file not found, it seems it would be good to check to ensure that the file exists before using GetPrivateProfileString. If it were me, I probably would leave the other error as-is and use it to test my error-checking code, then fix the error.



 
 
AliiZ





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

Well, i tried it with 2 backslashes, but the edit-box still stays empty.


 
 
Simple Samples





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

Did you add the error-checking code that I described

Do you know how to use the de**** Do you know how to set breakpoints and single-step through the program



 
 
AliiZ





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

Did you add the error-checking code that I described

Do you know how to use the de**** Do you know how to set breakpoints and single-step through the program

I'm not so very good with C++ yet, so i have no idea.
I just started a few days ago.


 
 
Simple Samples





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

Let's not bother with the error-checking code for now.

It will help if you know how to debug programs. See Walkthrough: Using the Visual Studio IDE. Scroll down the page to "Testing a Program". It describes breakpoints and how to "Step Over". The Step Over command will execute one line of source code at a time when debugging a program; it is very useful. Also see:

Which are all in the general topic Debugging in Visual Studio. You can use the de**** to see the actual data your program is using.



 
 
Holger Grund





PostPosted: Visual C++ Language, cannot convert parameter 1 from 'char *' to 'System::String ^' Top

The functional-notation casts don't do what you expect there. You have defined _UNICODE & UNICODE therefore GetPrivateProfileString binds to the wide character version. You should read up on how the two above macros. And when the compiler complains that it can't convert a type just throwing a dumb C-style cast at it certainly isn't a good idea in C++.

You should use the TEXT() or _T macros; e.g. _T("ADR") instead of LPCWSTR("ADR"). The latter still produces an array of 8 bit characters, which you tell the compiler to interpret as an array to 16 bit wide characters.

-hg

BTW: einaros was kind enough to write something up here http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=849851&SiteID=1