我一直在尝试使用 Jacob 从 Java 调用 dll 函数,但没有成功。我已经按照此处所述使用 regasm 完成了 dll 的注册 - http://www.dreamincode.net/forums/topic/114094-using-dll-library-in-java-application-using-jacob/ 。我的代码:
String serverName = "...", fileName = "...";
Dispatch dispatch = new Dispatch("dllx32conn.dbconn");
Dispatch.call(dispatch, "pass_para", serverName, fileName);
这行不通。它抛出 com.jacob.com.ComFailException: 无法将名称映射到 dispid: pass_para
因此我决定使用 JetBrains dotPeek 反编译来分析 dll 函数。这是我发现的
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace dllx32conn
{
public class dbconn
{
public static string conn_str = "";
public static string strFilePath = "";
public static SqlConnection Conn = new SqlConnection();
public static DataTable tbl;
public static SqlDataAdapter dap;
public static void pass_para(string servname, string csvpth)
{
dbconn.conn_str = "Data Source=" + servname + ";Initial Catalog=Billing;User Id=Scd;Password=Smart11Siri";
dbconn.strFilePath = csvpth;
}
}
}
我真的很感谢一些帮助来弄清楚这里没有发生什么。谢谢。
最佳答案
问题已解决 - 由于这篇文章,我只需从 DLL 方法中删除“静态”函数声明 - http://jumbloid.blogspot.com/2009/12/making-net-dll-com-visible.html
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace dllx32conn
{
public class dbconn
{
public static string conn_str = "";
public static string strFilePath = "";
public static SqlConnection Conn = new SqlConnection();
public static DataTable tbl;
public static SqlDataAdapter dap;
public void pass_para(string servname, string csvpth)
{
dbconn.conn_str = "Data Source=" + servname + ";Initial Catalog=xxx;User Id=xxx;Password=xxx";
dbconn.strFilePath = csvpth;
}
}
}
关于java - 雅各布 com.jacob.com.ComFailException : Can't map name to dispid:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26929686/