Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Wherever and whenever I write to lpsh "type: LPSHFILEOPSTRUCTA" that raises the error:
Exception thrown: write access violation.
lpsh was nullptr.


What I have tried:

I've tried
1-Writing to the unwritten portions of LPSHFILEOPSTRUCTA (filled ones were hwnd, wFunc, pFrom, pTo and fFlags only) but it didn't work
2-freeing the memory of lpsh using ZeroMemory but it didn't work, too.

Here's my code:
C++
SHFILEOPSTRUCTA lpsh{};
// old api
			    // ignore most of stuff, the wanted ones are wFunc, pFrom, pTo amd fFlags.
// Here I have choosed LPSHFILEOPSTRUCTA for maximum compatabilty
			    ZeroMemory(&lpsh, sizeof(SHFILEOPSTRUCT));
			    BOOL fc{};
				LPVOID pfs{};
				lpsh.hwnd = hWnd;
				lpsh.wFunc = FO_COPY;
				lpsh.pFrom = (LPCSTR)"C:\\Users\\*.*\0";
				lpsh.pTo = (LPCSTR)"C:\\UData\0";
				lpsh.fFlags = FOF_ALLOWUNDO | FOF_NOCOPYSECURITYATTRIBS;
				lpsh.fAnyOperationsAborted = fc;
				lpsh.hNameMappings = pfs;
				SHFileOperationA(lpsh);

How do I solve this?
Posted
Updated 30-Mar-23 1:58am
Comments
Rick York 30-Mar-23 11:16am    
One detail : ZeroMemory does not free memory - it sets it to all zeros.

1 solution

You seem to be confused about the correct use of pointers in your code. You define lpsh as:
C++
SHFILEOPSTRUCTA lpsh{};

But that is not an array, it is a structure, so you need to pass its address to the SHFileOperationA function. You should rename it (and pfs) so it makes more sense:
C++
SHFILEOPSTRUCTA shFileOPStruct{};
			    ZeroMemory(&shFileOPStruct, sizeof(SHFILEOPSTRUCT));
			    // BOOL fc{}; this is not needed.
				shFileOPStruct.hwnd = hWnd;
				shFileOPStruct.wFunc = FO_COPY;
				shFileOPStruct.pFrom = (LPCSTR)"C:\\Users\\*.*\0";
				shFileOPStruct.pTo = (LPCSTR)"C:\\UData\0";
				shFileOPStruct.fFlags = FOF_ALLOWUNDO | FOF_NOCOPYSECURITYATTRIBS;
				shFileOPStruct.fAnyOperationsAborted = 0; // this receives a status return value.
				HANDLE fsHandle = 0; // see the documentation for complete details 	
        		shFileOPStruct.hNameMappings = &fsHandle; // this needs to be a pointer to a handle 
				SHFileOperationA(&shFileOPStruct); // pass the pointer to the structure.
 
Share this answer
 
v2
Comments
Shao Voon Wong 31-Mar-23 0:30am    
This ZeroMemory line

ZeroMemory(&shFileOPStruct, sizeof(SHFILEOPSTRUCT));

should be updated to

ZeroMemory(&shFileOPStruct, sizeof(SHFILEOPSTRUCTA));

Got my 5!
Richard MacCutchan 31-Mar-23 3:38am    
That should not be necessary since SHFILEOPSTRUCT will be defined as SHFILEOPSTRUCTA in the header. And if the program is built with UNICODE defined, it will be defined as SHFILEOPSTRUCTW. You can see examples of this in any of the Windows header files.

Oh, and thanks for the 5.
Member 15925018 31-Mar-23 4:59am    
MSDN says:
You shouldn't use SHFILEOPSTRUCTW directly
Richard MacCutchan 31-Mar-23 5:04am    
Which is why I never do.
Shao Voon Wong 31-Mar-23 8:07am    
If the project has UNICODE defined, then SHFILEOPSTRUCT will be SHFILEOPSTRUCTW but shFileOPStruct is the type of SHFILEOPSTRUCTA, the ZeroMemory will cause a buffer overrun.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900