TooTallNate / ref

Turn Buffer instances into "pointers"
http://tootallnate.github.com/ref
453 stars 141 forks source link

LPCTSTR type #51

Closed ntlvs closed 8 years ago

ntlvs commented 8 years ago

Is there any way to create LPCTSTR type? I tried

var CString = ref.refType('CString');
var path = ref.alloc(CString, 'C:\\path\\to\\file');

and

var wchar_string = wchar.string;
var path = ref.alloc(wchar_string, 'C:\\path\\to\\file');

I use this with WinAPI but this file cannot be found. Any ideas? Thanks.

TooTallNate commented 8 years ago

Can you show me the Windows code that you're trying to emulate?

ntlvs commented 8 years ago
var wchar_string = wchar.string;
var path = ref.alloc(wchar_string, 'C:\\path\\to\\file');
var kernel = new ffi.Library('kernel32', {
    'GetFileAttributesW': ['int', [wchar_string]],
});
var file = kernel.GetFileAttributesW(path); // -1

or

var winm = new ffi.Library('winmm', {
    'PlaySoundW': ['bool', [wchar_string, intPtr, 'int']]
});
winm.PlaySoundW(path, ref.NULL, 0x00020000 | 0x0001); // default sound
TooTallNate commented 8 years ago

You don't need to do the ref.alloc() part, just pass the JS String directly to the PlaySoundW() function:

winm.PlaySoundW('C:\\path\\to\\file', ref.NULL, 0x00020000 | 0x0001); // default sound
ntlvs commented 8 years ago

It works. Thanks!