I think it's really time that you abandoned oldsyntax
and moved to C++/CLI. Anyway, even doing that is not going to solve your problem. You define a delegate like this:
delegate bool CFuncDelegate(PSObject Arg);
And then you assign a delegate variable like this:
CFuncDelegate func = (CFuncDelegate)Marshal.GetDelegateForFunctionPointer(
funcObj, typeof(CFuncDelegate));
The documentation for Marshal.GetDelegateForFunctionPointer
says:
Converts an unmanaged function pointer to a delegate.
So, your code can only work if funcObj
is an unmanaged function. And your addData
method is certainly not that. You need to stop using GetDelegateForFunctionPointer
here. It is simply not compatible with calling the managed function addData
.
I've no idea where funcObj
comes from. You said:
funcObj (a IntPtr) is a pointer to addData function in cpp (i didnt write that code here, since it is not relevant)
In fact it is not only relevant to the problem, it is the root cause of the problem. What I would expect to see here would be for you to add the C++ assembly to your C# project as a reference, at which point addData
could be referenced directly.
Of course, none of this even mentions the fact that your calling conventions are mis-matched. The managed code uses clrcall
and your unmanaged function pointer is taken to be stdcall
.
Update
There's some more information in the comments. Pass addData
as a delegate. You'll need to declare the delegate type in the C# code which I believe you reference from your C++ assembly.