Thema: Delphi Daten freigeben

Einzelnen Beitrag anzeigen

N47R0N

Registriert seit: 26. Mai 2005
117 Beiträge
 
#5

Re: Daten freigeben

  Alt 25. Jul 2005, 03:27
Ich habe mir deinen vorschlag zu herzen genommen und das ist dabei rausgekommen :

Habe in einem Post von Rai diesen code gefunden

Delphi-Quellcode:
unit LANShare;

interface

uses Windows, Sysutils ;

  procedure shareFldr(path,netname,password:String;hidden:Boolean);
  procedure UnshareFldr(netname:String;hidden:Boolean);

  Type LMSTR = Windows.LPWSTR;
    LPBYTE = Windows.PBYTE;
    NET_API_STATUS = DWORD;

  function NetShareAdd(servername: LMSTR; level: DWORD; buf: LPBYTE; parm_err: LPDWORD): NET_API_STATUS; stdcall;
  function NetShareDel(servername, netname: LMSTR; reserved: DWORD): NET_API_STATUS; stdcall;

  type
    LPSHARE_INFO_2 = ^SHARE_INFO_2;
    PSHARE_INFO_2 = ^SHARE_INFO_2;
    _SHARE_INFO_2 = record
      shi2_netname: LMSTR;
      shi2_type: DWORD;
      shi2_remark: LMSTR;
      shi2_permissions: DWORD;
      shi2_max_uses: DWORD;
      shi2_current_uses: DWORD;
      shi2_path: LMSTR;
      shi2_passwd: LMSTR;
    end;
    SHARE_INFO_2 = _SHARE_INFO_2;
    TShareInfo2 = SHARE_INFO_2;
    PShareInfo2 = PSHARE_INFO_2;

    LPSHARE_INFO_50 = ^SHARE_INFO_50;
    PSHARE_INFO_50 = ^SHARE_INFO_50;
    _SHARE_INFO_50 = record
       shi50_netname: array [0..12] of Char;
       shi50_type: Byte;
       shi50_flags: Word;
       shi50_remark: PChar;
       shi50_path: PChar;
       shi50_rw_password: array [0..8] of Char;
       shi50_ro_password: array [0..8] of Char;
    end;
    SHARE_INFO_50 = _SHARE_INFO_50;
    TShareInfo50 = SHARE_INFO_50;
    PShareInfo50 = PSHARE_INFO_50;

const

// share_info_2.permissions
  ACCESS_READ = $01;
  ACCESS_WRITE = $02;
  ACCESS_CREATE = $04;
  ACCESS_EXEC = $08;
  ACCESS_DELETE = $10;
  ACCESS_ATRIB = $20;
  ACCESS_PERM = $40;

  ACCESS_GROUP = $8000;

  ACCESS_NONE = 0;
  ACCESS_ALL = (ACCESS_READ or ACCESS_WRITE or ACCESS_CREATE or ACCESS_EXEC or ACCESS_DELETE or ACCESS_ATRIB or ACCESS_PERM);

// parm_err
  SHARE_NETNAME_PARMNUM = 1;
  SHARE_TYPE_PARMNUM = 3;
  SHARE_REMARK_PARMNUM = 4;
  SHARE_PERMISSIONS_PARMNUM = 5;
  SHARE_MAX_USES_PARMNUM = 6;
  SHARE_CURRENT_USES_PARMNUM = 7;
  SHARE_PATH_PARMNUM = 8;
  SHARE_PASSWD_PARMNUM = 9;
  SHARE_FILE_SD_PARMNUM = 501;

// share_info_2.shi2_type and share_info_50.shi50_type
  STYPE_DISKTREE = 0;
  STYPE_PRINTQ = 1;
  STYPE_DEVICE = 2;
  STYPE_IPC = 3;

  STYPE_TEMPORARY = $40000000; // share_info_2 only
//STYPE_TEMPORARY = $40; // share_info_50 only
  STYPE_SPECIAL = DWORD($80000000); // share_info_2 only
//STYPE_SPECIAL = $80; // share_info_50 only

// share_info_50.shi50_flags
  SHI50F_RDONLY = 1;
  SHI50F_FULL = 2;
  SHI50F_ACCESSMASK = (SHI50F_RDONLY or SHI50F_FULL);
  SHI50F_PERSIST = $0100;
  SHI50F_SYSTEM = $0200;

