2009-08-26 115 views
0

我试着将下面的代码转换为c#。但是当我使用它时,我收到了很多错误。 像||有关的错误和& &在c#版本中使用的符号。将vb.net转换为c#

任何一个可以帮助我转换为完美的C#代码?谢谢。

Option Explicit On 
Option Strict On 

Imports Microsoft.Win32 
Imports System.Runtime.InteropServices 

Public Class Kiosk 
    Implements IDisposable 

#Region "IDisposable" 

    '' Implementing IDisposable since it might be possible for 
    '' someone to forget to cause the unhook to occur. I didn''t really 
    '' see any problems with this in testing, but since the SDK says 
    '' you should do it, then here''s a way to make sure it will happen. 

    Public Overloads Sub Dispose() Implements IDisposable.Dispose 
     Dispose(True) 
     GC.SuppressFinalize(Me) 
    End Sub 

    Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean) 
     If disposing Then 
      '' Free other state (managed objects). 
     End If 
     If m_hookHandle <> 0 Then 
      UnhookWindowsHookEx(m_hookHandle) 
      m_hookHandle = 0 
     End If 
     If m_taskManagerValue > -1 Then 
      EnableTaskManager() 
     End If 
    End Sub 

    Protected Overrides Sub Finalize() 
     Dispose(False) 
    End Sub 

#End Region 

    Shared Sub main() 

    End Sub 
    Private Delegate Function LowLevelHookDelegate(ByVal code As Integer, ByVal wParam As Integer, ByRef lParam As KeyboardLowLevelHookStruct) As Integer 

    Private Const Hc_Action As Integer = 0 
    Private Const WindowsHookKeyboardLowLevel As Integer = 13 
    Private Const LowLevelKeyboardHfAltDown As Integer = &H20 

    Private Enum WindowsMessage 
     KeyDown = &H100 
     KeyUp = &H101 
     SystemKeyDown = &H104 
     SystemKeyUp = &H105 
    End Enum 

    Private Enum Vk 
     Tab = &H9 
     Escape = &H1B 
     Shift = &H10 
     Control = &H11 
     Menu = &H12   '' ALT key. 
     Alt = &H12 
     Pause = &H13 
     LeftWindows = &H5B '' Left Windows key (Microsoft® Natural® keyboard). 
     RightWindows = &H5C '' Right Windows key (Natural keyboard). 
     Applications = &H5D '' Applications key (Natural keyboard). 
    End Enum 

    Private Structure KeyboardLowLevelHookStruct 
     Public VirtualKeyCode As Integer 
     Public ScanCode As Integer 
     Public Flags As Integer 
     Public Time As Integer 
     Public ExtraInfo As UInt32 
    End Structure 

    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal hook As Integer, ByVal address As LowLevelHookDelegate, ByVal [mod] As Integer, ByVal threadId As Integer) As Integer 
    Private Declare Function CallNextHookEx Lib "user32" (ByVal handle As Integer, ByVal code As Integer, ByVal wParam As Integer, ByVal lParam As KeyboardLowLevelHookStruct) As Integer 
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal handle As Integer) As Integer 
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal virtualKey As Integer) As Integer 

    Private m_hookHandle As Integer 

    Private Function LowLevelHook(ByVal code As Integer, ByVal wParam As Integer, ByRef lParam As KeyboardLowLevelHookStruct) As Integer 

     If code = Hc_Action Then 

      If (wParam = WindowsMessage.KeyDown) OrElse _ 
       (wParam = WindowsMessage.SystemKeyDown) OrElse _ 
       (wParam = WindowsMessage.KeyUp) OrElse _ 
       (wParam = WindowsMessage.SystemKeyUp) Then 

       ''Dim alt As Boolean = (GetAsyncKeyState(Vk.Alt) And &H8000) = &H8000 
       ''Dim shift As Boolean = (GetAsyncKeyState(Vk.Shift) And &H8000) = &H8000 
       Dim control As Boolean = (GetAsyncKeyState(Vk.Control) And &H8000) = &H8000 

       Dim suppress As Boolean 

       '' CTRL+ESC 
       If control AndAlso lParam.VirtualKeyCode = Vk.Escape Then 
        suppress = True 
       End If 

       '' ALT+TAB 
       If (lParam.Flags And LowLevelKeyboardHfAltDown) = LowLevelKeyboardHfAltDown AndAlso lParam.VirtualKeyCode = Vk.Tab Then 
        suppress = True 
       End If 

       '' ALT+ESC 
       If (lParam.Flags And LowLevelKeyboardHfAltDown) = LowLevelKeyboardHfAltDown AndAlso lParam.VirtualKeyCode = Vk.Escape Then 
        suppress = True 
       End If 

       '' Left Windows button. 
       If lParam.VirtualKeyCode = Vk.LeftWindows Then 
        suppress = True 
        MessageBox.Show("Pressed Left windows key") 
       End If 

       '' Right Windows button. 
       If lParam.VirtualKeyCode = Vk.RightWindows Then 
        suppress = True 
        MessageBox.Show("Pressed Right windows key") 
       End If 

       '' Applications button. 
       If lParam.VirtualKeyCode = Vk.Applications Then 
        suppress = True 
       End If 

       If suppress Then 
        Return 1 
       End If 

      End If 

      Return CallNextHookEx(m_hookHandle, code, wParam, lParam) 

     End If 

    End Function 

    Public Sub Disable() 
     If m_hookHandle = 0 Then 
      m_hookHandle = SetWindowsHookEx(WindowsHookKeyboardLowLevel, AddressOf LowLevelHook, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0) 
     End If 
    End Sub 

    Public Sub Enable() 
     If m_hookHandle <> 0 Then 
      UnhookWindowsHookEx(m_hookHandle) 
      m_hookHandle = 0 
     End If 
    End Sub 

