【发布时间】:2021-12-01 12:56:09
【问题描述】:
我一直在使用 Indy TIdTCPServer 对象并在 TIdTCPServer.OnExecute 事件期间实例化 TXMLDocument 对象实例。当xml.Active 设置为 true 时出现异常,我感到非常惊讶:
未安装 Microsoft MSXML
procedure TForm4.tcpRXExecute(AContext: TIdContext);
var
sResponseXML : string;
xml:IXMLDocument;
begin
// get message from client
sResponseXML := AContext.Connection.IOHandler.ReadLn;
xml:=TXMLDocument.Create(nil);
// error here: "Microsoft MSXML is not installed"
xml.Active:=true;
xml.Encoding:='UTF-8';
xml.LoadFromXML(sResponseXML);
// use the xml document
//AContext.Connection.IOHandler.WriteLn('... message sent from server :)');
end;
深入研究,我发现异常发生是因为TMSXMLDOMDocumentFactory.TryCoCreateInstance() 无法创建正确的文档对象实例,尽管它收到了与从主线程在应用程序的其他部分收到的相同的GuidList。我不明白为什么如果从组件的线程调用该对象没有实例化。
这里是 Embarcadero 代码,该对象应该被实例化:
class function TMSXMLDOMDocumentFactory.TryCoCreateInstance(const GuidList: array of TGUID): IUnknown;
var
I: Integer;
Status: HResult;
begin
for I := Low(GuidList) to High(GuidList) do
begin
// never successful if the XML document object was being used from the Execute event handler.
Status := CoCreateInstance(GuidList[I], nil, CLSCTX_INPROC_SERVER or
CLSCTX_LOCAL_SERVER, IDispatch, Result);
if Status = S_OK then Exit;
end;
end;
我认为它一定与 CLSCTX_INPROC_SERVER 或 CLSCTX_LOCAL_SERVER (https://docs.microsoft.com/en-us/windows/win32/api/wtypesbase/ne-wtypesbase-clsctx) 有关,但我不明白为什么这些可能是个问题。
即使这是原因,我如何在该事件处理程序中使用TXMLDocument?
【问题讨论】:
标签: delphi indy win32com indy10 txmldocument