Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to write htis code using IFileOperation in Vista

924 views
Skip to first unread message

Mustanseer M S

unread,
Apr 28, 2008, 6:36:01 AM4/28/08
to
Hello,

I have this piece of code which i am using in my application developed on
VS2003. i have migrated the code to VS2005 and have cleaned the code, but now
i have to port it to Vista.

The code uses SHFILEOPSTRUCT and SHFileOperation which are nwo deprecated
and a new interface IFileOperation has been introduced for Vista. I did a lot
of head banging but still i could not figure out how to modify my code except
the use of SetOperationFlag() function of IFileInterface. So please see if
you could help.

Here are the snippets

//For Deleting a file

SHFILEOPSTRUCT shfileop;

shfileop.hwnd = NULL;

shfileop.wFunc = FO_DELETE;

shfileop.pFrom = pstrfrom;

shfileop.pTo = NULL;

shfileop.fFlags = FOF_NOCONFIRMATION;

shfileop.fAnyOperationsAborted = 0;

shfileop.hNameMappings = 0;

shfileop.lpszProgressTitle = 0;

SHFileOperation(&shfileop);

I get the pFrom and pTo from LPCTSTR type strings. I would also like to know
about the double null termination of strings.

Thanks and regards,

Mustanseer


David Lowndes

unread,
Apr 28, 2008, 7:08:29 AM4/28/08
to
>The code uses SHFILEOPSTRUCT and SHFileOperation which are nwo deprecated

The MSDN documentation is perhaps being a bit liberal in its
descriptions of "replaced" - it's more a case of "extended". The API
should still work fine in Vista so if it does what you want in
previous OS, there should be no need to change it.

>I get the pFrom and pTo from LPCTSTR type strings. I would also like to know
>about the double null termination of strings.

What in particular do you want to know that's not mentioned in the
documentation
http://msdn2.microsoft.com/en-us/library/bb759795(VS.85).aspx ?

The community content at
http://msdn2.microsoft.com/en-us/library/bb762164.aspx also mentions
it.

Dave

Giovanni Dicanio

unread,
Apr 28, 2008, 7:30:59 AM4/28/08
to
Hi,

I would use code something like this (I need to better test that, however):

If you don't want to be distracted from COM error checking, these are some
steps (also outlined by comments in source code):


Convert from TCHAR to wchar_t
because IFileOperation::DeleteItem works only
with Unicode UTF-16 strings.

Initialize COM engine

Create COM instance of IFileOperation

Set parameters for current operation

Create IShellItem instance associated to file to delete

Declare this shell item (file) to be deleted

Perform the deleting operation

Cleanup COM


I called the function DeletFileWithIFO to disambiguate from DeleteFile.
(IFO = IFileOperation COM interface)

<code>

//=======================================================================
//
// Deletes a file given its name (with full path).
//
// Uses new Vista IFileOperation COM interface
// (works in both ANSI/MBCS and Unicode builds, thanks to internal
// string conversion).
//
// Check HRESULT return value to see if operation was successful
// (SUCCEEDED( DeleteFile(...) )).
//
//=======================================================================
HRESULT DeleteFileWithIFO( LPCTSTR szFilename )
{
//
// Check input parameter
//
ASSERT( szFilename != NULL );
if ( szFilename == NULL )
return E_POINTER;


//
// Convert from TCHAR to wchar_t
// because IFileOperation::DeleteItem works only
// with Unicode UTF-16 strings.
//
CT2W wszFileToDelete( szFilename );


//
// Initialize COM engine
//
HRESULT hr = CoInitializeEx(NULL,
COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);

if (SUCCEEDED(hr))
{
//
// Create COM instance of IFileOperation
//
IFileOperation *pfo = NULL;
hr = CoCreateInstance(CLSID_FileOperation, NULL,
CLSCTX_ALL, IID_PPV_ARGS(&pfo));

if (SUCCEEDED(hr))
{
//
// Set parameters for current operation
//
hr = pfo->SetOperationFlags(
FOF_SILENT | // do not display progress dialog-box
FOF_NOERRORUI // do not display error message to the user
);

if (SUCCEEDED(hr))
{
//
// Create IShellItem instance associated to file to delete
//
IShellItem *psiFileToDelete = NULL;
hr = SHCreateItemFromParsingName(
wszFileToDelete, NULL,
IID_PPV_ARGS(&psiFileToDelete));

if (SUCCEEDED(hr))
{
//
// Declare this shell item (file) to be deleted
//
hr = pfo->DeleteItem( psiFileToDelete, NULL );
}

// Cleanup file-to-delete shell item
psiFileToDelete->Release();
psiFileToDelete = NULL;

}

if (SUCCEEDED(hr))
{
//
// Perform the deleting operation
//
hr = pfo->PerformOperations();
}
}

// Cleanup file operation object
pfo->Release();
pfo = NULL;
}

//
// Cleanup COM
//
CoUninitialize();


//
// Return operation result
//
return hr;
}
</code>


HTH,
Giovanni

"Mustanseer M S" <Mustan...@discussions.microsoft.com> ha scritto nel
messaggio news:8B64DCAA-F1E8-46BE...@microsoft.com...

Mustanseer M S

unread,
Apr 29, 2008, 1:05:00 AM4/29/08
to
Hi Giovanni,

Thank you for your trouble. You code is alomst proper but i am yet not able
to check it as i have not got my hand on the new SDK on .NET framework 3.5
and hence don't have the .h file which contains the interface definition.
Yet, i am quite sure this is what i was looking for. Thanks once again.

Regards,
Mustanseer.

0 new messages