library Nhdll;
{ 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,
  Unit1 in 'Unit1.pas',
  Unit2 in 'Unit2.pas' {Form2};
{$R *.res}
 exports
  ShowDLLForm,GetCaption;
beginend.
//Unit1 是空的不贴了。
unit Unit2;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, StdCtrls, Buttons, DB, DBClient, MConnect, ADODB;
type
    TForm2 = class(TForm)
    Label1: TLabel;
    ADOQuery1: TADOQuery;
    cdsCoopTreaPers: TClientDataSet;
    cdsCoopTreaPersCDSDesigner3: TStringField;
    cdsCoopTreaPersCDSDesigner5: TStringField;
    cdsCoopTreaPersCDSDesigner6: TStringField;
    cdsCoopTreaPersCDSDesigner4: TStringField;
    cdsCoopTreaPersCDSDesigner: TStringField;
    cdsCoopTreaPersCDSDesigner2: TStringField;
    cdsCoopTreaPersCDSDesigner7: TDateTimeField;
    cdsCoopTreaPersCDSDesigner8: TDateTimeField;
    cdsCoopTreaPersCDSDesigner9: TStringField;
    cdsCoopTreaPersCDSDesigner10: TBooleanField;
    cdsCoopTreaPersCDSDesigner11: TStringField;
    cdsCoopTreaPersCDSDesigner12: TStringField;
    cdsCoopTreaPersCDSDesigner13: TStringField;
    cdsCoopTreaPersCDSDesigner14: TStringField;
    cdsCoopTreaPersCDSDesigner15: TStringField;
    cdsCoopTreaPersCDSDesigner16: TStringField;
    cdsCoopTreaPersCDSDesigner17: TStringField;
    cdsCoopTreaPersField: TIntegerField;
    DataSource1: TDataSource;
    dcomConn: TDCOMConnection;
    dcomCTPS: TDCOMConnection;
    dcommconn: TDCOMConnection;
    DBGrid1: TDBGrid;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
//定义ShowDLLForm,用于打开本窗体
function ShowDLLForm(AHandle: THandle; ACaption: string): Boolean; Stdcall;
//输出标题
function GetCaption: Pchar; stdcall;
var
  Form2: TForm2;implementation{$R *.dfm}//输出标题
function GetCaption: Pchar; stdcall;
begin
  Result := '插件演示NO1';
end;//打开本窗体
function ShowDLLForm(AHandle: THandle; ACaption: string): Boolean;
var
  DLL_Form: TForm2;
begin
  result := true;
  try
    application.Handle := AHandle; //传递应用程序地址
    DLL_Form := TForm2.Create(Application);//创建窗体
    try
      DLL_Form.caption := Acaption;//给窗体标题赋值
      DLL_Form.ShowModal;  //模式显示窗体
    finally
      DLL_Form.Free;
    end;
  except
    result := false;
  end;
end;end.我用cb调用的测试
void __fastcall TForm1::Button1Click(TObject *Sender)
{
      String path,DllPath;
      DllPath="D:\\test\\Delphiapp\\Nhdll.dll";
      DLLHandle=LoadLibrary(DllPath.c_str());
      if(!DLLHandle)
      {
       ShowMessage("接口初始化失败,"+DllPath+"文件不存在!");
       return ;
      }
      pShowDLLForm=(ShowDLLForm*)GetProcAddress(DLLHandle,"ShowDLLForm");
      bool i=pShowDLLForm(this->Handle,"呵呵");
      //FreeLibrary(DLLHandle);
}
去掉Unit2里的TADOQuery和TDCOMConnection控件重新编译用cb调用就可以,请大家帮我看看应该改哪里,谢了。

解决方案 »

  1.   

    唉,DLL的dpr文件里加上:uses
        ActiveX;procedure DLLEntryProc(Reason: Integer);
    begin
      case Reason of  // case
        DLL_PROCESS_ATTACH, DLL_THREAD_ATTACH:
          CoInitialize(nil);
        DLL_THREAD_DETACH, DLL_PROCESS_DETACH:
          CoUninitialize;
      end;  // end cas
    end;begin
      DllProc := DLLEntryProc;
      DLLEntryProc(DLL_PROCESS_ATTACH);
    end.
      

  2.   

    这些都是常见问题啦,google里搜下就可以了
      

  3.   

    用CoInitialize和CoUninitialize来初始化COM
      

  4.   

    呵呵,不好意思见笑,我用的cb,调用别人delphi做的dcom动态库,不太好用所以我先用delphi做个动态库调用他的dcom动态库,然后再用cb调用我做的delphi动态库,多费了手续,不太懂delphi的。问题就是各位所说的,谢谢。