CfExecute function usage

Diakov Dmytro 1 Reputation point
2020-11-17T13:20:29.197+00:00

Hi!
I am using CfExecute function in my .Net Core application.
For this purpose I have marshalled it this way:

[DllImport(Lib.CldApi, SetLastError = false, ExactSpelling = true)]
[PInvokeData("cfapi.h", MSDNShortId = "6AC8958D-B060-4468-9811-9BAB0E6A06D3")]
public static extern HRESULT CfExecute(in CF_OPERATION_INFO OpInfo, ref CF_OPERATION_PARAMETERS OpParams);

I have noticed that it does not working correctly with arrays. When I am trying to pass an array of placeholders to it I got memory corrupt error.

var transferPlaceholders = new CF_OPERATION_PARAMETERS.TRANSFERPLACEHOLDERS();
            transferPlaceholders.CompletionStatus = completionStatus;
            transferPlaceholders.PlaceholderArray = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(CF_PLACEHOLDER_CREATE_INFO)) * aPlaceholders.Length);
            transferPlaceholders.PlaceholderArray.Write<CF_PLACEHOLDER_CREATE_INFO>(aPlaceholders, 0, Marshal.SizeOf(typeof(CF_PLACEHOLDER_CREATE_INFO)) * aPlaceholders.Length);
            transferPlaceholders.PlaceholderCount = (uint)aPlaceholders.Length;
            transferPlaceholders.PlaceholderTotalCount = (uint)aPlaceholders.Length;
            transferPlaceholders.Flags = flags;

var opParams = CF_OPERATION_PARAMETERS.Create<CF_OPERATION_PARAMETERS.TRANSFERPLACEHOLDERS>(transferPlaceholders);

var opInfo = new CF_OPERATION_INFO()
            {
                StructSize = (uint)Marshal.SizeOf<CF_OPERATION_INFO>(),
                Type = operationType,
                ConnectionKey = CallbackInfo.ConnectionKey,
                TransferKey = CallbackInfo.TransferKey,
                RequestKey = CallbackInfo.RequestKey,
                CorrelationVector = CallbackInfo.CorrelationVector
                //SyncStatus = IntPtr.Zero
            };

            ThrowIfError(CfExecute(opInfo, ref opParams));

Is there any advices how to fix it ?
Thanks

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,575 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.