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.