Call unmanaged fastcall C function by address in C# - 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#?

Related

How to free the memory of a string returned from Rust in C#?

I have these two functions exposed from Rust
extern crate libc;
use std::mem;
use std::ffi::{CString, CStr};
use libc::c_char;
pub static FFI_LIB_VERSION: &'static str = env!("CARGO_PKG_VERSION"); // '
#[no_mangle]
pub extern "C" fn rustffi_get_version() -> *const c_char {
let s = CString::new(FFI_LIB_VERSION).unwrap();
let p = s.as_ptr();
mem::forget(s);
p as *const _
}
#[no_mangle]
pub extern "C" fn rustffi_get_version_free(s: *mut c_char) {
unsafe {
if s.is_null() {
return;
}
let c_str: &CStr = CStr::from_ptr(s);
let bytes_len: usize = c_str.to_bytes_with_nul().len();
let temp_vec: Vec<c_char> = Vec::from_raw_parts(s, bytes_len, bytes_len);
}
}
fn main() {}
They are imported by C# as below
namespace rustFfiLibrary
{
public class RustFfiApi
{
[DllImport("rustffilib.dll", EntryPoint = "rustffi_get_version")]
public static extern string rustffi_get_version();
[DllImport("rustffilib.dll", EntryPoint = "rustffi_get_version_free")]
public static extern void rustffi_get_version_free(string s);
}
}
The memory of the string returned from rustffi_get_version is not managed by Rust anymore as mem::forget has been called. In C#, I want to call the get version function, get the string, and then pass it back to Rust for memory deallocation like below.
public class RustService
{
public static string GetVersion()
{
string temp = RustFfiApi.rustffi_get_version();
string ver = (string)temp.Clone();
RustFfiApi.rustffi_get_version_free(temp);
return ver ;
}
}
But the C# program crashes when it runs rustffi_get_version_free(temp). How to free the forgotten string memory in C#? What should be passed back to Rust for deallocation?
Instead of defining string as the argument in the C# extern, I changed it to pointer.
[DllImport("rustffilib.dll", EntryPoint = "rustffi_get_version")]
public static extern System.IntPtr rustffi_get_version();
[DllImport("rustffilib.dll", EntryPoint = "rustffi_get_version_free")]
public static extern void rustffi_get_version_free(System.IntPtr s);
public static string GetVersion()
{
System.IntPtr tempPointer = RustFfiApi.rustffi_get_version();
string tempString = Marshal.PtrToStringAnsi(tempPointer);
string ver = (string)tempString.Clone();
RustFfiApi.rustffi_get_version_free(tempPointer);
return ver ;
}
The IntPtr from rustffi_get_version can be successfully converted to a C# managed string type. tempString and ver are good.
When rustffi_get_version_free(tempPointer) runs, it throws an exception saying stack unbalanced:
A call to PInvoke function 'rustFfiLibrary!rustFfiLibrary.RustFfiApi::rustffi_get_version_free' 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.
sizeof(IntPtr) and sizeof(char *) are both 4 on my system. Plus, IntPtr works for return value; why doesn't it work as an input parameter?
extern "C" fn in Rust means the function uses the C calling convention.
C# expects P/Invoke functions to use the stdcall calling convention by default.
You can tell C# to use the C calling convention:
[DllImport("rustffilib.dll", CallingConvention = CallingConvention.Cdecl)]
Alternatively, you could use extern "stdcall" fn on the Rust side.

Why does my program crash immediately after calling delegate?

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);

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

How can I make an interop call using a struct as a parameter that contains a string

I am attempting to call a C++ function in a DLL from C# via interop. The DLL was written by someone else. The function signature is as follows:
AXI_DLL_EXPORT int __stdcall GetFileType(StringParam *stringParam, int *fileType)
StringParam is a struct defined as follows:
struct StringParam
{
int size; // 4 byte integer for the size of the string buffer
LPWSTR buff; // Pointer to a wide char buffer
}
This struct is used for passing strings back and forth. It can be used as both in and out parameters. The purpose of the size field in the struct is primarily for returning strings to the caller and to indicate the size of the buffer required. If the size of the buffer (provided by the caller) is too small to hold the string the caller can be informed of this and can then provide a larger buffer in a successive call to the function.
In C# I created a corresponding struct as follows:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct StringParam
{
public int Size;
[MarshalAs(UnmanagedType.LPWStr)]
public string Text;
public StringParam(string text)
{
this.Text = text;
this.Size = text.Length;
}
}
I declared the DllImport function call as follows:
[DllImport("Custom.dll", CharSet = CharSet.Unicode)]
public static extern int GetFileType(StringParam stringParam, out int fileType);
The call fails with the following error:
A call to PInvoke function '... GetFileType' 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.
How should I declare the struct and call the native function from C#?
public static extern int GetFileType(ref StringParam stringParam, out int fileType);
Have you tried :
[DllImport("Custom.dll", CharSet = CharSet.Unicode)]
public static extern int GetFileType(StringParam stringParam, ref int fileType);
And I think you have to pin the ref int before you pInvoke.

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();

Categories