'Jacob com.jacob.com.ComFailException: Can't map name to dispid:
I've been trying to call a dll function from Java using Jacob without any success. I have done registration of the dll with regasm as described here - http://www.dreamincode.net/forums/topic/114094-using-dll-library-in-java-application-using-jacob/. My code:
String serverName = "...", fileName = "...";
Dispatch dispatch = new Dispatch("dllx32conn.dbconn");
Dispatch.call(dispatch, "pass_para", serverName, fileName);
This doesn't work. It throws com.jacob.com.ComFailException: Can't map name to dispid: pass_para
So I decided to analyze the dll functions by decompiling it using JetBrains dotPeek. Here is what I found
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;
}
}
}
I would really appreciate some help with figuring out what's not happening here. Thanks.
Solution 1:[1]
Problem solved - I just had to remove the 'static' function declaration from my DLL methods thanks to this article - 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;
}
}
}
Solution 2:[2]
So I was getting this error and ultimately my problem was that I had two instances of the classes on the class-path. I had the classes of a dependent jar unpacked as well as the jar itself on the path. I got rid of the jar and voila it all started working beautifully.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Uncle Boni |
| Solution 2 | Uncle Iroh |
