using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace TestDll
{
class Program
{
[DllImport("user32.dll", EntryPoint = "MessageBoxA")]
static extern int MsgBox(int hWnd, string msg, string caption, int type);
static void Main(string[] args)
{
Program.MsgBox(0, "测试", "Test", 0x30);
}
}
}
public class HelloWorld {
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)Native.loadLibrary("user32.dll", CLibrary.class);
int MessageBoxA(int hWnd, byte[] msg, byte[] caption, int type);
}
int MessageBox(int hWnd, String msg, String caption, int type)
{
byte[] gbkMsg = null;
byte[] gbkCaption = null;
try {
gbkMsg = msg.getBytes("GBK");
gbkCaption = caption.getBytes("GBK");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return CLibrary.INSTANCE.MessageBoxA(hWnd, gbkMsg, gbkCaption, type);
}
public static void main(String[] args) {
HelloWorld hel = new HelloWorld();
hel.MessageBox(0, "测试", "Test", 0x30);
}
}
JNA库下载地址:
JNA包:
https://maven.java.net/content/repositories/releases/net/java/dev/jna/jna/4.0.0/jna-4.0.0.jar