Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
Hi All,

I am working on a sample application where in I am capturing the RGB data of the images of the content rendered on to a form.

This application takes care of resizing the webbrowser setup to appropriate width and height whenever resolution or color depth changes in the system.

I am using IViewObject.Draw funtion to capture the content and LockBits to get the scan pointer. Sample snippet is as below.

C#
    [ComImport, Guid("0000010D-0000-0000-C000-000000000046"), InterfaceType((short)1), ComConversionLoss]
    public interface IViewObject
    {
        void Draw([MarshalAs(UnmanagedType.U4)] UInt32 dwDrawAspect,
                  int lindex,
                  IntPtr pvAspect,
                  [In] IntPtr ptd,
                  IntPtr hdcTargetDev,
                  IntPtr hdcDraw,
                  [MarshalAs(UnmanagedType.Struct)] ref _RECTL lprcBounds,
                  [In] IntPtr lprcWBounds,
                  IntPtr pfnContinue,
                  [MarshalAs(UnmanagedType.U4)] UInt32 dwContinue);
    }
.....
Object docObj;
IViewObject iView = null;
IntPtr srcPtr;
BitmapData srcData = null;
bitmap = new Bitmap(width, height, pixelFrmt);
srcArea = new Rectangle(0, 0, bitmap.Width, bitmap.Height);
docObj = (Object)webBrowser.Document.DomDocument;
iView = docObj as IViewObject;

iView.Draw(1, -1, (IntPtr)0, (IntPtr)0, (IntPtr)0,
                        (IntPtr)hdc, ref bounds, (IntPtr)0, (IntPtr)0, 0);

srcData = bitmap.LockBits(srcArea, ImageLockMode.ReadOnly, pixelFrmt);
srcPtr = srcData.Scan0;

This is working fine most of the time. But on changin the resolution or color depth, on few occasions the iView.Draw gives the following exception
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
StackTrace:
at IViewObject.Draw(UInt32 dwDrawAspect, Int32 lindex, IntPtr pvAspect, IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, _RECTL& lprcBounds, IntPtr lprcWBounds, IntPtr pfnContinue, UInt32 dwContinue)

I am not able to understand the reason for this. I treid google but didn't get any useful pointers for this issue.

Any help in understanding this issue is really appreciated.

Thanks in advance.

[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 17-Aug-11 21:24pm
v4
Comments
Richard MacCutchan 18-Aug-11 5:39am    
It means that the address you are trying to access is outside the address space of your program. You need to check the values in your IViewObject.Draw() method to ensure they are valid.

Itz definitely security problem. Basically, this operation attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Message comes thro System.Exception {System.AccessViolationException}

To be specific, it could be iteration through a collection of COM objects and the need to explicitly release the memory allocated to the COM Object. As a case study, if there is a parameter whose object represents a collection COM objects and if there is a problem with either the collection or any of the objects, then there will be a problem in releasing the memory allocated to the object with the problem which results in this error.

As a solution, you can manipulate the settings in DEP(Data Execution Prevention). Select the Advanced System Settings from my computer properties. In Performance category, you can turn on DEP for all programs and services except those u select.
 
Share this answer
 
Comments
HalliHaida 25-Oct-11 1:23am    
Hi,

Please help me in resolving this issue. It is blocking my development of the application :(.

Another thing that I have noticed is this issue occurs if I render a online flash content but not with simple html page content. Why is it so?

Any tools or way to debug what is causing this?

Any help or pointer is really appreciated.
I also faced the same problem but in VC++ code , and fortunately found the solution ,
and i already proposed in the below link

http://social.msdn.microsoft.com/Forums/en-IN/winforms/thread/2ddb4f4f-f6b4-4c87-ac25-390e03f5f858

Thanks,
Nobody is Perfect in this world, except its Creater the GOD(who never born , never dies, remains evergreen but cannot be seen with our selfish eyes , due to our sinful acts)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900