Conversion en .NET

cs_Safireo Messages postés 55 Date d'inscription lundi 17 mars 2003 Statut Membre Dernière intervention 24 février 2011 - 15 avril 2005 à 11:05
cs_Safireo Messages postés 55 Date d'inscription lundi 17 mars 2003 Statut Membre Dernière intervention 24 février 2011 - 16 avril 2005 à 14:01
slut tlm !
je cherche à terminer la conversion de ce code en .net.
j'arrive à éliminer les erreurs mais pas à faire fonctionner le logiciel qui doit me rendre le chemin d'accès du fichier joué par winamp

voici le code presque entièrement converti :
---------------------------------------------------------------------------------------

Option
Strict
Off


Option
Explicit
On


Imports VB = Microsoft.VisualBasic


Friend
Class Form1



Inherits System.Windows.Forms.Form


#
Region "Code généré par le Concepteur Windows Form "



Public
Sub
New()



MyBase.New()



If m_vb6FormDefInstance
Is
Nothing
Then



If m_InitializingDefInstance
Then


m_vb6FormDefInstance =
Me



Else



Try



'Pour le formulaire de démarrage, la première instance créée est l'instance par défaut.



If System.Reflection.Assembly.GetExecutingAssembly.EntryPoint.DeclaringType
Is
Me.GetType
Then


m_vb6FormDefInstance =
Me



End
If



Catch



End
Try



End
If



End
If



'Cet appel est requis par le Concepteur Windows Form.


InitializeComponent()



End
Sub



'La méthode substituée Dispose du formulaire pour nettoyer la liste des composants.



Protected
Overloads
Overrides
Sub Dispose(
ByVal Disposing
As
Boolean)



If Disposing
Then



If
Not components
Is
Nothing
Then


components.Dispose()



End
If



End
If



MyBase.Dispose(Disposing)



End
Sub



'Requis par le Concepteur Windows Form



Private components
As System.ComponentModel.IContainer



Public ToolTip1
As System.Windows.Forms.ToolTip



'REMARQUE : la procédure suivante est requise par le Concepteur Windows Form



'Il peut être modifié à l'aide du Concepteur Windows Form.



'Ne pas le modifier à l'aide de l'éditeur de code.


<System.Diagnostics.DebuggerStepThrough()>
Private
Sub InitializeComponent()



Me.components =
New System.ComponentModel.Container



Me.ToolTip1 =
New System.Windows.Forms.ToolTip(
Me.components)



'



'Form1



'



Me.AutoScaleBaseSize =
New System.Drawing.Size(5, 13)



Me.BackColor = System.Drawing.SystemColors.Control



Me.ClientSize =
New System.Drawing.Size(768, 46)



Me.Cursor = System.Windows.Forms.Cursors.Default



Me.Location =
New System.Drawing.Point(4, 30)



Me.Name = "Form1"



Me.RightToLeft = System.Windows.Forms.RightToLeft.No



Me.Text = "Form1"



End
Sub


#
End
Region


#
Region "Prise en charge de la mise à niveau "



Private
Shared m_vb6FormDefInstance
As Form1



Private
Shared m_InitializingDefInstance
As
Boolean



Public
Shared
Property DefInstance()
As Form1



Get



If m_vb6FormDefInstance
Is
Nothing
OrElse m_vb6FormDefInstance.IsDisposed
Then


m_InitializingDefInstance =
True


m_vb6FormDefInstance =
New Form1()


m_InitializingDefInstance =
False



End
If


DefInstance = m_vb6FormDefInstance



End
Get



Set


m_vb6FormDefInstance = Value



End
Set



End
Property


#
End
Region

'-----------------------------------------------------jusqu'ici, rien d'autre que le code généré qutomatiquement par vb.net



Private
Declare
Function FindWindow
Lib "user32"
Alias "FindWindowA"(
ByVal lpszClassName
As
String,
ByVal lpszWindowName
As
String)
As
Integer





Private
Declare
Function GetWindowThreadProcessId
Lib "user32" (
ByVal hwnd
As
Integer,
ByRef lpdwProcessId
As
Integer)
As
Integer





Private
Declare
Function OpenProcess
Lib "kernel32" (
ByVal dwDesiredAccess
As
Integer,
ByVal bInheritHandle
As
Integer,
ByVal dwProcessId
As
Integer)
As
Integer





Private
Const PROCESS_VM_READ
As
Integer = &H10s





Private
Declare
Function SendMessage
Lib "user32"
Alias "SendMessageA"(
ByVal hwnd
As
Integer,
ByVal msg
As
Integer,
ByVal wParam
As
Integer,
ByVal lParam
As
Integer)
As
Integer





Private
Const WM_USER
As
Integer = &H400s





Private
Const IPC_GETPLAYLISTFILE
As
Integer = 211





