从浏览器运行ATL COM DLL(调用方法) [英] Run a ATL COM DLL from a Browser (call a method)

查看:207
本文介绍了从浏览器运行ATL COM DLL(调用方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用ATL(没有MFC支持)创建的COM对象



对象有一个方法打开一个对话框>

目前我从另一个EXE调用它:

  hr = CoCreateInstance $ b CLSID_MyControl,
NULL,
CLSCTX_INPROC_SERVER,
IID_IMyControl,
(void **)& pMyControl
);

,然后:

  hr = pMyControl-> MyMethod(ATL :: CComBSTR(InputString1),ATL :: CComBSTR(InputString2),& IntReturned,& IntReturned); 

是否可以从浏览器调用它?



如何实例化对象并从浏览器调用我的方法(使用params)?

解决方案

有些要点可以回答您的问题:




  • 您不能在IE以外的任何浏览器中使用COM对象,也不能使用基于WebBrowser 的应用程序。


  • href =http://msdn.microsoft.com/en-us/library/aa768224%28v=vs.85%29.aspx =nofollow> IObjectSafety 接口允许IE创建您的对象。当然,对象应该是安全的脚本由任何不受信任的源。理想情况下,您应该锁定对象到您自己的网站列表。您可以使用 SiteLock 模板。


  • 对象应该实现 IDispatch 接口,以便用于脚本。最好的方法是使用ATL的 IDispatchImpl (很可能,它已经在你的代码中完成)。


  • 您的示例中的 MyMethod 使用两个 [out] IntReturned 的参数。 JavaScript只允许一个输出 [out,retval] 参数。如果你需要返回多个值,你必须使用VBScript。




示例(替换您的CLSID):

 <!DOCTYPE html PUBLIC -  // W3C // DTD HTML 4.01 Transitional // EN> 
< html>
< head>
< title>< / title>
< script type =text / vbscript>
选项显式
window.onload = GetRef(OnLoadHandler)

Sub OnLoadHandler
Dim InputString1
Dim InputString2
Dim IntReturned1
Dim IntReturned2

InputString1 =a
InputString1 =b
testObject.MyMethod InputString1,InputString2,IntReturned1,IntReturned

alert结果:& IntReturned1& ,& IntReturned
End Sub
< / script>
< / head>

< body>
< object id =testObjectclassid =clsid:12345678-1234-1234-1234-1234567890AB>
< span>无法创建对象。< / span>
< / object>
< / body>
< / html>

如果不实现 IObjectSafety 您仍然可以运行此代码作为HTML应用程序。将其另存为.HTA文件,并以 C:\Windows \SysWOW64\ mshta.exe C:\users\user\Documents\test.hta 如果你的C ++ COM DLL是32位,或者 C:\Windows \System32\mshta.exe C:\users\user\Documents\test.hta 如果是64位。



COM DLL需要先注册 regsvr32.exe (如果你可以从C ++客户端项目中使用它,你可能已经这样做了)。


I have a COM Object created using ATL (without MFC Support)

The Object has 1 method that opens a Dialog (that does all the rest)

Currently I call it from another EXE:

hr = CoCreateInstance(
    CLSID_MyControl,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IMyControl,
    (void**) &pMyControl
    );

and then:

hr = pMyControl->MyMethod (ATL::CComBSTR(InputString1), ATL::CComBSTR(InputString2), &IntReturned, &IntReturned);

Is it possible to call it as is from a browser ?

How can I Instantiate the object and invoke my method (with params) from the browser ?

解决方案

Some points to answer your question:

  • You won't be able to use a COM object in any browser other than IE or a WebBrowser-based app.

  • You'd need to implement IObjectSafety interface to allow IE to create your object. Naturally, the object should be safe for scripting by any untrusted source. Ideally, you should lock the object to your own list of sites. You could use SiteLock template for this.

  • The object should implement IDispatch interface, to be available for scripting. The best way is to use ATL's IDispatchImpl (most likely, it's already done in your code).

  • The MyMethod in your sample uses two [out] arguments for IntReturned. JavaScript only allows one output [out, retval] argument. If you need to return more than one value, you'd have to use VBScript.

Example (substitute your CLSID):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<html>
<head>
    <title></title>
    <script type="text/vbscript">
        Option Explicit
        window.onload = GetRef("OnLoadHandler")

        Sub OnLoadHandler
            Dim InputString1
            Dim InputString2
            Dim IntReturned1
            Dim IntReturned2

            InputString1 = "a"  
            InputString1 = "b"  
            testObject.MyMethod InputString1, InputString2, IntReturned1, IntReturned

            alert "Result: " & IntReturned1 & ", " & IntReturned
        End Sub
    </script>
</head>

<body>
    <object id="testObject" classid="clsid:12345678-1234-1234-1234-1234567890AB">
        <span>Unable to create the object.</span>
    </object>
</body>
</html>

If you don't implement IObjectSafety, you can still run this code as HTML Application. Save it as an .HTA file, and run as C:\Windows\SysWOW64\mshta.exe C:\users\user\Documents\test.hta if your C++ COM DLL is 32-bit, or as C:\Windows\System32\mshta.exe C:\users\user\Documents\test.hta if it is 64-bit.

The COM DLL needs to be registered first with regsvr32.exe (you've probably already done this if you can use it from a C++ client project).

这篇关于从浏览器运行ATL COM DLL(调用方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