End Class 
+3

附加C#代码也一样,我们会纠正错误......没有人会花那么多的时间来转换代码到C#,而且,你可以使用反射工具进行自动转换 – 2009-08-26 07:29:23

+2

正如你所说,所有转换后的代码都包含相同的22个错误,请不要只发布C#代码,还要错误,以便这里的人们不必重现你的所有工作。 – Residuum 2009-08-26 07:53:43

+1

您能否列出这些错误以及C#代码! – Darknight 2009-08-26 08:31:05

回答

4

首先,所提供的代码不会因为它调用一个不会在示例代码中存在的方法EnableTaskManager()的编译。但为了解决这个问题,我将它改为调用示例中存在的Enable函数。

所提供的代码也没有使用,这不是在代码的任何声明的变量:m_taskManagerValue,要解决这个问题我declaraed,由于在我的例子一个int = 0。

此外,功能LowLevelHook没有一个代码路径返回一个值,所以我只是说,因此在这种情况下返回0(我不知道它想回到那里)

最后,C#没有像子最后与处置,所以我刚刚删除它。 ;)

好吧。在此之后,我将代码转换为C#http://www.developerfusion.com/tools/convert/vb-to-csharp/

我修正了将int与非int进行比较,并将ctype值与int进行比较的错误。

然后我编译了vb版本,并在反射中查看它以获取SetWindowHookEx行的工作。

瞧,这将编译:

using System; 
using System.Configuration; 
using System.Xml; 
using System.Runtime.InteropServices; 
using System.Windows.Forms; 
using Microsoft.Win32; 
using System.Reflection; 
using Microsoft.VisualBasic; 

public class Kiosk1 : IDisposable 
{ 

    #region "IDisposable" 

    // Implementing IDisposable since it might be possible for 
    // someone to forget to cause the unhook to occur. I didn't really 
    // see any problems with this in testing, but since the SDK says 
    // you should do it, then here's a way to make sure it will happen. 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 
    int m_taskManagerValue=0; 

    protected virtual void Dispose(bool disposing) 
    { 
     if (disposing) 
     { 
     } 
     // Free other state (managed objects). 
     if (m_hookHandle != 0) 
     { 
      UnhookWindowsHookEx(m_hookHandle); 
      m_hookHandle = 0; 
     } 
     if (m_taskManagerValue > -1) 
     { 
      Enable(); 
     } 
    } 



    #endregion 

    public static void main() 
    { 

    } 
    private delegate int LowLevelHookDelegate(int code, int wParam, ref KeyboardLowLevelHookStruct lParam); 

    private const int Hc_Action = 0; 
    private const int WindowsHookKeyboardLowLevel = 13; 
    private const int LowLevelKeyboardHfAltDown = 0x20; 

    private enum WindowsMessage 
    { 
     KeyDown = 0x100, 
     KeyUp = 0x101, 
     SystemKeyDown = 0x104, 
     SystemKeyUp = 0x105 
    } 

    private enum Vk 
    { 
     Tab = 0x9, 
     Escape = 0x1b, 
     Shift = 0x10, 
     Control = 0x11, 
     Menu = 0x12, 
     // ALT key. 
     Alt = 0x12, 
     Pause = 0x13, 
     LeftWindows = 0x5b, 
     // Left Windows key (Microsoft® Natural® keyboard). 
     RightWindows = 0x5c, 
     // Right Windows key (Natural keyboard). 
     Applications = 0x5d 
     // Applications key (Natural keyboard). 
    } 

    [StructLayout(LayoutKind.Sequential)] 
    private struct KeyboardLowLevelHookStruct 
    { 
     public int VirtualKeyCode; 
     public int ScanCode; 
     public int Flags; 
     public int Time; 
     public UInt32 ExtraInfo; 
    } 
    [DllImport("user32", EntryPoint = "SetWindowsHookExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] 
    private static extern int SetWindowsHookEx(int hook, LowLevelHookDelegate address, int mod, int threadId); 
    [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] 
    private static extern int CallNextHookEx(int handle, int code, int wParam, KeyboardLowLevelHookStruct lParam); 
    [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] 
    private static extern int UnhookWindowsHookEx(int handle); 
    [DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)] 
    private static extern int GetAsyncKeyState(int virtualKey); 


    private int m_hookHandle; 

    private int LowLevelHook(int code, int wParam, ref KeyboardLowLevelHookStruct lParam) 
    { 

     if (code == Hc_Action) 
     { 

      if ((wParam == (int)WindowsMessage.KeyDown) || (wParam == (int)WindowsMessage.SystemKeyDown) || (wParam == (int)WindowsMessage.KeyUp) || (wParam == (int)WindowsMessage.SystemKeyUp)) 
      { 

       //Dim alt As Boolean = (GetAsyncKeyState(Vk.Alt) And &H8000) = &H8000 
       //Dim shift As Boolean = (GetAsyncKeyState(Vk.Shift) And &H8000) = &H8000 
       bool control = (GetAsyncKeyState((int)Vk.Control) & 0x8000) == 0x8000; 

       bool suppress = false; 

       // CTRL+ESC 
       if (control && lParam.VirtualKeyCode == (int)Vk.Escape) 
       { 
        suppress = true; 
       } 

       // ALT+TAB 
       if ((lParam.Flags & LowLevelKeyboardHfAltDown) == LowLevelKeyboardHfAltDown && lParam.VirtualKeyCode == (int)Vk.Tab) 
       { 
        suppress = true; 
       } 

       // ALT+ESC 
       if ((lParam.Flags & LowLevelKeyboardHfAltDown) == LowLevelKeyboardHfAltDown && lParam.VirtualKeyCode == (int)Vk.Escape) 
       { 
        suppress = true; 
       } 

       // Left Windows button. 
       if (lParam.VirtualKeyCode == (int)Vk.LeftWindows) 
       { 
        suppress = true; 
        MessageBox.Show("Pressed Left windows key"); 
       } 

       // Right Windows button. 
       if (lParam.VirtualKeyCode == (int)Vk.RightWindows) 
       { 
        suppress = true; 
        MessageBox.Show("Pressed Right windows key"); 
       } 

       // Applications button. 
       if (lParam.VirtualKeyCode == (int)Vk.Applications) 
       { 
        suppress = true; 
       } 

       if (suppress) 
       { 
        return 1; 

       } 
      } 


      return CallNextHookEx(m_hookHandle, code, wParam, lParam); 

     } 
     return 0; 
    } 

    public void Disable() 
    { 
     if (m_hookHandle == 0) 
     { 

      m_hookHandle = SetWindowsHookEx(WindowsHookKeyboardLowLevel, new LowLevelHookDelegate(this.LowLevelHook), Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]).ToInt32(), 0); 
     } 
    } 

    public void Enable() 
    { 
     if (m_hookHandle != 0) 
     { 
      UnhookWindowsHookEx(m_hookHandle); 
      m_hookHandle = 0; 
     } 
    } 

} 
+0

我试图转换提供的所有网站中的代码。 所有转换的代码都抛出相同的错误。 总共有22个错误。 如果将代码粘贴到类文件中并进行构建。你可以看到问题。 请帮忙。 – Anuya 2009-08-26 07:41:07

+0

修正了我可以更新我的答案。 – Stefan 2009-08-26 18:01:55

+0

再次更新一次,现在使用编译的版本。 – Stefan 2009-08-26 19:07:28

0

另一种方法是编译dll,然后用reflector读取它。你可以选择语言反射器来解决这个问题。

+0

对不起,这会产生不可维护的代码。这样做的唯一原因就是当你想在一个DLL中合并一个VB Lib和一个C#lib/codebase以便最终发布时。 – Dykam 2009-08-26 18:05:18

+0

反射器的导出代码通常需要大量清理,尤其是在用于翻译时 - 它在掐但不是银子弹中很有用 – STW 2009-08-26 18:19:28

+0

反射器解决了我的问题,我无法修复SetWindowsHookEx行,没有想法去做什么。编译VB代码,在Reflector中检查该行并获得解决方案。因此,修复那些卡住的地方可能会很好。 – Stefan 2009-08-26 19:03:26

2

Telerik的有很大的free online converter

下被他们的转换器产生:

using Microsoft.Win32; 
using System.Runtime.InteropServices; 

public class Kiosk : IDisposable 
{ 
#region "IDisposable" 

    //' Implementing IDisposable since it might be possible for 
    //' someone to forget to cause the unhook to occur. I didn''t really 
    //' see any problems with this in testing, but since the SDK says 
    //' you should do it, then here''s a way to make sure it will happen. 

    public void IDisposable.Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 

    protected virtual void Dispose(bool disposing) 
    { 
     if (disposing) { 
      //' Free other state (managed objects). 
     } 
     if (m_hookHandle != 0) { 
      UnhookWindowsHookEx(m_hookHandle); 
      m_hookHandle = 0; 
     } 
     if (m_taskManagerValue > -1) { 
      EnableTaskManager(); 
     } 
    } 

    protected override void Finalize() 
    { 
     Dispose(false); 
    } 
#endregion 

    static void main() 
    { 

    } 
    private delegate int LowLevelHookDelegate(int code, int wParam, ref KeyboardLowLevelHookStruct lParam); 

    private const int Hc_Action = 0; 
    private const int WindowsHookKeyboardLowLevel = 13; 
    private const int LowLevelKeyboardHfAltDown = 0x20; 

    private enum WindowsMessage 
    { 
     KeyDown = 0x100, 
     KeyUp = 0x101, 
     SystemKeyDown = 0x104, 
     SystemKeyUp = 0x105 
    } 

    private enum Vk 
    { 
     Tab = 0x9, 
     Escape = 0x1b, 
     Shift = 0x10, 
     Control = 0x11, 
     Menu = 0x12, 
     //' ALT key. 
     Alt = 0x12, 
     Pause = 0x13, 
     LeftWindows = 0x5b, 
     //' Left Windows key (Microsoft® Natural® keyboard). 
     RightWindows = 0x5c, 
     //' Right Windows key (Natural keyboard). 
     Applications = 0x5d 
     //' Applications key (Natural keyboard). 
    } 

    private struct KeyboardLowLevelHookStruct 
    { 
     public int VirtualKeyCode; 
     public int ScanCode; 
     public int Flags; 
     public int Time; 
     public UInt32 ExtraInfo; 
    } 


// ERROR: Not supported in C#: DeclareDeclaration 
// ERROR: Not supported in C#: DeclareDeclaration 
// ERROR: Not supported in C#: DeclareDeclaration 
// ERROR: Not supported in C#: DeclareDeclaration 
    private int m_hookHandle; 

    private int LowLevelHook(int code, int wParam, ref KeyboardLowLevelHookStruct lParam) 
    { 

     if (code == Hc_Action) { 

      if ((wParam == WindowsMessage.KeyDown) || (wParam == WindowsMessage.SystemKeyDown) || (wParam == WindowsMessage.KeyUp) || (wParam == WindowsMessage.SystemKeyUp)) { 

       //'Dim alt As Boolean = (GetAsyncKeyState(Vk.Alt) And &H8000) = &H8000 
       //'Dim shift As Boolean = (GetAsyncKeyState(Vk.Shift) And &H8000) = &H8000 
       bool control = (GetAsyncKeyState(Vk.Control) & 0x8000) == 0x8000; 

       bool suppress; 

       //' CTRL+ESC 
       if (control && lParam.VirtualKeyCode == Vk.Escape) { 
        suppress = true; 
       } 

       //' ALT+TAB 
       if ((lParam.Flags & LowLevelKeyboardHfAltDown) == LowLevelKeyboardHfAltDown && lParam.VirtualKeyCode == Vk.Tab) { 
        suppress = true; 
       } 

       //' ALT+ESC 
       if ((lParam.Flags & LowLevelKeyboardHfAltDown) == LowLevelKeyboardHfAltDown && lParam.VirtualKeyCode == Vk.Escape) { 
        suppress = true; 
       } 

       //' Left Windows button. 
       if (lParam.VirtualKeyCode == Vk.LeftWindows) { 
        suppress = true; 
        MessageBox.Show("Pressed Left windows key"); 
       } 

       //' Right Windows button. 
       if (lParam.VirtualKeyCode == Vk.RightWindows) { 
        suppress = true; 
        MessageBox.Show("Pressed Right windows key"); 
       } 

       //' Applications button. 
       if (lParam.VirtualKeyCode == Vk.Applications) { 
        suppress = true; 
       } 

       if (suppress) { 
        return 1; 
       } 

      } 

      return CallNextHookEx(m_hookHandle, code, wParam, lParam); 

     } 

    } 

    public void Disable() 
    { 
     if (m_hookHandle == 0) { 
      m_hookHandle = SetWindowsHookEx(WindowsHookKeyboardLowLevel, LowLevelHook, Marshal.GetHINSTANCE(System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0)).ToInt32, 0); 
     } 
    } 

    public void Enable() 
    { 
     if (m_hookHandle != 0) { 
      UnhookWindowsHookEx(m_hookHandle); 
      m_hookHandle = 0; 
     } 
    } 

}