简体   繁体   中英

Handle C# events in C++ unmanaged code using COM-Interop

I have a TcpSocketServer written in C# and I want to use it in native C++ without CLI. The class rises events on client connection/disconnection and when data is received. Untill now I got the server working and accepting connection, however I still can't send/receive data.

Notice that this is one of several classes I need to work with, so please do not suggest me to use native C++ sockets.

The (relevant) C# code is bellow:

[ComVisible(true)]
public delegate void TcpClientConnectedEventHandler(Socket s);

[ComVisible(true)]
public delegate void TcpClientDisconnectedEventHandler(EndPoint ep);

[ComVisible(true)]
public delegate void TcpDataReceivedEventHandler(TcpPacket p);

[ComVisible(true)]
[Guid("7cf49dcd-c11b-4e94-9f73-d5a24fe027c3")]
public interface ISocketTcpServer
{
    event TcpClientConnectedEventHandler ClientConnected;
    event TcpClientDisconnectedEventHandler ClientDisconnected;
    event TcpDataReceivedEventHandler DataReceived;

    [ComVisible(true)]
    int SendToAll(byte[] buffer);
}

Now, in C++ I have the following (relevant) code:

// Function Prototypes
void onClientConnected();
void onClientDisconnected();
void onDataReceived(ITcpPacket *packet);
VARIANT_BOOL started(ISocketTcpServer *server);
long send(ISocketTcpServer *server, char *s);

int main(int argc, char* argv[])
{
    ISocketTcpServer *server = NULL;
    _TcpDataReceivedEventHandler *dataReceivedPtr = NULL;
    _TcpClientConnectedEventHandler *clientConnectedPtr = NULL;
    _TcpClientDisconnectedEventHandler *clientDisconnectedPtr = NULL;
    char sInput[1024];

    std::cout << "C# Sockets Interop example.\r\n";

    // Initialize COM.
    CoInitialize(NULL);

    // Create the interface pointers.
    HRESULT hr = CoCreateInstance(__uuidof(SocketTcpServer), NULL,
        CLSCTX_INPROC_SERVER, __uuidof(ISocketTcpServer), (void **)&server);

    if((hr != S_OK) || (server == NULL))
    {
        std::cout << "Create instance failed\r\n";
        CoUninitialize();
        return 0;
    }

    // Configure TCP Server
    server->put_Port(2000);
    dataReceivedPtr = (_TcpDataReceivedEventHandler *)&onDataReceived;
    clientConnectedPtr = (_TcpClientConnectedEventHandler *)&onClientConnected;
    clientDisconnectedPtr =
        (_TcpClientDisconnectedEventHandler *)&onClientDisconnected;

    /* HERE IS WHERE I SET UP THE EVENTS */
    server->add_ClientConnected(clientConnectedPtr);
    server->add_ClientDisconnected(clientDisconnectedPtr);
    server->add_DataReceived(dataReceivedPtr);

    // Start TCP Server
    std::cout << "Starting TCP Server on port 2000.\r\n";
    server->Start();
    while(!started(server)) Sleep(100);
    while(started(server))
    {
        std::cin >> sInput;
        send(server, sInput);
    }

    // Uninitialize COM.
    CoUninitialize();
    return 0;
}

long send(ISocketTcpServer *server, char *s)
{
    long result = 0;
    server->SendToAll((SAFEARRAY *) s, &result);
    return result;
}

All other functions have its definition and works fine, so I did'n include them.

The main question is: How must be declared the functions which will handle the events to get it work. I supose they are not working because they do not match the asociated delegate, but I can not access the classes Socket and EndPoint of the .NET Framework.

Also the method

SendToAll(byte[] buffer):int

mapped to the function

SendToAll(SAFEARRAY *buffer, long *pRetVal):HRESULT

but may be this is a cast problem.

Any help or tip you could give me will be usefull.

Taanks in advance.

Assuming that you know how to handle the events from a standard COM component in VC++ (I'm not a C++ dev so I can't help with that part), then the best thing to do is to make your C# look like it is a standard COM component using the ComSourceInterfacesAttribute . MSDN has a simplistic How To on doing this.

Your C# code will end up looking something like this (note, I removed the [ComVisible(true)] attributes since they are unnecessary if your assembly is marked with it):

public delegate void TcpClientConnectedEventHandler(Socket s);
public delegate void TcpClientDisconnectedEventHandler(EndPoint ep);
public delegate void TcpDataReceivedEventHandler(TcpPacket p);

public interface ISocketTcpServerEvents
{
    void ClientConnected;
    void ClientDisconnected;
    void DataReceived;
}

[ComSourceInterfaces(typeof(ISocketTcpServerEvents))]
public class SocketTcpServer
{   
    public event TcpClientConnectedEventHandler ClientConnected;
    public event TcpClientDisconnectedEventHandler ClientDisconnected;
    public event TcpDataReceivedEventHandler DataReceived;

    int SendToAll(byte[] buffer);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM