#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    18
    Rep Power
    0

    C# memory allocation


    Hi,

    Im attempting to convert a small section of C++ code into C#.

    A third party component method expects an address to be passed pointing to an area of allocated memory.

    The component then stores data starting from this address.

    Is it possible using C#?


    Many thanks.
  2. #2
  3. C# Addict.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Location
    Earth
    Posts
    283
    Rep Power
    28
    yes. look into System.Runtime.InteropServices.Marshal and how to use p/invoke.
    There are none so blind as those who will not see. Jonathan Swift

    My 2D Physics Engine.
    My Remake of UQM.
    Both are written in C#.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    18
    Rep Power
    0
    Hi thanks ill take a look.

    I have started to get somewhere however:

    Code:
    int lSize = new int();
    lSize = (int)&lSize;
    I pass lSize (the memory address as an integer) to the third party control and it successfull places the size in bytes of the data in this variable.

    The next step is to store the actual data. This will be a bitmap.

    I am unsure how to apply to same approach however. I think i need to declare and initalize something, store the address of this in an integer, then pass this integer address to the third party control.

    Any suggestions?
  6. #4
  7. C# Addict.
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Location
    Earth
    Posts
    283
    Rep Power
    28
    Since memory can be represent in many ways lets represent yours with a byte array.

    Here is a code sample i created for you.

    Code:
    //You first have to make C# aware of the methods you are going to call.
    
    [DllImport("somedll.dll")]
    static extern int GetLength();
    
    // lets say your method is void FillMemory(void* ptr)
    [DllImport("somedll.dll")]
    static extern void FillMemory(IntPtr ptr);
    
    static void Test()
    {
        //you call your method to get how large this chunk of memory needs to be. 
        int length = GetLength();
        //you create a byte array of that size.
        Byte[] buffer = new Byte[length];
        //Then you pin that array.
        GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
        try
        {
            // and get a IntPtr to it. 
            IntPtr bufferPtr = Marshal.UnsafeAddrOfPinnedArrayElement(buffer, index);
            //You pass that pointer to the C++ method.
            FillMemory(bufferPtr);
        }
        finally
        {
            //then unpin the array 
            handle.Free();
        }
        // deal with the data in the array.
    }

    Comments on this post

    • dan2003 agrees
    There are none so blind as those who will not see. Jonathan Swift

    My 2D Physics Engine.
    My Remake of UQM.
    Both are written in C#.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    18
    Rep Power
    0
    Thanks for all the help and taking the time to do the example.

    Ill give it a try.

IMN logo majestic logo threadwatch logo seochat tools logo