In current project there is situation when file should not be just deleted but deleted to Recycle Bin. It is possible to do this by executing SHFileOperation. The main problem is how to do this from ruby. This example shows how to execute external method with passing structure as a parameter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
require 'dl/import' require 'dl/struct' module User32 extend DL::Importable typealias "BOOL", "int" typealias "UINT", "unsigned int" typealias "PVOID", "void *" typealias "LONG", "long" typealias "HWND", "void *" typealias "LPCTSTR", "char *" typealias "LPVOID", "void *" typealias "LPCTSTR", "char *" typealias "FILEOP_FLAGS", "long" SHFileOperation = struct [ "HWND hwnd", "UINT wFunc", "LPCTSTR pFrom", "LPCTSTR pTo", "FILEOP_FLAGS fFlags", "BOOL fAnyOperationsAborted", "LPVOID hNameMappings", "LPCTSTR lpszProgressTitle" ] dlload "shell32.dll" extern "BOOL SHFileOperation(PVOID)" FO_DELETE = 3; FOF_ALLOWUNDO = 64; FOF_NOCONFIRMATION = 16; def self.trash_file(file) file_info = User32::SHFileOperation::malloc file_info.wFunc = FO_DELETE file_info.pFrom = file file_info.pTo = file file_info.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION file_info.wFunc = FO_DELETE puts file_info.pFrom if File.readable?(file) sHFileOperation(file_info.to_ptr) end end end |


