因为我不能在 jna repo github 上发帖,所以我在这里发帖。在 intellij 上创建了新的 maven 项目。将此代码添加到 main:
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.Advapi32;
import com.sun.jna.platform.win32.Winsvc;
public class Main {
public static void main(String[] args) throws InterruptedException {
registerHandler();
System.out.println(1);
Thread.sleep(60000);
}
public static void registerHandler(){
Winsvc.SERVICE_STATUS_HANDLE service_status_handle = Advapi32.INSTANCE.RegisterServiceCtrlHandlerEx("hvldlpxpa", new Winsvc.HandlerEx() {
@Override
public int callback(int i, int i1, Pointer pointer, Pointer pointer1) {
return i;
}
}, Pointer.NULL);
}
public static void stopService(String[] args) throws Exception {
System.exit(0);
}
}
MAVEN 文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>jnaReg</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna-platform</artifactId>
<version>5.12.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>Main</mainClass>
</manifest>
</archive>
<finalName>JNA</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>
在带网络的安全模式下,它可以完美运行。 Winapi 文档没有提供太多帮助。我还在github上搜索了jna问题。
有什么帮助吗?