java调用dll后总结

最近项目需要发送短信,用的短信发射器,需要调用dll文件

1、下载JNative,解压后获得三个文件JNativeCpp.dll,libJNativeCpp.so,JNative.jar 。

JNativeCpp.dll Windows下用的,拷贝到windows / system32目录下;
libJNativeCpp.so Linux下的,拷贝到系统目录下;
JNative.jar 这是一个扩展包,导入工程LIB中或将其拷贝到jdk\jre\lib\ext 下,系统会自动加载。

2、用的短信发射器的dll文件sms.dll,直接贴代码如下

import java.io.File;
import java.util.HashMap;

import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;
import org.xvolks.jnative.exceptions.NativeException;
public class SendSms {
    private static SendSms instance = null;
    protected SendSms(){}
    
    public static SendSms getInstance() {    
             if(instance == null) {    
                 instance = new SendSms();    
              }    
              return instance;    
    } 
    
    public int Sms_Connection(String comid){   
          JNative n = null;   
          int returnInt=0;
          String Mobile_Type="";
          String CopyRightToCOM="";
          String CopyRightStr="";
          try {              
                   n = new JNative("sms.dll", "Sms_Connection");   
                   n.setRetVal(Type.INT);   
                   int i = 0;   
                   n.setParameter(i++, Type.STRING,CopyRightStr);   
                   n.setParameter(i++, Type.INT,"3");   
                   n.setParameter(i++, Type.INT,"9600");  
                   n.setParameter(i++, Type.STRING,Mobile_Type);
                   n.setParameter(i++, Type.STRING,CopyRightToCOM);
                   n.invoke();   
                   returnInt=Integer.parseInt(n.getRetVal());
               } catch(Exception ex){
                   System.out.println(ex.toString());
                   
               }finally {
                   try{
                       if (n != null)
                            n.dispose();   
                   }catch(Exception ex1)
                   {
                       System.out.println(ex1.toString());
                   }
                } 
               return returnInt;   
    }
    
    public int Sms_Disconnection(){   
          JNative n = null;   
          int returnInt=0;
          try {              
                   n = new JNative("sms.dll", "Sms_Disconnection");   
                   n.setRetVal(Type.INT);   
                   n.invoke();   
                   returnInt=Integer.parseInt(n.getRetVal());
               } catch(Exception ex){
                   System.out.println(ex.toString());
                   
               }finally {
                   try{
                       if (n != null)
                            n.dispose();   
                   }catch(Exception ex1)
                   {
                       System.out.println(ex1.toString());
                   }
                } 
               return returnInt;   
    }
    public int Sms_Send(String phone,String smscontent){   
          JNative n = null;   
          int returnInt=0;
          try {              
                   n = new JNative("sms.dll", "Sms_Send");   
                   n.setRetVal(Type.INT);   
                   int i = 0;   
                   n.setParameter(i++, Type.STRING,phone);   
                   n.setParameter(i++, Type.STRING,smscontent);
                   n.invoke();   
                   returnInt=Integer.parseInt(n.getRetVal());
               } catch(Exception ex){
                   System.out.println(ex.toString());
                   
               }finally {
                   try{
                       if (n != null)
                            n.dispose();   
                   }catch(Exception ex1)
                   {
                       System.out.println(ex1.toString());
                   }
                } 
               return returnInt;   
    }
    //接收指定类型的短信
    //Sms_Type:短信类型(0:未读短信;1:已读短信;2:待发短信;3:已发短信;4:全部短信)
    //Sms_Text:返回指定类型的短信内容字符串(短信内容字符串说明:短信与短信之前用"|"符号作为分隔符,每条短信中间的各字段用"#"符号作为分隔符)

    public HashMap Sms_Receive(String Sms_Type){   
          JNative n = null;   
          int returnInt=0;
          String smscontent="";;
          HashMap map=new HashMap();
          try {              
                   n = new JNative("sms.dll", "Sms_Receive");   
                   n.setRetVal(Type.INT);   
                   int i = 0;   
                   n.setParameter(i++, Type.STRING,Sms_Type);   
                   n.setParameter(i++, Type.STRING,smscontent);
                   n.invoke();   
                   returnInt=Integer.parseInt(n.getRetVal());
                   map.put("jsbz",returnInt);
                   map.put("smscontent", smscontent);
               } catch(Exception ex){
                   System.out.println(ex.toString());
                   
               }finally {
                   try{
                       if (n != null)
                            n.dispose();   
                   }catch(Exception ex1)
                   {
                       System.out.println(ex1.toString());
                   }
                } 
               return map;   
    }
    //加载dll,根据路径加载,必须是dll文件的绝对路径
    public  void loadDll(String path){   
         path += "sms.dll";   
         System.load(path);    
    }
    //加载dll,装载Windows\System32下或jre\bin或Tomcat\bin目录下的本地链接库
    public  void loadDll1(){   
           System.loadLibrary("sms");   
    }
    //0:连接失败1:发送成功,2:发送失败
    public int sendSms(String phone,String smscontent)
    {
        int ljint=0;
        int fsinfo=0;
        int returnInt=0;
        String returmMsg="";
        //String port=String.valueOf((int)(3+Math.random()*8));
        String port=String.valueOf((int)(3+Math.random()*4));
        //loadDll("D:/sms/");
        ljint=Sms_Connection(port);
        if(ljint==1)
        {
            fsinfo=Sms_Send(phone,smscontent);
            if(fsinfo==0)
                returnInt=2;
            else if(fsinfo==1){
                returnInt=1;    
                Sms_Disconnection();
            }
        }
        return returnInt;
            
        
    }    
    //0:连接失败1:发送成功,2:发送失败
    public int sendSms(String port,String phone,String smscontent)
    {
        int ljint=0;
        int fsinfo=0;
        int returnInt=0;
        String returmMsg="";
        //port=String.valueOf((int)(3+Math.random()*8));
        //loadDll("D:/sms/");
        ljint=Sms_Connection(port);
        if(ljint==1)
        {
            fsinfo=Sms_Send(phone,smscontent);
            if(fsinfo==0)
                returnInt=2;
            else if(fsinfo==1){
                returnInt=1;    
                Sms_Disconnection();
            }
        }
        return returnInt;
            
        
    }
    //0:连接失败1:发送成功,2:发送失败
    public int sendSms(String path,String port,String phone,String smscontent)
    {
        int ljint=0;
        int fsinfo=0;
        int returnInt=0;
        String returmMsg="";
        //loadDll(path);
        ljint=Sms_Connection(port);
        if(ljint==1)
        {
            fsinfo=Sms_Send(phone,smscontent);
            if(fsinfo==0)
                returnInt=2;
            else if(fsinfo==1){
                returnInt=1;    
                Sms_Disconnection();
            }
        }
        return returnInt;
            
        
    }
    
    
    
}

Sms_Connection中就是调用了sms.dll的Sms_Connection方法

 

参考文档:

http://tvjody.iteye.com/blog/125643

 

转载于:https://www.cnblogs.com/shangguan/archive/2013/06/14/3135098.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值