I am creating an Autodesk Revit add-in in C#. Output of this project is a DLL itself. Within this project, I am loading another unmanaged DLL by calling:
IntPtr pDll = LoadLibrary("E:\\Rep3DLib\\builds\\Release\\Index.dll");
int a = Marshal.GetLastWin32Error();
Within debugger, I see that pDll
has value 0 and a
has value 127 (Error: The specified procedure could not be found.). This is surprising because I am not even looking any particular procedure within the DLL. I just want to load the DLL.
Now I delete the unmanaged DLL from E:\\Rep3DLib\\builds\\Release\\Index.dll
and in the above code, a
has value 126 (Error: The specified module could not be found.). This does make sense. It also means that when the DLL is present at the correct location, the code is able to find it but somehow it's looking for a procedure which it's not able to find.
To debug this problem, I created another project within the same solution. Here's the code for this project:
namespace testIndex {
class Program {
[DllImport("kernel32.dll", SetLastError = true)]
public static extern IntPtr LoadLibrary(string dllToLoad);
static void Main(string[] args) {
IntPtr pDll = LoadLibrary("E:\\Rep3DLib\\builds\\Release\\Index.dll");
int a = Marshal.GetLastWin32Error();
}
}
}
When I run this, pDll
has a non-zero value and I am able to call the functions defined within the Index.dll
.
Note that both the projects have target platform as x64. I also verified that if I build the Revit add-in and run it on a different machine, it does find the DLL. So somehow it's not finding the DLL only on my machine although the path E:\\Rep3DLib\\builds\\Release\\Index.dll
is definitely correct.
Any idea what could be wrong?
createIndex()
, I was getting the error thatIndex.dll
could not be found. This was even if I was callingSetDllDirectory("E:\\Rep3DLib\\builds\\Release");
before callingcreateIndex()
. I switched toLoadLibrary()
because I thought it would be easier to debug. – Neerav Mehta