[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IObjectSafety
    {
        // methods
        void GetInterfacceSafyOptions(
            System.Int32 riid,
            out System.Int32 pdwSupportedOptions,
            out System.Int32 pdwEnabledOptions);
        void SetInterfaceSafetyOptions(
            System.Int32 riid,
            System.Int32 dwOptionsSetMask,
            System.Int32 dwEnabledOptions);
    }

 

[ComImport, Guid("00000118-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleClientSite
    {
        void SaveObject();
        void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
        void GetContainer(out IOleContainer ppContainer);
        void ShowObject();
        void OnShowWindow(bool fShow);
        void RequestNewObjectLayout();
    }

 

 [ComImport, Guid("0000011B-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleContainer
    {
        void EnumObjects([In, MarshalAs(UnmanagedType.U4)] int grfFlags,
        [Out, MarshalAs(UnmanagedType.LPArray)] object[] ppenum);
        void ParseDisplayName([In, MarshalAs(UnmanagedType.Interface)] object pbc,
        [In, MarshalAs(UnmanagedType.BStr)] string pszDisplayName,
        [Out, MarshalAs(UnmanagedType.LPArray)] int[] pchEaten,
        [Out, MarshalAs(UnmanagedType.LPArray)] object[] ppmkOut);
        void LockContainer([In, MarshalAs(UnmanagedType.I4)] int fLock);
    }

 

 

[ComVisible(true)]
    [ProgId("WorkFlowDesignTimeControl")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [Guid("8d838c92-9eff-4f34-b685-76897809f382")]
    public partial class WorkFlowDesignTimeControl : UserControl, IObjectSafety
    {
        public WorkFlowDesignTimeControl()
        {
            InitializeComponent();
        }

        public void ShowMessage(string msg)
        {
            if (msg != null)
            {
                MessageBox.Show(msg);
            }
            CallJavaScript("PPP.Test('" + msg + "')");
        }

        private void CallJavaScript(string code)
        {
            Type typeIOleObject = this.GetType().GetInterface("IOleObject", true);
            object oleClientSite = typeIOleObject.InvokeMember("GetClientSite",
            BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
            null,
            this,
            null);

            IOleClientSite oleClientSite2 = oleClientSite as IOleClientSite;
            IOleContainer pObj;
            oleClientSite2.GetContainer(out pObj);

            //获取页面的Script集合
            IHTMLDocument2 pDoc2 = (IHTMLDocument2)pObj;
            IHTMLWindow2 win2 = (IHTMLWindow2)pDoc2.parentWindow;


            win2.execScript(code,  "javascript");
            //object script = pDoc2.Script;

            //try
            //{
            //    script.GetType().InvokeMember(functionName,
            //    BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
            //    null,
            //    script,
            //    args);
            //}
            //catch { }
        }


        #region IObjectSafety 成员

        public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
        {
            // TODO:  添加 WebCamControl.GetInterfacceSafyOptions 实现
            pdwSupportedOptions = 1;
            pdwEnabledOptions = 2;
        }

        public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
        {
            // TODO:  添加 WebCamControl.SetInterfaceSafetyOptions 实现            
        }

 

        #endregion

        [ComRegisterFunction()]
        public static void RegisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            RegistryKey ctrl = k.CreateSubKey("Control");
            ctrl.Close();

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
            inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
            inprocServer32.Close();

            k.Close();
        }

        [ComUnregisterFunction()]
        public static void UnregisterClass(string key)
        {
            StringBuilder sb = new StringBuilder(key);
            sb.Replace(@"HKEY_CLASSES_ROOT\", "");

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            k.DeleteSubKey("Control", false);

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);

            k.DeleteSubKey("CodeBase", false);

            k.Close();
        }

    }

 

[assembly: AllowPartiallyTrustedCallers()]

[assembly: ComVisible(true)]

 

regasm AClass.dll /tlb /codebase

posted on 2008-10-09 14:08  大斌锅  阅读(1262)  评论(1编辑  收藏  举报