PC Review


Reply
Thread Tools Rate Thread

how translate UNICODE to ANSI

 
 
=?Utf-8?B?TmF2ZWVuIE11a2tlbGxp?=
Guest
Posts: n/a
 
      29th Dec 2004
Hi,

I need to send a string from my DLL( in eVC 4.0)
to a C# application.

I have declared the string as "char *someString".
for example,

********code in my DLL*******

char *someString;
someString = (GetString)(...); // GetString is a pointer to a function in a
// third party DLL. This
function returns a char *.

SendMessage(,, (LPARAM)someString);

***********code in my DLL ends****************

######## code in my C# application########
unsafe
{
string str = new string((char *)m.LParam.ToPointer());
}
MessageBox.Show(str);

######## code in my C# application ends here ########

in my C# application, MessageBox is
showing some symbols rather than the actual string. I presume that
the string which is sent by the DLL might be in UNICODE.

I want to display the actual string, How can I do this.

Kindly let me know.

Cheers,

Naveen.

 
Reply With Quote
 
 
 
 
Alex Feinman [MVP]
Guest
Posts: n/a
 
      29th Dec 2004
Well, the easiest thing to do would be using Unicode in your C++ module. As
for conversions, there are several ways to approach this:

StringBuilder sb = new StringBuilder();
int pos = 0;
byte ch = Marshal.ReadByte( m.LParam, pos++ );
while( ch != 0 )
{
sb.Append(ch);
ch = Marshal.ReadByte( m.LParam, pos++ );
}

Of course you will need to guarantee that the trailing zero is there - you
cannot catch unmanaged exception from the managed code.
Ideally, you would want to pass string length as wParam.

Even better way to handle this would be to use message as the signal for the
application to make a call into the DLL passing a byte buffer, which DLL
will fill with the string. Then you would use Encoding.ASCII.GetString to
convert the buffer contents to string

--
Alex Feinman
---
Visit http://www.opennetcf.org
"Naveen Mukkelli" <(E-Mail Removed)> wrote in
message news:102455B5-B56D-45C9-87DF-(E-Mail Removed)...
> Hi,
>
> I need to send a string from my DLL( in eVC 4.0)
> to a C# application.
>
> I have declared the string as "char *someString".
> for example,
>
> ********code in my DLL*******
>
> char *someString;
> someString = (GetString)(...); // GetString is a pointer to a function in
> a
> // third party DLL. This
> function returns a char *.
>
> SendMessage(,, (LPARAM)someString);
>
> ***********code in my DLL ends****************
>
> ######## code in my C# application########
> unsafe
> {
> string str = new string((char *)m.LParam.ToPointer());
> }
> MessageBox.Show(str);
>
> ######## code in my C# application ends here ########
>
> in my C# application, MessageBox is
> showing some symbols rather than the actual string. I presume that
> the string which is sent by the DLL might be in UNICODE.
>
> I want to display the actual string, How can I do this.
>
> Kindly let me know.
>
> Cheers,
>
> Naveen.
>



 
Reply With Quote
 
=?Utf-8?B?TmF2ZWVuIE11a2tlbGxp?=
Guest
Posts: n/a
 
      31st Dec 2004
Hi,

I have tried the second approach.. passing a byte[] in to my C dll(eVC).
please go thorough my code..

----------- My managed code ----------------

char[] buf = new char[bufferSize+1];
byte[] buffer = Encoding.ASCII.GetBytes(buf); // or can I just send a
byte[]. I mean
// byte[]
buffer = new byte[100].
// byte[]
result = GetString(buffer).

byte[] result = GetString(buffer); // method in my DLL.
string s = Encoding.ASCII.GetString(result,0,result.Length);

------------- My unmanaged code in C dll --------------------

char* GetString(char* buffer) // NotSupportedException is occurring here.
{
strcpy(buffer, string);
return buffer;
}

When I run this code.. I am getting "NotSupportedException" at the function
GetString. I'm not sure that how to receive byte[] in
C dll in EVC. How to resolve this problem.

My doubt is, what is a good practice for sending byte[] to C dll and how do
we can we manipulate this buffer in unmanaged code.

Where can I get detailed information about the translation between any two
string representations in .NET Compact Framework/.NET Framework.

Kindly let me know.

Cheers,
Naveen.

"Alex Feinman [MVP]" wrote:

> Well, the easiest thing to do would be using Unicode in your C++ module. As
> for conversions, there are several ways to approach this:
>
> StringBuilder sb = new StringBuilder();
> int pos = 0;
> byte ch = Marshal.ReadByte( m.LParam, pos++ );
> while( ch != 0 )
> {
> sb.Append(ch);
> ch = Marshal.ReadByte( m.LParam, pos++ );
> }
>
> Of course you will need to guarantee that the trailing zero is there - you
> cannot catch unmanaged exception from the managed code.
> Ideally, you would want to pass string length as wParam.
>
> Even better way to handle this would be to use message as the signal for the
> application to make a call into the DLL passing a byte buffer, which DLL
> will fill with the string. Then you would use Encoding.ASCII.GetString to
> convert the buffer contents to string
>
> --
> Alex Feinman
> ---
> Visit http://www.opennetcf.org
> "Naveen Mukkelli" <(E-Mail Removed)> wrote in
> message news:102455B5-B56D-45C9-87DF-(E-Mail Removed)...
> > Hi,
> >
> > I need to send a string from my DLL( in eVC 4.0)
> > to a C# application.
> >
> > I have declared the string as "char *someString".
> > for example,
> >
> > ********code in my DLL*******
> >
> > char *someString;
> > someString = (GetString)(...); // GetString is a pointer to a function in
> > a
> > // third party DLL. This
> > function returns a char *.
> >
> > SendMessage(,, (LPARAM)someString);
> >
> > ***********code in my DLL ends****************
> >
> > ######## code in my C# application########
> > unsafe
> > {
> > string str = new string((char *)m.LParam.ToPointer());
> > }
> > MessageBox.Show(str);
> >
> > ######## code in my C# application ends here ########
> >
> > in my C# application, MessageBox is
> > showing some symbols rather than the actual string. I presume that
> > the string which is sent by the DLL might be in UNICODE.
> >
> > I want to display the actual string, How can I do this.
> >
> > Kindly let me know.
> >
> > Cheers,
> >
> > Naveen.
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unicode and ANSI MarkC Windows XP General 2 27th May 2007 05:28 PM
ANSI / UNICODE PPP Microsoft Outlook Discussion 1 15th Aug 2006 02:48 AM
Unicode to ANSI news.s5.net Microsoft Excel Misc 0 3rd May 2005 04:19 PM
ANSI and UNICODE Nikolay Petrov Microsoft VB .NET 1 14th Dec 2004 10:04 PM
ANSI or Unicode Jeff B. Microsoft C# .NET 11 30th Mar 2004 10:25 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:09 AM.