[Win32utils-devel] PathFindExtension and wide strings

Heesob Park phasis at gmail.com
Mon Nov 10 21:19:44 EST 2008


2008/11/11 Daniel Berger <djberg96 at gmail.com>:
> Heesob Park wrote:
>>
>> 2008/11/11 Berger, Daniel <Daniel.Berger at qwest.com>:
>>>
>>> Hi,
>>>
>>> What's happening here?
>>>
>>> require 'windows/path'
>>> require 'windows/unicode'
>>>
>>> include Windows::Path
>>> include Windows::Unicode
>>>
>>> file_a = 'bar.txt'
>>> file_w = multi_to_wide(file_a)
>>>
>>> p PathFindExtensionA(file_a) # '.txt' => OK
>>> p PathFindExtensionW(file_w) # '.'    => WRONG
>>>
>>> Is Ruby chopping the result because of an embedding null? How do we deal
>>> with it?
>>>
>> The result is chopped by this code in api.c:
>>
>>      case _T_STRING:
>>         v_return = rb_str_new2((TCHAR*)return_value);
>>         break;
>>
>> In case of Unicode something like this would be possible:
>>
>>      case _T_STRING:
>>         v_return = rb_str_new((TCHAR*)return_value,
>> wcslen((wchar_t*)return_value));
>>         break;
>
> Ah, yes, thanks.
>
> That will work for ANSI, too, won't it? I don't have my C dev environment
> setup on this laptop yet (I've been lazy), so I can't test.
>
> If it will work for both Unicode and ANSI then please test and commit the
> change if all goes well. If not, what do you recommend?
>
That is not working for ANSI.
Though there might be a better way to detect unicode string,
I recommend this code:

case _T_STRING:
	{
	VALUE v_proc = rb_iv_get(self, "@effective_function_name");
	char *proc = RSTRING(v_proc)->ptr;
	if(proc[strlen(proc)-1]=='W')
		v_return = rb_str_new((TCHAR*)return_value,
				wcslen((wchar_t*)return_value)*2);
	else
		v_return = rb_str_new2((TCHAR*)return_value);
	}


Regards,

Park Heesob


More information about the win32utils-devel mailing list