Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
The client is in c# and the server is in C++.
Client is as follows:
client.cs
namespace clinetspace
{
      public class client
      {
           [DllImport("Server.dll")]
           static public extern void CallServer(IntPtr obj, int a);
           [DllImport("Server.dll")]
           static public extern IntPtr CreateServer();

           IntPtr obj;
           public static void main()
           {
                 obj = CreateServer();
                 CallServer(obj, 1);
           }
      }
}

a client has other methods also that I am calling in the same way as the CallServer() method and all are working fine.
and the server is as follows:
Server.h
extern "C" __declspec(dllexport) MyServer* CreateServer();
extern "C" __declspec(dllexport) void CallServer(MyServer* server, int i);

Server.cpp

#include "Server.h"
#include "MyServer.h"
public void CreateServer()
{
     return new MyServer();
}

public void CallServer(MyServer* server, int i)
{
       server->StartServer(i);
}

internally StartServer() is doing the other operations. All the operations and calling methods of c++ from c# is working fine.
Now my question is if server gives some error how can I pass the error to the client and the client act accordingly and show the proper error message to the user.
Error is as structure:
structure Error
{
    int errorCode;
    string errorMessage;
}


What I have tried:

I am not sure whatever I am trying is correct or not.
I am trying to solve this using delegates in c#. code is as follows:
 Modify client.cs as follows:

 [UnmanagedFunctionPointer(CallingConvention.StdCall)]
 delegate void ProgressCallback(int key, string value);
 [DllImport("Server.dll")]
 public static extern void DoWork([MarshalAs(UnmanagedType.FunctionPtr)] ProgressCallback callbackPointer);

public static void ServerResponse(int key, string value)
{
     Console.WriteLine("Errorcode is"+ key + "Error Message is"+value);
}
modify the main method as:
ProgressCallBack pcb = new ProgressCallBack(ServerResponse);
DoWork(ServerResponse);

and modify the server as follows:
Server.h
extern "C" typedef void (__stdcall * ProgressCallback)(int, char*);
extern "C" __declspec(dllexport) void DoWork(ProgressCallback progressCallback);

and Server.cpp
void DoWork(ProgressCallback progressCallback)
{
    if (progressCallback)
    {
        progressCallback(0, "Error Message");
    }
}

This method parameter has the reference of ProgressCallback, which is coming from the client. Is there any way to store this reference in any global variable so that it will be available everywhere on the server and when the server wants to send an error to client will use this variable.
Posted
Updated 26-Jul-20 18:43pm
v3
Comments
[no name] 25-Jul-20 17:25pm    
The server returns "error codes" as part of the request-response cycle; it doesn't do ad-hoc callbacks.
Member 14686752 27-Jul-20 0:19am    
This error code is defined by me. If anything fails to the server, I am assigning the error codes and corresponding error message. Now I want to pass this information to the client somehow.

1 solution

The C# client can do polling. While polling is easy to do, it is inefficient and a waste of CPU cycles. C++ server can create event(CreateEvent[^]) and signal the event handle whatever there is data to be received. C# client will create object AutoResetEvent Class (System.Threading) | Microsoft Docs[^] by passing false for the initialState to the constructor. C# client will wait by calling WaitOne(); which is a blocking call or OP prefers to call WaitAny Method[^] which has a timeout overloaded method.
 
Share this answer
 
Comments
Member 14686752 27-Jul-20 1:06am    
Thanks for the response. I am the beginner in this, any small sample application or any sample application reference will be more helpful.
Shao Voon Wong 27-Jul-20 2:04am    
Hi, I have put sample code at GitHub.

https://github.com/shaovoon/CSharpCppDllComm

Click the "Create Server" button, then click the "Send Cpp Msg" button to simulate a signal from Cpp Server to tell C# to get the message.
Member 14686752 27-Jul-20 3:19am    
Thanks for this sample application. If your c++ dll has other methods and those methods have some error messages that you need to send to the client how to do that in your sample application.
Shao Voon Wong 27-Jul-20 4:09am    
Just add 2 methods, HasError() and GetLastError(). Keep the interop simple.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900