Added demo for cpp module and updated the CppModule

This commit is contained in:
2025-06-24 22:11:15 +03:00
parent 89385a8c89
commit 962d813466
16 changed files with 304 additions and 62 deletions

View File

@@ -4,12 +4,6 @@ namespace CppCompatibilityModule.Extern;
public static class Delegates
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ProcessObject(ref object obj);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ExecuteDelegateFunction();
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void SetExternFunctionPointerDelegate(IntPtr funcPtr);

View File

@@ -116,23 +116,6 @@ namespace CppCompatibilityModule.Extern
return result;
}
public void CallFunction(string methodName, ref object parameter)
{
var functionDelegate = GetDelegateForFunctionPointer<Delegates.ProcessObject>(methodName);
functionDelegate(ref parameter);
_Logger.Log($"Function {methodName} called successfully with parameter");
}
public void CallFunction(string methodName)
{
var functionDelegate = GetDelegateForFunctionPointer<Delegates.ExecuteDelegateFunction>(methodName);
functionDelegate();
_Logger.Log($"Function {methodName} called successfully");
}
}
}

View File

@@ -17,24 +17,14 @@ public class ExternalApplication
this._Logger = logger;
}
internal void CallFunction(string methodName, ref object parameter)
{
_ExternLibrary.CallFunction(methodName, ref parameter);
}
internal void CallFunction(string methodName)
{
_ExternLibrary.CallFunction(methodName);
}
internal T GetDelegateForFunctionPointer<T>(string methodName) where T : Delegate
{
return _ExternLibrary.GetDelegateForFunctionPointer<T>(methodName);
}
internal void SetExternFunctionToPointToFunction(string externalFunctionName, Delegates.CsharpFunctionDelegate localFunction)
internal object? SetExternFunctionToPointToFunction<TLocalFunctionDelegate>(string externalFunctionName, TLocalFunctionDelegate localFunction) where TLocalFunctionDelegate : Delegate
{
_ExternLibrary.SetExternFunctionSetterPointerToCustomDelegate<Delegates.SetExternFunctionPointerDelegate, Delegates.CsharpFunctionDelegate>(externalFunctionName, localFunction);
return _ExternLibrary.SetExternFunctionSetterPointerToCustomDelegate<Delegates.SetExternFunctionPointerDelegate, TLocalFunctionDelegate>(externalFunctionName, localFunction);
}
internal void FreeLibrary()

View File

@@ -43,25 +43,26 @@ public class ExternalApplicationHandler
_ExternalApplicationManager.FreeApplication(applicationId);
}
public void CallFunctionWithParameter(Guid appId, string functionName, ref object parameter)
public T GetFunctionDelegate<T>(Guid applicationId, string functionName) where T : Delegate
{
if (_ExternalApplicationManager is null)
{
_Logger.Log("Failed to call function because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
return;
_Logger.Log("Failed to get function delegate because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
return default!;
}
_ExternalApplicationManager.ExecuteApplicationFunctionWithParameter(appId, functionName, ref parameter);
return _ExternalApplicationManager.GetFunctionDelegate<T>(applicationId, functionName);
}
public void CallFunctionWithoutParameter(Guid appId, string functionName)
public object? SetExternFunctionToPointToFunction<TLocalFunctionDelegate>(Guid applicationId, string functionName,
TLocalFunctionDelegate localFunction) where TLocalFunctionDelegate : Delegate
{
if (_ExternalApplicationManager is null)
if(_ExternalApplicationManager is null)
{
_Logger.Log("Failed to call function because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
return;
_Logger.Log("Failed to set external function pointer because the manager is not initialized. This should have never happened in the first place!!!", this, LogType.Critical);
return null;
}
_ExternalApplicationManager.ExecuteApplicationFunctionWithoutParameter(appId, functionName);
return _ExternalApplicationManager.SetExternFunctionToPointToFunction(applicationId, functionName, localFunction);
}
}

View File

@@ -46,30 +46,28 @@ internal class ExternalApplicationManager
_Logger.Log($"Application with id {applicationId} freed successfully");
}
public void ExecuteApplicationFunctionWithParameter(Guid appId, string functionName, ref object parameter)
public T GetFunctionDelegate<T>(Guid applicationId, string functionName) where T : Delegate
{
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == appId);
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == applicationId, null);
if(application is null)
{
_Logger.Log($"Couldn't find application with id {appId}");
return;
_Logger.Log($"Couldn't find application with id {applicationId}");
return default!;
}
application.CallFunction(functionName, ref parameter);
return application.GetDelegateForFunctionPointer<T>(functionName);
}
public void ExecuteApplicationFunctionWithoutParameter(Guid appId, string functionName)
public object? SetExternFunctionToPointToFunction<TLocalFunctionDelegate>(Guid applicationId, string externalFunctionName, TLocalFunctionDelegate localFunction) where TLocalFunctionDelegate : Delegate
{
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == appId);
var application = _ExternalApplications.FirstOrDefault(app => app.ApplicationId == applicationId, null);
if(application is null)
{
_Logger.Log($"Couldn't find application with id {appId}");
return;
_Logger.Log($"Couldn't find application with id {applicationId}");
return null;
}
application.CallFunction(functionName);
return application.SetExternFunctionToPointToFunction<TLocalFunctionDelegate>(externalFunctionName, localFunction);
}
}