Access Violation when returning from a dynamic call to DLL

Hi all.

I have a problem: when I use libloading crate to call a function from external DLL, application crashes with ACCESS_VIOLATION exception.

This is the Rust code:

fn analyze_dll(text: *const u8, len: u16) -> std::io::Result<()> {
    unsafe {
        match ANA_DLL {
            Some(ref lib) => {
                let external_analyze: libloading::Symbol<unsafe extern fn(p: *const u8, len: u16) -> ()> = lib.get(b"analyys")?;
                Ok(external_analyze(text, len))
            None => Err(Error::from(ErrorKind::NotFound))

the signature of the function is as follows: procedure analyys (p : pchar; len : word); far stdcall export; (it’s in Delphi 2.0), so it does not return a value but sets result in-place under p address.

When I debug execution, I see that after call to analyys, the area under p contains expected data, so the call itself was more or less ok. But trying to leave the Rust function never succeeds - exception happens with these arguments:

0:000> .exr -1
ExceptionAddress: 00c4e670
ExceptionCode: c0000005 (Access violation)
ExceptionFlags: 00000000
NumberParameters: 2
Parameter[0]: 00000008
Parameter[1]: 00c4e670
Attempt to execute non-executable address 00c4e670

As I readabout the _stdcall convention… this sounds like a gibberish to me :frowning:

So, should I change the calling code, or is it a problem in libloading or is it something else? Probably the stack gets corrupted or misused, but I can’t figure out where and how exactly.

Thanks for any suggestions!

UPD: preliminary looks like adding "stdcall" into extern function signature seems to resolve this specific problem, at least I don’t get ACCESS_VIOLATION any more. Now, however I have another problem which might or might not be related

and now… it works! thanks to everyone!! especially @fvnever, although he probably does not sit on this forum

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.