PDA

View Full Version : سوال: تبدیل یک تابع از زبان vb به دلفی



Delphi7_love
چهارشنبه 10 شهریور 1389, 14:14 عصر
با سلام
یه تابع به زبان vb هست که سریال فیزیکی فلش رو میده
کسی میتونه به دلفی تبدیلش کنه ؟

Public Function GetSerialNumber(ByVal DriveLetter As String) As String
Dim wmi_ld, wmi_dp, wmi_dd As ManagementObject
Dim temp, parts(), ans As String

ans = ""
' get the Logical Disk for that drive letter
wmi_ld = New ManagementObject("Win32_LogicalDisk.DeviceID='" & _
DriveLetter.TrimEnd("\"c) & "'")
' get the associated DiskPartition
For Each wmi_dp In wmi_ld.GetRelated("Win32_DiskPartition")
' get the associated DiskDrive
For Each wmi_dd In wmi_dp.GetRelated("Win32_DiskDrive")
' the serial number is embedded in the PnPDeviceID
temp = wmi_dd("PnPDeviceID").ToString
If Not temp.StartsWith("USBSTOR") Then
Throw New ApplicationException(DriveLetter & " doesn't appear to be USB Device")
End If
parts = temp.Split("\&".ToCharArray)
' The serial number should be the next to the last element
ans = parts(parts.Length - 2)
Next
Next

Return ans
End Functionl number should be the next to the last element
ans = parts(parts.Length - 2)
Next
Next

Return ans
End Function

--------------------
اینم یه تابع دیگه که همون کارو انجام میده :
دوستان هرکس یاد داره لطفا کمک کنه

Public Function GetSerialNumber(ByVal DriveLetter As String) As String
Dim wmi_ld, wmi_dp, wmi_dd As ManagementObject
Dim temp, parts(), ans As String

ans = ""
' get the Logical Disk for that drive letter
wmi_ld = New ManagementObject("Win32_LogicalDisk.DeviceID='" & _
DriveLetter.TrimEnd("\"c) & "'")
' get the associated DiskPartition
For Each wmi_dp In wmi_ld.GetRelated("Win32_DiskPartition")
' get the associated DiskDrive
For Each wmi_dd In wmi_dp.GetRelated("Win32_DiskDrive")
' There is a bug in WinVista that corrupts some of the fields
' of the Win32_DiskDrive class if you instantiate the class via
' its primary key (as in the example above) and the device is
' a USB disk. Oh well... so we have go thru this extra step
Dim wmi As New ManagementClass("Win32_DiskDrive")
' loop thru all of the instances. This is silly, we shouldn't
' have to loop thru them all, when we know which one we want.
For Each obj As ManagementObject In wmi.GetInstances
' do the DeviceID fields match?
If obj("DeviceID").ToString = wmi_dd("DeviceID").ToString Then
' the serial number is embedded in the PnPDeviceID
temp = obj("PnPDeviceID").ToString
If Not temp.StartsWith("USBSTOR") Then
Throw New ApplicationException(DriveLetter & " doesn't appear to be USB Device")
End If
parts = temp.Split("\&".ToCharArray)
' The serial number should be the next to the last element
ans = parts(parts.Length - 2)
End If
Next
Next
Next

Return ans
End Function

mofrad
پنج شنبه 11 شهریور 1389, 02:00 صبح
سلام. فکر کنم این کار کنه:


function GetDiskVolSerialID( cDriveName : char ): cardinal;
var
VolumeSerialNumber: DWORD;
MaximumComponentLength: DWORD;
FileSystemFlags: DWORD;
diskdrive: string;
begin
diskdrive := cDriveName + ':\';
GetVolumeInformation(pchar(diskdrive),
nil,
0,
@VolumeSerialNumber,
MaximumComponentLength,
FileSystemFlags,
nil,
0);
result := VolumeSerialNumber;
end;
در ضمن در این لینک همین تالار (http://www.barnamenevis.org/forum/showthread.php?t=242660) بحث شده!

Felony
پنج شنبه 11 شهریور 1389, 06:11 صبح
سلام. فکر کنم این کار کنه:


function GetDiskVolSerialID( cDriveName : char ): cardinal;
var
VolumeSerialNumber: DWORD;
MaximumComponentLength: DWORD;
FileSystemFlags: DWORD;
diskdrive: string;
begin
diskdrive := cDriveName + ':\';
GetVolumeInformation(pchar(diskdrive),
nil,
0,
@VolumeSerialNumber,
MaximumComponentLength,
FileSystemFlags,
nil,
0);
result := VolumeSerialNumber;
end;
در ضمن در این لینک همین تالار (http://www.barnamenevis.org/forum/showthread.php?t=242660) بحث شده!
خیر این تابع سریال فیزیکی رو نمیده .

اگر مطمئن هستید کد بالا ( پست اول ) درست کار میکنه در مورد WMI و استفاده از اون تو دلفی تحقیق کنید .