我尝试使用下面的代码将其转换为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
答案 0 :(得分:4)
首先,提供的代码无法编译,因为它调用了示例代码中不存在的方法 EnableTaskManager()。但是为了解决这个问题,我将其更改为调用示例中存在的Enable-function。
提供的代码也使用了一个未在代码中任何地方声明的变量: m_taskManagerValue ,以解决我在我的示例中声明为int = 0的情况。
另外,函数LowLevelHook没有在一个代码路径上返回一个值,所以我刚刚添加,所以在这种情况下它返回0(我不知道它会返回那里)
最后,C#不喜欢sub最后使用dispose所以我只是删除了它。 ;)
好。在那之后,我将代码转换为C#here http://www.developerfusion.com/tools/convert/vb-to-csharp/
我修复了错误,它抱怨将int与非int比较,将ctype的值转换为int。
然后我编译了vb-version并用反射查看它以使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;
}
}
}
答案 1 :(得分:3)
答案 2 :(得分: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;
}
}
}
答案 3 :(得分:0)
另一种方法是编译dll然后用reflector读取它。你可以选择语言反射器来解决这个问题。