ElektroStudios
ElektroStudios

Reputation: 20464

Which is the equivalent of this VB6 code to VBNET?

The purpose of this VB6 code is to return the current playing filename (not title) of WinAmp.

This is the lines I need to translate to VBNET:

Temp = StrConv(Buffer, vbUnicode)
strFileName = Left$(Temp, InStr(Temp, Chr$(0)) - 1)

Buffer is a Byte type, temp and strFileName are string types.

Also here:

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

...I've changed "ByRef lpBuffer As Any" to "ByRef lpBuffer As Byte" (I think I did the correct change there)

This is the full code:

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

UPDATE

This is the updated code following the suggestions, but it is failling in the try/catch line with an error: STARTINDEX CANNOT BE LESS THAN ZERO

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

Upvotes: 1

Views: 1746

Answers (2)

Andy G
Andy G

Reputation: 19367

The equivalent in VB.NET to converting to unicode with StrConv() is Encoding.Convert.

The equivalent to Left() is String.Substring, and to Instr() is String.IndexOf.

Added In answer to the error message, InStr() indexes from 1, not 0 (VB.NET). You'll need to make adjustments to such values.

Upvotes: 4

Hamster
Hamster

Reputation: 102

remember to import system.text

dim aux as UnicodeEncoding = new UnicodeEncoding
temp = aux.GetString(buffer)
strfilename = temp.substring(temp.indexof(cchar("0"))-1)

Upvotes: 1

Related Questions