C# 在C中调用Cygwin GCC DLL#挂起在malloc上
我试图用为Linux编写的C/C++程序制作一个DLL。我让它在Cygwin shell中运行,并为DLL创建了一个接口。我也曾调用该函数,但现在当DLL想要在c文件中分配内存时,它会挂起 我将问题分解为以下简单程序: malloctest.cpp锁是这样的C# 在C中调用Cygwin GCC DLL#挂起在malloc上,c#,c++,c,cygwin,dllimport,C#,C++,C,Cygwin,Dllimport,我试图用为Linux编写的C/C++程序制作一个DLL。我让它在Cygwin shell中运行,并为DLL创建了一个接口。我也曾调用该函数,但现在当DLL想要在c文件中分配内存时,它会挂起 我将问题分解为以下简单程序: malloctest.cpp锁是这样的 #include <stdlib.h> #include "test.h" extern "C" __declspec(dllexport) int malloctest(); int malloctest(){ in
#include <stdlib.h>
#include "test.h"
extern "C" __declspec(dllexport) int malloctest();
int malloctest(){
int check = test();
return check;
}
我在Cygwin shell中这样编译它
gcc -c malloctest.cpp test.c
gcc -shared -o malloctest.dll malloctest.o test.o
我用C#这样称呼它:
class Program
{
[DllImport("kernel32", CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = true)]
static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
[DllImport("kernel32", SetLastError = true)]
static extern IntPtr LoadLibrary(string lpFileName);
public delegate int test();
static void Main(string[] args)
{
IntPtr pcygwin = LoadLibrary("cygwin1.dll");
IntPtr pcyginit = GetProcAddress(pcygwin, "cygwin_dll_init");
Action init = (Action)Marshal.GetDelegateForFunctionPointer(pcyginit, typeof(Action));
init();
IntPtr libptr = LoadLibrary("malloctest.dll");
IntPtr funcptr = GetProcAddress(libptr, "malloctest");
test malloctest = (test)Marshal.GetDelegateForFunctionPointer(funcptr, typeof(test));
int check = malloctest(); //hold
Console.WriteLine(check);
}
}
现在它只是挂起,没有错误。
如果我在malloctest.cpp中使用malloc,它就可以工作,但是只要我调用使用malloc的C文件,dll就会挂起
编辑:
这是我真正的界面
extern "C" {
typedef struct det det_t;
struct det{
int id;
int hamming;
float goodness;
float decision_margin;
double c[2];
double lu[2];
double ld[2];
double ru[2];
double rd[2];
};
__declspec(dllexport) int scanJPGfromBUF(det_t * dets, uint8_t *, char *);
__declspec(dllexport) int test ();
}
尝试将
委托更改为:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int test();
NET的默认值应该是Stdcall
,但C中的默认值是Cdecl
我将C结构转换为:
struct det
{
int id;
int hamming;
float goodness;
float decision_margin;
double c1;
double c2;
double lu1;
double lu2;
double ld1;
double ld2;
double ru1;
double ru2;
double rd1;
double rd2;
}
因此,我将删除所有数组并将它们转换为多个元素。C方法的签名可以在C#中以多种方式翻译
带有*
的每个参数可以是ref
、out
或[]
(但不是aref[]
)。。。因此可能是:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int scanJPGfromBUF(ref det_t dets, byte[] bytes1, byte[] chars1);
在编写windows程序而不是Unix程序(cygwin是Unix)时,请使用mingw编译器或交叉编译器,而不是cygwin编译器。问题是C/C++程序使用POSIX。上次我查的时候,mingw对这个不起作用。我可以和你一起跑。我想如果我像这样使用cygwin DLL应该可以工作。你试过从C#运行cygwin程序吗?可能您的问题是缺少到C:\cygwin\bin
的路径,无法加载cygwin1.dll
我的cygwin1.dll与我的exe位于同一目录中。是的,这个Hello World程序运行得很好。它在这个示例中起作用,但在我想要使用的真正的软件中不起作用。我注意到,如果我在实际程序的C++文件中使用MALOC,它也会挂起。但是我看不出样品和真的有什么区别。对不起,我弄错了。这个例子不适用。但我会将其保存在代码中。@Haldoryn显示要调用的C方法的签名。[UnmanagedFunctionPointer(CallingConvention.Cdecl)]委托int-scanJPGfromBUF(out-det[]det,Byte[]buffer,string-famname);[UnmanagedFunctionPointer(CallingConvention.Cdecl)]公共委托int test();
struct det
{
int id;
int hamming;
float goodness;
float decision_margin;
double c1;
double c2;
double lu1;
double lu2;
double ld1;
double ld2;
double ru1;
double ru2;
double rd1;
double rd2;
}
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int scanJPGfromBUF(ref det_t dets, byte[] bytes1, byte[] chars1);