elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  Ayuda para traducir una función anónima de C# a VBNET
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ayuda para traducir una función anónima de C# a VBNET  (Leído 2,536 veces)
Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.866



Ver Perfil
Ayuda para traducir una función anónima de C# a VBNET
« en: 4 Junio 2013, 19:19 pm »

Hola

No puedo hallar la manera de traducir esta línea de código por mi mismo, espero que puedan ayudarme.

El problema es en este For, que se usa la función anónima "MyEnumThreadWindowsProc" como un argumento, sin pasarle argumentos a dicha función, y esto no sé traducirlo a VBNET.

Código
  1. foreach (ProcessThread t in p.Threads)
  2. {
  3.  EnumThreadWindows(t.Id, MyEnumThreadWindowsProc, IntPtr.Zero);
  4. }

Los traductores no saben traducirlo corréctamente, y obtengo este reusltado:

Código
  1. For Each t As ProcessThread In p.Threads
  2.  EnumThreadWindows(t.Id, MyEnumThreadWindowsProc, IntPtr.Zero)
  3. Next

Según una persona me ha comentado hace poco, esto podría ser una solución:
Código
  1.   For Each t As ProcessThread In p.Threads
  2.  Dim returnMyEnumThreadWindowsProc As Object
  3.  returnMyEnumThreadWindowsProc = MyEnumThreadWindowsProc ( ... )
  4.  EnumThreadWindows(t.Id, returnMyEnumThreadWindowsProc , IntPtr.Zero)
  5. Next

...Pero aún así sigo con el problema de no saber que parámetros enviarle a "MyEnumThreadWindowsProc " en esa última modificación.



