Do not work without println?

type HMODULE = isize;
type PCWSTR = *const u16;
type PCSTR = *const u8;
#[link(name = "windows.0.48.5")]
extern {
  fn LoadLibraryExW(s:PCWSTR, flag:u32)-> HMODULE;
  fn GetProcAddress(m:HMODULE, s:PCSTR)-> fn()-> usize;
  fn FreeLibrary(m:HMODULE)-> i32;
fn main() {
  let name = "MyDllPath.dll\0".encode_utf16().collect::<Vec<u16>>();
  // println!("{:?}", name);
  let fname = b"print\0";
    let lib = LoadLibraryExW(name.as_ptr(), 0);
    GetProcAddress(lib, fname as PCSTR)();

Codes above seems well, but exit with error 0xc0000005.

It is strange that if I insert println or assert_eq macros about name before the let lib, everything works well.

Another way to run successfully is to let name = "MyDllPath.dll".encode_utf16().chain(Some(0)).collect(), and it works without the macros.

There are no relationships between the chain(Some(0)) and the macros. I'm thinking about the reason about the lazy loading for Vec pointers, but it is under the compiler.

If i got stupid problems, I'm paying for a apology.

Have you checked the function pointer returned by GetProcAddress, to see if it's NULL? 0xc0000005 is an access violation, which in particular would happen if you're calling a NULL function pointer.

Hypothesis: it's working when you insert println because that causes your program to contain a function named print; otherwise, print doesn't exist, so GetProcAddress returns NULL and your program crashes.

Also, this is probably a question better posted to


Thanku for new ideas. I got pass if insert any func that will not be optimised.

I'm still thinking about the reason of NULL returning without the extra func.

If there's knowledge you can share about this, please reply to me, and as learner I'm going to users communication for next learning. Thanku for patience.

What did you expect to happen? What, in your mind, should GetProcAddress do when asked to load a function named print from a DLL that does not define any such function?

It would also help us help you if you could show us the complete and unedited output of

dumpbin /exports MyDllPath.dll

This command might produce a lot of output and we want to see all of it.