::: 델파이 Tip&Trick :::

델파이 Tip&Trick 성격에 맞지 않는 광고,비방,질문의 글은 즉시 삭제하며
내용을 복사하여 사용할 경우 반드시 이곳(http://www.howto.pe.kr)을 출처로 명시하여 주세요


Category

  김영대(2003-03-07 22:13:07, Hit : 5135, Vote : 1253
 윈도우즈 공유폴더 리스트 얻기

unit GetShare;
(******************************************************************************
The following code is pieced together from a bunch of sources, and you're free
to use it however you want. I don't take responsibility for anything, however.
******************************************************************************)

interface

uses windows,sysutils,classes;

{This procedure will fill "List" (which you supply) with a list of all local
disk drives and CDROMs that this computer has shared with the network. You
could pass these to GetLocalShareInfo, below.}

procedure GetLocalShareNames(List: TStringList);


{This will convert a Local UNC file ID to a Local file ID. Example:
    Local machine name  : dan
    C: drive share name : cdrive
    sLocalUNCFileID     : \dancdriveautoexec.bat
    Result              : c:autoexec.bat
If the passed file file ID can't be converted, it is returned without modification.}

function ConvertLocalUNCFileIDToLocalFileID(sLocalUNCFileID: string): string;


{This function will convert a local share name to a drive letter, and return
other information also. Example:
    C: drive share name : cdrive
    sLocalShareName     : cdrive
    Result sPath        : "c:"}
function GetLocalShareInfo(sLocalShareName: string; var

sPath,sRemark,sRWPassword,sROPassword: string) : boolean;


{encapsulation of the windows API}

function GetComputerNameStr: shortstring;


{just a little easier to use this}

function IsWindowsNT: boolean;

implementation

const
  SHI50F_RDONLY           = $0001;
  SHI50F_FULL             = $0002;
  SHI50F_DEPENDSON        = SHI50F_RDONLY OR SHI50F_FULL;
  SHI50F_ACCESSMASK       = SHI50F_RDONLY OR SHI50F_FULL;
  SHI50F_PERSIST          = $0100;
  SHI50F_SYSTEM           = $0200;
  STYPE_DISKTREE          = 0;
  SHI_USES_UNLIMITED      = DWORD(-1);
  MB_ERR_INVALID_CHARS    = $00000008;

type
  u_char = Char;
  u_short = Word;
  u_int = Integer;
  u_long = Longint;
  NET_API_STATUS = DWORD;

  share_info_50 = record
    shi50_netname       : array[1..13] of char;
    shi50_type          : u_char;
    shi50_flags         : u_short;
    shi50_remark        : pchar;
    shi50_path          : pchar;
    shi50_rw_password   : array[1..9] of char;
    shi50_ro_password   : array[1..9] of char;
    szWhatever          : array[1..256] of char;
  end; {share_info_50}

  share_info_2  = record
    shi2_netname        : pwidechar;
    shi2_type           : dword;
    shi2_remark         : pwidechar;
    shi2_permissions    : dword;
    shi2_max_uses       : dword;
    shi2_current_uses   : dword;
    shi2_path           : pwidechar;
    shi2_passwd         : pwidechar;
  end; {_SHARE_INFO_2}
  pshare_info_2 = ^share_info_2;

  TNetShareGetInfo_SVRAPI   = function(servername : LPSTR;
                                       netname    : LPSTR;
                                       level      : DWORD;
                                       var buf;
                                       buflen     : DWORD;
                                       var totalavail: DWORD):
NET_API_STATUS; stdcall;
  TNetShareGetInfo_NETAPI32 = function(servername : LPSTR;
                                       netname    : pwidechar;
                                       level      : DWORD;
                                       var buf): NET_API_STATUS; stdcall;

function GetComputerNameStr: shortstring;
var
  dwSize : dword;
begin
  dwSize := sizeof(Result)-2;
  GetComputerName(@Result[1],dwSize);
  Result[0] := char(dwSize);
end; {GetComputerNameStr}

function IsWindowsNT: boolean;
var
  osvi: TOSVersionInfo;
begin
  fillchar(osvi,sizeof(osvi),0);
  osvi.dwOSVersionInfoSize := sizeof(osvi);
  if not GetVersionEx(osvi) then
    raise Exception.Create('Error attempting to retrieve VersionInfo');
  Result := osvi.dwPlatformId = VER_PLATFORM_WIN32_NT;
end; {IsWindowsNT}

procedure GetLocalShareNames(List: TStringList);
type
  PNetResourceArray = ^TNetResourceArray;
  TNetResourceArray = array[0..maxint div sizeof(TNetResource)-1] of TNetResource;
var
  dw: dword;
  x,
  Count,
  BufSize,
  Size,
  NetResult: DWORD;
  NetHandle: THandle;
  NetResources: PNetResourceArray;
  sComputerName: string;
begin
  {clear the result list}
  List.Clear;

  {alloc some storage -- if this isn't enough, we'll re-alloc below}
  BufSize := 10*sizeof(TNetResource);
  GetMem(NetResources,BufSize);
  try
    {get the Computer Name}
    dw := 255;
    SetLength(sComputerName, dw);
    if not GetComputerName(PCHAR(sComputerName), dw) then
      raise exception.create('GetComputerName failed');
    sComputerName := '\'+sComputerName;

    {initialize the resource array -- for the call to WNetOpenEnum, we will put
     some information in the first item of the array and use this as input to the function}
    fillchar(NetResources^, BufSize, 0);
    NetResources^[0].lpRemoteName := PCHAR(sComputerName);
    NetResources^[0].dwUsage := RESOURCEUSAGE_CONTAINER;
    NetResources^[0].dwScope := RESOURCE_GLOBALNET;
    NetResources^[0].dwType  := RESOURCETYPE_DISK;

    {start the enumeration process}
    if WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK, 0, PNetResourceA(NetResources), NetHandle) <> NO_ERROR then
      raise Exception.Create('WNetOpenEnum failed');

    try
      {Now we enumerate until we're all done
       NOTE: There's a CONTINUE in this code}
      while true do
      begin
        {setup and call the Enumeration function}
        Count := DWORD(-1);
        Size := BufSize;
        NetResult := WNetEnumResource(NetHandle, Count, NetResources, Size);

        {if the function tells us there's more data available, we re-allocate our storage and call it again}
        if NetResult = ERROR_MORE_DATA then
        begin
          BufSize := Size;
          ReallocMem(NetResources, BufSize);
          CONTINUE;
        end;

        {are we done?}
        if NetResult <> NO_ERROR then
          exit;

        {pull out each share name}
        for x := 0 to Count-1 do
          List.Add(NetResources^[x].lpRemoteName);
      end; {of while}
    finally
      WNetCloseEnum(NetHandle);
    end;
  finally
    FreeMem(NetResources, BufSize);
  end;
end; {GetLocalShareNames}

{this is how you do it for Windows 95}
function W95GetShareInfo(sLocalShareName: string; var sPath,sRemark,sRWPassword,sROPassword: string): boolean;
var
  shi50: share_info_50;
  dwSize: dword;
  hModule: THandle;
  NetShareGetInfo: TNetShareGetInfo_SVRAPI;
begin
  Result := false;
  dwSize := sizeof(share_info_50);
  hModule := LoadLibrary('SVRAPI.DLL');
  if hModule = 0 then
    raise Exception.Create('Could not LoadLibrary(''SVRAPI.DLL'')');
  try
    @NetShareGetInfo := GetProcAddress(hModule, 'NetShareGetInfo');
    if not Assigned(NetShareGetInfo) then
      raise Exception.Create('Could not GetProcAddress(''NetShareGetInfo'') in SVRAPI.DLL');
    sLocalShareName := UpperCase(sLocalShareName);
    if NetShareGetInfo(nil, pchar(sLocalShareName), 50, shi50, dwSize, dwSize) = 0 then
    begin
      sPath := StrPas(shi50.shi50_path);
      sRemark := StrPas(shi50.shi50_remark);
      sRWPassword := StrPas(@shi50.shi50_rw_password);
      sROPassword := StrPas(@shi50.shi50_ro_password);
      Result := true;
    end;
  finally
    FreeLibrary(hModule);
  end; {try..finally}
end; {W95GetShareInfo}

{this is how you do it for NT}
function WNTGetShareInfo(sLocalShareName: string; var
sPath,sRemark,sRWPassword,sROPassword: string): boolean;
var
  shi2: share_info_2;
  pShi2: pshare_info_2;
  hModule: THandle;
  NetShareGetInfo: TNetShareGetInfo_NETAPI32;
  bUnicodeShare,
  bUnicodePath,
  bUnicodeRemark,
  bUnicodeRWPassword: pwidechar;
  need: Integer;
begin
  bUnicodeShare := nil;
  bUnicodePath := nil;
  hModule := 0;
  Result := false;
  try

    {allocate storage for unicode (double-byte) string}
    bUnicodeShare := GlobalAllocPtr(GHND, (length(sLocalShareName)*2)+2);
    if bUnicodeShare = nil then
      raise Exception.Create('Out of memory');
    bUnicodePath := GlobalAllocPtr(GHND,(256*2)*3);
    bUnicodeRemark := bUnicodePath + 512;
    bUnicodeRWPassword := bUnicodeRemark + 512;

    // Convert sharename to Unicode
    sLocalShareName := UpperCase(sLocalShareName);
    MultiByteToWideChar(CP_ACP, MB_ERR_INVALID_CHARS,
                        pchar(sLocalShareName), length(sLocalShareName),
                        bUnicodeShare, (length(sLocalShareName)*2)+2);

    fillchar(shi2,sizeof(shi2),0);
    shi2.shi2_netname := bUnicodeShare;
    shi2.shi2_remark := bUnicodeRemark;
    shi2.shi2_passwd := bUnicodeRWPassword;
    shi2.shi2_path := bUnicodePath;
    sROPassword := '';

    hModule := LoadLibrary('NETAPI32.DLL');
    if hModule = 0 then
      raise Exception.Create('Could not LoadLibrary(''NETAPI32.DLL'')');

    @NetShareGetInfo := GetProcAddress(hModule,'NetShareGetInfo');
    if not Assigned(NetShareGetInfo) then
      raise Exception.Create('Could not GetProcAddress(''NetShareGetInfo'' in NETAPI32.DLL');
    if NetShareGetInfo(nil, bUnicodeShare, 2, shi2) = 0 then
    begin
      {This is weird...the netname member points to SHARE_INFO_2 instead of a
       name as expected. By casting a pointer, the whole thing works}
      pShi2 := pshare_info_2(shi2.shi2_netname);

      need :=
        WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
                            pShi2^.shi2_remark, -1,
                            nil, 0,
                            nil, nil);
      SetLength(sRemark, need);
      WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
                          pShi2^.shi2_remark, -1,
                          pchar(sRemark), length(sRemark),
                          nil, nil);

      need :=
        WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
                            pShi2^.shi2_path, -1,
                            nil, 0,
                            nil, nil);
      SetLength(sPath, need);
      WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
                          pShi2^.shi2_path, -1,
                          pchar(sPath), need,
                          nil, nil);

      need :=
        WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
                            pShi2^.shi2_passwd, -1,
                            nil, 0,
                            nil, nil);
      SetLength(sRWPassword, need);
      WideCharToMultiByte(CP_ACP, WC_COMPOSITECHECK,
                          pShi2^.shi2_passwd, -1,
                          pchar(sRWPassword), length(sRWPassword),
                          nil, nil);
      Result := true;
    end;
  finally
    if hModule <> 0 then
      FreeLibrary(hModule);
    if bUnicodeShare <> nil then
      GlobalFreePtr(bUnicodeShare);
    if bUnicodePath <> nil then
      GlobalFreePtr(bUnicodePath);
  end; {try..finally}