Private
Const IPC_GETLISTPOS
As
Integer = 125





'UPGRADE_ISSUE: La déclaration d'un paramètre 'As Any' n'est pas prise en charge. Cliquez ici : 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup1016"'



'Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As any, ByVal nSize As Integer, ByRef lpNumberOfBytesRead As Integer) As Integer



'j'ai remplacé la ligne précédente par les deux suivantes



Private
Declare
Function ReadProcessMemory
Lib "kernel32" (
ByVal hProcess
As
Integer,
ByVal lpBaseAddress
As
Integer,
ByRef lpBuffer
As
String,
ByVal nSize
As
Integer,
ByRef lpNumberOfBytesRead
As
Integer)
As
Integer



Private
Declare
Function ReadProcessMemorylong
Lib "kernel32" (
ByVal hProcess
As
Integer,
ByVal lpBaseAddress
As
Integer,
ByRef lpBuffer
As
Long,
ByVal nSize
As
Integer,
ByRef lpNumberOfBytesRead
As
Integer)
As
Integer



Private
Declare
Function CloseHandle
Lib "kernel32" (
ByVal hObject
As
Integer)
As
Integer





Private
Const MAX_PATH
As
Integer = 260





Private hWndWinamp
As
Integer





Private
Sub Form1_Click(
ByVal eventSender
As System.Object,
ByVal eventArgs
As System.EventArgs)
Handles
MyBase.Click



Me.Text = GetPlayingFileName



End
Sub





'quand j'appelle la fonction getwinampwindow, sans aucun message d'erreur, le programme se termine.

Private
Function GetWinampWindow()
As
Integer




GetWinampWindow = FindWindow("Winamp v1.x", vbNullString)





End
Function





Public
Function GetPlayingFileName()
As
String





'récupère le chemin d'accès du fichier joué par winamp





Dim strFileName
As
String





Dim lp, lpWinamp
As
Integer





Dim iIndex
As
Integer





Dim PID
As
Integer





Dim bRet
As
Integer





Dim dwRead
As
Integer





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)









'UPGRADE_ISSUE: La constante vbUnicode n'a pas été mise à niveau. Cliquez ici : 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2070"'


Temp = StrConv(System.Text.UnicodeEncoding.Unicode.GetString(Buffer), vbUnicode)



'le code vb6 était Temp = StrConv(Buffer, vbUnicode).





strFileName = VB.Left(Temp, InStr(Temp, Chr(0)) - 1)








GetPlayingFileName = strFileName









End
Function


---------------------------------------------------------------------------------------------

voici le code vb.6
--------------------------------------------------------------------------
Option Explicit


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 Const PROCESS_VM_READ As Long = &H10


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 Const WM_USER As Long = &H400


Private Const IPC_GETPLAYLISTFILE As Long = 211


Private Const IPC_GETLISTPOS As Long = 125


Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesRead As Long) As Long


Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Private Const MAX_PATH As Long = 260


Private hWndWinamp As Long


Private Sub Form_Click()
Me.Caption = GetPlayingFileName
End Sub


Private Function GetWinampWindow() As Long


GetWinampWindow = FindWindow("Winamp v1.x", vbNullString)


End Function


Public Function GetPlayingFileName() As String


'récupère le chemin d'accès du fichier joué par winamp


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

-------------------------------------------------------------------------------------
merci beaucoup pour votre aide
safireo

3 réponses

cboulas Messages postés 2641 Date d'inscription mercredi 2 juin 2004 Statut Membre Dernière intervention 8 janvier 2014 16
16 avril 2005 à 11:37
Whouaa, tu as utiliser le convertisseur MS.Net, c'est pas trop une bonne chose.
Je te conseil plutôt de réécrire ta source directement dans le .Net et de virer la référence :
microsoft.visualbasic qui deviens quasi inutile mais à laquelle tu pourra faire appel en le précisant comme dans l'exemple suivant :

microsoft.visualbasic.[fonction]

MS.Net offrant déjà pas mal de possibilité autant retirer l'inutile, dès que j'ai un petit moment je jèterais un zieux à ton code pour le simplifier.


Chris...
Web : Firstruner - eMail : [mailto:support@firstruner.com Support]&nbs
0
cboulas Messages postés 2641 Date d'inscription mercredi 2 juin 2004 Statut Membre Dernière intervention 8 janvier 2014 16
16 avril 2005 à 12:16
Re,

J'ai donc étudier ton script en essayant de l'ajuster à .Net :

