无法将线程并发模型设置为多线程单元

Unable to set thread concurrency model to multithreaded apartment

我创建了一个新的 DUnit 测试项目,我正尝试在其开始时设置多线程单元。问题是在一台电脑上改变了户型。

program COMApartment;

{$IFDEF CONSOLE_TESTRUNNER}
{$APPTYPE CONSOLE}
{$ENDIF}

uses
  Winapi.ActiveX,
  TestuApartmentInfo in 'TestuApartmentInfo.pas',
  DUnitTestRunner;

{R *.RES}

begin
  CoUninitialize;
  CoInitializeEx(nil, COINIT_MULTITHREADED); // Result is S_OK
  Log(GetCurrentApartmentType); //APTTYPE_MTA on both computers.
  DUnitTestRunner.RunRegisteredTests;
end.

现在,当我 运行 这个简单的测试时:

unit TestuApartmentInfo;

interface

uses
  TestFramework, Winapi.Windows, uApartmentInfo;

type
  TestIComThreadingInfo = class(TTestCase)
  published
    procedure ApartmentType;
  end;

implementation

uses
  Dialogs, System.SysUtils;

procedure TestIComThreadingInfo.ApartmentType;
begin
  //This gives APTTYPE_MTA on my dev computer (Windows 7) and APTTYPE_MAINSTA or APTTYPE_STA on virtual machine (Windows 2007 Server).
  Log(GetCurrentApartmentType); 
end;

initialization
  RegisterTest(TestIComThreadingInfo.Suite);
end.

我不明白不同计算机上的不同行为。这是由于不同的操作系统吗?在我的测试中,我可以生成另一个线程并为其指定单元模型并且它会起作用,但我的好奇天性想知道为什么在上述情况下会出现不同的结果。

GetCurrentApartmentType 已在 this 文章中实现,并且可以正常工作。这是一个示例应用程序,用于说明我在多线程单元模型中需要 运行 的一些 COM 对象的问题。

this post 中描述了我的问题的解决方案。我的错误是在其他一些代码已经调用它之后调用了 CoUninitialize/CoInitializeCoInitialize 应在任何其他代码之前调用。