Java使用JNA调用SWMM模型的DLL
1、对于SWMM我了解的不多,只知道是干嘛的。
SWMM(storm water management model,暴雨洪水管理模型)是一个动态的降水-径流模拟模型,主要用于模拟城市某一单一降水事件或长期的水量和水质模拟。对于这个我只需要了解这么多
2、Java简单使用JNA。
2.1、添加JNA依赖
<!-- https://mvnrepository.com/artifact/net.java.dev.jna/jna -->
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.2.1</version>
</dependency>
2.2、HelloWorld
package com.sun.jna.examples;
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Platform;
/** Simple example of JNA interface mapping and usage. */
public class HelloWorld {
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)
Native.loadLibrary((Platform.isWindows() ? "msvcrt" : "c"),
CLibrary.class);
void printf(String format, Object... args);
}
public static void main(String[] args) {
CLibrary.INSTANCE.printf("Hello, World\n");
for (int i=0;i < args.length;i++) {
CLibrary.INSTANCE.printf("Argument %d: %s\n", i, args[i]);
}
}
}
这就是JNA使用的简单的官方例子,可能有人认为这个例子太简单了,因为使用的是系统自带的动态链接库。之后会说到调用实现的函数库
3、仿照C程序读取DLL
3.1、Swmm5_iface
网上看了下这方面相关的东西,发现了个C程序写的 https://www.epa.gov/sites/production/files/2016-09/swmm5_iface.zip 点击直接下载。或者网盘下载
链接:https://pan.baidu.com/s/1KWbStCDFxm6gLJSxY39W5Q
提取码:0yrb
下载解压看到的是
为了打开C程序,安装了个Visual Studio 2019。附上软件下载和序列号
链接:https://pan.baidu.com/s/1ZJ7BvVMXUCcShdcTKZHhYQ
提取码:i57n
其中,test.c是一个读取文件的demo,swmm5_iface.c封装了一些基本的操作方法,包括上面dos命令转化,swmm5_iface.h 类似于java的接口, 其他文件就看不懂了。
test.c
// Test of the SWMM5 C Interfacing functions
// This is a command line executable that takes the name
// of a SWMM input file as its only command line argument
// and produces a time series listing to the console of the
// following system output results: total rainfall, total
// runoff and total outfall flow.
// This file must be compiled along with swmm5_iface.c
// and swmm5.h, and linked with swmm5.dll and swmm5.lib.
#include <stdio.h>
#include "swmm5_iface.h"
int main(int argc, char *argv[])
{
int i, r