Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.4 KiB

  1. #define UNICODE
  2. #define _UNICODE
  3. #include <nt.h>
  4. #include <ntrtl.h>
  5. #include <nturtl.h>
  6. #include <windows.h>
  7. #define SHARE_ALL (FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE)
  8. int NumberOfLinks(char *FileName)
  9. {
  10. FILE_STANDARD_INFORMATION FileInfo;
  11. WCHAR FileNameBuf[MAX_PATH + 50];
  12. HANDLE FileHandle;
  13. NTSTATUS Status;
  14. IO_STATUS_BLOCK Iosb;
  15. OBJECT_ATTRIBUTES Obj;
  16. UNICODE_STRING uPrelimFileName,
  17. uFileName;
  18. RtlCreateUnicodeStringFromAsciiz(&uPrelimFileName, FileName);
  19. lstrcpy(FileNameBuf, L"\\DosDevices\\");
  20. lstrcat(FileNameBuf, uPrelimFileName.Buffer);
  21. RtlInitUnicodeString(&uFileName, FileNameBuf);
  22. InitializeObjectAttributes(&Obj, &uFileName, OBJ_CASE_INSENSITIVE, NULL, NULL);
  23. Status = NtOpenFile(&FileHandle, SYNCHRONIZE, &Obj, &Iosb,
  24. SHARE_ALL, FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT);
  25. if (!NT_SUCCESS(Status)) {
  26. SetLastError(RtlNtStatusToDosError(Status));
  27. return 0;
  28. }
  29. Status = NtQueryInformationFile(FileHandle, &Iosb, &FileInfo,
  30. sizeof(FileInfo), FileStandardInformation);
  31. NtClose(FileHandle);
  32. if (!NT_SUCCESS(Status)) {
  33. SetLastError(RtlNtStatusToDosError(Status));
  34. return 0;
  35. }
  36. return FileInfo.NumberOfLinks;
  37. }