Problem calling midi windows api using winapi/winmm for extern

Hi,

I'm new to rust and am just trying to call a windows midi function - I found a nice intro to this at lowmidi.htm

So my code compiles and runs, but I get no names coming back in the structure, while others (in other languages) do here (search cos cannot post links) - how-to-get-midi-device-names

The output of running my code is below, I was hoping that the AAA would be set by the windows function to the names of my midi devices. It correctly notices my midi device when I turn it on/off in the count, but the names do not appear.

Any ideas? Am I making an obvious rookie error? I'm on Windows 10 Home 1909, a pretty vanilla dell build.

C:/Users/dave/.cargo/bin/cargo.exe run --color=always --package playground_r --bin playground_r
   Compiling playground_r v0.1.0 (C:\working\rust\playground_r)
    Finished dev [unoptimized + debuginfo] target(s) in 0.48s
     Running `target\debug\playground_r.exe`
Hello, world!
Got num_midi_in of 1
sz is 76
For 0 got ok of 0 and name of Ok("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") all is MIDIINCAPSW { wMid: 0, wPid: 0, vDriverVersion: 0, szPname: [65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65], dwSupport: 0 }
Got num_midi_out of 2
sz is 84
For 0 got ok of 0 and name of Ok("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") all is MIDIOUTCAPSW { wMid: 0, wPid: 0, vDriverVersion: 0, szPname: [65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65], wTechnology: 0, wVoices: 0, wNotes: 0, wChannelMask: 0, dwSupport: 0 }
For 1 got ok of 0 and name of Ok("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") all is MIDIOUTCAPSW { wMid: 0, wPid: 0, vDriverVersion: 0, szPname: [65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65, 65], wTechnology: 0, wVoices: 0, wNotes: 0, wChannelMask: 0, dwSupport: 0 }

Process finished with exit code 0

using cargo.toml of

[package]
name = "playground_r"
version = "0.1.0"
authors = ["dave"]
edition = "2018"

[dependencies]
winmm-sys = "0.2.0"
winapi = "0.2.8"

and code of

#![allow(dead_code)]
#![allow(non_snake_case)]
extern crate winmm;
extern crate winapi;
use std::mem;
use winapi::mmsystem;

mod tests1;

fn main() {
    println!("Hello, world!");
    check_midi_in();
    check_midi_out();
}

fn check_midi_out() {
    let num_midi = unsafe {
        winmm::midiOutGetNumDevs()
    };
    println!("Got num_midi_out of {}", num_midi);
    let wchar32_space: [u16; 32] = [65; 32];
    #[allow(non_snake_case)]
    let caps = mmsystem::MIDIOUTCAPSW {
        wMid: 0,
        wPid: 0,
        vDriverVersion: 0,
        szPname: wchar32_space,
        wTechnology: 0,
        wVoices: 0,
        wNotes: 0,
        wChannelMask: 0,
        dwSupport: 0,
    };
    let raw_caps = Box::into_raw(Box::new(caps));
    let sz = mem::size_of::<mmsystem::MIDIOUTCAPSW>();
    println!("sz is {}", sz);
    for i in 0..num_midi {
        let id = i as u64;
        let name_ok = unsafe {
            winmm::midiOutGetDevCapsW(id, raw_caps, sz as u32)
        };
        println!("For {} got ok of {} and name of {:?} all is {:?}", id, name_ok, String::from_utf16(&caps.szPname), &caps);
    }
}

fn check_midi_in() {
    let num_midi_in = unsafe {
        winmm::midiInGetNumDevs()
    };
    println!("Got num_midi_in of {}", num_midi_in);
    let wchar32_space: [u16; 32] = [65; 32];
    let caps = mmsystem::MIDIINCAPSW {
        wMid: 0,
        wPid: 0,
        vDriverVersion: 0,
        szPname: wchar32_space,
        dwSupport: 0
    };
    let raw_caps = Box::into_raw(Box::new(caps));
    let sz = mem::size_of::<mmsystem::MIDIINCAPSW>();
    println!("sz is {}", sz);
    for i in 0..num_midi_in {
        let id = i as u64;
        let name_ok = unsafe {
            winmm::midiInGetDevCapsW(id, raw_caps, sz as u32)
        };
        println!("For {} got ok of {} and name of {:?} all is {:?}", id, name_ok, String::from_utf16(&caps.szPname), &caps);
    }
}

Please read Forum Code Formatting and Syntax Highlighting and edit your prior post by using the pencil-shaped edit button under that post. Note that you can specify the language syntax to be used in highlighting by specifying the language at the end of the first row of three backticks. (Rust is the default language if you don't specify toml or c++ or something else.)

Many readers of this forum will ignore code snippets, compiler error reports, etc that do not follow the posting guidelines for new contributors that are pinned to the top of this forum. Even those who do respond may feel that the lack of following the forum posting guidelines is disrespectful of their time. Thanks. :clap:

Thanks for the info, I was wondering how I should do that but didn't find that link. Fixed my post.

I fished through the midir library to see why that works and mine doesn't and if I switch to using

[dependencies]
winrt = "0.7.2"
winapi = { version = "0.3.9", features = ["mmsystem", "mmeapi"] }

then everything seems to work. So I'm going to close this.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.