// share_info_2.max_uses
  SHI_USES_UNLIMITED = DWORD(-1);

implementation

function NetApi32 : String ;
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then Result:='netapi32.dllelse Result:='Svrapi.dll';
end;

function GetModuleHandle(lpModuleName: LPCSTR): HMODULE; stdcall; external kernel32 name 'GetModuleHandleA';
function LoadLibrary(lpLibFileName: LPCSTR): HMODULE; stdcall; external kernel32 name 'LoadLibraryA';
function GetProcAddress(hModule: HMODULE; lpProcName: LPCSTR): FARPROC; stdcall; external kernel32 name 'GetProcAddress';

procedure GetProcedureAddress(var P: Pointer; const ModuleName, ProcName: string);
var
  ModuleHandle: HMODULE;
begin
  if not Assigned(P) then
  begin
    ModuleHandle := GetModuleHandle(PChar(ModuleName));
    if ModuleHandle = 0 then
    begin
      ModuleHandle := LoadLibrary(PChar(ModuleName));
      if ModuleHandle = 0 then raise Exception.Create('Library not found: ' + ModuleName);
    end;
    P := GetProcAddress(ModuleHandle, PChar(ProcName));
    if not Assigned(P) then raise Exception.Create('Function not found: ' + ModuleName + '.' + ProcName);
  end;
end;

var
  _NetShareAdd: Pointer;

function NetShareAdd;
begin
  GetProcedureAddress(_NetShareAdd, netapi32, 'NetShareAdd');
  asm
    mov esp, ebp
    pop ebp
    jmp [_NetShareAdd]
  end;
end;


var
  _NetShareDel: Pointer;

function NetShareDel;
begin
  GetProcedureAddress(_NetShareDel, netapi32, 'NetShareDel');
  asm
    mov esp, ebp
    pop ebp
    jmp [_NetShareDel]
  end;
end;


procedure shareFldr(path,netname,password:String;Hidden:Boolean);
var
share: TShareInfo2;
share50: TShareInfo50;
i:integer;
begin
  while path[Length(path)] in ['\','/']
  do path:=copy(path,0,length(path)-1);
     case Win32Platform of
       VER_PLATFORM_WIN32_NT:
         begin
           if hidden then netname:=netname+'$';
           Share.shi2_netname:=PWideChar(@((Widestring(netname))[1]));
           share.shi2_type:=STYPE_DISKTREE;
           share.shi2_permissions:=ACCESS_ALL;
           Share.shi2_remark:='';
           share.shi2_max_uses:=DWORD(-1);
           share.shi2_current_uses:=0;
           if length(password)>0
           then share.shi2_passwd:=PWideChar(@((Widestring(password))[1]))
           else share.shi2_passwd:=nil;
           share.shi2_path:=PWideChar(@((Widestring(path))[1]));
           NetShareAdd(nil,2,@Share,nil);
         end;
       VER_PLATFORM_WIN32_WINDOWS:
         begin
           for i:=1 to 7 do share50.shi50_netname[i]:=netname[i];
           share50.shi50_type:=STYPE_DISKTREE;
           if hidden then share50.shi50_flags:=SHI50F_SYSTEM else share50.shi50_flags:=SHI50F_FULL;
           share50.shi50_remark:='';
           share50.shi50_rw_password:='';
           share50.shi50_ro_password:='';
           if length(password)>0 then for i:=1 to 7 do share50.shi50_rw_password[i]:=password[i];
           share50.shi50_path:=PChar(@((UpperCase(path))[1]));
           NetShareAdd(nil,50,@share50,nil);
         end;
       else raise Exception.Create('Das Betriebssystem wurde nicht erkannt. Freigabe nicht möglich.');
     end;
end;

procedure UnshareFldr(netname:String;hidden:Boolean);
begin
  if (hidden) and (Win32Platform = VER_PLATFORM_WIN32_NT) then netname:=netname+'$';
  NetShareDel(nil,PWideChar(@((WideString(netname))[1])),0);
end;

end.
Nur eine Frage hät ich da noch , wie es scheint ist dies nur für LAN Freigaben zuständig
oder funktioniert es auch im Internet??
  Mit Zitat antworten Zitat