Why does my program crash immediately after calling delegate? - c#

I'm trying to wrap this function defined by SDL2.
It's signature is
void SDL_AddEventWatch(SDL_EventFilter filter, void* userdata)
Where SDL_EventFilter is
typedef int (SDLCALL * SDL_EventFilter) (void *userdata, SDL_Event * event);
Thus, I've defined my wrapper like so:
public delegate int EventFilter(IntPtr userData, IntPtr type);
[DllImport("SDL2.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "SDL_AddEventWatch")]
public static extern void AddEventWatch(EventFilter filter, IntPtr userData);
And I'm testing it like so:
SDL.AddEventWatch((data, e) =>
{
return 0;
}, IntPtr.Zero);
When I run my program it actually enters the lambda function, but then immediately crashes as soon as it exits the function ("vshost32.exe has stopped working").
What might be causing the crash?

#define SDLCALL __cdecl
You have a calling convention mismatch. Your native code requires a __cdecl function but your C# code declares a delegate that will be mapped to a an __stdcall callback. The default for unmanaged code interop. You must declare it like this:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int EventFilter(IntPtr userData, IntPtr type);

Related

Call unmanaged fastcall C function by address in C#

I'm injecting my C# dll into an unmanaged process written in C. How would I go about calling a function in this process? I know the address of the function and the function signature. The function uses fastcall calling convention. I'm not sure if this complicates things.
I have tried using delegates like so:
[UnmanagedFunctionPointer(CallingConvention.FastCall, CharSet = CharSet.Unicode)]
public delegate IntPtr _PrintText(string msg, int color);
Which raises a warning about FastCall not being supported.
Here's a more complete, simple example of what I've tried:
C++ function:
void __fastcall PrintText (wchar_t * wMessage, int nColor)
Address: 0x31E3A0
C# code:
public class Example
{
[UnmanagedFunctionPointer(CallingConvention.FastCall, CharSet = CharSet.Unicode)]
public delegate IntPtr _PrintText(string msg, int color);
public static int EntryPoint(string pwzArgument)
{
var ptr = new IntPtr(0x31E3A0);
_PrintText MyPrint = Marshal.GetDelegateForFunctionPointer<_PrintText>(ptr);
MyPrint("TESTING", 0);
}
}
How do I properly define and call a FastCall C function using C#?

PInvoke for complex method calls C#

I am using a 3. party SDK, which is made up from .dll, .lib and .h files.
I am using the .dll's to PInvoke against. And the .h files to see the function names and parameters. (So I am not using the .lib files).
The SDK is rather complex, so making the PInvoke wrappers have proven to be a challenge. All the functions/structs/enums is defined in the .h files.
The function I am trying to wrap is called InitBaseComponent, and I can call it, but it returns a "Error In Argument" enum back. So my guess is it is marshalling that creates this problem.
So the question is: I am doing this right?
Function: InitBaseComponent(...)
//C Function: InitBaseComponent(...)
ERROR InitBaseComponent(
Method_Interface* methodInterface, //[in]
void* methodInst, //[in]
ErrorCallBackFunction errorCallbackFunc, //[in]
void* ErrorCallbackInst, //[in]
Component* BaseComponent //[in, out]
);
//C# Function: InitBaseComponent(...)
[DllImport("externalSDK.dll", EntryPoint = "InitBaseComponent", CallingConvention = CallingConvention.Cdecl)]
public static extern ERROR InitBaseComponent(
Method_Interface methodInterface,
IntPtr methodInst,
ErrorCallBackFunction errorCallbackFunc,
IntPtr ErrorCallbackInst,
out Component BaseComponent
);
Enum: ERROR
//C Enum: ERROR
typedef enum ERROR_E {
OK = 0, //Everything is ok
E_ARG = 1, //Error in the Arguments
E_DATA = 2 //Data error
//And more...
} ERROR;
//C# Enum: ERROR
public enum ERROR
{
OK = 0, //Everything is ok
E_ARG = 1, //Error in the Arguments
E_DATA = 2 //Data error
//And more...
}
Struct: Method_Interface
//C struct: Method_Interface
typedef struct Method_Interface_S
{
void* (*Method1)(void* Inst, size_t size);
void* (*Method2)(void* Inst, size_t nelements, size_t bytes);
void* (*Method3)(void* Inst, void *pointer, size_t size);
void (*Method4)(void* Inst, void* pointer);
}Method_Interface;
//C# class: Method_Interface
[StructLayout(LayoutKind.Sequential)]
public class Method_Interface
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Method1_delegate(IntPtr Inst, uint size);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Method2_delegate(IntPtr Inst, uint nelements, uint bytes);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Method3_delegate(IntPtr Inst, IntPtr pointer, uint size);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void Method4_delegate(IntPtr Inst, IntPtr pointer);
public Method1_delegate Method1;
public Method2_delegate Method2;
public Method3_delegate Method3;
public Method4_delegate Method4;
}
Delegate: ErrorCallBackFunction
//C ErrorCallBackFunction
typedef void (*ErrorCallBackFunction)(void* errorCallBackInst, ERROR errorCode, const char* szMessage, const char* szDetail);
//C# delegate: ErrorCallBackFunction
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ErrorCallBackFunction(IntPtr errorCallBackInst, ERROR errorCode, string szMessage, string szDetail);
Struct: Component
//C struct: Component
typedef struct Component_S
{
void* ObjPointer;
unsigned long number;
} Component;
//C# class: Component
[StructLayout(LayoutKind.Sequential)]
public class Component
{
public IntPtr ObjPointer;
public ulong number;
}
Anyone knows what I am doing wrong?
You have declared Component in the C# as a class. That means that it is already a reference. But then you passed it as an out parameter which adds an extra layer of indirection, one too many. So, you need to remove the out, just as you did for methodInterface.
[DllImport(...)]
public static extern ERROR InitBaseComponent(
Method_Interface methodInterface,
IntPtr methodInst,
ErrorCallBackFunction errorCallbackFunc,
IntPtr ErrorCallbackInst,
Component BaseComponent
);
Obviously you need to instantiate the Component object in your C# before you call InitBaseComponent.
Some other observations:
size_t is pointer sized, so your translation as uint will fail on 64 bit platforms.
C# long is 64 bits, but C++ long is 32 bits, on Windows. So your translation of the C++ Component struct is wrong. The number field must be declared with type uint.

PInvoke DLL in C#

I want to pass a structure to C function and I write the following code.
When I run it, the first function - Foo1 is working and then function Foo gets an exception. Can you help me to understand what the problem is?...
The C code:
typedef struct
{
int Size;
//char *Array;
}TTest;
__declspec(dllexport) void Foo(void *Test);
__declspec(dllexport) int Foo1();
void Foo(void *Test)
{
TTest *X = (TTest *)Test;
int i = X->Size;
/*for(int i=0;i<Test->Size;Test++)
{
Test->Array[i] = 127;
}*/
}
int Foo1()
{
return 10;
}
The C# code:
using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
[StructLayout(LayoutKind.Sequential)]
public class TTest
{
public int Size;
}
class Program
{
[DllImport(#"C:\.net course\unmanaged1\unmanaged3\Debug\unmanaged3.dll", CharSet = CharSet.Auto)]
public static extern void Foo(
[MarshalAs(UnmanagedType.LPStruct)]
TTest lplf // characteristics
);
[DllImport(#"C:\.net course\unmanaged1\unmanaged3\Debug\unmanaged3.dll", CharSet = CharSet.Auto)]
public static extern int Foo1();
static void Main(string[] args)
{
TTest Test = new TTest();
Test.Size = 25;
int XX = Program.Foo1();
Program.Foo(Test);
}
}
}
To the downvoters: This answer solves two issues: the immediate issue of the calling convention/the MarhsalAs attribute, and the issue he will soon find where his TTest parameter won't work if he takes my suggestion of turning TTest into a struct.
Your native code is asking for a void*, which in C# is an IntPtr. First you should define TTest as a struct and not a class. Second, you should change the declaration of Foo to:
[DllImport(#"C:\.net course\unmanaged1\unmanaged3\Debug\unmanaged3.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern void Foo(IntPtr lplf);
And third, you should pin the TTest using the fixed keyword and pass it's pointer to Foo. If you're using a class, you can use Marhsal.StructureToPtr to get an IntPtr from your TTest.
This provides the same functionality on both sides, where a pointer to any type can be passed in. You can also write overloads with all the class types that you want to use since they all equate to void* on the native side. With a struct, your parameters would be prepended with a ref.
What I'm curious about is why your native code wants a void* instead of a TTest* when the first thing you do in the unmanaged code is cast to a TTest*. If you switched the parameter to a TTest*, then providing identical functionality becomes simpler. You declaration would become:
[DllImport(#"C:\.net course\unmanaged1\unmanaged3\Debug\unmanaged3.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public static extern void Foo(ref TTest lplf);
And you would call the function as Program.Foo(ref Test);
If you're using the class, the ref isn't necessary as classes are reference types.
You are using C call so you need to specify CallingConvention.Cdecl
[DllImport(#"C:\.net course\unmanaged1\unmanaged3\Debug\unmanaged3.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
By default its stdcall in C# pinvoke as i remember; You can also do change C code instead and leave your C# code as is like in below
__declspec(dllexport) void __stdcall Foo(void *Test);
But for me best is to both declare __cdecl (or stdcall) in your C export and CallingConvention.Cdecl (or stdcall) in your C# code to keep convenience. You can check https://learn.microsoft.com/en-gb/cpp/cpp/argument-passing-and-naming-conventions?view=vs-2017 and https://learn.microsoft.com/en-gb/dotnet/api/system.runtime.interopservices.callingconvention?view=netframework-4.7.2 for further info

Problem invoking C DLL in C#

I'm currently trying to invoke a method made in C from C#
C code looks like this:
extern "C" int addSum(int a, int b)
{
return a*b;
}
extern "C" int getCount()
{
return 12;
}
and C# code looks like this:
[DllImport("mydll.dll", SetLastError=true)]
private static extern int addSum(IntPtr a, IntPtr b);
[DllImport("mydll.dll", SetLastError = true)]
private static extern int getCount();
public static int mySum(int a, int b)
{
return suma(a, b);
}
public static int getMyCount()
{
return getCount();
}
The code returns the right values but i'm getting the following error:
addSum' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
Any sugestion regarding this issue ?
Thanks
In addtion to the datatype which might or might not be a problem depending on the target platform, you might also need to look at the calling convention. It is the calling convention that determines amoung other thing who is responsible for the the stack clean-up and the order that arguments are pushed on the stack or assigned to registers etc.
It is common for C code to use the cdecl calling convention.
[DllImport("mydll.dll",
SetLastError=true,
CallingConvention=CallingConvention.Cdecl)]
You don't need to use IntPtr as argument. You could directly use integer values when defining the method signature:
[DllImport("mydll.dll", SetLastError = true)]
public static extern int addSum(int a, int b);
[DllImport("mydll.dll", SetLastError = true)]
public static extern int getCount();
And then directly invoke the function:
int result = SomeClass.addSum(3, 4);
int count = SomeClass.getCount();

Call c++ function pointer from c#

Is it possible to call a c(++) static function pointer (not a delegate) like this
typedef int (*MyCppFunc)(void* SomeObject);
from c#?
void CallFromCSharp(MyCppFunc funcptr, IntPtr param)
{
funcptr(param);
}
I need to be able to callback from c# into some old c++ classes. C++ is managed, but the classes are not ref classes (yet).
So far I got no idea how to call a c++ function pointer from c#, is it possible?
dtb is right. Here a more detailed example for Marshal.GetDelegateForFunctionPointer. It should work for you.
In C++:
static int __stdcall SomeFunction(void* someObject, void* someParam)
{
CSomeClass* o = (CSomeClass*)someObject;
return o->MemberFunction(someParam);
}
int main()
{
CSomeClass o;
void* p = 0;
CSharp::Function(System::IntPtr(SomeFunction), System::IntPtr(&o), System::IntPtr(p));
}
In C#:
public class CSharp
{
delegate int CFuncDelegate(IntPtr Obj, IntPtr Arg);
public static void Function(IntPtr CFunc, IntPtr Obj, IntPtr Arg)
{
CFuncDelegate func = (CFuncDelegate)Marshal.GetDelegateForFunctionPointer(CFunc, typeof(CFuncDelegate));
int rc = func(Obj, Arg);
}
}
Have a look at the Marshal.GetDelegateForFunctionPointer method.
delegate void MyCppFunc(IntPtr someObject);
MyCppFunc csharpfuncptr =
(MyCppFunc)Marshal.GetDelegateForFunctionPointer(funcptr, typeof(MyCppFunc));
csharpfuncptr(param);
I don't know if this really works with your C++ method, though, as the MSDN documentation states:
You cannot use this method with function pointers obtained through C++

Categories