Что является эквивалентом этого кода VB6 для VBNET?
Цель этого кода VB6 - вернуть текущее воспроизводимое имя файла (не заголовок) WinAmp.
Вот строки, которые мне нужно перевести на VBNET:
Temp = StrConv(Buffer, vbUnicode)
strFileName = Left$(Temp, InStr(Temp, Chr$(0)) - 1)
Buffer - это тип Byte, temp и strFileName - это строковые типы.
Также здесь:
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Byte, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long
... Я изменил "ByRef lpBuffer As Any" на "ByRef lpBuffer As Byte" (я думаю, что я сделал там правильное изменение)
Это полный код:
Public Class Form1
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpszClassName As String, ByVal lpszWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Byte, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const PROCESS_VM_READ As Long = &H10
Private Const WM_USER As Long = &H400
Private Const IPC_GETPLAYLISTFILE As Long = 211
Private Const IPC_GETLISTPOS As Long = 125
Private Const MAX_PATH As Long = 260
Private hWndWinamp As Long
Private Function GetWinampWindow() As Long
GetWinampWindow = FindWindow("Winamp v1.x", vbNullString)
End Function
Public Function GetPlayingFileName() As String
Dim strFileName As String
Dim lp As Long, lpWinamp As Long
Dim iIndex As Long
Dim PID As Long
Dim bRet As Long
Dim dwRead As Long
Dim Buffer(MAX_PATH) As Byte
Dim Temp As String
hWndWinamp = GetWinampWindow
If hWndWinamp = 0 Then
GetPlayingFileName = ""
Exit Function
End If
iIndex = SendMessage(hWndWinamp, WM_USER, 0, IPC_GETLISTPOS)
lp = SendMessage(hWndWinamp, WM_USER, iIndex, IPC_GETPLAYLISTFILE)
If lp = 0 Then
GetPlayingFileName = ""
Exit Function
End If
Call GetWindowThreadProcessId(hWndWinamp, PID)
lpWinamp = OpenProcess(PROCESS_VM_READ, 0, PID)
If lpWinamp = 0 Then
GetPlayingFileName = ""
Exit Function
End If
bRet = ReadProcessMemory(lpWinamp, lp, Buffer(0), MAX_PATH, dwRead)
Call CloseHandle(lpWinamp)
Temp = StrConv(Buffer, vbUnicode)
strFileName = Left$(Temp, InStr(Temp, Chr$(0)) - 1)
GetPlayingFileName = strFileName
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(GetPlayingFileName())
End Sub
End Class
ОБНОВИТЬ
Это обновленный код после предложений, но в строке try/catch происходит сбой с ошибкой: STARTINDEX НЕ МОЖЕТ БЫТЬ МЕНЕЕ, ЧЕМ НОЛЬ
Public Class Form1
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpszClassName As String, ByVal lpszWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, ByRef lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Byte, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Const PROCESS_VM_READ As Long = &H10
Private Const WM_USER As Long = &H400
Private Const IPC_GETPLAYLISTFILE As Long = 211
Private Const IPC_GETLISTPOS As Long = 125
Private Const MAX_PATH As Long = 260
Private hWndWinamp As Long
Private Function GetWinampWindow() As Long
GetWinampWindow = FindWindow("Winamp v1.x", vbNullString)
End Function
Public Function GetPlayingFileName() As String
Dim strFileName As String
Dim lp As Long, lpWinamp As Long
Dim iIndex As Long
Dim PID As Long
Dim bRet As Long
Dim dwRead As Long
Dim Buffer(MAX_PATH) As Byte
Dim Temp As String
hWndWinamp = GetWinampWindow()
If hWndWinamp = 0 Then
GetPlayingFileName = ""
Exit Function
End If
iIndex = SendMessage(hWndWinamp, WM_USER, 0, IPC_GETLISTPOS)
lp = SendMessage(hWndWinamp, WM_USER, iIndex, IPC_GETPLAYLISTFILE)
If lp = 0 Then
GetPlayingFileName = ""
Exit Function
End If
Call GetWindowThreadProcessId(hWndWinamp, PID)
lpWinamp = OpenProcess(PROCESS_VM_READ, 0, PID)
If lpWinamp = 0 Then
GetPlayingFileName = ""
Exit Function
End If
bRet = ReadProcessMemory(lpWinamp, lp, Buffer(0), MAX_PATH, dwRead)
Call CloseHandle(lpWinamp)
' Original VB6 code
'Temp = StrConv(Buffer, vbUnicode)
'strFileName = Left$(Temp, InStr(Temp, Chr$(0)) - 1)
Temp = System.Text.UnicodeEncoding.Unicode.GetString(Buffer)
Try
strFileName = Temp.Substring(Temp.IndexOf(CChar("0")) - 1)
Catch ex As Exception
MsgBox(ex.Message)
End Try
GetPlayingFileName = strFileName
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MsgBox(GetPlayingFileName())
End Sub
End Class
2 ответа
Не забудьте импортировать system.text
dim aux as UnicodeEncoding = new UnicodeEncoding
temp = aux.GetString(buffer)
strfilename = temp.substring(temp.indexof(cchar("0"))-1)
Эквивалент в VB.NET для преобразования в Unicode с StrConv()
является Encoding.Convert.
Эквивалентно Left()
является String.Substring, и для Instr()
является String.IndexOf.
В ответ на сообщение об ошибке добавлен индекс InStr() от 1, а не от 0 (VB.NET). Вам нужно будет внести коррективы в такие значения.