windows 开发 ActiveX 控件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1751629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Developing ActiveX controls
提问by Matt
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?
我想开发一个 ActiveX 控件,由于我没有 Visual Studio,我想知道我是否可以单独使用 VisualC++ express 版本,还是我还需要 Windows Platform SDK?
回答by i_am_jorf
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 控件。Active X 控件只是一个以实现 IUnknown 和 IObjectSafety 的特定方式注册的 COM 对象。
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.
您不需要创建 Visual Studio Active X 项目。您只需创建一个普通的 DLL,添加适当的清单并使用 CAB SDK 工具对其进行 cab。
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.
您不必使用 ATL 来编写 Active X 控件。事实上,在您了解 OLE 接口在 IE 可扩展性模型中的工作原理之前,最好不要使用它。
So yes, you'll be just fine with Visual Studio Express.
所以是的,你会很好地使用 Visual Studio Express。
EDIT:
编辑:
- 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.
- 您应该从Active X 控件简介开始。
- 这是CAB SDK。
- 您应该可以通过搜索 google 等找到基本 ActiveX 控件的示例。
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.
这是一个示例清单,名为 YOURCONTROL.inf。显然,用您称呼您的人的任何名称替换 YOURCONTROL,并生成您自己的 GUID 和版本号。这是您需要的最低清单。
[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
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).
您的项目中将需要一个标准的 .DEF 文件,其中列出了 COM 和自注册所需的导出函数。DllGetClassObject 是 COM 调用您获取 COM 对象的类工厂的地方。RegisterServer 和 UnregisterServer 是您应该将初始状态写入注册表的地方(例如,您的 COM 对象注册等)。
EXPORTS
DllCanUnloadNow PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
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:
您还需要一个 IDL 文件,以便您可以定义 COM 对象的调度接口,以便可以从脚本中调用它,从而可以向 Javascript 触发事件。像这样的东西:
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;
}
}
And, finally, your DLL entry point should look something like this:
最后,您的 DLL 入口点应如下所示:
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;
}
回答by ChrisF
This Microsoft Support postfrom 2006 would indicates that you can use the VC++ Express edition for developing ActiveX controls:
这篇来自 2006 年的Microsoft 支持帖子表明您可以使用 VC++ Express 版本来开发 ActiveX 控件:
APPLIES TO
Microsoft ActiveX Template Library 3.0, when used with:
Microsoft Visual C++ 6.0 Enterprise Edition
Microsoft Visual C++ 6.0 Professional Edition
Microsoft Visual C++, 32-bit Learning Edition 6.0
Microsoft Visual C++ 2005 Express Edition
Microsoft Visual C++ .NET 2003 Standard Edition
Microsoft Visual C++ .NET 2002 Standard Edition
适用于
Microsoft ActiveX 模板库 3.0,与以下一起使用时:
Microsoft Visual C++ 6.0 企业版
Microsoft Visual C++ 6.0 专业版
Microsoft Visual C++,32 位学习版 6.0
Microsoft Visual C++ 2005 速成版
Microsoft Visual C++ .NET 2003 标准版
Microsoft Visual C++。 NET 2002 标准版
It's been a while since I developed using ActiveX, but I don't remember having to have the Windows Platform SDK installed.
我已经有一段时间没有使用 ActiveX 进行开发了,但我不记得必须安装 Windows Platform SDK。