end; {WNTGetShareInfo}

function GetLocalShareInfo(sLocalShareName: string; var
sPath,sRemark,sRWPassword,sROPassword: string) : boolean;
begin
  if IsWindowsNT then
    Result := WNTGetShareInfo(sLocalShareName, sPath, sRemark, sRWPassword, sROPassword)
  else
    Result := W95GetShareInfo(sLocalShareName, sPath, sRemark, sRWPassword, sROPassword);
end; {ConvertLocalUNCFileIDToLocalFileID}

function ConvertLocalUNCFileIDToLocalFileID(sLocalUNCFileID: string): string;
var
  n: integer;
  s,
  sShareName,
  sPath,
  sRemark,
  sRWPassword,
  sROPassword: string;
begin
  Result := sLocalUNCFileID;

  {does it start with "\"?}
  if (length(sLocalUNCFileID) < 4) or (copy(sLocalUNCFileID, 1, 2) <> '\') then
    exit;

  {work with a copy}
  s := sLocalUNCFileID;

  {delete the "\"}
  delete(s, 1, 2);

  {find the next ""}
  n := pos('', s);
  if n < 2 then
    exit;

  {make sure the server portion of the file ID is our local computer}
  if CompareText(copy(s, 1, n-1),GetComputerNameStr) <> 0 then
    exit;

  {delete the computer name portion}
  delete(s, 1, n);

  {find the trailing backslash, if any -- it is legal to pass "\danvol1"}
  n := pos('', s);
  if n < 1 then
    n := length(s) + 1;

  {pull out the share name}
  sShareName := copy(s, 1, n-1);

  {and remove it from the string}
  delete(s, 1, n);

  {using this share name, get the local path if possible}
  if (GetLocalShareInfo(sShareName, sPath, sRemark, sRWPassword, sROPassword))
      and (sPath <> '') then
    {return the local drive letter plus the file ID}
    Result := sPath + s;
end; {ConvertLocalUNCFileIDToLocalFileID}


end.





691   [일반/컴포넌트] 델파이 에디트에서 '}', ']', ')' 의 시작점을 찾아주는 단축키  구창민 2003/03/14 4114 1011
690   [일반/컴포넌트] 레지스트리를 사용하는 사용자 함수 만들어 보기  구창민 2003/03/14 4596 1034
689   [데이터베이스] DB에서 자음(ㄱ,ㄴ,ㄷ....)으로 SELECT 하기  김영대 2003/03/12 5015 1170
688   [일반/컴포넌트] TreeView 노드조회  신훈재 2003/03/11 5578 1058
687   [일반/컴포넌트] 델파이안에 익스플로러 삽입  신훈재 2003/03/10 5762 1024
686   [윈도우즈 API] 화면 캡쳐하여 JPEG로 저장  신훈재 2003/03/10 5045 1006
685   [윈도우즈 API] 휴지통에 파일을 버리는 법  신훈재 2003/03/10 4386 1137
684   [윈도우즈 API] treeview 에 있는 내용을 출력하기  신훈재 2003/03/10 4398 1031
683   [윈도우즈 API] TreeView에 Data 사용법  신훈재 2003/03/10 4527 1043
682   [시스템] 시스템 고유의 ID 를 생성하려면...  김영대 2003/03/07 6359 1416
681   [일반/컴포넌트] TEdit의 Text를 left, center, right 정렬하기  김영대 2003/03/07 4486 1059
680   [윈도우즈 API] 마우스 위치의 콘트롤(콤포넌트)을 조사하기  김영대 2003/03/07 4247 1061
679   [네트웍/인터넷] Sending Raw IP Packets  김영대 2003/03/07 6162 1199
678   [네트웍/인터넷] 모든 TCP/IP interfaces 의 IP/Netmask 구하기  김영대 2003/03/07 5320 1177
677   [일반/컴포넌트] Outlook Express 설치여부 알아내기  김영대 2003/03/07 4745 1074
676   [네트웍/인터넷] HTTP 프로토콜을 이용하여 파일 다운받기  김영대 2003/03/07 6048 1581
675   [시스템] Windows 9x/ME/NT/2000/XP 버전 구별  김영대 2003/03/07 4473 1199
674   [시스템] mutex를 이용한 중복 실행 방지  김영대 2003/03/07 6441 1453
673   [네트웍/인터넷] HTTP로 파일 다운받기 (WinInet 이용)  김영대 2003/03/07 6020 1547
672   [일반/컴포넌트] TStringGrid 인쇄하기  김영대 2003/03/07 4623 1120
  [시스템] 윈도우즈 공유폴더 리스트 얻기  김영대 2003/03/07 5135 1253
670   [네트웍/인터넷] HTTP로 파일 다운받기2 (WinInet 이용)  김영대 2003/03/07 6405 1598
669   [윈도우즈 API] IE 의 즐겨찾기 읽기  김영대 2003/03/07 5479 1432
668   [일반/컴포넌트] Convert UNIX, TDateTime  김영대 2003/03/07 6010 1537
667   [윈도우즈 API] Task bar 에 나타나지 않는 프로그램 만들기  김영대 2003/03/07 5635 1436
666   [데이터베이스] how to get records from Oracle StoredProcedure  김영대 2003/03/07 4276 1107
665   [일반/컴포넌트] 메인메뉴가 팝업될때 이벤트를 탈수있는방법  김영대 2003/03/07 6352 1658
664   [일반/컴포넌트] System Menu 의 닫기 메뉴 disable 시키기  김영대 2003/03/07 3555 955
663   [일반/컴포넌트] StringGrid - multiline text  김영대 2003/03/07 5761 1346
662   [일반/컴포넌트] TStringGrid 인쇄하기  김영대 2003/03/07 5414 1344
661   [일반/컴포넌트] RichEdit 에 tab 설정하기  김영대 2003/03/07 4006 1096
660   [시스템] Hooking methods (Application.OnMessage)  김영대 2003/03/07 6647 1467
659   [시스템] WndProc for TComonent  김영대 2003/03/07 4720 1296
658   [일반/컴포넌트] IE(Internet Explorer) toolbar 에 버튼 올리기  김영대 2003/03/07 4090 1047
657   [시스템] IP address 바꾸기  김영대 2003/03/07 5338 1333
656   [시스템] CPU 이름 구하기  김영대 2003/03/07 5106 1305
655   [시스템] process 가 사용한 메모리 구하기  김영대 2003/03/07 4546 1192
654   [시스템] ROM-BIOS 정보 읽기  김영대 2003/03/07 5109 1200
653   [시스템] Shutdown/Reboot/Logoff Windows 9x/NT/Me/2000 ?  김영대 2003/03/07 4893 1307
652   [일반/컴포넌트] 커서(캐럿)가 line 위에 있는지 검사하기  김영대 2003/03/07 3958 1035

[1][2][3][4][5][6][7] 8 [9][10]..[25] [다음 10개]
 

Copyright 1999-2019 Zeroboard / skin by zero