简体   繁体   中英

how to call java script function from activex or dll in c#

I have active-x class written in c# which is multi-threaded. I need to call javascript from my activeX. I tried it by Microsoft.mshtml .

/*JS

function do_Print() {
    control.setPage(this);
    control.scriptPrint();
}

function report_back(report){
    alert("Report:"+report);
}


C#

    public void setPage(mshtml.HTMLWindow2Class JSFile) {
                window = JSFile;
    }
    public void scriptPrint(){
                window.execScript("report_back('Printing complete!')", "JScript");
    }
    */

But its throwing exception

"unable to cast COM object of type 'mshtml.HTMLWindow2Class' to interface type 'mshtml.DispHTMLWindow2'"

Is there another way round. I am able to call active-x function from java script but vice versa still got above exception. Any idea for multi-threaded c# active-x calling javascript function ???

you can access html like this

private void MyControl_Load(object sender, EventArgs e)
        {
            if (this.Site != null)
            {
                htmldoc = (HtmlDocument)this.Site.GetService(typeof(HtmlDocument));

            }


        }

then on any button click on our C# control invoke method to click html button

 HtmlElement setCLIButton = htmldoc.GetElementById("searchButton");
                setCLIButton.InvokeMember("onClick");

by this way you can call your javacsript function hope it will help someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM