Problems with C and C # interop
I have a problem calling a C function from my C # code. I wanted to add some functionality for VLC player (we use it in our software via vlcdotnet) and cross-compiled it on my ubuntu 12.10 for windows using mingw. I wrote a function, called it Foo:
__declspec(dllexport) void Foo(vlc_object_t* bar);
Now I want to call it from C #:
[LibVlcFunction("Foo")]
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void Foo(IntPtr pointer);
........
public LibVlcFunction<Foo> Foo { get; private set; }
......
Foo = new LibVlcFunction<Foo>(myLibVlcCoreDllHandle, VlcVersion);
And he fails. Inside the LibVlcFunction constructor, we have a combination of GetProcAddress and GetDelegateForFunctionPointer. GetProcAddress does not work with "Function address" Foo "does not exist ..." but dumpbin and dep. the walker says there is a function and its name is not crippled. I tried to write a C ++ application that loads libvlc.dll and gets a pointer to my function and it worked. But in C # this fails. What should I do? Any suggestions?
source to share
Try not to use stdcall and use cdecl instead, e.g .:
extern "C" __declspec(dllexport) void Foo(vlc_object_t* bar);
Calling your platform call:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
public class libvlc
{
[DllImport("the-vlc.dll", EntryPoint = "Foo")]
extern public static void Foo( IntPtr bar );
}
You will treat vlc_object_t * as opaque descriptors. You just pass them on. This assumes that vlc_object_t is allocated and deallocated in your shared VLC library (i.e. the DLL).
source to share