Hi all,
I've been trying to write a function that sends a file, given the fully-qualified path, to the Recycle Bin. (To those who think you can just copy the file to "<logical-drive-name>\RECYCLER", you can't). I've posted my attempt below. The problem is, the file is not sent to the bin, and it's still stuck in the same directory. Essentially, the function doesn't work. My question is: what am I doing wrong?
JNIEXPORT void JNICALL Java_JNative_sendFileToRecycleBin (JNIEnv *env, jclass cls, jstring fileName)
{
LPCTSTR src;
HANDLE file;
SHFILEOPSTRUCT opFile;
src = (LPTSTR)(*env)->GetStringUTFChars (env, fileName, NULL);
file = CreateFile (src, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
// Checks if file exists.
if (file == INVALID_HANDLE_VALUE)
{
(*env)->ReleaseStringUTFChars (env, fileName, src);
(*env)->ThrowNew (env, (*env)->FindClass (env, "java/io/IOException"),
"Error while opening file.");
return;
}
CloseHandle (file);
opFile.wFunc = FO_DELETE;
opFile.pFrom = src;
opFile.fFlags = FOF_ALLOWUNDO;
if (!SHFileOperation (&opFile))
{
(*env)->ReleaseStringUTFChars (env, fileName, src);
(*env)->ThrowNew (env, (*env)->FindClass (env, "java/io/IOException"),
"Error while opening file.");
}
(*env)->ReleaseStringUTFChars (env, fileName, src);
}
Thanks in advance!