library regeidt;{ 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,
  Registry,
  windows,
  Classes;{$R *.res}
function regsystem(setreg:string):string;stdcall;
var
reg:TRegistry;
begin
reg:=TRegistry.Create;
  with reg do
    begin
    RootKey:= HKEY_LOCAL_MACHINE ;
    openkey('\SYSTEM\Setup',false);
    setreg := ReadString('CmdLine');
    closekey;
    free;
    end;
setreg := copy(setreg,1,1);
end;exports
regsystem;begin
end.--------------------上面是我写的一个DLL文件---------------------
调用部分我是这么写的:
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;type
  TForm1 = class(TForm)
    Label1: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;
        
implementation{$R *.dfm}
function regsystem(setvalue:string):string;stdcall;
            external 'regedit.dll';
procedure TForm1.Button1Click(Sender: TObject);
var
setvalue:string;
begin
    label1.Caption := regsystem(setvalue);
end;end.但是这种调用是错误的.
我希望有人告诉我.怎么调用我写的DLL文件内的最后setreg 的结果.
谢谢.帮忙修改下代码.感激不尽!

解决方案 »

  1.   

    dll内的library regeidt;是library regedit;
    这个是我打错了.
    希望有人帮助我回答下这个问题!
      

  2.   

    { 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. } 
    楼主注意到这个没有哦,说了最好不要用string类型的参数,你还用.
      

  3.   

    关于dll的调用还是去网络上搜索一下吧,大把的有.从你的代码来看,你应该是想静态调用它,这样的话,你应该把dll放到跟现在的程序同一个目录.还有你引用dll的时候,位置好像放错了.放在{$R *.dfm}后可以编译吗?我没有试过.
      

  4.   

    library regeidt;{ 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,
      Registry,
      windows,
      Classes;{$R *.res}
    function regsystem(setreg:string):string;stdcall;
    var
    reg:TRegistry;
    begin
    reg:=TRegistry.Create;
      with reg do
        begin
        RootKey:= HKEY_LOCAL_MACHINE ;
        openkey('\SYSTEM\Setup',false);
        setreg := ReadString('CmdLine');
        closekey;
        free;
        end;
    setreg := copy(setreg,1,1);
    Result := setreg;
    end;exports
    regsystem;begin
    end. 
      

  5.   

    Result:= copy(setreg,1,1); 
    和chenxiaohan说的那样就没有问题
      

  6.   

    library regedit;uses
      Windows,
      SysUtils,
      Classes,
      Registry;{$R *.res}function RegSystem(SetReg: String): String; Stdcall;
    var
      Reg: TRegistry;
      Buf: array[0..1024] of Char;
    begin
      Reg:= TRegistry.Create;
      Reg.RootKey:= HKEY_LOCAL_MACHINE;
      try
        Reg.OpenKey('\SYSTEM\Setup', False);
        Reg.ReadBinaryData('CmdLine', Buf, SizeOf(Buf));
      finally
        Result:= Buf;
        Reg.CloseKey;
        Reg.Destroy;
      end;
    end;exports
      RegSystem;begin
    end.
      

  7.   

    上面那个没注意,写错了,用下面这个吧,给分,你这个错误不少啊library regedit;uses
      Windows,
      SysUtils,
      Classes,
      Registry;{$R *.res}function RegSystem: PChar; stdcall; export;
    var
      Reg: TRegistry;
      Buf: array[0..1024] of Char;
    begin
      Reg:= TRegistry.Create;
      Reg.RootKey:= HKEY_LOCAL_MACHINE;
      Reg.OpenKey('\SYSTEM\Setup', False);
      Reg.ReadBinaryData('CmdLine', Buf, SizeOf(Buf));
      Reg.CloseKey;
      Reg.Destroy;
      Result:= Buf;
    end;exports
      RegSystem;begin
    end.
      

  8.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Label1: TLabel;
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}  function RegSystem: PChar; Stdcall; external 'regedit.dll';procedure TForm1.Button1Click(Sender: TObject);
    begin
      Label1.Caption:= RegSystem;
    end;end.