Unmanaged pointer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • eusebiu

    #1

    Unmanaged pointer

    Hello... I am making a Sytem.IntPtr with Marshal.AllocHG lobal(int)
    (Allocates memory from the unmanaged memory of the process using
    GlobalAlloc - MSDN Definition for Marshal.AllocHG lobal) and after
    copying the data from a byte[] buffer to the IntPtr(with
    Marshal.Copy), I send the IntPtr to an unmanaged library. In the
    unmanaged library the function's signature has a unsigned char *
    parameter(my IntPtr from C#) and in function's code-block I want to
    get the pointer's value.

    I use this code to put the pointer's value into a txt file

    //C++ code
    char* b = new char[10];
    sprintf(b,"%2d" , (int)pMyPointer );
    ofstream out((char *)"aaa.txt");
    out.write((cons t char*)b, 10);
    delete[] b;

    In C#, when debugging, I have a value for my System.IntPtr but when I
    open the txt file, I see other value.

    Why is that? Isn't Marshal.AllocHG lobal return pointer a pointer on
    the unmanaged heap to an unmanaged memory?
    Thanks

  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Unmanaged pointer

    It seems like you are just trying to pass a string to your function.
    Why not just use a String parameter and declare the function with the
    MarshalAs attribute with a value of UnmanagedType.L PStr?


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "eusebiu" <MarcuEusebiu@g mail.comwrote in message
    news:1185209849 .736289.153850@ n60g2000hse.goo glegroups.com.. .
    Hello... I am making a Sytem.IntPtr with Marshal.AllocHG lobal(int)
    (Allocates memory from the unmanaged memory of the process using
    GlobalAlloc - MSDN Definition for Marshal.AllocHG lobal) and after
    copying the data from a byte[] buffer to the IntPtr(with
    Marshal.Copy), I send the IntPtr to an unmanaged library. In the
    unmanaged library the function's signature has a unsigned char *
    parameter(my IntPtr from C#) and in function's code-block I want to
    get the pointer's value.
    >
    I use this code to put the pointer's value into a txt file
    >
    //C++ code
    char* b = new char[10];
    sprintf(b,"%2d" , (int)pMyPointer );
    ofstream out((char *)"aaa.txt");
    out.write((cons t char*)b, 10);
    delete[] b;
    >
    In C#, when debugging, I have a value for my System.IntPtr but when I
    open the txt file, I see other value.
    >
    Why is that? Isn't Marshal.AllocHG lobal return pointer a pointer on
    the unmanaged heap to an unmanaged memory?
    Thanks
    >

    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: Unmanaged pointer


      "eusebiu" <MarcuEusebiu@g mail.comwrote in message
      news:1185209849 .736289.153850@ n60g2000hse.goo glegroups.com.. .
      Hello... I am making a Sytem.IntPtr with Marshal.AllocHG lobal(int)
      (Allocates memory from the unmanaged memory of the process using
      GlobalAlloc - MSDN Definition for Marshal.AllocHG lobal) and after
      copying the data from a byte[] buffer to the IntPtr(with
      Marshal.Copy), I send the IntPtr to an unmanaged library. In the
      unmanaged library the function's signature has a unsigned char *
      parameter(my IntPtr from C#) and in function's code-block I want to
      get the pointer's value.
      >
      I use this code to put the pointer's value into a txt file
      >
      //C++ code
      char* b = new char[10];
      sprintf(b,"%2d" , (int)pMyPointer );
      ofstream out((char *)"aaa.txt");
      out.write((cons t char*)b, 10);
      delete[] b;
      I think that ought to work, but really, that's some atrocious code. Try
      instead:

      ofstream out("aaa.txt");
      out << hex << pMyPointer << flush;
      >
      In C#, when debugging, I have a value for my System.IntPtr but when I
      open the txt file, I see other value.
      Maybe two different runs of the program? The pointer isn't guaranteed to be
      the same every time.

      Anyway, is there are reason you aren't using a pinned byte*?


      Comment

      Working...