Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 105,772 C# Programmers for FREE! Ask your question and get quick answers from experts. There are 1,463 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Accessing the Object Model from Within an ActiveX Control

 
Reply to this topicStart new topic

Accessing the Object Model from Within an ActiveX Control

Janders9er
post 7 Jul, 2008 - 09:29 AM
Post #1


New D.I.C Head

*
Joined: 30 Jun, 2008
Posts: 10

Hi all,
I am following along with the tutorial here that describes how to accomplish this in C++ but I haven't found any equivalent in C#.

The high level approach to solving this problem in C++ is:
• Obtain IWebBrowserApp from its containing HTML page.
• Get the document property of IWebBrowserApp.
• Get the script property of the document.

In particular, I can't find a C# equivalent for the step "Obtain IWebBrowserApp from its containing HTML page."

Can anyone point me in the right direction? I'm not new to C# so a nudge might be all I need smile.gif

This post has been edited by Janders9er: 8 Jul, 2008 - 05:15 AM
User is offlineProfile CardPM

Go to the top of the page


Janders9er
post 9 Jul, 2008 - 09:28 AM
Post #2


New D.I.C Head

*
Joined: 30 Jun, 2008
Posts: 10

I have continued working on this and have made some progress. However, I've got a problem that I have narrowed down (at least I think I narrowed it down smile.gif ) to a SecurityPermission problem, but haven't had any luck getting it to work.

I'm including part of the code with exception information included.
CODE

try
            {
                SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);

                //###########################
                //Assert throws exception
                //Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

                //Stack: at System.Security.CodeAccessSecurityEngine.CheckNReturnSO(PermissionToken permToken, CodeAccessPermission demand, StackCrawlMark stackMark, Int32 unrestrcitedOverride, Int32 create)
                perm.Assert();
                //###########################              

                // Now typeIOleObject has all the required information about class IOleObject
                Type typeIOleObject = this.GetType().GetInterface("IOleObject", true);

                //###########################
                //InvokeMember throws exception
                //Message: IOleObject.GetClientSite()
                //Stack:
                //       at System.Reflection.MethodBase.PerformSecurityCheck(Object obj, RuntimeMethodHandle method, IntPtr parent, UInt32 invocationFlags)
                //       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
                //First permission failed: System.Security.PermissionSet
                object oleClientSite = typeIOleObject.InvokeMember("GetClientSite", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, this, null);
                                
                //Also tried:  typeIOleObject.InvokeMember("GetClientSite", BindingFlags.InvokeMethod, null, this, null);
                //###########################

                IServiceProvider serviceProvider = oleClientSite as IServiceProvider;
                Guid guidIServiceProvider = typeof(IServiceProvider).GUID;
                object objIServiceProvider;
                serviceProvider.QueryService(ref SID_STopLevelBrowser, ref guidIServiceProvider, out objIServiceProvider);
                serviceProvider = objIServiceProvider as IServiceProvider;
                object objIWebBrowser;
                Guid guidIWebBrowser = typeof(IWebBrowser).GUID;
                serviceProvider.QueryService(ref SID_SWebBrowserApp, ref guidIWebBrowser, out objIWebBrowser);
                IWebBrowser webBrowser = objIWebBrowser as IWebBrowser;
                MessageBox.Show(webBrowser.LocationURL);
                object flags = null;
                object targetFrameName = null;
                object postData = null;
                object headers = null;
                webBrowser.Navigate("about:blank", ref flags, ref targetFrameName, ref postData, ref headers);
                Console.WriteLine("Called unmanaged code with permission.");
            }
            catch (SecurityException ex)
            {
                MessageBox.Show(ex.Message + "\n\nStack:" + ex.StackTrace + "\n\nData:" + ex.Data + "\n\nSource:" + ex.Source);
                MessageBox.Show("\n\nTo String:" + ex.ToString() + "\n\nInner" + ex.InnerException + "\n\nFirst permission failed" + ex.FirstPermissionThatFailed);
             }
            catch (Exception e)
            {
                MessageBox.Show(e.Message + "\n\nStack:" + e.StackTrace + "\n\nData:" + e.Data + "\n\nSource:" + e.Source + "\n\nTo String:" + e.ToString() + "\n\nInner" + e.InnerException);
            }


