Visual Studio's C# Shared Project. Helps to dynamically load an unmanaged library at runtime.
Designed to work on Windows, but should be crossplatform with .NET 5.0
Abstract (base) class, requires implementation. Example (partial class)
private delegate int ProcedureNameFromDLL(IntPtr someParam1, ref int someParam2);
private ProcedureNameFromDLL myFunc;
public int MethodName(IntPtr someParam1, ref int someParam2)
{
// do something with parameters here if needed
return myFunc(someParam1, ref someParam2);
}
// And initialize "myFunc" somewhere (for example in constructor):
GetReadyDelegate(ref myFunc);
or
private delegate int myNameForDelegate(IntPtr someParam1, ref int someParam2);
private myNameForDelegate myFunc;
public int MethodName(IntPtr someParam1, ref int someParam2)
{
// do something with parameters here if needed
return myFunc(someParam1, ref someParam2);
}
// And initialize "myFunc" somewhere (for example in constructor):
GetReadyDelegate(ref myfunc, "ProcedureNameFromDLL");
private delegate int ProcedureNameFromDLL(IntPtr someParam1, ref int someParam2);
private ProcedureNameFromDLL myFunc;
public int MethodName(IntPtr someParam1, ref int someParam2)
{
// do something with parameters here if needed
return myFunc(someParam1, ref someParam2);
}
// And initialize "myFunc" somewhere (for example in constructor):
myFunc = GetReadyDelegate<ProcedureNameFromDLL>();
or
private delegate int myNameForDelegate(IntPtr someParam1, ref int someParam2);
private myNameForDelegate myFunc;
public int MethodName(IntPtr someParam1, ref int someParam2)
{
// do something with parameters here if needed
return myFunc(someParam1, ref someParam2);
}
// And initialize "myFunc" somewhere (for example in constructor):
myFunc = GetReadyDelegate<myNameForDelegate>("ProcedureNameFromDLL");
private delegate int ProcedureNameFromDLL(IntPtr someParam1, ref int someParam2);
private ProcedureNameFromDLL myFunc;
public int MethodName(IntPtr someParam1, ref int someParam2)
{
if (myFunc is null) return SOME_ERROR_CODE;
// do something with parameters here if needed
return myFunc(someParam1, ref someParam2);
}
// And initialize "myFunc" somewhere (for example in constructor),
// TryGetReadyDelegate() will not throw exception if procedure is not found:
bool isProcReceived = TryGetReadyDelegate(ref myFunc);
or
private delegate int myNameForDelegate(IntPtr someParam1, ref int someParam2);
private myNameForDelegate myFunc;
public int MethodName(IntPtr someParam1, ref int someParam2)
{
if (myFunc is null) return SOME_ERROR_CODE;
// do something with parameters here if needed
return myFunc(someParam1, ref someParam2);
}
// And initialize "myFunc" somewhere (for example in constructor),
// TryGetReadyDelegate() will not throw exception if procedure is not found:
bool isProcReceived = TryGetReadyDelegate(ref myFunc, "ProcedureNameFromDLL");