小女先给各位官人跪一个。
实在是被逼疯了。
公司要写一个wrapper, c# 调用 c dll, 接口为
typedef void* (*mallocfunc) (size_t);
typedef void* (*reallocfunc) (void*, size_t);
typedef void (*freefunc) (void*);
unsigned int cdecl fun(mallocfunc mallocfn, reallocfunc reallocfn,, freefunc freefn)我先是用了 clr, 很简单的写道:
int i = fun(malloc, realloc, free);运行过了,但出现 OleInitialize error 80010106。无奈转为尝试非托管直接调用,可怎么也搞不懂我该用什么来代替 malloc(), realloc(), free()
这... 这可怎么调用啊?!求各位高人指点,我这有100分,您先拿着,不够再加。
实在是被逼疯了。
公司要写一个wrapper, c# 调用 c dll, 接口为
typedef void* (*mallocfunc) (size_t);
typedef void* (*reallocfunc) (void*, size_t);
typedef void (*freefunc) (void*);
unsigned int cdecl fun(mallocfunc mallocfn, reallocfunc reallocfn,, freefunc freefn)我先是用了 clr, 很简单的写道:
int i = fun(malloc, realloc, free);运行过了,但出现 OleInitialize error 80010106。无奈转为尝试非托管直接调用,可怎么也搞不懂我该用什么来代替 malloc(), realloc(), free()
这... 这可怎么调用啊?!求各位高人指点,我这有100分,您先拿着,不够再加。
delegate IntPtr Malloc(int cb);[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate IntPtr Realloc(IntPtr ptr, IntPtr cb);[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate void Free(IntPtr ptr);static Malloc malloc = new Malloc(Marshal.AllocHGlobal);
static Realloc realloc = new Realloc(Marshal.ReAllocHGlobal);
static Free free = new Free(Marshal.FreeHGlobal);static void Main(string[] args)
{
int i = fun(malloc, realloc, free);
}
但是您这方法我用了,发现报错:A call to PInvoke function '...Form1::mouse_event' has unbalanced the stack我水平不行,也搞不懂到底怎么回事儿。后来又用回 clr, 程序勉强凑合着运行。
很感谢gomoku , 我也学到了UnmanagedFunctionPointer用法,这东西我以前都没听说过。100分您笑纳。