Java使用JNA调用SWMM模型的DLL

本文介绍了如何使用Java的JNA库调用SWMM5的DLL文件进行暴雨洪水管理模型的模拟。首先简述了SWMM的作用,接着详细说明了Java中JNA的使用步骤,包括添加依赖和编写HelloWorld示例。然后,通过分析C程序的读取DLL方法,提供了C源代码和DLL库的下载链接,并特别指出调用32位DLL时需确保使用32位JDK。最后,展示了Java调用DLL的Swmm5_iface.java和Test.java代码示例。
摘要由CSDN通过智能技术生成

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
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值