在这里,将我的经验给大家分享一下:新手少走弯路
  DLLApp:=APPLICATION;
  application:=APP;
  //application.Handle:=APP.Handle;//加入时会出现跳动现像
  //application.Icon.Handle:=APP.Icon.Handle; //加入主程序退出时会出错,
  Form1 := TForm.Create(app);在DLL 中就是这五句话,搞了一个通宵, 1: “//”的语句经对不能出现,否则主程序就会出现一些莫明奇怪的错误
 2:STRING字符型不能出现在DLL中! 希望看贴顶我!--下面是DLL源码
library ErpSystemDLL;
uses
  windows,
  SysUtils,
  StdCtrls,
  XPMenu,
  messages,
  Controls,
  forms;
Const
  MS_DLL = WM_USER+$1005;{$R *.res}
var
  DLLApp: TApplication;
  DllScr:TScreen;
 //密码框或录入框
function InputPassword(APP:Tapplication;ACaption, APrompt,Value:pchar;mtype:Integer;mdefault:Integer):pchar;stdcall;
var
  Form1: TForm;
  Prompt: TLabel;
  Edit1: TEdit;
  XPMenu:TXPMenu;
begin
  DLLApp:=APPLICATION;
  application:=APP;
  //application.Handle:=APP.Handle;//加入时会出现跳动现像
  //application.Icon.Handle:=APP.Icon.Handle; //加入主程序退出时会出错,
  Form1 := TForm.Create(app);
  with Form1 do
    try
      color:=$00EFB1BC;
      Canvas.Font := Font;
      BorderStyle := bsDialog;
      Caption := ACaption;
      Position := poScreenCenter;
      width:=250;
      height:=125;
      Prompt := TLabel.Create(Form1);
      with Prompt do
      begin
        Parent := Form1;
        AutoSize := True;
        Left :=20;
        Top :=18;
        Caption := APrompt+':';
      end;
      Edit1 := TEdit.Create(Form1);
      with Edit1 do
      begin
        Parent := Form1;
        if mtype=0 then //password
        PasswordChar:='*';
        Left := Prompt.Left+Prompt.Width+5;
        Top  :=Prompt.top;
        Width :=125;
        MaxLength := 255;
        Text := Value;
        SelectAll;
      end;
      with TButton.Create(Form1) do
      begin
        Parent := Form1;
        Left:=Prompt.left;
        TOP:=Prompt.top+30;
        Caption :='确定(&O)';
        ModalResult := mrOk;
        if mdefault=0 then
        Default := True;
      end;
      with TButton.Create(Form1) do
      begin
        Parent := Form1;
        Caption :='取消(&C)';
        Left:=Prompt.left+width+20;
        TOP:=Prompt.top+30;
        ModalResult := mrCancel;
        if mdefault>0 then
        Default := True;
      end;
      XPMenu:=TXPMenu.Create(Form1);
      with xpmenu do
      begin
        Parent := Form1;
        XPMenu.AutoDetect:=true;
        Active:=true;
      end;
      if ShowModal = mrOk then
      begin
        Result :=Pchar(Trim(Edit1.Text));
      end else begin if mtype<>99 then Result :='' else result:=Pchar('99');end;
    finally
      FreeAndNil(Form1);
    end;
end;
Exports
 InputPassword;
procedure DLLUnloadProc(Reason: Integer); register;
begin
 if (Reason = DLL_PROCESS_DETACH) or (Reason = DLL_THREAD_DETACH) then
  begin
    //sendmessage(Application.MainForm.Handle,MS_DLL,0,0);
    Application := DLLApp;
    Screen := DLLScr;
  end;
end;begin
  DLLApp := Application; //保存Dll工程自身的Application
  DLLScr := Screen; //保存Dll工程自身的Screen;
  DLLProc := @DLLUnloadProc;
end.

解决方案 »

  1.   

    顶!
    不过好像DLLScr没用到哦~
      

  2.   

    我是用来留用的,本人发现网上的经验好多不全,都留一手,将主要的代码去掉,上面的例子中
    在网上看到的就是:
       //application.Handle:=APP.Handle;//加入时会出现跳动现像
      //application.Icon.Handle:=APP.Icon.Handle; //加入主程序退出时会出错没有application:=APP;本人不断的调试才得到application:=APP;这一句,如果不加这一个,调用的窗体不完美
      

  3.   

    “2:STRING字符型不能出现在DLL中!”用PChar会安全一点,其实在创建DLL程序时,Delphi每次提示过,呵呵。
    可曾注意这段英文,注意最后一句~~~
    { 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. }
      

  4.   

    1. //注释的写法,本来就是有问题(实际上InputPassword函数中参数不应该用Tapplication);
    而是应该用THandle; 即只传入主程序的Handle(如H:THandle然后Applicatioin.Handle:=H;)2.string是可以出现在dll中的,只不过为了不同语言调用的兼容问题,最好是用PChar
      

  5.   

    楼上,我用STRING就出错!不知为什么!
      

  6.   

     还有,照楼上的意思:我传入APPLICATION时,如果APPLICATION.HANDLE=APP.HANDLE
    主程式进入假死现像,要用SHOWWINDOWS激法,但又有主窗体跳动不已的问题!
     我再试试APP定主为HANDLE试试