bstr string to New character string

K

Karthik

Hello!

I am not a wizard in this area! Just need some help out in this. I am
trying to convert bstr string to new character string. Here is the
snippet of my code.

**** Code Start****

GlobalInterfacePtr->GetName(bstr, bstrTNamePtr, &retVal);

wcscpy (wstr1, *bstrTNamePtr);

char text2[20];

WideCharToMultiByte (CP_ACP, 0, wstr1, -1, text2, sizeof (text2), NULL,
NULL);

**** Code END****

What I am doing in this code is getting a string value from the C#.NET
to VC++ . The C# .NET sends the correct string value. When VC++
receives it, it comes up with a junk value for the string "Text2". I
scripted this code with the help from MSDN. Dont know why I am geting
the junk value. I guess the conversion to new character string is not
done properly. This code was working two months back. I dug it out to
polish it here and there. But, it would not work. I would apprecite if
anyone could help me out here!!!

Thanks a much!

Karthik
 
V

Victor Bazarov

Karthik said:
I am not a wizard in this area! Just need some help out in this. I am
trying to convert bstr string to new character string. Here is the
snippet of my code.

**** Code Start****

GlobalInterfacePtr->GetName(bstr, bstrTNamePtr, &retVal);

What does that function do? Since it's not part of C++ library, we
don't know, you have to tell us (if it's relevant).

wcscpy (wstr1, *bstrTNamePtr);

I can probaby guess that 'bstrTNamePtr' contains a pointer to a wide
character string... Now, 'wstr1' is filled with the same wide chars
as the string at '*pstrTNamePtr'. OK.
char text2[20];

WideCharToMultiByte (CP_ACP, 0, wstr1, -1, text2, sizeof (text2),
NULL, NULL);

Are we supposed to know what that function does, as well? It's not
a standard C++ function. Are you in the right newsgroup? It seems
that you need help on Windows API. Try comp.os.ms-windows.programmer.
**** Code END****

What I am doing in this code is getting a string value from the C#.NET
to VC++ . The C# .NET sends the correct string value. When VC++
receives it, it comes up with a junk value for the string "Text2".

What's "Text2"? Do you mean "text2"?
I
scripted this code with the help from MSDN. Dont know why I am geting
the junk value.

Have you tried reading about 'WideCharToMultiByte' function?
I guess the conversion to new character string is not
done properly. This code was working two months back. I dug it out to
polish it here and there. But, it would not work. I would apprecite if
anyone could help me out here!!!

Well, if that code used to work, you need to analyse what's changed
since that time. That's how we all fix regression bugs.

Also, see FAQ section 5 for many good suggestions.

V
 
T

Thomas J. Gritzan

Karthik said:
Hello!

I am not a wizard in this area! Just need some help out in this. I am
trying to convert bstr string to new character string. Here is the
snippet of my code.

bstr is MFC oder COM. Ask in a Windows specific newsgroup.
**** Code Start****

GlobalInterfacePtr->GetName(bstr, bstrTNamePtr, &retVal);

wcscpy (wstr1, *bstrTNamePtr);

char text2[20];

WideCharToMultiByte (CP_ACP, 0, wstr1, -1, text2, sizeof (text2), NULL,
NULL);

**** Code END****

What I am doing in this code is getting a string value from the C#.NET
to VC++ . The C# .NET sends the correct string value. When VC++
receives it, it comes up with a junk value for the string "Text2".

Maybe the buffer is too small. Did you check the return value of
WideCharToMultiByte? It indicates an error in this case.

Just ask Google on this function, the first hit usually is on msdn.

Thomas
 
K

Karthik

Hello Victor...

Let me explain the what each line does (OR SUPPOSED TO DO...)

**** Code Start****
// A Call function to C#.NET to get the string value. bstr string value
is sent to C#. The C# does some manipulation and returns the value to
// VC++ to bstrTNamePtr.
GlobalInterfacePtr->GetName(bstr, bstrTNamePtr, &retVal);

//The wcscpy copies the *bstrTNamePtr to wstr1
wcscpy (wstr1, *bstrTNamePtr);

char text2[20];

//Converting the WideChar (wstr1) to New Character String (text2)
WideCharToMultiByte (CP_ACP, 0, wstr1, -1, text2, sizeof (text2), NULL,
NULL);

**** Code END****

Do you need more explaination? any leads as to where to post this query
would also be of much help!

thanks again.

Karthik
 
V

Victor Bazarov

Karthik said:
Hello Victor...

Let me explain the what each line does (OR SUPPOSED TO DO...)

That's not going to help.
**** Code Start****
[..]
**** Code END****

Do you need more explaination?

Read FAQ section 5, please. You will know the answer.
any leads as to where to post this
query would also be of much help!

Try 'microsoft.public.win32.programmer.*', that's the first NG
hierarchy that comes to mind for all Windows programming problems.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top