从非托管代码回调到托管代码

问题描述 投票:4回答:1

我正在触发我的托管代码并启动对非托管代码的调用。非托管代码中有回调。从非托管我得到我的托管方法'DelegateMethod'回调。但我没有从非托管代码中获取正确的参数/参数值。请在这件事上给予我帮助

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace TestApp
{
    public class Program
    {
        public delegate void fPointer(byte[] Sendapdu, ref int Sendlen, byte[] Recvapdu, ref int Recvlen);

        public struct sCommsAbstraction
        {
            ///Function to send and receive.
            public fPointer pf_TxRx;

            ///Other functions if necessary, e.g. reset
        }

        // Create a method for a delegate. 
        public static void DelegateMethod(byte[] Sendapdu, ref int Sendlen, byte[] Recvapdu, ref int Recvlen)
        {
            //This is called from unmanaged code. I am not getting proper arguments
            Console.WriteLine(Sendlen);
        }

        [DllImport("AuthLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int CmdLib_RegisterItsIO(ref sCommsAbstraction psCommsFunctions);

        [DllImport("AuthLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int CmdLib_OpenApplication();

        [DllImport("TransparentChannel.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern int TC_Transceive(byte[] writeBuf, ref int writeBufLen, byte[] readBuf, ref int pwReadBufferLen);

        static void Main(string[] args)
        {
            sCommsAbstraction psCommsFunctions = new sCommsAbstraction();
            // Instantiate the delegate.
            psCommsFunctions.pf_TxRx = DelegateMethod;

            CmdLib_RegisterItsIO(ref psCommsFunctions);
            CmdLib_OpenApplication();
        }
    }
}

我的非托管代码存在于此处 - Cmd Lib.c

//C code - unmanaged

typedef int32_t (*pFTransceive)(const uint8_t *prgbWriteBuffer, const uint16_t *pwWriteBufferLen, uint8_t *prgbReadBuffer, uint16_t *pwReadBufferLen);

typedef struct sCommsAbstraction
{
    ///Function to send and receive.
    pFTransceive pf_TxRx;

    ///Other functions if necessary, e.g. reset
}sCommsAbstraction_d

static sCommsAbstraction_d sCommsAbstraction = {0};

void CmdLib_RegisterItsIO(const sCommsAbstraction_d *psCommsFunctions)
{
    sCommsAbstraction.pf_TxRx = psCommsFunctions->pf_TxRx;    
}
void CmdLib_OpenApplication()
{
    sCommsAbstraction.pf_TxRx(rgbAPDUBuffer,&wTotalLength,rgbAPDUBuffer,&psApduData->wResponseLength);
}
c# pinvoke
1个回答
3
投票

您的委托声明与pFTransceive函数指针声明不匹配。这会在回调方法中生成垃圾参数值。

靠近:

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void fPointer(
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
        byte[] Sendapdu, ref short Sendlen,
        [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)]
        byte[] Recvapdu, ref short Recvlen
    );

请注意必需的属性以匹配C / C ++程序中使用的默认调用约定。和uint16_t匹配的短参数类型

更多关于在this post召集公约的奥秘。


    sCommsAbstraction psCommsFunctions = new sCommsAbstraction();

这是你必须担心的另一件事。只要本机代码可以进行回调,该对象就需要存活。现在它没有,下一个垃圾收集将破坏它,因为垃圾收集器无法找出本机代码正在使用它。当本机代码进行回调时Kaboom。你还没有看到那个bug,调试器现在让对象保持活着状态。这不会发生在生产中。

如上所述,您需要将此语句添加到Main()方法的底部:

    GC.KeepAlive(psCommsFunctions);

将它存储在静态变量中是更好的方法,只有当你知道本机代码不再进行回调这一事实时才会将其设置回null

© www.soinside.com 2019 - 2024. All rights reserved.