Solved

SHFileOperation with FOF_WANTMAPPINGHANDLE ??

Posted on 2000-03-02
9
740 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to determine file rename from EventLog ? 2 49
Change owner from userA to server Local Administrators 7 63
Path of Workbook 3 78
SQUD PROXY SERVER, UNIX, SLL/HTTPS 5 95
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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