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. Passing pointers 'round and 'round?

Passing pointers 'round and 'round?

Scheduled Pinned Locked Moved C#
csharpc++question
4 Posts 3 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.
  • O Offline
    O Offline
    o m n i
    wrote on last edited by
    #1

    If I have a c++ app that gets a pointer to a c++ char, will I be able to read that value into a C# char from the address from the c++ app? Or are they incompatible?

    D 1 Reply Last reply
    0
    • O o m n i

      If I have a c++ app that gets a pointer to a c++ char, will I be able to read that value into a C# char from the address from the c++ app? Or are they incompatible?

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      AFAIK IntPtr is the closest you can get through managed code. If there is a method in the dll which returns char*, try getting the return value into an IntPtr and then use it.

      50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

      O 1 Reply Last reply
      0
      • D dan sh

        AFAIK IntPtr is the closest you can get through managed code. If there is a method in the dll which returns char*, try getting the return value into an IntPtr and then use it.

        50-50-90 rule: Anytime I have a 50-50 chance of getting something right, there's a 90% probability I'll get it wrong...!!

        O Offline
        O Offline
        o m n i
        wrote on last edited by
        #3

        I can't figure out what is wrong with my c#/c++ code trying to do this... c# Code

        try
        {
        char c;
        string s;
        int i;
        i = System.Convert.ToInt32(Environment.GetCommandLineArgs()[1]);
        unsafe
        {
        char* charp;
        charp = (char*)i;
        c = *charp;
        }
        s = c.ToString();

                    System.IO.File.WriteAllText("C:\\\\users\\\\name\\\\file.txt", System.IO.File.ReadAllText("C:\\\\users\\\\name\\\\file.txt") + Environment.NewLine + s);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException);
                }
        

        C++ Code that launches the above and passes the pointer to a char.

        int inn = &the_char;
        ShellExecute(NULL,NULL,"C:\\csharpfile.exe",inn,NULL,0);

        L 1 Reply Last reply
        0
        • O o m n i

          I can't figure out what is wrong with my c#/c++ code trying to do this... c# Code

          try
          {
          char c;
          string s;
          int i;
          i = System.Convert.ToInt32(Environment.GetCommandLineArgs()[1]);
          unsafe
          {
          char* charp;
          charp = (char*)i;
          c = *charp;
          }
          s = c.ToString();

                      System.IO.File.WriteAllText("C:\\\\users\\\\name\\\\file.txt", System.IO.File.ReadAllText("C:\\\\users\\\\name\\\\file.txt") + Environment.NewLine + s);
                  }
                  catch (Exception ex)
                  {
                      MessageBox.Show(ex.Message + Environment.NewLine + ex.InnerException);
                  }
          

          C++ Code that launches the above and passes the pointer to a char.

          int inn = &the_char;
          ShellExecute(NULL,NULL,"C:\\csharpfile.exe",inn,NULL,0);

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I cannot figure out what your code is trying to do. Take the first command line argument and convert it to an integer. In an unsafe block cast the integer to a character pointer and take the first character of that array. Convert that character to a string. Use that string for some purpose. Did you try running this through the debugger to see what results you are getting?

          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