excelvbadllruststdcall

Calling functions from Rust DLL with Excel VBA


I'm trying to create a Rust DLL and call those functions from 64bit Excel (Windows 10 and Office 365).

Currently my lib.rs looks like this:

#[no_mangle]
pub extern "stdcall" fn square(x: f64) -> f64 {
    {x * x}
}

I'm compiling with command:

rustc --crate-type=cdylib lib.rs

My VBA code looks like this:

Private Declare PtrSafe Function square Lib "C:\Users\user\rust\excelfunctions\src\lib.dll" (ByVal x As Double) As Double

Sub testsquare()
    MsgBox square(10)
End Sub

Excel is "helping" me to debug this by giving always the same error:

Run-time error '48':

File not found. C:\Users\user\rust\excelfunctions\src\lib.dll

I've obviously checked that the file is there and I can see the function with DLL Export Viewer. If I replace the file with C library, I can make the function work. I've managed to make this work with Haskell as well, so I know Excel is talking with the outside world, it's just not saying anything more specific when giving the error message.

I've spent a lot of time with this and tried all possible combinations I could come up with. I would highly appreciate if the person who answers this would first double check that the proposed solution really works in his/her machine, so we don't end up with a very long ping pong of questions and "no" answers (been there already). It's just not possible to list here all the possible variations I have already tried.

EDIT: I had Rust Nightly installed in Settings -> Apps & features, which was messing my setup somehow. After uninstalling Nightly, everything works!


Solution

  • I had Rust some old explicitly installed Nightly in Settings -> Apps & features (of Windows 10), which was messing my setup somehow. After uninstalling Nightly, everything works.