[英]Converting vb.net to c#
我尝试下面的代码将其转换为C#。 但是我在使用它时遇到很多错误。 与||相关的错误 和&&符号在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
首先,所提供的代码将无法编译,因为它会调用示例代码中不存在的方法EnableTaskManager() 。 但是只是为了解决该问题,我将其更改为调用示例中确实存在的Enable函数。
提供的代码还使用了未在代码中任何地方声明的变量: m_taskManagerValue ,以避开我在示例中将其声明为int = 0的问题。
另外,函数LowLevelHook不在一个代码路径上返回值,所以我只是添加了它,因此在这种情况下它返回0(我不知道该返回什么)
最后,C#不喜欢sub的处理方式,所以我只删除了它。 ;)
好。 修复后,我将代码转换为C#, 网址为http://www.developerfusion.com/tools/convert/vb-to-csharp/
我通过ctype将值与int进行比较,修复了抱怨将int与non-int进行比较的错误。
然后,我编译了vb版本,并进行了反射查看,以使SetWindowHookEx-row起作用。
瞧,这将编译为:
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;
}
}
}
Telerik有一个很棒的免费在线转换器
以下是他们的转换器制作的:
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;
}
}
}
另一种方法是编译dll,然后使用反射器将其读取。 您可以选择将语言反汇编为il。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.