On Monday, 31 October 2016 at 11:44:25 UTC, Cleverson Casarin Uliana wrote:
Although the above code works, I have an impression that it could be more elegant and concise, but don't know how to improve it...

That's OK except for the last line... the length isn't necessarily correct so your slice can be wrong.

I'd just use printf or some other function that handles the zero-terminated char* instead of slicing it.

char[] f = "C:/base/portavox/som/_fon102.wav".dup;
const(wchar)* arq = cast(const(wchar)*)&f;
void* nulo;
uint SND_FILENAME;
PlaySound (arq, nulo, SND_FILENAME);


Oh, that can be much, much, much simpler. Try

PlaySoundW("c:/file.wav"w.ptr, null, SND_FILENAME);


So a few notes:

* SND_FILENAME is a constant defined in the header. You shouldn't define it yourself, it isn't meant to be a variable.

* The PlaySoundW function takes a wstring, which you can get in D by sticking the `w` at the end of the literal. So `"foo"` is a normal string, but `"foo"w` is a wstring. (The difference is normal is utf-8, wstring is utf-16, which Windows uses internally.)

* It furthermore takes a pointer, but you want a pointer to data. A D string or array has a pointer internally you can fetch via the `.ptr` property. Windows expects this string to be zero-terminated... which D string literals are, but other D strings may not be. There's a function in `std.utf` that guarantees it:

http://dpldocs.info/experimental-docs/std.utf.toUTF16z.html

const(wchar)* safe_to_pass_to_windows = toUTF16z("your string");

Reply via email to