Api win32 facile

Description

petit exemple d'utilisation des api

j'ai utilisé P/Invoke Interop Assistant pour les quelques fonctions démontrées

Source / Exemple :


Imports System.IO
Imports Shell32
Public Class Form1
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="ShellAboutW")> _
      Public Shared Function ShellAboutW(ByVal hWnd As System.IntPtr, <System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal szApp As String, <System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal szOtherStuff As String, ByVal hIcon As System.IntPtr) As Integer
    End Function

    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="SHFormatDrive")> _
       Public Shared Function SHFormatDrive(ByVal hwnd As System.IntPtr, ByVal drive As UInteger, ByVal fmtID As UInteger, ByVal options As UInteger) As UInteger
    End Function
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="SHFindFiles")> _
          Public Shared Function SHFindFiles(ByVal pidlFolder As String, ByVal pidlSaveFile As UInteger) As Boolean
    End Function
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="SHGetDiskFreeSpaceExW")> _
       Public Shared Function SHGetDiskFreeSpaceExW(<System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal pszVolume As String, ByRef pqwFreeCaller As ULong, ByRef pqwTot As ULong, ByRef pqwFree As ULong) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    End Function

    <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="Beep")> _
         Public Shared Function Beep(ByVal dwFreq As UInteger, ByVal dwDuration As UInteger) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    End Function

    <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="IsDebuggerPresent")> _
      Public Shared Function IsDebuggerPresent() As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    End Function

    <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="SetComputerNameW")> _
        Public Shared Function SetComputerNameW(<System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal lpComputerName As String) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    End Function

    <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="SetVolumeLabelW")> _
     Public Shared Function SetVolumeLabelW(<System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal lpRootPathName As String, <System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal lpVolumeName As String) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    End Function

    <System.Runtime.InteropServices.DllImportAttribute("kernel32.dll", EntryPoint:="Sleep")> _
          Public Shared Sub Sleep(ByVal dwMilliseconds As UInteger)
    End Sub

    Private Declare Function AnimateWindow Lib "user32" ( _
                       ByVal hWnd As Integer, _
                       ByVal dwTime As Integer, _
                       ByVal dwFlags As Integer) As Integer

    <System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint:="FlashWindow")> _
           Public Shared Function FlashWindow(<System.Runtime.InteropServices.InAttribute()> ByVal hWnd As System.IntPtr, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> ByVal bInvert As Boolean) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    End Function

    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="FindExecutableW")> _
             Public Shared Function FindExecutableW(<System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal lpFile As String, <System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPTStr)> ByVal lpDirectory As String, <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> ByVal lpResult As String) As System.IntPtr
    End Function
    <System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint:="SHObjectProperties")> _
        Public Shared Function SHObjectProperties(ByVal hwnd As System.IntPtr, ByVal shopObjectType As UInteger, <System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> ByVal pszObjectName As String, <System.Runtime.InteropServices.InAttribute(), System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> ByVal pszPropertyPage As String) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '  Permet de quitter si un cracker est
        ' en train de tracer votre programme.
        If IsDebuggerPresent() <> 0 Then End
        ' Cet appel de fonction fera s'afficher progressivement la
        ' fenêtre de son centre vers ses bords
        AnimateWindow(Me.Handle, 1000, &H10 Or &H20000)
    End Sub
    ''' <summary>
    ''' format a:\
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SHFormatDrive(Me.Handle, 0, 0, 0)
    End Sub
    ''' <summary>
    ''' about
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ShellAboutW(Me.Handle, Application.ProductName, "ceci est un exemple", 0)
    End Sub
    ''' <summary>
    ''' recherche des fichiers
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        SHFindFiles(String.Empty, 0)
    End Sub
    ''' <summary>
    ''' espace disque en MB ( ici C:)
    ''' la fonction renvoit des bytes alors on divise par 1024 et puis encore par 1024
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim total, free, tot As ULong
        SHGetDiskFreeSpaceExW("C:\", free, total, tot)
        MsgBox("total disk space " & (total / 1024) / 1024 & vbCrLf & "free disk space " & (free / 1024) / 1024)

    End Sub
    ''' <summary>
    '''  'dwFreq [in] The frequency of the sound, in hertz.
    ''' This parameter must be in the range 37 through 32767 (0x25 through 0x7FFF).
    '''       dwDuration([in])The duration of the sound, in milliseconds. 
    ''' la fonction sleep "endord" le thread pour la durée
    '''  en millisecondes qui est passée en parramètres
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        Beep(50, 500)
        Sleep(2000)
        Beep(2000, 500)
    End Sub
    ''' <summary>
    ''' Attention cela change le nom de votre PC
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If MsgBox("changer le nom de votre PC sur le réseau ? " & vbCrLf & " actuellement " & My.Computer.Name.ToString(), MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            SetComputerNameW(InputBox("Nouveau nom de votre PC après redémmarrage ?"))

        End If
    End Sub
    ''' <summary>
    ''' changer le label d'un disque ( ici A:)
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        If MsgBox("Changer le label de la disquette se trouvant dans A: ?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
            SetVolumeLabelW("A:\", InputBox("Nouveau Label de la disquette ? "))
        End If
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        For x = 0 To 20
            Sleep(200)
            FlashWindow(Me.Handle, True)
            Application.DoEvents()
        Next
    End Sub
    ''' <summary>
    ''' dialogue de config du firewall
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Microsoft.VisualBasic.Shell("Rundll32.exe shell32.dll,Control_RunDLL firewall.cpl")
        'cela fonctionne avec d'autre parrametre (*.cpl ) à vous d'essayer 
        'ex :  Shell("Rundll32.exe shell32.dll,Control_RunDLL sysdm.cpl") 'sys admin

    End Sub
    ''' <summary>
    ''' trouver le programme associé avec un type de document
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Dim odf As New OpenFileDialog
        Dim result As String = String.Empty
        If odf.ShowDialog = Windows.Forms.DialogResult.OK Then
            FindExecutableW(odf.FileName, String.Empty, result)
            MsgBox(result)
        End If
    End Sub
    ''' <summary>
    ''' Zip un répertoire en utilisant shell32.dll
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        Dim mesdoc As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\mesdoc.zip"
        Dim folder As New FolderBrowserDialog
        If folder.ShowDialog() = Windows.Forms.DialogResult.OK Then
            CompressFolder(mesdoc, folder.SelectedPath)
            MsgBox(folder.SelectedPath & vbCrLf & vbCrLf & "compressé dans " & vbCrLf & mesdoc)
        End If
    End Sub
    ''' <summary>
    ''' fonction qui fait le zip
    ''' </summary>
    ''' <param name="CompressedFileName"></param>
    ''' <param name="FolderToCompress"></param>
    ''' <remarks></remarks>
    Sub CompressFolder(ByVal CompressedFileName As String, ByVal FolderToCompress As String)

        Dim B(21) As Byte

        B(0) = 80 : B(1) = 75 : B(2) = 5 : B(3) = 6

        File.WriteAllBytes(CompressedFileName, B) 'Make an empty PKZip file.

        Dim SH As New Shell

        Dim SF As Folder = SH.NameSpace(CompressedFileName)

        Dim DF As Folder = SH.NameSpace(FolderToCompress)

        SF.CopyHere(DF)

    End Sub

    ''' <summary>
    ''' associer un Passport .net avec votre compte utilisateur
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        Microsoft.VisualBasic.Shell("rundll32.exe netplwiz.dll, PassportWizardRunDll")

    End Sub
    ''' <summary>
    ''' affiche les propriétes d'un objet ( imprimante , fichier ,disque )
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Dim ret As Int16 = InputBox("entrer un numéro de 1 à 3")
        Select Case ret
            Case 1
                'changer le nom de l'imprimante si vous voulez
                SHObjectProperties(0, 1, "Microsoft Office Document Image Writer", 0)
            Case 2
                Dim odf As New OpenFileDialog
                odf.Title = "choisissez un fichier"
                If odf.ShowDialog = Windows.Forms.DialogResult.OK Then
                    SHObjectProperties(0, 2, odf.FileName, 0)
                End If
            Case 3
                'changer "c:\" pour un autre disque si vous vouler
                SHObjectProperties(0, 4, "c:\", 0)

        End Select
    End Sub
End Class

Conclusion :


P/Invoke Interop Assistant est un outil formidable et très utile pour vb200*

disponible gratuitement ici

http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/CLRInsideOut2008_01.exe

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.