Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encoding problem with string

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
TH
Hello,

VS 2008 3.5.Net

I am getting a string from a call back in a C++ native DLL which I have written. And for some reason it displays wrong something like this. i.e. [][[[[[[[[[[[[[ or ????????????????? with ASCII encoding.

My code is below. I think the DLL is using unicode, and I am trying to encode it in ASCII. The parameter in call back definition is char*. And in my C# I am using a string.

I have also tried Unicode, UTF-8, and ASCII. All of them display incorrectly.

Am I doing something wrong with my code?

Many thanks for any suggestions,

Code:
*.hpp file ==============
typedef int (__stdcall *ptrIncomingCall)(int callID, char *caller);
MOBILEDLL_API int drvIncomingCall(ptrIncomingCall cb);

*.cpp file ==============
int drvIncomingCall(ptrIncomingCall cb)
{
	int callerID = cb(20, "Joe bloggs");

	return callerID;
}

C# code ===============
 private delegate int incomingCallDelegate(int callerID, string caller);
        [DllImport("MobileDLL.dll")]
        static extern int drvIncomingCall(incomingCallDelegate cb);

        int OnIncomingCall(int callerID, string caller)
        {
            this.label1.Text = callerID.ToString();
            this.label2.Text = caller;

            return 1;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            drvIncomingCall((incomingCallDelegate)OnIncomingCall);   
        }


 
Sorry, in my last post I left out the encoding I was doing.

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] callerBytes = encoding.GetBytes(caller);

this.label1.Text = callerID.ToString();
this.label2.Text = encoding.GetString(callerBytes, 0, callerBytes.Length);
 
Hello,

I tested on both .Net and CF. In .Net everything works as expected. However, with CF you have to do the following:


byte[] callerBytes = System.Text.UnicodeEncoding.Unicode.GetBytes(caller.ToString());
this.label6.Text = System.Text.Encoding.Default.GetString(callerBytes, 0, callerBytes.Length);

Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top