LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

SHFileOperation

I have made a CIN that utilizes SHFileOperation to send a file to the Recycle Bin.
 
It fails silently.  I know that hWnd is valid because I use MessageBox to validate it.  I think my problem may be with .PFrom and/or .pTo in the SHFILEOPSTRUCT, but I can't figure out what it is!
 
I bet it's something Stooooopid.
 
Any ideas?
 
/* CIN source file */
#include "extcode.h"
#include <windows.h>
#include <shellapi.h>   // for SHFileOperation
MgErr CINRun(int32 *hWnd, LStrHandle Filename, int32 *errNum);
MgErr CINRun(int32 *hWnd, LStrHandle Filename, int32 *errNum)
 {
//SHFileOperation allows moving file to recycle bin
WINSHELLAPI int WINAPI SHFileOperation(LPSHFILEOPSTRUCT lpFileOp);
 // Create the SHFILEOPSTRUCT
 SHFILEOPSTRUCT fos;
 // and empty it.
 memset(&fos, 0, sizeof(fos));
 // Set hwnd to the calling vi to make the
 // labview the owner of the progress dialog.
 fos.hwnd = (HWND) hWnd;
 // set pFrom to path string
 fos.pFrom = (const char *) LStrBuf(*Filename);
 // set pTo to NULL
 fos.pTo = NULL;
 // set fFlags to send file to Recycle Bin
 fos.fFlags = FOF_ALLOWUNDO;
 // set SHFileOperation function to Delete
 fos.wFunc = FO_DELETE;
 // perform Delete, set errnum (0 if OK)
 *errNum = SHFileOperation(&fos);

 // A simple message box, with only the OK button.
 MessageBox((HWND) *hWnd, LStrBuf(*Filename), "File Deletion",  MB_OK);
 
 return noErr;
 }
0 Kudos
Message 1 of 6
(3,739 Views)
If you want to send a file to recycle bin, try "Delete plus" in G Toolbox (http://gtoolbox.yeah.net)
 
George Zou
0 Kudos
Message 2 of 6
(3,734 Views)
Problem is, I am working with LabVIEW 7.1.1, and also I want to know HOW to do it, ot just be able to do it! Your vi is outdated and uncompilable on my version, and doesn't come with the source code.  But thanks anyway.
0 Kudos
Message 3 of 6
(3,728 Views)

There is an example online that does exactly what you are looking to do in CVI (NI's C environment) and it might be a good guide for you to follow. Check it out.

Kind Regards,

E. Sulzer
Applications Engineer
National Instruments
0 Kudos
Message 4 of 6
(3,711 Views)
Dosen't help me much.  LabWIndows reads a C string from the control.  I get a 'LSTRHandle' passed to the CIN, and have to use LSTRBUF to get the string, cast it to a char array and pass it to the DLL.  I believe I have done that, but no joy!
 
I believe my problem lies here:
 
// set pFrom to path string
 fos.pFrom = (LPCSTR) LStrBuf(*Filename);
 
Let's say I have a text file located at C:\Test.txt.  I can pass "C:\\Test.txt", "C:\\Test.txt\0", "C:\\Test.txt\0\0", "C:\Test.txt", or whatever, and it won't 'Recycle'.
0 Kudos
Message 5 of 6
(3,701 Views)

I found an interesting website on the SHFILEOPSTRUCT that I thought I'd share. It isn't necessarily directly related to what you are doing, but it talks about a particular hang-up that could be related to the problem you're experiencing. Two of the members of the SHNAMEMAPPINGS struct are declared as LPSTRs, however, they are laid out as DWORDs. I wonder if maybe the same is applicable to the two LPSTRs that are suspicious in your case (pTo and pFrom). At the bottom of the page, the website illustrates the correct way to accomodate them.

Kind Regards,

E. Sulzer
Applications Engineer
National Instruments
0 Kudos
Message 6 of 6
(3,691 Views)