Solved

SHFileOperation with FOF_WANTMAPPINGHANDLE ??

Posted on 2000-03-02
9
742 Views
Last Modified: 2013-11-20
Can somebody tell me what is wrong with this piece of code??
pShNM doesn't points to an SHNAMEMAPPING struct

SHFILEOPSTRUCT      shfo;
TCHAR from[256];
TCHAR to[256];

_tcscpy(from, _T("c:\\adobeapp\\test.txt"));
_tcscpy(to, _T("c:\\test.txt"));
from[_tcslen(from) +1]=0;
to[_tcslen(to)+1]=0;

memset(&shfo, 0x00, sizeof(shfo));
shfo.hwnd = AfxGetMainWnd()->m_hWnd;
shfo.wFunc = FO_COPY;
shfo.pFrom = from;
shfo.pTo = to;
shfo.fFlags = FOF_RENAMEONCOLLISION|FOF_WANTMAPPINGHANDLE;

if (0 == ::SHFileOperation(&shfo))
{
    if (shfo.hNameMappings)
    {
      SHNAMEMAPPING nameMapping;

        // try to obtain a pointer
        // to the first (and in this
        // case the only)
      // SHNAMEMAPPING struct of the
        // structure pointed to by
        // shfo.hNameMappings

        // THIS IS THE PROBLEM: pShNM doesn't point to an
        // SHNAMEMAPPING struct when the file being operated on
        // was renamed by the file system
      SHNAMEMAPPING* pShNM =   (SHNAMEMAPPING*)((char*)shfo.hNameMappings + sizeof(int));
            }

            ::SHFreeNameMappings((HANDLE)shfo.hNameMappings);
}

The docs say the following: "Treat hNameMappings as a pointer to a structure whose first member is an INT value, followed by an array of SHNAMEMAPPING structures. The INT value will be set to the number of structures in the array. Each SHNAMEMAPPING structure will contain the old and new path name for one of the renamed files."

The first member of the struct pointed to by SHFILEOPSTRUCT.hNameMappings (the number of SHNAMEMAPPING structs that will follow this int value) seems correct but the bytes that follow this integer value are just junk ... and not an SHNAMEMAPPING struct
Environment: NT4 SP5 IE5.0
(maybe it's something trivial but I don't know what I'm doing wrong)
0
Comment
Question by:searching
  • 7
9 Comments
 

Author Comment

by:searching
ID: 2577814
Adjusted points to 150
0
 
LVL 8

Expert Comment

by:gelbert
ID: 2577843
I think under NT SHNAMEMAPPING includes UNICODE strings. So you would have to convert them.
Example:

SHNAMEMAPPING* pShNM =   (SHNAMEMAPPING*)((char*)shfo.hNameMappings + sizeof(int));


TCHAR szOldPath[MAX_PATH] = {0};
WideCharToMultiByte(CP_ACP, 0, reinterpret_cast<LPWSTR>(pShNM->pszOldPath), MAX_PATH, szOldPath, MAX_PATH, NULL, NULL );

Under Win95 you should not have this problem

0
 

Author Comment

by:searching
ID: 2579251
Edited text of question.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:searching
ID: 2579275
Edited text of question.
0
 

Author Comment

by:searching
ID: 2579279
Adjusted points to 200
0
 

Author Comment

by:searching
ID: 2579280
I checked the memory pointed to by pShNM and it doesn't points to a wide character version of an SHNAMEMAPPING struct ...
Anyway tnx for the tip gelbert, all further suggestions are welcome ...
0
 

Author Comment

by:searching
ID: 2579678
Adjusted points to 225
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 225 total points
ID: 2580297
I think I've found it:

It seems MSDN's description is buggy. The hNameMappings does NOT point to an int followed by an array of SHNAMEMAPPING structures. It points to an int follows by an array of pointers to SHNAMEMAPPING structures.

I.e. following code works on my system:

SHNAMEMAPPING* pShNM =   (SHNAMEMAPPING*)*( (int*)shfo.hNameMappings + 1 );
TCHAR szOldPath[MAX_PATH] = {0};
TCHAR szNewPath[MAX_PATH] = {0};
WideCharToMultiByte(CP_ACP, 0, reinterpret_cast<LPWSTR>(pShNM->pszOldPath), MAX_PATH, szOldPath, MAX_PATH, NULL, NULL );
WideCharToMultiByte(CP_ACP, 0, reinterpret_cast<LPWSTR>(pShNM->pszNewPath), MAX_PATH, szNewPath, MAX_PATH, NULL, NULL );
printf( "Renamed %s to %s\n", szOldPath, szNewPath );

hope that helps,

ZOPPO
0
 

Author Comment

by:searching
ID: 2580712
Many tnx Zoppo, you deserve the points!

BTW: In my opinion it's awfull that MS don't describe this bug in their Knowledge Base, after all this function exists since the introduction of Win32 if I'm not wrong ...
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Unix / Linux grid computing 5 163
Add content to output file 4 67
tripleUp challenge 7 86
unix example issues 18 98
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question