Aquí tienen la class de C#:
Código
  1. using System;
  2. using System.Text;
  3. using System.Runtime.InteropServices;
  4. using System.Diagnostics;
  5.  
  6. namespace TaskbarHide
  7. {
  8.    /// <summary>
  9.    /// Helper class for hiding/showing the taskbar and startmenu on
  10.    /// Windows XP and Vista.
  11.    /// </summary>
  12.    public static class Taskbar
  13.    {
  14.        [DllImport("user32.dll")]
  15.        private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
  16.        [DllImport("user32.dll", CharSet = CharSet.Auto)]
  17.        private static extern bool EnumThreadWindows(int threadId, EnumThreadProc pfnEnum, IntPtr lParam);
  18.        [DllImport("user32.dll", SetLastError = true)]
  19.        private static extern System.IntPtr FindWindow(string lpClassName, string lpWindowName);
  20.        [DllImport("user32.dll", SetLastError = true)]
  21.        private static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className,  string windowTitle);
  22.        [DllImport("user32.dll")]
  23.        private static extern IntPtr FindWindowEx(IntPtr parentHwnd, IntPtr childAfterHwnd, IntPtr className, string windowText);
  24.        [DllImport("user32.dll")]
  25.        private static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
  26.        [DllImport("user32.dll")]
  27.        private static extern uint GetWindowThreadProcessId(IntPtr hwnd, out int lpdwProcessId);
  28.  
  29.        private const int SW_HIDE = 0;
  30.        private const int SW_SHOW = 5;
  31.  
  32.        private const string VistaStartMenuCaption = "Start";
  33.        private static IntPtr vistaStartMenuWnd = IntPtr.Zero;
  34.        private delegate bool EnumThreadProc(IntPtr hwnd, IntPtr lParam);
  35.  
  36.        /// <summary>
  37.        /// Show the taskbar.
  38.        /// </summary>
  39.        public static void Show()
  40.        {
  41.            SetVisibility(true);
  42.        }
  43.  
  44.        /// <summary>
  45.        /// Hide the taskbar.
  46.        /// </summary>
  47.        public static void Hide()
  48.        {
  49.            SetVisibility(false);
  50.        }
  51.  
  52.        /// <summary>
  53.        /// Sets the visibility of the taskbar.
  54.        /// </summary>
  55.        public static bool Visible
  56.        {
  57.            set { SetVisibility(value); }
  58.        }
  59.  
  60.        /// <summary>
  61.        /// Hide or show the Windows taskbar and startmenu.
  62.        /// </summary>
  63.        /// <param name="show">true to show, false to hide</param>
  64.        private static void SetVisibility(bool show)
  65.        {
  66.            // get taskbar window
  67.            IntPtr taskBarWnd = FindWindow("Shell_TrayWnd", null);
  68.  
  69.            // try it the WinXP way first...
  70.            IntPtr startWnd = FindWindowEx(taskBarWnd, IntPtr.Zero, "Button", "Start");
  71.  
  72.            if (startWnd == IntPtr.Zero)
  73.            {
  74.                // try an alternate way, as mentioned on CodeProject by Earl Waylon Flinn
  75.                startWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, (IntPtr)0xC017, "Start");
  76.            }
  77.  
  78.            if (startWnd == IntPtr.Zero)
  79.            {
  80.                // ok, let's try the Vista easy way...
  81.                startWnd = FindWindow("Button", null);
  82.  
  83.                if (startWnd == IntPtr.Zero)
  84.                {
  85.                    // no chance, we need to to it the hard way...
  86.                    startWnd = GetVistaStartMenuWnd(taskBarWnd);
  87.                }
  88.            }
  89.  
  90.            ShowWindow(taskBarWnd, show ? SW_SHOW : SW_HIDE);
  91.            ShowWindow(startWnd, show ? SW_SHOW : SW_HIDE);
  92.        }
  93.  
  94.        /// <summary>
  95.        /// Returns the window handle of the Vista start menu orb.
  96.        /// </summary>
  97.        /// <param name="taskBarWnd">windo handle of taskbar</param>
  98.        /// <returns>window handle of start menu</returns>
  99.        private static IntPtr GetVistaStartMenuWnd(IntPtr taskBarWnd)
  100.        {
  101.            // get process that owns the taskbar window
  102.            int procId;
  103.            GetWindowThreadProcessId(taskBarWnd, out procId);
  104.  
  105.            Process p = Process.GetProcessById(procId);
  106.            if (p != null)
  107.            {
  108.                // enumerate all threads of that process...
  109.                foreach (ProcessThread t in p.Threads)
  110.                {
  111.                    EnumThreadWindows(t.Id, MyEnumThreadWindowsProc, IntPtr.Zero);
  112.                }
  113.            }
  114.            return vistaStartMenuWnd;
  115.        }
  116.  
  117.        /// <summary>
  118.        /// Callback method that is called from 'EnumThreadWindows' in 'GetVistaStartMenuWnd'.
  119.        /// </summary>
  120.        /// <param name="hWnd">window handle</param>
  121.        /// <param name="lParam">parameter</param>
  122.        /// <returns>true to continue enumeration, false to stop it</returns>
  123.        private static bool MyEnumThreadWindowsProc(IntPtr hWnd, IntPtr lParam)
  124.        {
  125.            StringBuilder buffer = new StringBuilder(256);
  126.            if (GetWindowText(hWnd, buffer, buffer.Capacity) > 0)
  127.            {
  128.                Console.WriteLine(buffer);
  129.                if (buffer.ToString() == VistaStartMenuCaption)
  130.                {
  131.                    vistaStartMenuWnd = hWnd;
  132.                    return false;
  133.                }
  134.            }
  135.            return true;
  136.        }
  137.    }
  138. }



Y aquí la class "traducida":

