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. access memory within unmanaged dll

access memory within unmanaged dll

Scheduled Pinned Locked Moved C#
csharpc++performancehelptutorial
7 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.
  • R Offline
    R Offline
    Rupel
    wrote on last edited by
    #1

    hi, i'm stuck. :( i'm currently trying to work with an old dll and marshalling and stuff went really fine until now. there's a function in that dll declared like

    int foo(void *ppMessage);

    the function will do some nasty stuff, allocate local memory for the result and write the pointer to the result in the parameter. (thats why there are two _p_s in the name) in c++ it would be something like

    byte *result;
    foo(&result);
    (*result)[0] //is the first byte of the result

    i have no idea, how to actually read the result using c# - does anyone know? looks somewhat unmanaged :~ thx in advance :wq

    R S 2 Replies Last reply
    0
    • R Rupel

      hi, i'm stuck. :( i'm currently trying to work with an old dll and marshalling and stuff went really fine until now. there's a function in that dll declared like

      int foo(void *ppMessage);

      the function will do some nasty stuff, allocate local memory for the result and write the pointer to the result in the parameter. (thats why there are two _p_s in the name) in c++ it would be something like

      byte *result;
      foo(&result);
      (*result)[0] //is the first byte of the result

      i have no idea, how to actually read the result using c# - does anyone know? looks somewhat unmanaged :~ thx in advance :wq

      R Offline
      R Offline
      Rupel
      wrote on last edited by
      #2

      ah. just found the Marshal-class - maybe this helps me... :) :wq

      1 Reply Last reply
      0
      • R Rupel

        hi, i'm stuck. :( i'm currently trying to work with an old dll and marshalling and stuff went really fine until now. there's a function in that dll declared like

        int foo(void *ppMessage);

        the function will do some nasty stuff, allocate local memory for the result and write the pointer to the result in the parameter. (thats why there are two _p_s in the name) in c++ it would be something like

        byte *result;
        foo(&result);
        (*result)[0] //is the first byte of the result

        i have no idea, how to actually read the result using c# - does anyone know? looks somewhat unmanaged :~ thx in advance :wq

        S Offline
        S Offline
        Stephane Rodriguez
        wrote on last edited by
        #3

        First of all, are you sure about the signature of your C method ? Isn't it more like this :

        int foo(void **ppMessage);

        To expose this C function to managed code, you need to declare it as :

        [DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)]
        public static extern int foo(out IntPtr ppMessage);

        where user32.dll must be replaced with the actual DLL name.


        And I swallow a small raisin.

        R 1 Reply Last reply
        0
        • S Stephane Rodriguez

          First of all, are you sure about the signature of your C method ? Isn't it more like this :

          int foo(void **ppMessage);

          To expose this C function to managed code, you need to declare it as :

          [DllImport("user32.dll", CharSet=CharSet.Auto, CallingConvention=CallingConvention.Cdecl)]
          public static extern int foo(out IntPtr ppMessage);

          where user32.dll must be replaced with the actual DLL name.


          And I swallow a small raisin.

          R Offline
          R Offline
          Rupel
          wrote on last edited by
          #4

          erm no, i looked it up in the header-file and there's only one star. i'm a bit confused too, but it seems you have to know, that you get a pointer in the specified location. actually i have solved the thing like this:

          [DllImport("theold.dll")]
          public static extern Int32 foo(IntPtr [] messagePtr);

          ...

          IntPtr [] messagePtr = new IntPtr[1];
          Int32 ret = foo(messagePtr);
          Int16 size = Marshal.ReadInt16(messagePtr[0]);
          byte [] message = new byte[size];
          Marshal.Copy(messagePtr[0],message,0,size);

          i know (from the specification) that the first 2 bytes contain the size of the result in bytes. :wq

          L 1 Reply Last reply
          0
          • R Rupel

            erm no, i looked it up in the header-file and there's only one star. i'm a bit confused too, but it seems you have to know, that you get a pointer in the specified location. actually i have solved the thing like this:

            [DllImport("theold.dll")]
            public static extern Int32 foo(IntPtr [] messagePtr);

            ...

            IntPtr [] messagePtr = new IntPtr[1];
            Int32 ret = foo(messagePtr);
            Int16 size = Marshal.ReadInt16(messagePtr[0]);
            byte [] message = new byte[size];
            Marshal.Copy(messagePtr[0],message,0,size);

            i know (from the specification) that the first 2 bytes contain the size of the result in bytes. :wq

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            Hi, it seems you should be able to remove the array like:

            [DllImport("theold.dll")]public static extern Int32 foo(out IntPtr messagePtr);
            ...
            IntPtr messagePtr;
            Int32 ret = foo(messagePtr);
            Int16 size = Marshal.ReadInt16(messagePtr);
            byte [] message = new byte[size];
            Marshal.Copy(messagePtr,message,2,size); //remmeber that is only the size, and it is not included in byte[]

            I'm interested, does this work too? MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

            R 1 Reply Last reply
            0
            • L leppie

              Hi, it seems you should be able to remove the array like:

              [DllImport("theold.dll")]public static extern Int32 foo(out IntPtr messagePtr);
              ...
              IntPtr messagePtr;
              Int32 ret = foo(messagePtr);
              Int16 size = Marshal.ReadInt16(messagePtr);
              byte [] message = new byte[size];
              Marshal.Copy(messagePtr,message,2,size); //remmeber that is only the size, and it is not included in byte[]

              I'm interested, does this work too? MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

              R Offline
              R Offline
              Rupel
              wrote on last edited by
              #6

              1. yep. out works indeed. good idea. but in the foo-call is to be inserted uswell. 2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes) finally:

              [DllImport("theold.dll")]
              public static extern Int32 foo(out IntPtr messagePtr);
              ...
              IntPtr messagePtr;
              Int32 ret = foo(out messagePtr);
              Int16 size = Marshal.ReadInt16(messagePtr);
              byte [] message = new byte[size];
              Marshal.Copy(messagePtr,message,0,size);

              thx again for your help an ideas :rose: :wq

              L 1 Reply Last reply
              0
              • R Rupel

                1. yep. out works indeed. good idea. but in the foo-call is to be inserted uswell. 2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes) finally:

                [DllImport("theold.dll")]
                public static extern Int32 foo(out IntPtr messagePtr);
                ...
                IntPtr messagePtr;
                Int32 ret = foo(out messagePtr);
                Int16 size = Marshal.ReadInt16(messagePtr);
                byte [] message = new byte[size];
                Marshal.Copy(messagePtr,message,0,size);

                thx again for your help an ideas :rose: :wq

                L Offline
                L Offline
                leppie
                wrote on last edited by
                #7

                Rüpel wrote: but in the foo-call is to be inserted uswell. oops :), happens when u write the code out of your head straight into the textbox :) Rüpel wrote: 2. no, the copy-call was ok, since the size-field is included (i.e. the value is at least 2 for the 2 size-bytes) But wont the fist 2 byte values (being a size value) affect the byte[]? (oops another typo :), meant to do this)

                byte [] message = new byte[size - 2];
                Marshal.Copy(messagePtr,message,2,size);

                or

                byte [] message = new byte[size - 2];
                Marshal.Copy(new IntPtr(messagePtr.ToInt32() + Marshal.SizeOf(typeof(byte))*2),message,0,size - 2);

                I'm just interested :) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D

                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