Jump to content
  • Advertisement
Sign in to follow this  
thedustbustr

c# unsafe code: pinning asynchronously

This topic is 3699 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

asynchronous might be the wrong word. I'm putting a C# GUI around a C++/CLI class. The GUI registers a C# callback with C++, this callback returns a writable pointer to a bitmap. Is this even possible to do with the unsafe/fixed keywords? I need to hold my C# image pointer fixed starting in the BeginRender callback, and ending in the EndRender callback. The image pointer cannot move between those calls.
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            BeginRender_callback = new ManagedSs.BeginRender_callback(BeginRender);
            EndRender_callback = new ManagedSs.EndRender_callback(EndRender);
            Resize_callback = new ManagedSs.Resize_callback(Resize_);

            ss = new ManagedSs();

            ss.RegisterBeginRender(BeginRender_callback);
            ss.RegisterEndRender(EndRender_callback);
            ss.RegisterResize(Resize_callback);

            ss.Connect("localhost");
        }

        public unsafe sbyte* BeginRender()
        {
            //pin image buf
            return nullptr; // fixed_image_write_pointer;
        }
        public unsafe void EndRender()
        {
            //refresh image
            //unpin
        }
        public void Resize_(int width, int height)
        {
            //resize image
        }

        private ManagedSs.BeginRender_callback BeginRender_callback;
        private ManagedSs.EndRender_callback EndRender_callback;
        private ManagedSs.Resize_callback Resize_callback;

        private ManagedSs;
    }




[Edited by - thedustbustr on July 2, 2008 9:53:14 AM]

Share this post


Link to post
Share on other sites
Advertisement
New problem. I can write to the bitmap, but the containing picturebox does not update. If I call picturebox.invalidate(), nothing happens, probably because this method is being executed as a callback from a separate thread. if I use picturebox.update(), it crashes telling me that I can't call that from a separate thread. if I use picturebox.invoke to call invalidate through a delegate, still nothing happens.

For googlers, here is the solution to the original problem:

        public unsafe sbyte* BeginRender()
{
Rectangle locked_area =
new Rectangle(0,0,target_image.Width, target_image.Height);
target_data = target_image.LockBits(
locked_area,
System.Drawing.Imaging.ImageLockMode.WriteOnly,
target_image.PixelFormat);

IntPtr pixels = target_data.Scan0;
return (sbyte*)pixels;
}
public unsafe void EndRender()
{
target_image.UnlockBits(target_data);
pictureBox1.Invalidate();
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!