1

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!

  • Are you sure that your bit-ness is correct, e.g. 64-Bit excel and 64-Bit rust toolchain target? – hellow Dec 19 '18 at 8:39
  • @ljedrz I think cdylib would be even more correct ^^ doc.rust-lang.org/reference/linkage.html – hellow Dec 19 '18 at 8:45
  • Fixed the type to dylib (have tried dylib, lib, cdylib, staticlib...). Sorry, but this is exactly what I was talking about in the end. My post represents the latest thing I have tried. There are just so many moving parts in this equation that we have to draw multidimensional matrix to represent all the possible combinations I should try. Replicating the situation on other computer is not hard since no dependencies is needed or anything. – A. Soikkeli Dec 19 '18 at 9:20
  • 1
    And @hellow: yes, I'm using stable-x86_64-pc-windows-msvc. – A. Soikkeli Dec 19 '18 at 9:27
  • 2
    I have to say: "Works for me" if I use cdylib and the correct target... There is something wrong with your setup. Please triple check that the path is correct (use a simpler one, e.g. D:\lib.dll) and use rustc --crate-type=cdylib --target=x86_64-pc-windows-msvc (and verify that you are running a 64-bit Excel by looking at the task manager!) – hellow Dec 19 '18 at 9:44
0

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.

  • 3
    I don't get what do mean by "settings -> Apps & features". There is no such option neither in rust, nor in rustup. I think you are using some kind of gui, but it doesn't have anything to do with rust itself.. – hellow Dec 19 '18 at 12:55
  • Ok, I edited the answer a bit to be more explicit. I had some kind of manually installed Rust Nightly package in Apps & features and thus I had two separate installations (one via rustup + manual App). I can't recall in what point did I install that, but uninstalling that solved the problem. Did this explain the problem? – A. Soikkeli Dec 20 '18 at 13:16
  • 1
    I must have used some old installers from time before rustup. For example from here you can still find links to Rust 1.0 Windows installers that still work: doc.rust-lang.org/1.0.0/book/installing-rust.html – A. Soikkeli Dec 20 '18 at 13:34

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.