我在线程里声明一个控件对象,首先在USESE里面引用了“MSCommLib_TLB”,然后再引用"ACTIVEX",接下来如下面的程序所示:
   procedure colThread.Execute;
begin
  FreeOnTerminate:=True;
  TColTMSComm.Create(Application);
  ColMSComm.InputLen :=1;
  ColMSComm.RThreshold :=1;
  ColMSComm.Settings :='9600,o,8,1';
  if colMSComm.PortOpen =false then
     colMSComm.PortOpen :=True;
 if(timeBeginPeriod(nTimerRes)=TIMERR_NOERROR) then
    begin
        nTimerID:=timeSetEvent(SamplePeriod,nTimerRes,@TimeProc,0,TIME_PERIODIC);//启动定时器
    end;
end;
  initialization
 CoInitialize(nil);
 finalization
  CoUnInitialize;
end.可是还是会出现“尚未调用CoInitialize”,请问一下这个是什么问题啊,搞了一个晚上了,深夜发贴

解决方案 »

  1.   

    在msdn中对于CoInitialize的解释如下:Initializes the COM library on the current apartment and identifies the concurrency model as single-thread apartment (STA). Applications must initialize the COM library before they can call COM library functions other than CoGetMalloc and memory allocation functions. 
      

  2.   

    单元初始化的时候执行的还是主线程啊execute里面才是在多线程中执行的代码在这里加入CoInitialize
      

  3.   

    如果使用com组件,需要调用coInitialize和CoUninitialize。
    使用方法:
    procedure colThread.Execute;
    begin
      coInitialize(nil);
      try    FreeOnTerminate:=True;
        TColTMSComm.Create(Application);
        ColMSComm.InputLen :=1;
        ColMSComm.RThreshold :=1;
        ColMSComm.Settings :='9600,o,8,1';
        if colMSComm.PortOpen =false then
          colMSComm.PortOpen :=True;
        if(timeBeginPeriod(nTimerRes)=TIMERR_NOERROR) then
        begin
          nTimerID:=timeSetEvent(SamplePeriod,nTimerRes,@TimeProc,0,TIME_PERIODIC);//启动定时器
        end;
      finally
        CoUninitialize;
      end;
    end.
      

  4.   

    同意 3楼  
    要先   Initialize(nil);   在create