C
C#2y ago
Miya

Need some help with marshal

im using GetFunctionPointerForDelegate with CreateDelegate , so i can get a pointer to the method in my project, it worked for one thing, but for another thing rn it doesnt work and it outputs an error, ArgumentException The d parameter is a generic type definition. im not really sure what that means, any help?
4 Replies
WhiteBlackGoose
Would you please give a snippet of code with this problem?
Miya
Miya2y ago
here
void* functionPointer = (void*)Marshal.GetFunctionPointerForDelegate(CreateDelegate(theCSFunction,0));
void* functionPointer = (void*)Marshal.GetFunctionPointerForDelegate(CreateDelegate(theCSFunction,0));
where createdelegate is
public static Delegate CreateDelegate(this MethodInfo methodInfo, object target) {
Func<Type[], Type> getType;
var isAction = methodInfo.ReturnType.Equals((typeof(void)));
var types = methodInfo.GetParameters().Select(p => p.ParameterType);
if (isAction) {
getType = Expression.GetActionType;
}
else {
getType = Expression.GetFuncType;
types = types.Concat(new[] { methodInfo.ReturnType });
}
if (methodInfo.IsStatic) {
return Delegate.CreateDelegate(getType(types.ToArray()), methodInfo);
}
return Delegate.CreateDelegate(getType(types.ToArray()), target, methodInfo.Name);
}
public static Delegate CreateDelegate(this MethodInfo methodInfo, object target) {
Func<Type[], Type> getType;
var isAction = methodInfo.ReturnType.Equals((typeof(void)));
var types = methodInfo.GetParameters().Select(p => p.ParameterType);
if (isAction) {
getType = Expression.GetActionType;
}
else {
getType = Expression.GetFuncType;
types = types.Concat(new[] { methodInfo.ReturnType });
}
if (methodInfo.IsStatic) {
return Delegate.CreateDelegate(getType(types.ToArray()), methodInfo);
}
return Delegate.CreateDelegate(getType(types.ToArray()), target, methodInfo.Name);
}
^ this seems to happen with any function that has more than 0 arguments
Miya
Miya2y ago
trought debuggin i can see the function is made properly
Miya
Miya2y ago
so its the GetFunctionPointerForDelegate throwing an error oh ,i think i know what happens it probably means that the type of the delegate needs to be not a generic why so? thats quite a strange restriction Thx for help I guess