[Win32utils-devel] _mktemp on Windows

Daniel Berger djberg96 at gmail.com
Sun Jan 15 10:32:12 EST 2012


On Sun, Jan 15, 2012 at 8:06 AM, Daniel Berger <djberg96 at gmail.com> wrote:
> On Sun, Jan 15, 2012 at 1:59 AM, Heesob Park <phasis at gmail.com> wrote:
>> Hi,
>>
>> 2012/1/15 Daniel Berger <djberg96 at gmail.com>:
>>> On Fri, Jan 13, 2012 at 9:42 PM, Heesob Park <phasis at gmail.com> wrote:
>>>> Hi,
>>>>
>>>> 2012/1/14 Daniel Berger <djberg96 at gmail.com>:
>>>>> Is it odd that calling _mktemp more than once in the same process doesn't work?
>>>>>
>>>>> require 'ffi'
>>>>>
>>>>> class Win
>>>>>  extend FFI::Library
>>>>>
>>>>>  ffi_lib 'msvcrt'
>>>>>  attach_function :_mktemp, [:string], :string
>>>>>
>>>>>  def self.mktemp
>>>>>    5.times{ p _mktemp("rb_file_temp_XXXXXX") }
>>>>>  end
>>>>> end
>>>>>
>>>>> Win.mktemp
>>>>>
>>>>> # Output
>>>>> "rb_file_temp_a03680"
>>>>> nil
>>>>> nil
>>>>> nil
>>>>> nil
>>>>
>>>> According to MSDN, _mktemp is deprecated; consider using _mktemp_s instead.
>>>>
>>>> Refer to http://msdn.microsoft.com/en-us/library/34wc6k1f(v=vs.80).aspx
>>>
>>> True. I guess they were really serious about it this time. ;)
>>>
>>> Unfortunately I'm not sure how to get at _mktemp_s with FFI using
>>> mingw, since that function doesn't seem to be exported. Suggestions?
>>>
>>> Regards,
>>>
>>> Dan
>>
>> Well, I can use _mktemp_s with ffi using migw.
>> May be it depends on the version of msvcrt.dll.
>> C:\work>ruby -v mt.rb
>> ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
>> "rb_file_temp_a07024"
>>
>> =======================
>> require 'ffi'
>>
>> class Win
>>  extend FFI::Library
>>
>>  ffi_lib 'msvcrt'
>>  attach_function :_mktemp_s, [:string,:int], :string
>>
>>  def self.mktemp
>>   str = "rb_file_temp_XXXXXX"
>>   _mktemp_s(str,21)
>>   p str
>>  end
>> end
>>
>> Win.mktemp
>> ======================
>
> How?
>
> C:\>ruby test.rb
> c:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/library.rb:249:in
> `attach_function': Function '_mktemp_s' not found in [msvcrt] (FFI:
> :NotFoundError)
>        from test.rb:7:in `<class:Win>'
>        from test.rb:3:in `<main>'
>
> C:\>ruby -v
> ruby 1.9.3p0 (2011-10-30) [i386-mingw32]
>
> C:\>gem list
>
> *** LOCAL GEMS ***
>
> bigdecimal (1.1.0)
> ffi (1.0.11)
> io-console (0.3)
> json (1.5.4)
> minitest (2.5.1)
> rake (0.9.2.2)
> rdoc (3.9.4)
> rubygems-update (1.8.15)
>
> This is on Windows XP Pro.

I tried it again on Windows 7 and it worked. I guess it's not exported on XP.

Regards,

Dan


More information about the win32utils-devel mailing list