Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Encoding problem with string from DLL

Encoding problem with string from DLL

Scheduled Pinned Locked Moved C#
c++csharpvisual-studiohelpquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    steve_rm
    wrote on last edited by
    #1

    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); }

    N 1 Reply Last reply
    0
    • S steve_rm

      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); }

      N Offline
      N Offline
      ncjlee
      wrote on last edited by
      #2

      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..

      S 1 Reply Last reply
      0
      • N ncjlee

        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..

        S Offline
        S Offline
        steve_rm
        wrote on last edited by
        #3

        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); }

        N 1 Reply Last reply
        0
        • S steve_rm

          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); }

          N Offline
          N Offline
          ncjlee
          wrote on last edited by
          #4

          I am not sure why this would happen. rather than using marshall you could try use StringBuilder instead of string..

          S 1 Reply Last reply
          0
          • N ncjlee

            I am not sure why this would happen. rather than using marshall you could try use StringBuilder instead of string..

            S Offline
            S Offline
            steve_rm
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups