2009-08-26 214 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; 
     } 
    } 

}