Código
  1. '
  2. ' * Copyright (c) 2008..11 by Simon Baer
  3. ' *
  4. ' *  You may use this code for whatever you want.
  5. '
  6.  
  7.  
  8. Imports System
  9. Imports System.Text
  10. Imports System.Runtime.InteropServices
  11. Imports System.Diagnostics
  12.  
  13. Namespace TaskbarHide
  14.    ''' <summary>
  15.    ''' Helper class for hiding/showing the taskbar and startmenu on
  16.    ''' Windows XP and Vista.
  17.    ''' </summary>
  18.    Public NotInheritable Class Taskbar
  19.        Private Sub New()
  20.        End Sub
  21.        <DllImport("user32.dll")> _
  22.        Private Shared Function GetWindowText(hWnd As IntPtr, text As StringBuilder, count As Integer) As Integer
  23.        End Function
  24.        <DllImport("user32.dll", CharSet := CharSet.Auto)> _
  25.        Private Shared Function EnumThreadWindows(threadId As Integer, pfnEnum As EnumThreadProc, lParam As IntPtr) As Boolean
  26.        End Function
  27.        <DllImport("user32.dll", SetLastError := True)> _
  28.        Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As System.IntPtr
  29.        End Function
  30.        <DllImport("user32.dll", SetLastError := True)> _
  31.        Private Shared Function FindWindowEx(parentHandle As IntPtr, childAfter As IntPtr, className As String, windowTitle As String) As IntPtr
  32.        End Function
  33.        <DllImport("user32.dll")> _
  34.        Private Shared Function FindWindowEx(parentHwnd As IntPtr, childAfterHwnd As IntPtr, className As IntPtr, windowText As String) As IntPtr
  35.        End Function
  36.        <DllImport("user32.dll")> _
  37.        Private Shared Function ShowWindow(hwnd As IntPtr, nCmdShow As Integer) As Integer
  38.        End Function
  39.        <DllImport("user32.dll")> _
  40.        Private Shared Function GetWindowThreadProcessId(hwnd As IntPtr, lpdwProcessId As Integer) As UInteger
  41.        End Function
  42.  
  43.        Private Const SW_HIDE As Integer = 0
  44.        Private Const SW_SHOW As Integer = 5
  45.  
  46.        Private Const VistaStartMenuCaption As String = "Start"
  47.        Private Shared vistaStartMenuWnd As IntPtr = IntPtr.Zero
  48.        Private Delegate Function EnumThreadProc(hwnd As IntPtr, lParam As IntPtr) As Boolean
  49.  
  50.        ''' <summary>
  51.        ''' Show the taskbar.
  52.        ''' </summary>
  53.        Public Shared Sub Show()
  54.            SetVisibility(True)
  55.        End Sub
  56.  
  57.        ''' <summary>
  58.        ''' Hide the taskbar.
  59.        ''' </summary>
  60.        Public Shared Sub Hide()
  61.            SetVisibility(False)
  62.        End Sub
  63.  
  64.        ''' <summary>
  65.        ''' Sets the visibility of the taskbar.
  66.        ''' </summary>
  67.        Public Shared WriteOnly Property Visible() As Boolean
  68.            Set
  69.                SetVisibility(value)
  70.            End Set
  71.        End Property
  72.  
  73.        ''' <summary>
  74.        ''' Hide or show the Windows taskbar and startmenu.
  75.        ''' </summary>
  76.        ''' <param name="show">true to show, false to hide</param>
  77.        Private Shared Sub SetVisibility(show As Boolean)
  78.            ' get taskbar window
  79.            Dim taskBarWnd As IntPtr = FindWindow("Shell_TrayWnd", Nothing)
  80.  
  81.            ' try it the WinXP way first...
  82.            Dim startWnd As IntPtr = FindWindowEx(taskBarWnd, IntPtr.Zero, "Button", "Start")
  83.  
  84.            If startWnd = IntPtr.Zero Then
  85.                ' try an alternate way, as mentioned on CodeProject by Earl Waylon Flinn
  86.                startWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, DirectCast(&Hc017, IntPtr), "Start")
  87.            End If
  88.  
  89.            If startWnd = IntPtr.Zero Then
  90.                ' ok, let's try the Vista easy way...
  91.                startWnd = FindWindow("Button", Nothing)
  92.  
  93.                If startWnd = IntPtr.Zero Then
  94.                    ' no chance, we need to to it the hard way...
  95.                    startWnd = GetVistaStartMenuWnd(taskBarWnd)
  96.                End If
  97.            End If
  98.  
  99.            ShowWindow(taskBarWnd, If(show, SW_SHOW, SW_HIDE))
  100.            ShowWindow(startWnd, If(show, SW_SHOW, SW_HIDE))
  101.        End Sub
  102.  
  103.        ''' <summary>
  104.        ''' Returns the window handle of the Vista start menu orb.
  105.        ''' </summary>
  106.        ''' <param name="taskBarWnd">windo handle of taskbar</param>
  107.        ''' <returns>window handle of start menu</returns>
  108.        Private Shared Function GetVistaStartMenuWnd(taskBarWnd As IntPtr) As IntPtr
  109.            ' get process that owns the taskbar window
  110.            Dim procId As Integer
  111.            GetWindowThreadProcessId(taskBarWnd, procId)
  112.  
  113.            Dim p As Process = Process.GetProcessById(procId)
  114.            If p IsNot Nothing Then
  115.                ' enumerate all threads of that process...
  116.                For Each t As ProcessThread In p.Threads
  117.                    EnumThreadWindows(t.Id, MyEnumThreadWindowsProc, IntPtr.Zero)
  118.                Next
  119.            End If
  120.            Return vistaStartMenuWnd
  121.        End Function
  122.  
  123.        ''' <summary>
  124.        ''' Callback method that is called from 'EnumThreadWindows' in 'GetVistaStartMenuWnd'.
  125.        ''' </summary>
  126.        ''' <param name="hWnd">window handle</param>
  127.        ''' <param name="lParam">parameter</param>
  128.        ''' <returns>true to continue enumeration, false to stop it</returns>
  129.        Private Shared Function MyEnumThreadWindowsProc(hWnd As IntPtr, lParam As IntPtr) As Boolean
  130.            Dim buffer As New StringBuilder(256)
  131.            If GetWindowText(hWnd, buffer, buffer.Capacity) > 0 Then
  132.                Console.WriteLine(buffer)
  133.                If buffer.ToString() = VistaStartMenuCaption Then
  134.                    vistaStartMenuWnd = hWnd
  135.                    Return False
  136.                End If
  137.            End If
  138.            Return True
  139.        End Function
  140.    End Class
  141. End Namespace
  142.  
  143. '=======================================================
  144. 'Service provided by Telerik (www.telerik.com)
  145. 'Conversion powered by NRefactory.
  146. 'Twitter: @telerik, @toddanglin
  147. 'Facebook: facebook.com/telerik
  148. '=======================================================


