dahall / Vanara

A set of .NET libraries for Windows implementing PInvoke calls to many native Windows APIs with supporting wrappers.
MIT License
1.81k stars 196 forks source link

Custom marshalers cannot be used on fields of structures. #471

Closed jcljb closed 4 months ago

jcljb commented 4 months ago

When use Vanara.PInvoke.Shell32.SHFileOperation(SHFILEOPSTRUCT& lpFileOp) (target .NET 6.0)

got a System.TypeLoadException with Message = "Cannot marshal field 'pFrom' of type 'SHFILEOPSTRUCT': Custom marshalers cannot be used on fields of structures."

What's wrong ? Regards JC

dahall commented 4 months ago

You were correct, the definition of SHFILEOPSTRUCT was defined to use custom marshalers that aren't supported on structures. I have corrected it and supplied a test case to show its proper use.

using var from = SafeCoTaskMemHandle.CreateFromStringList([TestCaseSources.WordDoc, TestCaseSources.SmallFile]);
using var to = SafeCoTaskMemHandle.CreateFromStringList([TestCaseSources.TempChildDir]);
SHFILEOPSTRUCT shop = new() { wFunc = ShellFileOperation.FO_COPY, pFrom = from, pTo = to, fFlags = FILEOP_FLAGS.FOF_NOCONFIRMATION };
Win32Error.ThrowLastErrorIf(SHFileOperation(ref shop), i => i != 0);

You should be able to pull v4.0.3 from AppVeyor (see README) shortly and I'll release to NuGet in the next 2 weeks.