[英]How to import GetAsyncKeyState in C#?
要導入我正在使用的GetAsyncKeyState()
API:
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
所有的網頁都給了我相同的代碼,但是當我嘗試編譯編譯時拋出:
預期的類,委托,枚舉,接口或結構
修飾符'extern'對此項無效
我正在使用命令行直接編譯,但Visual C#也會拋出相同的錯誤。 那么導入函數的正確方法是什么?
這意味着您將聲明放在代碼的錯誤位置。 它需要INSIDE一個類,如下所示:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
// not here
namespace WindowsFormsApplication1
{
// not here
public partial class Form1 : Form
{
// put it INSIDE the class
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
public Form1()
{
// not inside methods, though
InitializeComponent();
}
}
}
編譯器引發的錯誤很明顯。 你應該把這個聲明放在一個類中:
namespace MyNameSpace
{
public class MyClass
{
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
}
}
在這里,您可以找到extern
關鍵字的參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.