# Ffi: How to use wchar\_t strings?

**URL:** <https://www.ruby-forum.com/t/ffi-how-to-use-wchar-t-strings/181810>\
**Category:** Ruby\
**Created:** [January 18, 2010, 6:06pm UTC](https://www.ruby-forum.com/t/ffi-how-to-use-wchar-t-strings/181810 "2010-01-18T18:06:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [January 18, 2010, 6:06pm UTC](https://www.ruby-forum.com/t/ffi-how-to-use-wchar-t-strings/181810/1 "2010-01-18T18:06:16Z")

</div>

Hello,

I’m trying to use the ffi gem to work with some WinAPI functions. Till  
now, I’ve used the win32-api gem which worked quite well, but I’d like  
to try out something new (and maybe easier 😉 ). So, here’s my  
question: How do I send a Unicode string (encoded in UTF-16LE) to a  
WinAPI function?

## This my code:

#Encoding: UTF-8  
require “ffi”

module Test  
extend FFI::Library

ffi\_lib “user32”  
ffi\_convention :stdcall

attach\_function :message\_box, :MessageBoxW, [:long, :string, :string,  
:int], :int  
end

module Test2  
extend FFI::Library

CP\_UTF8 = 65001  
MB\_PRECOMPOSED = 0x00000001

ffi\_lib “kernel32”  
ffi\_convention :stdcall

attach\_function :get\_last\_error, :GetLastError, [], :long  
attach\_function :multi\_byte\_to\_wide\_char, :MultiByteToWideChar,  
[:uint, :int, :string, :int, :buffer\_out, :int], :int  
end

buffer = “\0” \* 100\_000

#I know, I could use Rubies built-in encoding functions  
#and I WILL do so, but for now this is to ensure  
#everything is 100% correct as MS it expects.  
ret = Test2.multi\_byte\_to\_wide\_char(Test2::CP\_UTF8, 0, “Test”,  
“Test”.length, buffer, buffer.length)

buffer.force\_encoding(“UTF-16LE”)  
puts buffer.encode(“ISO-8859-1”).rstrip #Everything correct

## #The following line causes the error: Test.message\_box(0, buffer, buffer, 0)

## This is the error message:

## t2.rb:32:in `message_box': string contains null byte (ArgumentError) from t2.rb:32:in`’

An UTF-16LE encoded string _contains_ NUL bytes, I know–but I want to  
use Unicode functions, so: How to do that?

My ruby version: ruby 1.9.1p243 (2009-07-16 revision 24175)  
[i386-mingw32]  
My OS: Windows Vista Home Premium SP2 (32 bit)

Marvin

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [January 18, 2010, 9:04pm UTC](https://www.ruby-forum.com/t/ffi-how-to-use-wchar-t-strings/181810/2 "2010-01-18T21:04:05Z")

</div>

> ## This is the error message:
> 
> t2.rb:32:in `message_box': string contains null byte (ArgumentError) from t2.rb:32:in`’

Might be a bug in ffi. I’d ping their group.  
-r

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [January 19, 2010, 4:15am UTC](https://www.ruby-forum.com/t/ffi-how-to-use-wchar-t-strings/181810/3 "2010-01-19T04:15:49Z")

</div>

On Jan 18, 2:06 pm, Marvin Gülker [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## #Encoding: UTF-8 end attach\_function :get\_last\_error, :GetLastError, [], :long “Test”.length, buffer, buffer.length) t2.rb:32:in `message_box': string contains null byte (ArgumentError) from t2.rb:32:in `’
> 
> An UTF-16LE encoded string _contains_ NUL bytes, I know–but I want to  
> use Unicode functions, so: How to do that?

I think because :string definition is expecting a Ruby string, not a  
memory buffer as you’re trying to send.

wchar\_t should be defined as a new type and a proper memory pointer  
and buffer should be used to marshal the string from Ruby to the C  
realm 🙂

I suggest also bring this topic to FFI group:

[http://groups.google.com/group/ruby-ffi](http://groups.google.com/group/ruby-ffi)

Where you could get better pointers than my vague description.

Cheers,

---

<div class="post-metadata">

**Author:** ![quintus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/49beb7/32.png) [@quintus](https://www.ruby-forum.com/u/quintus)\
**Post date:** [January 19, 2010, 2:18pm UTC](https://www.ruby-forum.com/t/ffi-how-to-use-wchar-t-strings/181810/4 "2010-01-19T14:18:14Z")

</div>

Luis L. wrote:

> I think because :string definition is expecting a Ruby string, not a  
> memory buffer as you’re trying to send.
> 
> wchar\_t should be defined as a new type and a proper memory pointer  
> and buffer should be used to marshal the string from Ruby to the C  
> realm 🙂  
> Maybe true. I’m new to ffi, so I’ll have to look how to define my own  
> type.

> I suggest also bring this topic to FFI group:
> 
> [http://groups.google.com/group/ruby-ffi](http://groups.google.com/group/ruby-ffi)
> 
> Where you could get better pointers than my vague description.  
> I’ve done so:  
> [http://groups.google.com/group/ruby-ffi/browse\_thread/thread/ada0ea99600b7fa1](http://groups.google.com/group/ruby-ffi/browse_thread/thread/ada0ea99600b7fa1)

Marvin
