开发ActiveX控件 [英] Developing ActiveX controls

查看:95
本文介绍了开发ActiveX控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个ActiveX控件,而且由于我不拥有Visual Studio,所以我想知道是否可以自己使用VisualC ++ Express Edition,还是我还需要Windows Platform SDK?

I would like to develop an ActiveX control and as I don't own visual studio I'm wondering whether I can use VisualC++ express edition on it's own, or do I also need the Windows Platform SDK?

推荐答案

您不需要Visual Studio即可编写Active X控件。 Active X控件只是一个COM对象,它以实现IUnknown和IObjectSafety的特定方式注册。

You don't need Visual Studio to write an Active X control. An Active X control is simply a COM object that is registered in a specific way that implements IUnknown and IObjectSafety.

您不需要创建Visual Studio Active X项目。您可以只创建一个普通的DLL,添加适当的清单,然后使用CAB SDK工具对其进行管理。

You don't need to create a Visual Studio Active X project. You can just create a normal DLL, add the proper manifest and cab it using the CAB SDK tools.

您不必使用ATL来编写Active X控件。实际上,最好是不使用它,除非您了解IE扩展性模型中的OLE接口是如何工作的。

You don't have to use ATL to write an Active X control. In fact, you're probably better off not using it until you understand how the OLE interfaces work in the IE extensibility model.

所以是的,您会很好的

编辑:


  • 您应该以 Active X控件简介。

  • 这是 CAB SDK

  • 通过搜索Google等来查找基本ActiveX控件的示例应该没有问题。

  • You should start with Introduction to Active X Controls.
  • Here is the CAB SDK.
  • You should have no problem finding examples for basic ActiveX controls by searching google, etc.

示例清单,称为YOURCONTROL.inf。显然用您称呼您的家伙代替YOURCONTROL,并生成您自己的GUID和版本号。这是您需要的最低清单。

Here is a sample manifest, called YOURCONTROL.inf. Obviously replace YOURCONTROL with whatever you call your guy, and generate your own GUID and version numbers. This is the minimum manifest you'll need.

[version]
signature="$CHICAGO$"
AdvancedINF=2.0

[Add.Code]
YOURCONTROL.dll=YOURCONTROL.dll

[YOURCONTROL.dll]
file-win32-x86=thiscab
clsid={11111111-2222-3333-4444-555555555555}
FileVersion=1,2,3,4567
RegisterServer=yes

您的项目中将需要一个标准的.DEF文件,该文件列出了COM和自我注册所需的导出功能。 DllGetClassObject是COM调用您的地方,以获取COM对象的类工厂。您应该在RegisterServer和UnregisterServer处将初始状态写入注册表(例如,COM对象注册等)。

You'll need a standard .DEF file in your project that lists the required exported functions for COM and self-registration. DllGetClassObject is where COM will call you to get the class factory for your COM object. RegisterServer and UnregisterServer is where you should write your initial state to the registry (e.g. your COM object registration, etc).

EXPORTS
    DllCanUnloadNow             PRIVATE
    DllGetClassObject   PRIVATE
    DllRegisterServer   PRIVATE
    DllUnregisterServer PRIVATE

您还将需要一个IDL文件,因此您可以定义COM对象的dispinterface,以便可以从脚本中调用它,从而可以将事件触发到Javascript。像这样:

You'll need an IDL file too, so you can define your COM object's dispinterface so it can be called from script, and so it can fire events to Javascript. Something like this:

import "oaidl.idl";
import "ocidl.idl";

#include "dispids.h"  // <-- define your DISPIDs here

[
        uuid(<<generate your own guid here>>),
        version(1.0),
]
library YOURCONTROLLIBRARY
{
    [
        uuid(<<generate your own guid here>>),
        hidden
    ]
    dispinterface DYOURCONTROLEvents
    {
        properties:
        methods:
        // Add outgoing events here.
        [id(DISPID_SOME_EVENT)]  void SomeEvent();
    }

    [
        dual,
        uuid(<<generate your own guid here>>)
    ]
    interface IYOURCONTROL : IDispatch
    {
        // Add methods and properties here.
        [id(DISPID_SOMEMETHOD)] HRESULT SomeMethod([in] BSTR bstrFoo);
    }

    [
        uuid(<<generate your own guid here>>)
    ]
    coclass YOURCONTROLCtl
    {
        [default] interface IYOURCONTROL;
        [source, default] dispinterface DYOURCONTROLEvents;
    }
}

最后,您的DLL入口点应该看起来像像这样:

And, finally, your DLL entry point should look something like this:

HINSTANCE g_hInstance;
LONG g_nDllRefs;

extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) {
    switch (dwReason) {
        case DLL_PROCESS_ATTACH:
            g_hInstance = hInstance;
            g_nDllRefs = 0;
            break;

        case DLL_PROCESS_DETACH:
            break;
    }

    return true;
}

// Call this whenever you create your object to keep your DLL loaded.
void DllAddRef() {
    InterlockedIncrement(&g_nDllRefs);
}

// Call this when your object is destroyed.
void DllRelease() {
    InterlockedDecrement(&g_nDllRefs);
}

STDAPI DllCanUnloadNow() {
    return (g_nDllRefs == 0 ? S_OK : S_FALSE);
}

// This is where you create your class factory.  See the IClassFactory documentation on msdn.
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv) {
    HRESULT hr;
    if (rclsid == CLSID_YOUROBJECTCtl) {
        CYOUROBJECTFactory *pYOUROBJECTFactory = new CYOUROBJECTFactory;
        if (pYOUROBJECTFactory == NULL) {
            hr = E_OUTOFMEMORY;
        } else {
            hr = pYOUROBJECTFactory ->QueryInterface(riid, ppv);
        }
    } else {
        hr = CLASS_E_CLASSNOTAVAILABLE;
    }
    return hr;
}

STDAPI DllRegisterServer() {
    // Write your registry keys for registering your ActiveX COM Object here.
    return S_OK;
}

STDAPI DllUnregisterServer() {
    // Delete your registry keys here.
    return S_OK;
}

这篇关于开发ActiveX控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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