www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Newbie: can't manage some types...

reply Cleverson Casarin Uliana via Digitalmars-d-learn writes:
Hello all, I'm trying to do two tasks which involves some type
conversion, and I'm having dificulties, probably because I haven't yet
understood well how such types works.

First, I wanted to convert UTF-8 strings to ansi, so it displays
correctly at the Windows command prompt. One function to do that is
"toMBSz" from std.windows.charset, which is declared as follows:
    const(char)* toMBSz(in char[] s, uint codePage = 0);

So, after some struggling, I managed to write the following code:

import std.stdio;
import std.windows.charset;
void main() {
string s = "Testando acentuação";
auto r = toMBSz (s, 1);
writeln (r[0..19]);
}

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...

I'd also like to play a sound file, so tried to use the function
"PlaySound" from core.sys.windows.mmsystem, declared as follows:
BOOL PlaySoundW(LPCWSTR, HMODULE, DWORD);

According to some error messages I receive, the first parameter is of
type const(char)*, which corresponds to the sound file name, but I
just can't figure out how to convert the string (or a char array) to
that type... Could you please give some snippet example? The closest I
have come, which doesn't compile at all, is as follows:
import core.sys.windows.mmsystem;

void main() {
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);
}

Thank you,
Cleverson
Oct 31 2016
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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");
Oct 31 2016
parent reply Cleverson Casarin Uliana via Digitalmars-d-learn writes:
Thank you very much, Adam, now I'm receiving another strange error. My
code is this:

import core.sys.windows.mmsystem;

void main() {
PlaySoundW("C:/base/portavox/som/_fon102.wav"w.ptr, null, SND_FILENAME);
}

When trying to compile, it returns:
OPTLINK (R) for Win32  Release 8.00.17
Copyright (C) Digital Mars 1989-2013  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
tocaSom.obj(tocaSom)
 Error 42: Symbol Undefined _PlaySoundW 12
--- errorlevel 1

I made some quick searches, but not sure whether there is some error
messages' index ?

Thanks for helping,
Cleverson
Oct 31 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 31 October 2016 at 14:02:01 UTC, Cleverson Casarin 
Uliana wrote:
  Error 42: Symbol Undefined _PlaySoundW 12
This is the most common linker error, it means you used a function without including the library. PlaySound's docs https://msdn.microsoft.com/en-us/library/windows/desktop/dd743680%28v=vs.85%29.aspx at the bottom list some facts about it. One is "Library - winmm.lib" When you use that function, gotta include the library file somehow. Easiest is to just list it on your compile command: dmd yourfile.d winmm.lib since winmm.lib is provided with the operating system, that should just work.
Oct 31 2016
next sibling parent Cleverson Casarin Uliana via Digitalmars-d-learn writes:
Thanks Adam for your kindness, it Works now.

Thanks Alfred for the hint on setting the console output to 65001,
will be useful as well.

Greetings
Cleverson
Oct 31 2016
prev sibling parent zabruk70 <sorry noem.ail> writes:
On Monday, 31 October 2016 at 16:06:48 UTC, Adam D. Ruppe wrote:
 dmd yourfile.d winmm.lib
i personally like https://dlang.org/spec/pragma.html#lib pragma(lib, "winmm.lib");
Oct 31 2016
prev sibling parent Alfred Newman <alfredonewman gmail.com> writes:
On Monday, 31 October 2016 at 11:44:25 UTC, Cleverson Casarin 
Uliana wrote:
 Hello all, I'm trying to do two tasks which involves some type 
 conversion, and I'm having dificulties, probably because I 
 haven't yet understood well how such types works.

 First, I wanted to convert UTF-8 strings to ansi, so it displays
 correctly at the Windows command prompt. One function to do 
 that is
 "toMBSz" from std.windows.charset, which is declared as follows:
     const(char)* toMBSz(in char[] s, uint codePage = 0);

 So, after some struggling, I managed to write the following 
 code:

 import std.stdio;
 import std.windows.charset;
 void main() {
 string s = "Testando acentuação";
 auto r = toMBSz (s, 1);
 writeln (r[0..19]);
 }

 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...

 I'd also like to play a sound file, so tried to use the 
 function "PlaySound" from core.sys.windows.mmsystem, declared 
 as follows: BOOL PlaySoundW(LPCWSTR, HMODULE, DWORD);

 According to some error messages I receive, the first parameter 
 is of
 type const(char)*, which corresponds to the sound file name, 
 but I
 just can't figure out how to convert the string (or a char 
 array) to
 that type... Could you please give some snippet example? The 
 closest I
 have come, which doesn't compile at all, is as follows:
 import core.sys.windows.mmsystem;

 void main() {
 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);
 }

 Thank you,
 Cleverson
Cleverson, About your question related to "Testando acentuação", and assuming you're using Windows, you can also do the following: import std.stdio, std.string; //A Windows function to set the code page of the console output extern (Windows): private int SetConsoleOutputCP(uint codepage); void main() { SetConsoleOutputCP(65001); string s = "Testando acentuação"; writeln("Output: ", s.toUpper()); } Cheers
Oct 31 2016