Unable to call into Rust dll on Windows


#1

I used the Rust installer from rust-lang.org.
rustc --version gives:
rustc 1.2.0 (082e47636 2015-08-03)

I’m following the “Rust Inside Other Languages” instructions located here:
http://doc.rust-lang.org/book/rust-inside-other-languages.html

I use the Rust code provided, then alter it to have the no_mangle attribute and pub extern keywords in front of the process function.
I also add the [lib] section to my Cargo.toml file and can tell that it works because it outputs embed.dll instead of libembed.rlib.

I’m running:
cargo build --release

When I use the python code to call into embed.dll I get this error:
Traceback (most recent call last):
File “embed.py”, line 3, in
lib = cdll.LoadLibrary(“target/release/embed.dll”)
File “c:\Python27\lib\ctypes_init_.py”, line 431, in LoadLibrary
return self.dlltype(name)
File "c:\Python27\lib\ctypes_init
.py", line 353, in init
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 193] %1 is not a valid Win32 application

Note that I did replace:
lib = cdll.LoadLibrary(“target/release/libembed.so”)
with:
lib = cdll.LoadLibrary(“target/release/embed.dll”)

I have also tried calling into it from C# with this PInvoke setup:
[DllImport(“embed.dll”, CallingConvention = CallingConvention.Cdecl)]
static extern void process();

And I get this error:
System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
at RustSpeedTest.Program.process()
snip

Is this working on windows for others?


#2

Most likely you are trying to load 32-bit dll into a 64-bit process or vice-versa.


#3

Thanks, I changed my C# build target to x64 and this did solve it.
My python install must be x86.
How do I tell cargo to build an x86 target?


#4

You’ll need to download the 32-bit installer.

In theory, this is done with cargo build --target=i686-pc-windows-gnu. However, currently Windows installers carry the standard library files only for the host platform, so such cross-building won’t work (unless you’ve downloaded the appropriate version of std manually).