dll工程中声明的一个函数,如下,功能是调用一个浏览器;
procedure launchExplorer();stdcall;
var
IE        : Variant;
begin
IE := CreateOleObject('InternetExplorer.Application');
end;编译我通过了,但是我在调用时老是报错,“EOLEsysError with message 尚未调用CoInitilization”
是不是dll不能调用OLE?

解决方案 »

  1.   

    下面是源码:
        Project1.DPR {主叫程序}
        Unit1.PAS {主叫程序单元}
        Project2.DPR {DLL}
        Unit2.PAS {DLL单元}
    {---------- DLL 主程序 Project2.DPR ----------}library Project2;uses
      SysUtils,
      Classes,
      Unit2 in 'Unit2.pas' {Form1};{$R *.RES}{ 下面的语句用于向调用该 DLL的程序提供调用接口 }
    exports
      DoTest;       { 过程来自单元Unit2 }begin
    end.
    {---------- DLL中的单元 Unit2.PAS ----------}unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      Db, ADODB, StdCtrls, Menus;type
      TForm1 = class(TForm)
        ADOConnection1: TADOConnection;{ 本地数据库连接 }
        Memo1: TMemo;                  { 用于显示信息   }
      private
      public
      end;{ 该过程向外提供 }
    procedure DoTest(H: THandle;           { 获得调用者的句柄       }
                     AConn: TADOConnection;{ 获得调用者的数据库连接 }
                     S: string;            { 获得一些文本信息       }
                     N: Integer);          { 获得一些数值信息       }
                     cdecl;                { 指定调用协议           } implementation{$R *.DFM}procedure DoTest(H: THandle; AConn: TADOConnection; S: string; N: Integer);
    begin
      Application.Handle := H;              { 将过程的句柄赋值为调用者的句柄 }
      { 上面语句的作用在于, DLL的句柄和调用者的句柄相同,在任务栏中就不会   }
      { 各自出现一个任务标题了。                                             }
      with TForm1.Create(Application) do try{ 创建窗体                       }
        Memo1.Lines.Append('成功调用');     { 显示一行信息                   }
        ADOConnection1 := AConn;            { 获得数据库连接的实例           }
        Memo1.Lines.Append(
          ADOConnection1.ConnectionString +
          ' - ' + S + ' - ' + IntToStr(N)); { 根据得到的参数显示另一行信息   }
        ShowModal;                          { 模式化显示窗体                 }
      finally
        Free;                               { 调用结束时销毁窗口             }
      end;
    end;end.
    {---------- 调用者 Project1.DPR,很普通的工程文件 ----------}program Project1;uses
      Forms,
      Unit1 in 'Unit1.pas' {Form1};{$R *.RES}begin
      Application.Initialize;
      Application.CreateForm(TForm1, Form1);
      Application.Run;
    end.
    {---------- 调用者单元Unit1.PAS ----------}unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Db, ADODB;type
      TForm1 = class(TForm)
        Button1: TButton;   { 按此按钮进行调用 }
        ADOConnection1: TADOConnection;         { 本地数据库连接,将传递给 DLL }
        procedure Button1Click(Sender: TObject);{ 调用 DLL}
      private
      public
      end;var
      Form1: TForm1;implementation{$R *.DFM}{ 外部声明必须和 DLL中的参数列表一致,否则会运行时错误    }
    procedure DoTest(H: THandle;             { 传递句柄       }
                     AConn: TADOConnection;  { 传递数据库连接 }
                     S: string;              { 传递文本信息   }
                     N: Integer);            { 传递数值信息   }
                     cdecl;                  { 指定调用协议   }
                     external 'Project2.dll';{ 指定过程来源   }{ 调用过程 }
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      DoTest(Application.Handle,
             ADOConnection1,
             'Call OK',
             256);
    end;end.
      

  2.   

    在动态库中用到Active组件,需要调用CoInitialize(nil);
    一般在initialization和finalization中调用initialization
      CoInitialize(nil);
    finalization
      CoUninitialize;
      

  3.   


    uses
      ActiveX,//问题之关键
      Windows,
      Messages,
      Types,
      SysUtils,
      Classes,
      Forms;
    //...procedure launchExplorer();stdcall;
    var
    IE        : Variant;
    begin
    IE := CreateOleObject('InternetExplorer.Application');
    end;
    procedure mPROC(Proc: DWord);
    begin
      case Proc of
        DLL_PROCESS_ATTACH:
        begin
          CoInitialize(nil);
        end;
        DLL_PROCESS_DETACH:
        begin
          CoUnInitialize;
        end;
        DLL_THREAD_ATTACH:
        begin    end;
        DLL_THREAD_DETACH:
        begin    end;
      end;
    end;
    exports
      launchExplorer;begin
      DllProc := @mPROC;
      mPROC(DLL_PROCESS_ATTACH);
    end.
      

  4.   

    报错的原因很多,可能性DLL本身就有错,可能是注册时的问题,可能是调用的程序段有问题,我以前就写过DLL,注册是按资料做,就是不行,第二天开机就可以用了,我现在还没想通是怎么回事!我是在控制面板中注册的,这方面楼主还是看看开发指南吧
      

  5.   

    使用COM时必须先初始化CoInitialize(nil);,使用完毕后在调用CoUnInitialize(),如果是在线程中执行则必须使用CoInitialize(nil,2);
    ok了
      

  6.   

    呵在DLL的Use中加入sharmemry并且放在第一個這問題俺也有過