Declare to SHFileOperation - How to build Structure correctly?

I’m struggling to get this working for Windows 64Bit builds:
https://msdn.microsoft.com/en-us/library/windows/desktop/bb762164(v=vs.85).aspx

Here is what I currently have (and what’s working in 32Bit):

Declare Function SHFileOperation lib "shell32.dll" ( ByRef pFileOpStruct as FileOpStruct ) As Int32

and the FileOpStruct Structure:

hwnd As Integer Func As Integer pczzFrom As Ptr pczzTo As Ptr Flags As Integer AnyOperationsAborted As Integer NameMappings As Integer pczzProgressTitle As Ptr

The “from/to” Memory-Blocks (Ptr) are created like this:

Private Function pczzMemoryBlock(psText As String) As MemoryBlock psText = Trim(ConvertEncoding(psText, Encodings.WindowsANSI)) Dim mb As MemoryBlock mb = New MemoryBlock( (Len( psText + Chr(0) + Chr(0) )) ) mb.CString( 0 ) = psText return mb End Function

That crashes when run in 64Bit. So I guess it’s because of a wrong Structure when in 64Bit? Or is it the MemoryBlock?
@ has a great DataType-HowTo… but I can’t find the FILEOP_FLAGS or PCZZTSTR in there…

FILEOP_FLAGS is a WORD so its an UInt16 (I’ve added it to my list, thanks)
PCZZTSTR I’ll add later (as it will need a little piece of demo code to go along with it), but its a pointer to a NullNull terminated string of WCHARS (so CString + Chr(0))

hwnd As Integer
Func As UInt32
pczzFrom As Ptr
pczzTo As Ptr
Flags As UInt16
AnyOperationsAborted As Int32
NameMappings As Ptr
pczzProgressTitle As Ptr

Your pczzMemoryBlock looks ok but I’d change:

mb.CString( 0 ) = psText

to

mb.CString( 0 ) = psText + Chr(0) + Chr(0)

just to be safe as I don’t think that memory blocks are guaranteed to be zero’d when created.

Try that, if it doesn’t work send me a link to a really simple example project and I’ll fix it as it could be an 64bit structure alignment issues in Windows <https://xojo.com/issue/51124> which I can’t fix without some testing.

Even for the simple looking things like Func As UInt32, you need to run it past my site and put the correct type in or it will break on 64bit 100% of the time when using structures :slight_smile:

Thanks - I’ve updated the DataType’s.
But it still crashes if compiled for Win-64Bit… if you really want, you can try this Declare test project of mine.
Have a look at the PushButtons “AuthDeleteFile” and “AuthReplaceFile”.

If you use MBS Win Plugin, you could do this with our WindowsFileCopyMBS class.

[quote=372733:@Jürg Otter]Thanks - I’ve updated the DataType’s.
But it still crashes if compiled for Win-64Bit… if you really want, you can try this Declare test project of mine.
Have a look at the PushButtons “AuthDeleteFile” and “AuthReplaceFile”.[/quote]

Now that the rest of the structure is correct, thankfully that was quite a simple one after checking the offsets and structure size in VS.

It was just a case of duplicating FileOpStruct and adding a StructureAlignment 8 for 64bit only and omitting it for 32bit.

I tested it in 32bit and 64bit, “AuthDeleteFile” worked.

https://www.dropbox.com/s/clcocrh291jkx35/WinDeclares.rbp?dl=0

Oh, I notice you do it slightly different by calling the structures different names ending in 32 or 64 bit as in ShellExecuteInfoStructure32 and switching between them in code.

Just in case you don’t notice, I duplicated the FileOpStruct structure and set the “Included In” checkboxes under the gear icon of the Inspector.

Thanks! That was the missing piece of my puzzle…

[quote=372738:@]Oh, I notice you do it slightly different by calling the structures different names ending in 32 or 64 bit as in ShellExecuteInfoStructure32 and switching between them in code.
Just in case you don’t notice, I duplicated the FileOpStruct structure and set the “Included In” checkboxes under the gear icon of the Inspector.[/quote]
I do that because the source needs to work in REAL Studio 2011r3 - and there is no “Include in 32/64Bit” available there… so I have to do that in Code :slight_smile:
Luckily, a Target64Bit is already available there (even if no 64 Bit builds are possible). But that allows to Code in 2011r3 (and also provide WinXP/Carbon 10.6 support), and just compile with 2017 for 64Bit and/or HiDPI.

Ahha gotcha, nice nice.

No problem, glad its working for you too :slight_smile:

This 64-bit structure alignment bug just bit me today. Thanks for providing a workaround Julian.