现在的位置: 首页 > 综合 > 正文

关于共享 DELPHI

2017年09月08日 ⁄ 综合 ⁄ 共 2796字 ⁄ 字号 评论关闭

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, accctrl;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  TSHARE_INFO_502 = record
      shi502_netname: PWideChar;
      shi502_type: DWORD;
      shi502_remark: PWideChar;
      shi502_permissions: DWORD;
      shi502_max_uses: DWORD;
      shi502_current_uses: DWORD;
      shi502_path: PWideChar;
      shi502_passwd: PWideChar;
      shi502_reserved: DWORD;
      shi502_security_descriptor: PSECURITY_DESCRIPTOR;
  end;

  Function NetShareAdd(servername:Widestring;level: DWORD;buf: PBYTE;VAR parm_err: PDWORD ):DWORD;stdcall;
  function NetShareDel(ServerName: PWideChar;NetName: PWideChar;Reserved: DWord): DWord; stdcall;
var
  Form1: TForm1;

implementation

{$R *.dfm}
function NetShareAdd; external 'netapi32.DLL' name 'NetShareAdd';
function NetShareDel; external 'netapi32.DLL' name 'NetShareDel';

Procedure ShareDir(sDirName,sShareName:String);
var
  ServerName:pchar;
  si: TSHARE_INFO_502;
  parm_err:PDWORD;
  ret:DWORD;
  Function GetLocalHost:String;  //--获取计算机名
  var
    ComputerName: array[0..MAX_COMPUTERNAME_LENGTH+1] of char;  // holds the name
    Size: DWORD;                                              // holds the size
  begin
    Size := MAX_COMPUTERNAME_LENGTH+1;
    GetComputerName(ComputerName,siZe);
    Result:=ComputerName;
  end;

begin

  if DirectoryExists(sDirName) then
  begin
    si.shi502_netname :=PWideChar(WideString(sShareName));//   (共享名)
    si.shi502_type := 0;  //STYPE_DISKTREE
    si.shi502_remark := nil;
    si.shi502_max_uses := $FFFFFFFF;
    si.shi502_current_uses := 10;
    si.shi502_path := PWideChar(WideString(sDirName));//   (原路径)
    si.shi502_passwd := nil;
    si.shi502_reserved := 0;
    si.shi502_security_descriptor := nil;
    si.shi502_permissions := TRUSTEE_ACCESS_READ;
    ServerName:=stralloc(200);
    strpcopy(ServerName,GetLocalHost);//   ('vodserver'是机器名)
    try

      ret := NetShareAdd(ServerName, 502, @si, parm_err );
      case ret of
        ERROR_ACCESS_DENIED:ShowMessage('ERROR_ACCESS_DENIED');
        ERROR_INVALID_LEVEL:ShowMessage('ERROR_INVALID_LEVEL');
        ERROR_INVALID_NAME:ShowMessage('ERROR_INVALID_NAME');
        ERROR_INVALID_PARAMETER:ShowMessage('ERROR_INVALID_PARAMETER');
      end;

    Finally
      strdispose(ServerName);
    end;
  end;
end;

function DelShareDir(sDirName: String): integer;
var
  p: PChar;
  size: Cardinal;
begin
  size := MAX_COMPUTERNAME_LENGTH + 1;
  GetMem(p,size);
  GetComputerName(p,size);
  Result := NetShareDel(StringToOleStr(p),StringToOleStr(sDirName),0);
  FreeMem(p);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShareDir(Edit2.Text,Edit1.Text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  DelShareDir(Edit1.Text);
end;

end. 

【上篇】
【下篇】

抱歉!评论已关闭.