954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Cannot convert 'AnsiString' to 'const char *'

I am writing a browser program in C++ Builder 6 that loads a web page using the following code:

void __fastcall TForm1::ToolButton1Click(TObject *Sender)
{
     wchar_t buff[100];
     MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
     "http://www.daniweb.com", -1, buff, sizeof(buff));
     CppWebBrowser1->Navigate(buff, 0, 0, 0, 0);
}

To include the address bar implementation, I need a temporary string derived from an edit box. This is the code I have tried but receive the error that I cannot convert an AnsiString to const char * and require a type conversion:

void __fastcall TForm1::ToolButton1Click(TObject *Sender)
{
     const char *tempstring = Edit1->Text;
     wchar_t buff[100];
     MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
     tempstring, -1, buff, sizeof(buff));
     CppWebBrowser1->Navigate(buff, 0, 0, 0, 0);
}

I've tried to decipher how the TStringConverter VCL class works but I have been unsuccesful determining how it works either:

TStringConverter::AnsiToWide

Converts a null-terminated ANSI string to a wide (Unicode) string.
static LPWSTR AnsiToWide(LPCSTR src);

WideToAnsi calls MultiByteToWideChar(), using the ANSI code page, to convert the ANSI string specified by src to a null-terminated string of Unicode characters. The source string may be from a multibyte character set.

Thanks in advance for suggestions anyone might have as to how I can resolve this programming obstacle.

parse loki
Newbie Poster
1 post since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

After a little googling I found that Borland's AnsiString is actually a class, much like std::string.

So your code should say:

MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
    tempstring.c_str(), -1, buff, sizeof(buff));


Hope this helps.

Duoas
Postaholic
2,043 posts since Oct 2007
Reputation Points: 1,140
Solved Threads: 229
 

This is my proposal

CppWebBrowser1->Navigate (WideString(Edit1->Text));

This work certainly

alentt
Newbie Poster
1 post since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

You can't just assign a const char * to anything... You are infact assigning an address, not a temp string. And I doubt those are your intentions... Instead use strcpy() or something similar.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You