java调用.Net的类库dll文件
???? 最近项目开发中,需要用java去调用.net开发团队提供的service。一开始以为是以url形式提供的webservice去调用。后来才知道.net项目组提供给我们一个dll文件说里面有有两个方法供调用,一个是发送邮件的,一个是发送短信的。那么如何在通过java去调用这个用C#实现的类库中的方法呢。通过搜索一些资料最后终于实现了,下面说说是如何实现的,挺有意思的
?
using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace Org.Jawin.NETTest { public interface HelloFromNETItf { void ShowDialog(string str); void test(string s);}}
using System;using System.Runtime.InteropServices; // necessary for the Guid and ProgId attributesnamespace Org.Jawin.NETTest {public class HelloFromNET : HelloFromNETItf {// Need a public default no-arg constructor for COM Interop.public HelloFromNET() {}public void ShowDialog(string str) { Console.WriteLine(str);} public void test(string s) { Console.WriteLine("test" + s); }}}
?
?
using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace Org.Jawin.NETTest{ [Guid("3823a63d-5891-3b4f-A460-DB0FB847075F")] public interface TestHelloFromNETItf { void ShowDialog(string str, string caption); }}
using System;using Org.Jawin.NETTest;using System.Reflection;using System.Runtime.InteropServices;namespace Org.Jawin.NETTest{ [Guid("25c2f5a2-1afe-36ce-BE27-84E040F5E19F")] [ProgId("Jawin.HelloFromNET6")] // Register the CLSID under this shortcut ProgId public class TestHelloFromNET : TestHelloFromNETItf { // Need a public default no-arg constructor for COM Interop. public TestHelloFromNET() { } public void ShowDialog(string str,string caption) { System.Reflection.Assembly ass; Type type; Object obj; ass = Assembly.Load("hellotest"); type = ass.GetType("Org.Jawin.NETTest.HelloFromNET"); MethodInfo[] miarr = type.GetMethods(); obj = ass.CreateInstance("Org.Jawin.NETTest.HelloFromNET"); MethodInfo method = type.GetMethod("ShowDialog"); method.Invoke(obj, new string[] { "WriteString"}); //Org.Jawin.NETTest.HelloFromNETItf hfn = new HelloFromNET(); //hfn.ShowDialog(str); Console.WriteLine("testtttttttttttttttttttttttt"); } }}
?
/* * HelloFromNETItf.java - * * This file is part of the Jawin Project: http://jawinproject.sourceforge.net/ * * Please consult the LICENSE file in the project root directory, * or at the project site before using this software. *//* $Id: HelloFromNETItf.java,v 1.2 2004/07/18 14:56:59 arosii_moa Exp $ */package demos;import org.jawin.*;/** * Modified version of JTB generated code * * @version $Revision: 1.2 $ * @author Morten Andersen, arosii_moa (at) users.sourceforge.net */public class HelloFromNETItf extends DispatchPtr {public static final GUID DIID = new GUID("{3823a63d-5891-3b4f-A460-DB0FB847075F}");public static final int iidToken;static {iidToken = IdentityManager.registerProxy(DIID, HelloFromNETItf.class);}/** * The required public no arg constructor. * <br><br> * <b>Important:</b>Should never be used as this creates an uninitialized * HelloFromNETItf (it is required by Jawin for some internal working though). */public HelloFromNETItf() {super();}/** * For creating a new COM-object with the given progid and with * the HelloFromNETItf interface. * * @param progid the progid of the COM-object to create. */public HelloFromNETItf(String progid) throws COMException {super(progid, DIID);}/** * For getting the HelloFromNETItf interface on an existing COM-object. * This is an alternative to calling {@link #queryInterface(Class)} * on comObject. * * @param comObject the COM-object to get the HelloFromNETItf interface on. */public HelloFromNETItf(COMPtr comObject) throws COMException {super(comObject);}/** * For creating a new COM-object with the given clsid and with * the HelloFromNETItf interface. * * @param clsid the GUID of the COM-object to create. */public HelloFromNETItf(GUID clsid) throws COMException {super(clsid, DIID);}public int getIIDToken() {return iidToken;} /** * * @param str * @return void **/ public void ShowDialog(String str,String caption) throws COMException { invokeN("ShowDialog", new Object[] {str,caption}); }}/* * HelloNET.java - * * This file is part of the Jawin Project: http://jawinproject.sourceforge.net/ * * Please consult the LICENSE file in the project root directory, * or at the project site before using this software. *//* $Id: HelloNET.java,v 1.1 2004/07/05 11:55:59 arosii_moa Exp $ */package demos;import org.jawin.GUID;import org.jawin.win32.Ole32;/** * Demo showing how to call .NET code published as a COM object. * The .NET C# source is in the demos/net folder. * * @version $Revision: 1.1 $ * @author Morten Andersen, arosii_moa (at) users.sourceforge.net */public class HelloNET {/** * CLSID for the HelloFromNET COM object */public static final GUID CLSID = new GUID("{25c2f5a2-1afe-36ce-BE27-84E040F5E19F}");/** * ProgID for the HelloFromNET COM object - could be used instead of the CLSID */public static final String PROG_ID = "Jawin.HelloFromNET6";public static void main(String[] args) {try {Ole32.CoInitialize();HelloFromNETItf hello = new HelloFromNETItf(PROG_ID);//hello.setCaption("Jawin .NET test");hello.ShowDialog("Hello from .NET","test");hello.close();Ole32.CoUninitialize();} catch (Exception e) {e.printStackTrace();}}}
上面的2个java文件实现了调用.net中间件。