Board index » cppbuilder » cannot convert AnsiString to const Char*

cannot convert AnsiString to const Char*


2007-06-05 11:01:16 PM
cppbuilder86
I have a compilable program. It ran fine. Now I want to convet it to a
COM server by adding an COM Object to the project. After I add a COM
object through the wizard, I got a lot of "cannot convert AnsiString
to const Char*" errors on lines like the following:
AnsiString sMSG2=sMSG.SubString(iCommaPos+1,iLen-iCommaPos);
int iProcessID = StrToInt(sMSG2); //cannot convert AnsiString to
const Char*
Can anyone help to explain why this is happening?
Thank you very much!
 
 

Re:cannot convert AnsiString to const Char*

< XXXX@XXXXX.COM >wrote in message
Quote
I have a compilable program. It ran fine. Now I want to convet it to
a
COM server by adding an COM Object to the project. After I add a
COM
object through the wizard, I got a lot of "cannot convert AnsiString
to const Char*" errors on lines like the following:

AnsiString sMSG2=sMSG.SubString(iCommaPos+1,iLen-iCommaPos);
int iProcessID = StrToInt(sMSG2); //cannot convert AnsiString to
const Char*

Sounds like you aren't calling the correct StrToInt(). You want
SysUtils::StrToInt().
--
Bruce
 

Re:cannot convert AnsiString to const Char*

XXXX@XXXXX.COM wrote:
Quote
int iProcessID = StrToInt(sMSG2); //cannot convert AnsiString to
const Char*

Can anyone help to explain why this is happening?
StrToInt() is a WinAPI and doesn't take AnsiStrings.
You might have been using the Indy/VCL version in the past, but it got
masked out (default namespace?) when you added the shell header
shlwapi.h
Use either StrToInt(sMSG2.c_str) or sMSG2.ToInt();
Or specify the VCL namespace SysUtils::StrToInt
(Thanks Bruce, the help doesn't list the SysUtils version under
StrToInt)
 

{smallsort}

Re:cannot convert AnsiString to const Char*

On Jun 5, 11:38 am, Bob Gonder < XXXX@XXXXX.COM >wrote:
Quote
XXXX@XXXXX.COM wrote:
>int iProcessID = StrToInt(sMSG2); //cannot convert AnsiString to
>const Char*

>Can anyone help to explain why this is happening?

StrToInt() is a WinAPI and doesn't take AnsiStrings.

You might have been using the Indy/VCL version in the past, but it got
masked out (default namespace?) when you added the shell header
shlwapi.h

Use either StrToInt(sMSG2.c_str) or sMSG2.ToInt();

Or specify the VCL namespace SysUtils::StrToInt

(Thanks Bruce, the help doesn't list the SysUtils version under
StrToInt)
Thanks! This explains it. I added c_str() to every line, it worked.
 

Re:cannot convert AnsiString to const Char*

To convert to char* use
AnsiString MyString = "Fred";
char *Text = MyString.c_str();
printf("Text=%s\n", Text); // prints out "Fred"
To convert an AnsiString to an int use
AnsiString MyString = "1234";
printf("Number=%d\n", MyString.ToInt()); // prints out
1234
For better error checking use ToIntDef(0); // which
checks for invalid characters in the string and returns the
default value (0) if it finds them rather than throwing an
exception.
HTH Pete
< XXXX@XXXXX.COM >wrote in message
Quote
I have a compilable program. It ran fine. Now I want to
convet it to a
COM server by adding an COM Object to the project. After
I add a COM
object through the wizard, I got a lot of "cannot convert
AnsiString
to const Char*" errors on lines like the following:

AnsiString
sMSG2=sMSG.SubString(iCommaPos+1,iLen-iCommaPos);
int iProcessID = StrToInt(sMSG2); //cannot convert
AnsiString to
const Char*

Can anyone help to explain why this is happening?

Thank you very much!

 

Re:cannot convert AnsiString to const Char*

Thanks for all the help. Now I got another error after I used .c_str()
in a file to convert a Ansistring to an integer:
int width= StrToInt(edtWidth->Text.c_str());
[Linker Error] Unresolved external 'StrToIntA' referenced from ..
\ACTDRAWORDER.OBJ
Any idea how to solve this?
On Jun 5, 12:25 pm, "Pete Fraser"
< XXXX@XXXXX.COM >wrote:
Quote
To convert to char* use
AnsiString MyString = "Fred";
char *Text = MyString.c_str();
printf("Text=%s\n", Text); // prints out "Fred"

To convert an AnsiString to an int use
AnsiString MyString = "1234";
printf("Number=%d\n", MyString.ToInt()); // prints out
1234
For better error checking use ToIntDef(0); // which
checks for invalid characters in the string and returns the
default value (0) if it finds them rather than throwing an
exception.
HTH Pete

< XXXX@XXXXX.COM >wrote in message

news: XXXX@XXXXX.COM ...



>I have a compilable program. It ran fine. Now I want to
>convet it to a
>COM server by adding an COM Object to the project. After
>I add a COM
>object through the wizard, I got a lot of "cannot convert
>AnsiString
>to const Char*" errors on lines like the following:

>AnsiString
>sMSG2=sMSG.SubString(iCommaPos+1,iLen-iCommaPos);
>int iProcessID = StrToInt(sMSG2); //cannot convert
>AnsiString to
>const Char*

>Can anyone help to explain why this is happening?

>Thank you very much!- Hide quoted text -

- Show quoted text -
 

Re:cannot convert AnsiString to const Char*

XXXX@XXXXX.COM wrote:
Quote
Thanks for all the help. Now I got another error after I used .c_str()
in a file to convert a Ansistring to an integer:
int width= StrToInt(edtWidth->Text.c_str());

[Linker Error] Unresolved external 'StrToIntA' referenced from ..
\ACTDRAWORDER.OBJ

Any idea how to solve this?


This is because of a #define.
The activeX headers end up including shlwapi.h. This has
#define StrToInt StrToIntA for non unicode.
In you code before you call StrToInt use
#undef StrToInt
You should now be able to use the original code without the error and it
should link correctly.
John.
 

Re:cannot convert AnsiString to const Char*

XXXX@XXXXX.COM wrote:
Quote
[Linker Error] Unresolved external 'StrToIntA' referenced from ..

Any idea how to solve this?
Add shlwapi.lib to your project
Though I don't understand why you don't just use the VCL functions
since you are already using VCL objects.
 

Re:cannot convert AnsiString to const Char*

On Tue, 05 Jun 2007 11:25:12 -0700, XXXX@XXXXX.COM wrote:
Quote
[Linker Error] Unresolved external 'StrToIntA' referenced from ..
\ACTDRAWORDER.OBJ

Any idea how to solve this?
define NO_SHLWAPI_STRFCNS in your project settings
--
Vladimir Ulchenko aka vavan