Encoding problem with string from DLL
-
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,
*.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) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] callerBytes = encoding.GetBytes(caller); this.label1.Text = callerID.ToString(); this.label2.Text = encoding.GetString(callerBytes, 0, callerBytes.Length); return 1; } private void button4_Click(object sender, EventArgs e) { drvIncomingCall((incomingCallDelegate)OnIncomingCall); }
-
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,
*.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) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] callerBytes = encoding.GetBytes(caller); this.label1.Text = callerID.ToString(); this.label2.Text = encoding.GetString(callerBytes, 0, callerBytes.Length); return 1; } private void button4_Click(object sender, EventArgs e) { drvIncomingCall((incomingCallDelegate)OnIncomingCall); }
-
private delegate int incomingCallDelegate(int callerID, [MarshalAs(UnmanagedType.LPStr)] string caller); int OnIncomingCall(int callerID, [MarshalAs(UnmanagedType.LPStr)] string caller) I think this should work..
Hello, Thanks for your response. However, I have tried to get that to work. See code below. I am getting a run-time error: "NotSupportException was unhandled" Error No. 0x80131515 Stack Trace: at MobileApp.Form1.button4_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam) at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam) at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) at System.Windows.Forms.Application.Run(Form fm) at MobileApp.Program.Main()
private delegate int incomingCallDelegate(int callerID,[MarshalAs(UnmanagedType.LPStr)] string caller); [DllImport("MobileDLL.dll")] static extern int drvIncomingCall(incomingCallDelegate cb); int OnIncomingCall(int callerID,[MarshalAs(UnmanagedType.LPStr)] string caller) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] callerBytes = encoding.GetBytes(caller); this.label1.Text = callerID.ToString(); this.label2.Text = encoding.GetString(callerBytes, 0, callerBytes.Length); return 1; } private void button4_Click(object sender, EventArgs e) { drvIncomingCall((incomingCallDelegate)OnIncomingCall); }
-
Hello, Thanks for your response. However, I have tried to get that to work. See code below. I am getting a run-time error: "NotSupportException was unhandled" Error No. 0x80131515 Stack Trace: at MobileApp.Form1.button4_Click(Object sender, EventArgs e) at System.Windows.Forms.Control.OnClick(EventArgs e) at System.Windows.Forms.Button.OnClick(EventArgs e) at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam) at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam) at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) at System.Windows.Forms.Application.Run(Form fm) at MobileApp.Program.Main()
private delegate int incomingCallDelegate(int callerID,[MarshalAs(UnmanagedType.LPStr)] string caller); [DllImport("MobileDLL.dll")] static extern int drvIncomingCall(incomingCallDelegate cb); int OnIncomingCall(int callerID,[MarshalAs(UnmanagedType.LPStr)] string caller) { ASCIIEncoding encoding = new ASCIIEncoding(); byte[] callerBytes = encoding.GetBytes(caller); this.label1.Text = callerID.ToString(); this.label2.Text = encoding.GetString(callerBytes, 0, callerBytes.Length); return 1; } private void button4_Click(object sender, EventArgs e) { drvIncomingCall((incomingCallDelegate)OnIncomingCall); }
-
I am not sure why this would happen. rather than using marshall you could try use StringBuilder instead of string..
Hello, Thanks for your patiance with this question. This is what I have tried. I have tried to use the Encoding class. I have tried ANSII, UTF-8, Unicode, and default. Default was the first one I tried. I have also tried the char sets as well, auto, ASCII, etc. I have also tried using the stringbuilder instead of string. Failed also. Many thanks for any more suggestions, Steve