« Última modificación: 4 Junio 2013, 19:21 pm por EleKtro H@cker » En línea



Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.866



Ver Perfil
Re: Ayuda para traducir una función anónima de C# a VBNET
« Respuesta #1 en: 4 Junio 2013, 20:13 pm »

Bueno, aquí les dejo el código convertido a vbnet, ya lo conseguí:

Código
  1. Imports System
  2. Imports System.Text
  3. Imports System.Runtime.InteropServices
  4. Imports System.Diagnostics
  5.  
  6. Namespace TaskbarHide
  7.    ''' <summary>
  8.    ''' Helper class for hiding/showing the taskbar and startmenu on
  9.    ''' Windows XP and Vista.
  10.    ''' </summary>
  11.    Public NotInheritable Class Taskbar
  12.        Private Sub New()
  13.        End Sub
  14.        <DllImport("user32.dll")> _
  15.        Private Shared Function GetWindowText(hWnd As IntPtr, text As StringBuilder, count As Integer) As Integer
  16.        End Function
  17.        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
  18.        Private Shared Function EnumThreadWindows(threadId As Integer, pfnEnum As EnumThreadProc, lParam As IntPtr) As Boolean
  19.        End Function
  20.        <DllImport("user32.dll", SetLastError:=True)> _
  21.        Private Shared Function FindWindow(lpClassName As String, lpWindowName As String) As System.IntPtr
  22.        End Function
  23.        <DllImport("user32.dll", SetLastError:=True)> _
  24.        Private Shared Function FindWindowEx(parentHandle As IntPtr, childAfter As IntPtr, className As String, windowTitle As String) As IntPtr
  25.        End Function
  26.        <DllImport("user32.dll")> _
  27.        Private Shared Function FindWindowEx(parentHwnd As IntPtr, childAfterHwnd As IntPtr, className As IntPtr, windowText As String) As IntPtr
  28.        End Function
  29.        <DllImport("user32.dll")> _
  30.        Private Shared Function ShowWindow(hwnd As IntPtr, nCmdShow As Integer) As Integer
  31.        End Function
  32.        <DllImport("user32.dll")> _
  33.        Private Shared Function GetWindowThreadProcessId(hwnd As IntPtr, lpdwProcessId As Integer) As UInteger
  34.        End Function
  35.  
  36.        Private Const SW_HIDE As Integer = 0
  37.        Private Const SW_SHOW As Integer = 5
  38.  
  39.        Private Const VistaStartMenuCaption As String = "Start"
  40.        Private Shared vistaStartMenuWnd As IntPtr = IntPtr.Zero
  41.        Private Delegate Function EnumThreadProc(hwnd As IntPtr, lParam As IntPtr) As Boolean
  42.  
  43.        ''' <summary>
  44.        ''' Show the taskbar.
  45.        ''' </summary>
  46.        Public Shared Sub Show()
  47.            SetVisibility(True)
  48.        End Sub
  49.  
  50.        ''' <summary>
  51.        ''' Hide the taskbar.
  52.        ''' </summary>
  53.        Public Shared Sub Hide()
  54.            SetVisibility(False)
  55.        End Sub
  56.  
  57.        ''' <summary>
  58.        ''' Sets the visibility of the taskbar.
  59.        ''' </summary>
  60.        Public Shared WriteOnly Property Visible() As Boolean
  61.            Set(value As Boolean)
  62.                SetVisibility(value)
  63.            End Set
  64.        End Property
  65.  
  66.        ''' <summary>
  67.        ''' Hide or show the Windows taskbar and startmenu.
  68.        ''' </summary>
  69.        ''' <param name="show">true to show, false to hide</param>
  70.        Private Shared Sub SetVisibility(show As Boolean)
  71.            ' get taskbar window
  72.            Dim taskBarWnd As IntPtr = FindWindow("Shell_TrayWnd", Nothing)
  73.  
  74.            ' try it the WinXP way first...
  75.            Dim startWnd As IntPtr = FindWindowEx(taskBarWnd, IntPtr.Zero, "Button", "Start")
  76.  
  77.            If startWnd = IntPtr.Zero Then
  78.                ' try an alternate way, as mentioned on CodeProject by Earl Waylon Flinn
  79.                startWnd = FindWindowEx(IntPtr.Zero, IntPtr.Zero, CType(&HC017, IntPtr), "Start")
  80.            End If
  81.  
  82.            If startWnd = IntPtr.Zero Then
  83.                ' ok, let's try the Vista easy way...
  84.                startWnd = FindWindow("Button", Nothing)
  85.  
  86.                If startWnd = IntPtr.Zero Then
  87.                    ' no chance, we need to to it the hard way...
  88.                    startWnd = GetVistaStartMenuWnd(taskBarWnd)
  89.                End If
  90.            End If
  91.  
  92.            ShowWindow(taskBarWnd, If(show, SW_SHOW, SW_HIDE))
  93.            ShowWindow(startWnd, If(show, SW_SHOW, SW_HIDE))
  94.        End Sub
  95.  
  96.        ''' <summary>
  97.        ''' Returns the window handle of the Vista start menu orb.
  98.        ''' </summary>
  99.        ''' <param name="taskBarWnd">windo handle of taskbar</param>
  100.        ''' <returns>window handle of start menu</returns>
  101.        Private Shared Function GetVistaStartMenuWnd(taskBarWnd As IntPtr) As IntPtr
  102.            ' get process that owns the taskbar window
  103.            Dim procId As Integer
  104.            GetWindowThreadProcessId(taskBarWnd, procId)
  105.  
  106.            Dim p As Process = Process.GetProcessById(procId)
  107.            If p IsNot Nothing Then
  108.                ' enumerate all threads of that process...
  109.                For Each t As ProcessThread In p.Threads
  110.                    EnumThreadWindows(t.Id, AddressOf MyEnumThreadWindowsProc, IntPtr.Zero)
  111.                Next
  112.            End If
  113.            Return vistaStartMenuWnd
  114.        End Function
  115.  
  116.        ''' <summary>
  117.        ''' Callback method that is called from 'EnumThreadWindows' in 'GetVistaStartMenuWnd'.
  118.        ''' </summary>
  119.        ''' <param name="hWnd">window handle</param>
  120.        ''' <param name="lParam">parameter</param>
  121.        ''' <returns>true to continue enumeration, false to stop it</returns>
  122.        Private Shared Function MyEnumThreadWindowsProc(hWnd As IntPtr, lParam As IntPtr) As Boolean
  123.            Dim buffer As New StringBuilder(256)
  124.            If GetWindowText(hWnd, buffer, buffer.Capacity) > 0 Then
  125.                Console.WriteLine(buffer)
  126.                If buffer.ToString() = VistaStartMenuCaption Then
  127.                    vistaStartMenuWnd = hWnd
  128.                    Return False
  129.                End If
  130.            End If
  131.            Return True
  132.        End Function
  133.    End Class
  134. End Namespace


En línea



Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines