我发现了一个非常诱人的示例,它使用 Windows 中包含的 SysMon.ocx ActiveX 控件来创建类似性能监视器的功能。
我发现了一个在 HTML 页面中使用它的示例,该页面动态添加了几个性能计数器(在本例中,用于 SQL 服务器性能)。由于 Delphi 可以在运行时轻松创建 ActiveX/OLE 对象,我想我会尝试将此处的 HTML 示例转换为 Delphi。第一步是导入类型库并创建一个SystemMonitor_TLB.pas
,我创建的,通过通常的导入方式,使用 Delphi 2007。
我已经注册了 OCX,并使用类型库导入器导入了“系统监视器”OCX (SysMon.ocx),它创建了一个如下所示的导入 TLB.pas 文件。(此处仅显示文件的代表性示例)。
unit SystemMonitor_TLB;
// ************************************************************************ //
// WARNING
// -------
// ...
// Type Lib: C:\Windows\system32\sysmon.ocx (1)
// LIBID: {1B773E42-2509-11CF-942F-008029004347}
// LCID: 0
// Helpfile:
// HelpString: System Monitor Control
// DepndLst:
// (1) v2.0 stdole, (C:\Windows\system32\stdole2.tlb)
// Errors:
// Hint: TypeInfo 'SystemMonitor' changed to 'SystemMonitor_'
// Error creating palette bitmap of (TSystemMonitor_)
// : Registry key CLSID\{C4D2D8E0-D1DD-11CE-940F-008029004347}\ToolboxBitmap32 not found
// ************************************************************************ //
// *************************************************************************//
{$TYPEDADDRESS OFF} // Unit must be compiled without type-checked pointers.
{$WARN SYMBOL_PLATFORM OFF}
{$WRITEABLECONST ON}
{$VARPROPSETTER ON}
interface
uses Windows, ActiveX, Classes, Graphics, OleCtrls, OleServer, StdVCL, Variants;
// *********************************************************************//
// GUIDS declared in the TypeLibrary. Following prefixes are used:
// Type Libraries : LIBID_xxxx
// CoClasses : CLASS_xxxx
// DISPInterfaces : DIID_xxxx
// Non-DISP interfaces: IID_xxxx
// *********************************************************************//
const
// TypeLibrary Major and minor versions
SystemMonitorMajorVersion = 3;
SystemMonitorMinorVersion = 7;
LIBID_SystemMonitor: TGUID = '{1B773E42-2509-11CF-942F-008029004347}';
IID_... // about 20 more lines of GUIDs s
// --SNIP--
// about 5000 lines of pretty typical import-TLB-code snipped
// --SNIP--
procedure Register;
begin
RegisterComponents(dtlOcxPage, [TSystemMonitor_, TCounterItem, TLogFileItem]);
RegisterComponents(dtlServerPage, [TCounters, TLogFiles, TCounterItem2, TSystemMonitor2,
TAppearPropPage, TGeneralPropPage, TGraphPropPage, TSourcePropPage, TCounterPropPage]);
end;
end.
上面的整个文件可以在这里找到。
我安装了包含这个单元的 .dpk 包,并创建了一个演示表单,并且 ActiveX 控件似乎可以工作,至少在设计时是这样。当我运行我的演示应用程序时,如果我将组件添加到表单中,在设计时,我在运行时遇到异常,它似乎是通过实例化基类的一部分TOleControl
:
procedure TOleControl.CreateControl;
var
Stream: IStream;
CS: IOleClientSite;
X: Integer;
begin
// about 12 lines not shown.
OleCheck(FPersistStream.Load(Stream)); // here's where we get the mystery OLE error.
DestroyStorage;
// more code here.
end;
我想知道的是:
神秘的 OLE 错误是什么意思,有人知道我该如何解决吗?
或者,是否有人知道如何解决上述问题,或者以其他方式获得与 delphi 2007 或 xe2 一起使用的 PerfMon 的 sysmon.ocx 的工作(在设计时和运行时)版本?
评论中的提示是否是解决此问题的某种关键:
// Hint: TypeInfo 'SystemMonitor' changed to 'SystemMonitor_'
更新:请注意,我正在谈论设计时支持的解决方法。当我只在运行时而不是设计时创建控件时,它工作正常:
type
TSystemMonitor = TSystemMonitor_;
...
procedure TForm1.FormCreate(Sender: TObject);
begin
SysMonCtrl := TSystemMonitor.Create(Self);
SysMonCtrl.Align := alClient;
SysMonCtrl.Parent := Self;
SysMonCtrl.Show;
end;
也许我应该那样做?