Wrapper around IWebBrowser
CODE
      //from shlguid.h
        Guid SID_STopLevelBrowser = new Guid(0x4C96BE40, 0x915C, 0x11CF, 0x99,
            0xD3, 0x00, 0xAA, 0x00, 0x4A, 0xE8, 0x37);
        Guid SID_SWebBrowserApp = new Guid(0x0002DF05, 0x0000, 0x0000, 0xC0,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46);

        [ComImport, Guid("6d5140c1-7436-11ce-8034-00aa006009fa"),
            InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
        public interface IServiceProvider
        {
            void QueryService(ref Guid guidService, ref Guid riid,
        [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
        }

        [ComImport, TypeLibType((short)0x1050),
        Guid("EAB22AC1-30C1-11CF-A7EB-0000C05BAE0B")]
        public interface IWebBrowser
        {
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(100)]
             void GoBack();
            
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x65)]
            void GoForward();
            
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x66)]
            void GoHome();
            
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x67)]
            void GoSearch();
            
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x68)]
            void Navigate([In, MarshalAs(UnmanagedType.BStr)] string URL, [In,
                Optional, MarshalAs(UnmanagedType.Struct)] ref object Flags, [In, Optional,
                MarshalAs(UnmanagedType.Struct)] ref object TargetFrameName, [In, Optional,
                MarshalAs(UnmanagedType.Struct)] ref object PostData, [In, Optional,
                MarshalAs(UnmanagedType.Struct)] ref object Headers);

            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(-550)]
            void Refresh();
            
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x69)]
            void Refresh2([In, Optional, MarshalAs(UnmanagedType.Struct)] ref object Level);
            
            [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0x6a)]
            void Stop();
            
            [DispId(200)]
            object Application
            {
                [return: MarshalAs(UnmanagedType.IDispatch)]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(200)]
                get;
            }
            
            [DispId(0xc9)]
            object Parent
            {
                [return: MarshalAs(UnmanagedType.IDispatch)]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xc9)]
                get;
            }
            
            [DispId(0xca)]
            object Container
            {
                [return: MarshalAs(UnmanagedType.IDispatch)]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xca)]
                get;
            }
            
            [DispId(0xcb)]
            object Document
            {
                [return: MarshalAs(UnmanagedType.IDispatch)]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xcb)]
                get;
            }
            
            [DispId(0xcc)]
            bool TopLevelContainer { [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xcc)] get; }
            [DispId(0xcd)]
            string Type
            {
                [return: MarshalAs(UnmanagedType.BStr)]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xcd)]
                get;
            }
            
            [DispId(0xce)]
            int Left
            {
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xce)]
                get;
                [param: In]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xce)]
                set;
            }
            
            [DispId(0xcf)]
            int Top
            {
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xcf)]
                get;
                [param: In]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xcf)]
                set;
            }
            
            [DispId(0xd0)]
            int Width
            {
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xd0)]
                get;
                [param: In]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xd0)]
                set;
            }
            
            [DispId(0xd1)]
            int Height
            {
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xd1)]
                get;
                [param: In]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xd1)]
                set;
            }
            
            [DispId(210)]
            string LocationName
            {
                [return: MarshalAs(UnmanagedType.BStr)]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(210)]
                get;
            }
            
            [DispId(0xd3)]
            string LocationURL
            {
                [return: MarshalAs(UnmanagedType.BStr)]
                [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xd3)]
                get;
            }
            
            [DispId(0xd4)]
            bool Busy { [MethodImpl(MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), DispId(0xd4)] get; }
        }



If it helps any, I have been using this website and this Microsoft support page as references for my code.

Can anyone see what I am doing wrong?



Thanks in advance!

This post has been edited by Janders9er: 9 Jul, 2008 - 09:55 AM
User is offlineProfile CardPM

Go to the top of the page

Janders9er
post 9 Jul, 2008 - 01:44 PM
Post #3


New D.I.C Head

*
Joined: 30 Jun, 2008
Posts: 10

I've been trying to use a new SecurityPermission of type "SecurityPermissionFlag.Assertion" to fix this error, but I'm not sure this will lead me anywhere.

Does that seem like the right way to go? Any suggestion (even if you think it's a bad one) will be greatly appreciated!

User is offlineProfile CardPM

Go to the top of the page

Janders9er
post 11 Jul, 2008 - 08:18 AM
Post #4


New D.I.C Head

*
Joined: 30 Jun, 2008
Posts: 10

After strongly signing my assembly and adjusting the policy at the Machine Level to give "Full Trust" I am now longer getting an exception at

CODE
SecurityPermission perm = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);

                //###########################
                //Assert throws exception
                //Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

                //Stack: at System.Security.CodeAccessSecurityEngine.CheckNReturnSO(PermissionToken permToken, CodeAccessPermission demand, StackCrawlMark stackMark, Int32 unrestrcitedOverride, Int32 create)
                perm.Assert();
                //###########################    


However, I am still getting an exception here:


CODE


                                //###########################
                //InvokeMember throws exception
                //Message: System.MethodAccessException: IOleObject.GetClientSite() --> System.Security.SecurityException: Request failed.
                //Stack:
                //       at System.Reflection.MethodBase.PerformSecurityCheck(Object obj, RuntimeMethodHandle method, IntPtr parent, UInt32 invocationFlags)
                //       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
                //The action that failed was: Demand
                //The type of the first permission that failed was: System.Security.PermissionSet


                object oleClientSite = typeIOleObject.InvokeMember("GetClientSite", BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public, null, this, null);
                                
                //Also tried:  typeIOleObject.InvokeMember("GetClientSite", BindingFlags.InvokeMethod, null, this, null);
                //###########################


I have tried setting the "AllowPartialCallers" attribute in AssemblyInfo.cs with no luck. I've also tried placing "[SecurityPermission( SecurityAction.Demand, UnmanagedCode=true, SkipVerification=true)]" at the beginning of my class, although I'm not sure that is the best place for it.

Any suggestions?

This post has been edited by Janders9er: 11 Jul, 2008 - 08:20 AM
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 12 Jul, 2008 - 12:55 AM
Post #5


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 316



Thanked 5 times

Dream Kudos: 100
My Contributions


what is it are you trying to do? From you previous post it seems like you what to host an IE Browser control within webpage that is running on IE. Is this correct?

Edit*

I think I see what you are doing now (maybe)

Have you tried using this?
Marshal::GetIUnknownForObject()

I think you can pass in a reference to your activex control into this function and get back an IUnknown pointer.
From the IUnknown pointer you get back you should be able to query for the

IID_IServiceProvider interface followed by
SID_STopLevelBrowser interface and lastly
the IID_IWebBrowser2 interface.

This post has been edited by skaoth: 12 Jul, 2008 - 01:14 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/21/08 03:45PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month