Unable to import functions from a C compiled file

Unable to import functions from a C compiled file

benjamin_decreusefond
Participant Participant
276 Views
3 Replies
Message 1 of 4

Unable to import functions from a C compiled file

benjamin_decreusefond
Participant
Participant

Hi ! 

 

We are trying to import function from a compiled file written in C. It has been compiled long time ago in x64 and we don't have access to the source code. We are trying from a c# code to import function from this compiled file like so

    [DllImport("ScanToBIMLib.dll", SetLastError = true)]
    private static extern IntPtr createBlock(float voxelSize, bool useGPU);

 

All files are compiled in x64 architecture however, when we start Revit 2024 we always get the error Unable to load ScanToBIMLib.dll: module not found. We use this piece of code to get add the .dll file 

public class Load
{
    public static void LoadLib(string baseDirectory)
    {
        AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
        {
            string assemblyPath = Path.Combine(baseDirectory, "ScanToBIMLib.dll");

            Assembly asm = Assembly.LoadFrom(assemblyPath);
            return null;
        };
    }
}

 

It works fine for other dll but not this one. Since ScanToBIMLib.dll is a C compiled dll we can't add it directly as a reference to the c# dll. We also tried adding it as embedded resource but still no result 😕 

 

Would any of you already encountered that issue ?

 

Regards,

Benjamin

0 Likes
277 Views
3 Replies
Replies (3)
Message 2 of 4

m.de.vriesTH5VM
Enthusiast
Enthusiast

There are several possible causes for this, and most cannot be fixed if you do not have access to the source code.

 

I am assuming that the ScanToBIMLib.dll is actually present on the location where it is loaded from.

 

It may be that the C code is compiled for a different runtime. If that runtime is not present windows will not load the dll. You normally fix this by recompiling with the correct runtime. Or you have guess which runtime was used and install that. This can quickly add to the number of runtimes installed on your computer .

 

64 bit should be compatible with Revit, but often Revit addins are compiled for AnyCPU and this may cause compatibility issues. This cannot be fixed without recompiling. C and C++ do not really AnyCPU though, so that may cause problems in and of itself.

 

The debug may be configured differently (e.g /mtd instead or /mdd or vice versa). This can only be fixed by recompiling.

 

Use a dependency checker to see if you can find which additional dlls your ScanToBIMLib.dll depends on, and check if those can be found on your computer. This is tricky because not all checkers look in every possible folder that Windows looks in when trying to find a dll. And dlls that are installed with your application are usually marked as not found, even though at runtime it will be.

 

 

 

0 Likes
Message 3 of 4

ricaun
Advisor
Advisor

Usually if the dll is in the same folder of the addin you don't need to use AssemblyResolve.

 

In the RevitAddin.DllImport.Example project the just use [DllImport("CPlusPlus.dll")] and works.

 

Like @m.de.vriesTH5VM mention you need to build the cpp to dll using x64 and with the correct target framework (net framework or net core).

 

Because I have the source code I was experimenting to not use the DllImport and make the cpp dll to generate the references in C#.

 

Something like this:

https://raw.githubusercontent.com/ricaun-io/RevitAddin.DllImport.Example/1.1.0/assets/CppToCsharp.png

 

This sample works with Revit 2024 and Revit 2025, here the full documentation with some configuration in the cpp project: https://github.com/ricaun-io/RevitAddin.DllImport.Example

 

One thing you could do is create a NUnit test project and try to load the [DllImport("ScanToBIMLib.dll")] in there.

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 4 of 4

benjamin_decreusefond
Participant
Participant

Hi ! 

 

I figured out the issue on my side ! Actually I thought the binary was compiled in the correct format. After checking with Ghidra I realized it was in the wrong format. Everything is now working correctly !

 

Thanks for your help !

0 Likes