LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Call Windows DLL with structure and wchar_t string pointers

Solved!
Go to solution

Hello all, 

 

I'm trying to eliminate the call to a wrapper DLL by calling a Windows DLL directly and passing it a structure that also contains some string pointers. 

The function I'm trying to call is SHFileOperation located inside shell32.dll. I need this function to do a Windows file copy operation that can be cancelled by the operator. (copying a folder of >50GB of data)

 

The SHFileOperation function expects a structure as an input called SHFILEOPSTRUCT. This structure contains two pointers - a from location and a to location. These pointers are of type PCZZSTR. These are actually const wchar_t pointers. 

 

Here is the information from msdn for the function and the input structure. 

https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-shfileoperationw

https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/ns-shellapi-_shfileopstructa

 

The input structure is properly aligned. I verified that by passing the structure to a dummy wrapper DLL function I created and verifying if all fields are properly aligned. 

 

After debugging my suspicion goes out to the 'pFrom' and 'pTo' string pointers that need to be passed via the input structure. Although the info is there inside the structure, it seems like the function cannot access it or interpret it... 

 

To create the wchar_t pointers I called DSNewPtr and MultiByteToWideChar. (see attached LabVIEW code)

Note: I copied this string pointer allocation from some code of Rolf Kalbermatter I found online - credits go to him!

 

Unfortunately - even with this code I cannot get it to work.  

 

If I just create a wrapper DLL where I pass two strings and create the SHFILEOPSTRUCT structure inside the DLL everything works OK. But - I want to avoid having to create/call a wrapper DLL by calling the Windows shell32.dll function directly. 

 

While debugging the issue it seems like the structure is passed correctly and it contains the correct info inside the string. See also attached png file. I created a wrapper DLL to inspect/test the passing of the structure. In Visual C++ I see that the structure is correctly filled in but still it doesn't work. 

 

Anyone any ideas of what I'm doing wrong. I've attached the LabVIEW code. I'm using LabVIEW 2014 SP1 32 bit. 

The function is supposed to copy the complete folder contents (source folder) to another folder (destination folder). 

 

Thanks in advance for the help!

0 Kudos
Message 1 of 5
(2,732 Views)
Solution
Accepted by topic author noxus

Do you really need wide strings for your paths?  I usually use the ascii versions of the WinAPI functions whenever possible, in this case SHFileOperationA instead of SHFileOperationW.  As long as you pay attention to the fact that pFrom and pTo point to double NULL terminated strings (lists), then it is pretty straightforward to plug this function into your code.

Message 2 of 5
(2,690 Views)

Thanks Darin. I didn't know about the double null terminated strings. Learned something new, so thanks a million for taking the time to respond and help me with this issue!

0 Kudos
Message 3 of 5
(2,684 Views)

One additional comment, if you ever plan to use this VI in LabVIEW 64-bit you have to change the datatype of some of the cluster elements.

 

pFrom and pTo as well as hNameMappings and lpszProgressTitle will need to be 64-bit integers.

 

And the DSDisposePtr nodes should be configured as pointer sized integer too to avoid to have to change them.

 

Also the size parameter of MoveBlock is a size_t so it should be pointer sized too!

Rolf Kalbermatter
My Blog
0 Kudos
Message 4 of 5
(2,660 Views)

Thanks Rolf. I will make the necessary changes once we go to 64 bit LabVIEW!

0 Kudos
Message 5 of 5
(2,652 Views)