Private 
Declare 
Function FindWindow 
Lib "user32" 
Alias "FindWindowA" (
ByVal lpszClassName 
As 
String, 
ByVal lpszWindowName 
As 
String) 
As 
Integer
Private 
Declare 
Function GetWindowThreadProcessId 
Lib "user32" (
ByVal hwnd 
As 
Integer, 
ByRef lpdwProcessId 
As 
Integer) 
As 
Integer
Private 
Declare 
Function OpenProcess 
Lib "kernel32" (
ByVal dwDesiredAccess 
As 
Integer, 
ByVal bInheritHandle 
As 
Integer, 
ByVal dwProcessId 
As 
Integer) 
As 
Integer
Private 
Declare 
Function SendMessage 
Lib "user32" 
Alias "SendMessageA" (
ByVal hwnd 
As 
Integer, 
ByVal msg 
As 
Integer, 
ByVal wParam 
As 
Integer, 
ByVal lParam 
As 
Integer) 
As 
Integer
Private 
Declare 
Function ReadProcessMemory 
Lib "kernel32" (
ByVal hProcess 
As 
Integer, 
ByVal lpBaseAddress 
As 
Integer, 
ByRef lpBuffer 
As 
String, 
ByVal nSize 
As 
Integer, 
ByRef lpNumberOfBytesRead 
As 
Integer) 
As 
Integer
Private 
Declare 
Function ReadProcessMemorylong 
Lib "kernel32" (
ByVal hProcess 
As 
Integer, 
ByVal lpBaseAddress 
As 
Integer, 
ByRef lpBuffer 
As 
Long, 
ByVal nSize 
As 
Integer, 
ByRef lpNumberOfBytesRead 
As 
Integer) 
As 
Integer
Private 
Declare 
Function CloseHandle 
Lib "kernel32" (
ByVal hObject 
As 
Integer) 
As 
Integer
Private 
Const WM_USER 
As 
Integer = &amp;H400S

Private 
Const IPC_GETPLAYLISTFILE 
As 
Integer = 211

Private 
Const IPC_GETLISTPOS 
As 
Integer = 125

Private 
Const PROCESS_VM_READ 
As 
Integer = &amp;H10S

Private 
Const MAX_PATH 
As 
Integer = 260

Private 
Sub Form1_Click(
ByVal eventSender 
As System.Object, 
ByVal eventArgs 
As System.EventArgs) 
Handles 
MyBase.Click

Me.Text = GetPlayingFileName

End 
Sub



Public 
Function GetPlayingFileName() 
As 
String
'r&#233;cup&#232;re le chemin d'acc&#232;s du fichier jou&#233; par winamp
Dim strFileName, Temp 
As 
String
Dim lp, lpWinamp, iIndex, PID, bRet, dwRead 
As 
Integer
Dim Buffer(MAX_PATH) 
As 
Byte
Dim hWndWinamp 
As 
Integer = FindWindow("BaseWindow_RootWnd", "Winamp")

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   'Endrois o&#249; VB sort &#224; chaque fois
   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 = System.Text.UnicodeEncoding.Unicode.GetString(Buffer).ToString



'Temp = System.Text.UnicodeEncoding.Convert(System.Text.UnicodeEncoding.Unicode.GetString(Buffer))
'le code vb6 &#233;tait Temp = StrConv(Buffer, vbUnicode).



Return Microsoft.VisualBasic.Left(Temp, Microsoft.VisualBasic.InStr(Temp, Microsoft.VisualBasic.Chr(0)) - 1)

End 
Function


Il y a encore d'autre ajustement à faire mais c'est déjà plus clair. pour cela j'ai ouvert un nouveau projet, y ai retirer la référence Microsoft.VisualBasic et y ai mis le code ci-dessus


Chris...
Web : Firstruner - eMail : [mailto:support@firstruner.com Support]&nbs
0
cs_Safireo Messages postés 55 Date d'inscription lundi 17 mars 2003 Statut Membre Dernière intervention 24 février 2011
16 avril 2005 à 14:01
très gentil de ta part de m'aider...
mais bon, c'est pas l'appel de cette vielle fonction Left qui empêche le code de tourner.
en fait, si j'ai utilisé le convertisseur c que j'ai un peu de mal pour me repérer dans l'univers formé par toutes ces obscures déclarations, alors quand il faut les convertir, les problèmes empirent...
bref, l'erreur venait bien de l'utilisation de la fonction findwindow : ta modification ne fait pas fermer l'application. néanmoins, la fonction renvoit une chaîne ""... ?
je me suis dit qu'il ne trouvait peut-être pas la fenêtre alors j'ai tenté

FindWindow("Winamp v1.x", 0)

ce qui est un peu plus proche du code original et évite également le plantage. mais bon toujours aucun résultat.
j'ai trouvé une page sur le site de winamp qui donne qq indications mais ça m'a l'air d'être du vb6 : http://forums.winamp.com/showthread.php?threadid=180297

bah voila, je continue à chercher à partir de tes modifications...
merci beaucoup pour ton aide
safireo
0
Rejoignez-nous