----------------------------------------------------------
我的程序1,可以运行
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
  procedure ConnFilesM(pw,user,db,computer:string) stdcall;external 'bin\ConnDBDll.dll';var
  Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
   ConnFilesM('a','a','a','a');
end;end.
--------------------------------------------------------------------------------------------------------------------
把程序1改为多线程后,得程序2
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type  TConnDbThread = class(TThread)
  private
    { Private declarations }
  protected
    procedure Execute; override;
  end;  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
     Threads: TConnDbThread;
  public
    { Public declarations }
  end;
  procedure ConnFilesM(pw,user,db,computer:string) stdcall;external 'bin\ConnDBDll.dll';var
  Form1: TForm1;implementation{$R *.dfm}procedure TConnDbThread.Execute;
begin
  ConnFilesM('a','a','a','a');
end;procedure TForm1.Button1Click(Sender: TObject);
begin
   Threads:=TConnDbThread.Create(false);
end;end.
----------------------------------------------------------编译程序2时
提示:
project ***.exe raised exception class Eolesyserror with message"尚未调用coinitialize.",process stopped,use step or run to continue.这是为什么啊?尚未调用coinitialize说的是什么啊?

解决方案 »

  1.   

    在线程中调用Com对象时,应该使用CoInitalize(nil)来初始化.
    估计你调用的Dll中有Com对象。
      

  2.   

    在 dll 中的代碼最前面中加入
    coinitialize(nil);
      

  3.   

    不行啊?下面我DLL文件的源码,应该怎样改才行?
    -------------------------------------------------------
    library ConnDBDll;{ **}
    uses
      SysUtils,
      Classes,
      adodb,
      Forms,ComObj;{$R *.res} Function GetSQLServerName:Variant;stdcall;
    var
       SQLServer: Variant;//uses comobj;
       ServerList: Variant;
    begin
        SQLServer := CreateOleObject('SQLDMO.Application');
        ServerList:= SQLServer.ListAvailableSQLServers;
        Result:=ServerList;  
    end;  procedure ConnFilesM(pw,user,db,computer:string) stdcall;
      var connection:tadoconnection;
        connstr:string;
      begin
          try
        connection:=tadoconnection.Create(nil);
        connstr:= 'Provider=SQLOLEDB.1;Password='+pw+';Persist Security Info=True;User ID='+user+';Initial Catalog='+db+';Data Source='+computer;
        connection.ConnectionString:=connstr;
        connection.LoginPrompt:=false;
        connection.Connected:=true;
        with Application do     //uses Forms
          begin
          NormalizeTopMosts;
          MessageBox('连接数据库成功!', '系统提示',0);
          RestoreTopMosts;
          end;
        except
        with Application do
          begin
          NormalizeTopMosts;
          MessageBox('连接数据库失败!', '系统提示',0);
          RestoreTopMosts;
          end;
        end;
      end;  
      exports ConnFilesM,GetSQLServerName;
    begin
    end.
      

  4.   

    在“end.”前加入如下代码
    initialization
      coinitialize(nil);
    finalization
      CoUninitialize;
      

  5.   

    多谢各位的提点。按 trainbox(rain)的代码改写后可以运行;
      

  6.   

    原来是没有加入trainbox(rain)的代码;
    只是DLL改成如下:
    library ConnDBDll;{ Important note about DLL memory management: ShareMem must be the
      first unit in your library's USES clause AND your project's (select
      Project-View Source) USES clause if your DLL exports any procedures or
      functions that pass strings as parameters or function results. This
      applies to all strings passed to and from your DLL--even those that
      are nested in records and classes. ShareMem is the interface unit to
      the BORLNDMM.DLL shared memory manager, which must be deployed along
      with your DLL. To avoid using BORLNDMM.DLL, pass string information
      using PChar or ShortString parameters. }
    uses
      SysUtils,
      Classes,
      adodb,
      Forms,ComObj;{$R *.res} Function GetSQLServerName:Variant;stdcall;
    var
       SQLServer: Variant;//uses comobj;
       ServerList: Variant;
    begin
        SQLServer := CreateOleObject('SQLDMO.Application');
        ServerList:= SQLServer.ListAvailableSQLServers;
        Result:=ServerList;  
    end;  Function ConnFilesM(pw,user,db,computer:string):boolean; stdcall;
      var connection:tadoconnection;
        connstr:string;
      begin
          try
        connection:=tadoconnection.Create(nil);
        connstr:= 'Provider=SQLOLEDB.1;Password='+pw+';Persist Security Info=True;User ID='+user+';Initial Catalog='+db+';Data Source='+computer;
        connection.ConnectionString:=connstr;
        connection.LoginPrompt:=false;
        connection.Connected:=true;
        result := true;
        with Application do     //uses Forms
          begin
          NormalizeTopMosts;
          MessageBox('连接数据库成功!', '系统提示',0);
          RestoreTopMosts;
          end;
        except
        result := false;
        with Application do
          begin
          NormalizeTopMosts;
          MessageBox('连接数据库失败!', '系统提示',0);
          RestoreTopMosts;
          end;
        end;
      end;  
      exports ConnFilesM,GetSQLServerName;
    beginend.