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
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 > nilAccording to MSDN, _mktemp is deprecated; consider using _mktemp_s instead. Refer to http://msdn.microsoft.com/en-us/library/34wc6k1f(v=vs.80).aspx Regards, Park Heesob
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).aspxTrue. 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
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, > > DanWell, 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 ===================== Regards, Park Heesob
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. Regards, Dan
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
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 > nilIt is odd indeed. _mktemp not working in the loop. require ''ffi'' class Win extend FFI::Library ffi_lib ''msvcrt'' attach_function :_mktemp, [:string], :string def self.mktemp p _mktemp("rb_file_temp_XXXXXX") p _mktemp("rb_file_temp_XXXXXX") p _mktemp("rb_file_temp_XXXXXX") p _mktemp("rb_file_temp_XXXXXX") p _mktemp("rb_file_temp_XXXXXX") 5.times { p _mktemp("rb_loop_temp_XXXXXX") } end end Win.mktemp # Output "rb_file_temp_a05972" "rb_file_temp_a05972" "rb_file_temp_a05972" "rb_file_temp_a05972" "rb_file_temp_a05972" "rb_loop_temp_a05972" nil nil nil nil Regards, Park Heesob
On Sun, Jan 15, 2012 at 8:44 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 > It is odd indeed. > _mktemp not working in the loop. > > require ''ffi'' > > class Win > ?extend FFI::Library > > ?ffi_lib ''msvcrt'' > ?attach_function :_mktemp, [:string], :string > > ?def self.mktemp > ? p _mktemp("rb_file_temp_XXXXXX") > ? p _mktemp("rb_file_temp_XXXXXX") > ? p _mktemp("rb_file_temp_XXXXXX") > ? p _mktemp("rb_file_temp_XXXXXX") > ? p _mktemp("rb_file_temp_XXXXXX") > ?5.times { > ? p _mktemp("rb_loop_temp_XXXXXX") > ?} > ?end > end > > Win.mktemp > > ?# Output > "rb_file_temp_a05972" > "rb_file_temp_a05972" > "rb_file_temp_a05972" > "rb_file_temp_a05972" > "rb_file_temp_a05972" > "rb_loop_temp_a05972" > nil > nil > nil > nilI noticed that in the example of the bottom of the _mktemp documentation that they don''t use the char* string directly, but instead strcpy it to an array of chars and use that. I guess there''s a limit of 26 names. Regards, Dan
Hi, 2012/1/16 Daniel Berger <djberg96 at gmail.com>:> On Sun, Jan 15, 2012 at 8:44 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 >> It is odd indeed. >> _mktemp not working in the loop. >> >> require ''ffi'' >> >> class Win >> ?extend FFI::Library >> >> ?ffi_lib ''msvcrt'' >> ?attach_function :_mktemp, [:string], :string >> >> ?def self.mktemp >> ? p _mktemp("rb_file_temp_XXXXXX") >> ? p _mktemp("rb_file_temp_XXXXXX") >> ? p _mktemp("rb_file_temp_XXXXXX") >> ? p _mktemp("rb_file_temp_XXXXXX") >> ? p _mktemp("rb_file_temp_XXXXXX") >> ?5.times { >> ? p _mktemp("rb_loop_temp_XXXXXX") >> ?} >> ?end >> end >> >> Win.mktemp >> >> ?# Output >> "rb_file_temp_a05972" >> "rb_file_temp_a05972" >> "rb_file_temp_a05972" >> "rb_file_temp_a05972" >> "rb_file_temp_a05972" >> "rb_loop_temp_a05972" >> nil >> nil >> nil >> nil > > I noticed that in the example of the bottom of the _mktemp > documentation that they don''t use the char* string directly, but > instead strcpy it to an array of chars and use that. I guess there''s a > limit of 26 names. >I want to know why sequence of _mktemp works and _mktemp in the loop not works after first call. More over _mktemp_s segfaults in the loop after first call. #code require ''ffi'' class Win extend FFI::Library ffi_lib ''msvcrt'' ffi_convention :stdcall attach_function :_mktemp_s, [:string,:int], :string def self.mktemp 3.times { str = "rb_file_temp_XXXXXX" _mktemp_s(str,21) p str } end end Win.mktemp #output "rb_file_temp_a03212" mt.rb:13: [BUG] Segmentation fault ruby 1.9.3p0 (2011-10-30) [i386-mingw32] -- Control frame information ----------------------------------------------- c:0008 p:---- s:0022 b:0022 l:000021 d:000021 CFUNC :_mktemp_s c:0007 p:0027 s:0017 b:0017 l:000008 d:000016 BLOCK mt.rb:13 c:0006 p:---- s:0014 b:0014 l:000013 d:000013 FINISH c:0005 p:---- s:0012 b:0012 l:000011 d:000011 CFUNC :times c:0004 p:0012 s:0009 b:0009 l:000008 d:000008 METHOD mt.rb:11 c:0003 p:0037 s:0006 b:0006 l:001d0c d:00150c EVAL mt.rb:19 c:0002 p:---- s:0004 b:0004 l:000003 d:000003 FINISH c:0001 p:0000 s:0002 b:0002 l:001d0c d:001d0c TOP -- Ruby level backtrace information ---------------------------------------- mt.rb:19:in `<main>'' mt.rb:11:in `mktemp'' mt.rb:11:in `times'' mt.rb:13:in `block in mktemp'' mt.rb:13:in `_mktemp_s'' -- C level backtrace information ------------------------------------------- C:\Windows\SysWOW64\ntdll.dll(ZwWaitForSingleObject+0x15) [0x7756f8b1] C:\Windows\syswow64\kernel32.dll(WaitForSingleObjectEx+0x43) [0x75151194] C:\Windows\syswow64\kernel32.dll(WaitForSingleObject+0x12) [0x75151148] C:\Ruby193\bin\msvcrt-ruby191.dll(rb_vm_bugreport+0xf9) [0x62e5acc9] C:\Ruby193\bin\msvcrt-ruby191.dll(rb_name_err_mesg_new+0x17a) [0x62d3a68e] C:\Ruby193\bin\msvcrt-ruby191.dll(rb_bug+0x2f) [0x62d3b3ef] C:\Ruby193\bin\msvcrt-ruby191.dll(rb_check_safe_str+0x1a4) [0x62ded27c] [0x004011e6] C:\Windows\syswow64\kernel32.dll(GetProfileStringW+0x12aa3) [0x7519003f] C:\Windows\SysWOW64\ntdll.dll(RtlKnownExceptionFilter+0xb7) [0x775c74df] -- Other runtime information ----------------------------------------------- * Loaded script: mt.rb * Loaded features: 0 enumerator.so 1 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/encdb.so 2 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/cp949.so 3 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/trans/transdb.so 4 C:/Ruby193/lib/ruby/1.9.1/rubygems/defaults.rb 5 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/rbconfig.rb 6 C:/Ruby193/lib/ruby/1.9.1/rubygems/deprecate.rb 7 C:/Ruby193/lib/ruby/1.9.1/rubygems/exceptions.rb 8 C:/Ruby193/lib/ruby/1.9.1/rubygems/defaults/operating_system.rb 9 C:/Ruby193/lib/ruby/1.9.1/rubygems/custom_require.rb 10 C:/Ruby193/lib/ruby/1.9.1/rubygems.rb 11 C:/Ruby193/lib/ruby/1.9.1/rubygems/version.rb 12 C:/Ruby193/lib/ruby/1.9.1/rubygems/requirement.rb 13 C:/Ruby193/lib/ruby/1.9.1/rubygems/platform.rb 14 C:/Ruby193/lib/ruby/1.9.1/rubygems/specification.rb 15 C:/Ruby193/lib/ruby/1.9.1/rubygems/path_support.rb 16 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi_c.so 17 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/platform.rb 18 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/types.rb 19 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/library.rb 20 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/errno.rb 21 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/pointer.rb 22 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/memorypointer.rb 23 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/struct_layout_build er.rb 24 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/struct.rb 25 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/union.rb 26 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/managedstruct.rb 27 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/callback.rb 28 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/io.rb 29 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/autopointer.rb 30 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/variadic.rb 31 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/enum.rb 32 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi/ffi.rb 33 C:/Ruby193/lib/ruby/gems/1.9.1/gems/ffi-1.0.11/lib/ffi.rb 34 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/utf_16le.so 35 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/trans/korean.so 36 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/trans/utf_16_32.so 37 C:/Ruby193/lib/ruby/1.9.1/i386-mingw32/enc/trans/single_byte.so [NOTE] You may have encountered a bug in the Ruby interpreter or extension libraries. Bug reports are welcome. For details: http://www.ruby-lang.org/bugreport.html This application has requested the Runtime to terminate it in an unusual way. Please contact the application''s support team for more information. But, calling without loop block works. require ''ffi'' class Win extend FFI::Library ffi_lib ''msvcrt'' ffi_convention :stdcall attach_function :_mktemp_s, [:string,:int], :string def self.mktemp str = "rb_file_temp_XXXXXX" _mktemp_s(str,21) p str str = "rb_file_temp_XXXXXX" _mktemp_s(str,21) p str str = "rb_file_temp_XXXXXX" _mktemp_s(str,21) p str end end Win.mktemp #output "rb_file_temp_a03940" "rb_file_temp_a03940" "rb_file_temp_a03940" Regards, Park Heesob
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 > nilHere is a proper working version: require ''ffi'' class Win extend FFI::Library ffi_lib ''msvcrt'' attach_function :_mktemp, [:pointer], :string def self.mktemp 5.times { buf = FFI::MemoryPointer.from_string("rb_file_temp_XXXXXX") p _mktemp(buf) } end end Win.mktemp Regards, Park Heesob
On Tue, Jan 17, 2012 at 5:00 AM, 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 > > Here is a proper working version: > > > require ''ffi'' > > class Win > ?extend FFI::Library > > ?ffi_lib ''msvcrt'' > ?attach_function :_mktemp, [:pointer], :string > > ?def self.mktemp > ?5.times { > ? buf = FFI::MemoryPointer.from_string("rb_file_temp_XXXXXX") > ? p _mktemp(buf) > ?} > ?end > end > > Win.mktempThat''s better, though it seems to return the same string each time instead of providing a new one. I get: "rb_file_temp_a03200" "rb_file_temp_a03200" "rb_file_temp_a03200" "rb_file_temp_a03200" "rb_file_temp_a03200" Regards, Dan
On Tue, Jan 17, 2012 at 11:36 AM, Daniel Berger <djberg96 at gmail.com> wrote:> On Tue, Jan 17, 2012 at 5:00 AM, 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 >> >> Here is a proper working version: >> >> >> require ''ffi'' >> >> class Win >> ?extend FFI::Library >> >> ?ffi_lib ''msvcrt'' >> ?attach_function :_mktemp, [:pointer], :string >> >> ?def self.mktemp >> ?5.times { >> ? buf = FFI::MemoryPointer.from_string("rb_file_temp_XXXXXX") >> ? p _mktemp(buf) >> ?} >> ?end >> end >> >> Win.mktemp > > That''s better, though it seems to return the same string each time > instead of providing a new one. I get: > > "rb_file_temp_a03200" > "rb_file_temp_a03200" > "rb_file_temp_a03200" > "rb_file_temp_a03200" > "rb_file_temp_a03200"Actually, I get the same result with the C code that MS provides, too. I think I might be better off writing a custom mktemp method in pure Ruby. Regards, Dan
Hi, 2012/1/18 Daniel Berger <djberg96 at gmail.com>:> On Tue, Jan 17, 2012 at 11:36 AM, Daniel Berger <djberg96 at gmail.com> wrote: >> On Tue, Jan 17, 2012 at 5:00 AM, 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 >>> >>> Here is a proper working version: >>> >>> >>> require ''ffi'' >>> >>> class Win >>> ?extend FFI::Library >>> >>> ?ffi_lib ''msvcrt'' >>> ?attach_function :_mktemp, [:pointer], :string >>> >>> ?def self.mktemp >>> ?5.times { >>> ? buf = FFI::MemoryPointer.from_string("rb_file_temp_XXXXXX") >>> ? p _mktemp(buf) >>> ?} >>> ?end >>> end >>> >>> Win.mktemp >> >> That''s better, though it seems to return the same string each time >> instead of providing a new one. I get: >> >> "rb_file_temp_a03200" >> "rb_file_temp_a03200" >> "rb_file_temp_a03200" >> "rb_file_temp_a03200" >> "rb_file_temp_a03200" > > Actually, I get the same result with the C code that MS provides, too. > > I think I might be better off writing a custom mktemp method in pure Ruby. >I guess you overlooked the C code that MS provides, it created file with temp names. If you want the same result with the C code, here is a modified version: require ''ffi'' class Win extend FFI::Library ffi_lib ''msvcrt'' attach_function :_mktemp, [:pointer], :string def self.mktemp 5.times { buf = FFI::MemoryPointer.from_string("rb_file_temp_XXXXXX") p res = _mktemp(buf) File.open(res,"w") } end end Win.mktemp #output "rb_file_temp_a03144" "rb_file_temp_b03144" "rb_file_temp_c03144" "rb_file_temp_d03144" "rb_file_temp_e03144" Regards, Park Heesob
On Tue, Jan 17, 2012 at 5:47 PM, Heesob Park <phasis at gmail.com> wrote:> Hi, > > 2012/1/18 Daniel Berger <djberg96 at gmail.com>: >> On Tue, Jan 17, 2012 at 11:36 AM, Daniel Berger <djberg96 at gmail.com> wrote: >>> On Tue, Jan 17, 2012 at 5:00 AM, 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 >>>> >>>> Here is a proper working version: >>>> >>>> >>>> require ''ffi'' >>>> >>>> class Win >>>> ?extend FFI::Library >>>> >>>> ?ffi_lib ''msvcrt'' >>>> ?attach_function :_mktemp, [:pointer], :string >>>> >>>> ?def self.mktemp >>>> ?5.times { >>>> ? buf = FFI::MemoryPointer.from_string("rb_file_temp_XXXXXX") >>>> ? p _mktemp(buf) >>>> ?} >>>> ?end >>>> end >>>> >>>> Win.mktemp >>> >>> That''s better, though it seems to return the same string each time >>> instead of providing a new one. I get: >>> >>> "rb_file_temp_a03200" >>> "rb_file_temp_a03200" >>> "rb_file_temp_a03200" >>> "rb_file_temp_a03200" >>> "rb_file_temp_a03200" >> >> Actually, I get the same result with the C code that MS provides, too. >> >> I think I might be better off writing a custom mktemp method in pure Ruby. >> > I guess you overlooked the C code that MS provides, it created file > with temp names. > If you want the same result with the C code, here is a modified version: > > require ''ffi'' > > class Win > ?extend FFI::Library > > ?ffi_lib ''msvcrt'' > ?attach_function :_mktemp, [:pointer], :string > > ?def self.mktemp > ?5.times { > ? buf = FFI::MemoryPointer.from_string("rb_file_temp_XXXXXX") > ? p res = _mktemp(buf) > ? File.open(res,"w") > ?} > ?end > end > > Win.mktemp > > #output > "rb_file_temp_a03144" > "rb_file_temp_b03144" > "rb_file_temp_c03144" > "rb_file_temp_d03144" > "rb_file_temp_e03144"Oh, I see. It doesn''t actually create a unique name unless the file is actually created. It doesn''t just automatically bump the name. Thanks, Dan