C#DLL中的C ++函数指针

时间:2013-08-05 12:35:43

标签: c# c++ com interop com-interop

我有一个C ++ DLL通过COM调用C#DLL。当我初始化我的C#DLL时,我想传入一个回调函数。我似乎随机崩溃了。有人可以解释我做错了吗?

C ++

communications.h

#import "ultra_comm.tlb"

typedef void(*ExternalKickPtr)(const char*, const char*);

class UltraComm
{

public
:
    void Initialize(ExternalKickPtr kick);
private:
    ultra_comm::ICOMPtr iCOM;
};

communications.cpp

#include "communications.h"

void UltraComm::Initialize(ExternalKickPtr kick)
{
    ::CoInitialize(NULL);
    iCOM.CreateInstance(__uuidof(ultra_comm::Communications));

    iCOM->Initialize((long)kick);   **<-- this seems to be where it crashes**
}

ultra_css.h

void ExternalKickFunc(const char* steamId, const char* message);

ultra_css.cpp
UltraComm* ultra = NULL;

ultra = new UltraComm();
if(ultra)
{
ultra->Initialize(&ExternalKickFunc);
}

C#

Communications.cs

namespace Ultra_Comm
{
    [ComVisible(true)]
    [Guid("DAB7E676-44B4-4D3E-947A-7D9369AE2D43")]
    public interface ICOM
    {
        void Initialize(IntPtr kick);
    }

    [ComVisible(true)]
    [Guid("DFC77C60-945E-4712-AE2F-DCF5379BA83B")]
    public class Communications : ICOM, IWCF
    {
        delegate void FuncKickPlayer(string steamId, string message);

        static FuncKickPlayer _kick;

        public void Initialize(IntPtr kick)
        {
            _kick = Marshal.GetDelegateForFunctionPointer(kick, typeof(FuncKickPlayer)) as FuncKickPlayer;
        }
    }
}

0 个答案:

没有答案