读书人

诸位C++的dll 在C#中的调用总是出错

发布时间: 2013-11-18 11:57:15 作者: rapoo

各位,C++的dll 在C#中的调用总是出错!
C++中调用的代码,可以正常实现:

//PCO_SensorInfo 是一个结构体
PCO_SensorInfo strsensorinf;

memset((void*)&strsensorinf.wSize, 0, sizeof(PCO_SensorInfo));
strsensorinf.wSize = sizeof(PCO_SensorInfo);// Set size of structure
strsensorinf.iConversionFactor = 8; // Set conversion factor of camera
strsensorinf.iDarkOffset = 32; // Set dark offset of camera
strsensorinf.iDataBits = 12; // Set bit resolution of camera
//// Set color type and alignment of image data.
strsensorinf.iSensorInfoBits = CONVERT_SENSOR_COLORIMAGE;// Set color camera type;
m_hLut = NULL; //m_hLut 是void* 型的
//调用dll
int err = PCO_ConvertCreate(&m_hLut, (PCO_SensorInfo*)&strsensorinf.wSize, 2);


C++ dll
int PCOCONVERT_API PCO_ConvertCreate(HANDLE* ph, PCO_SensorInfo *strSensor, int iConvertType);

上面可以正常实现!

C#代码:
   CSharpDemo.sc2_cam.PCO_SensorInfo strsensorinf = new sc2_cam.PCO_SensorInfo();
strsensorinf.wSize =(ushort)(Marshal.SizeOf(typeof(CSharpDemo.sc2_cam.PCO_SensorInfo)));// Set size of structure
strsensorinf.iConversionFactor = 8; // Set conversion factor of camera
strsensorinf.iDarkOffset = 32; // Set dark offset of camera
strsensorinf.iDataBits = 12; // Set bit resolution of camera
//Set color type and alignment of image data.
strsensorinf.iSensorInfoBits = 1;// Set color camera type;
strsensorinf.dwzzDummy1 = new uint[38];
m_hLut = new IntPtr();
//这里该怎么调用?
int err = sc2_cam.PCO_ConvertCreate(ref m_hLut, ref strsensorinf, 2);


C#dll:
[DllImport("PCO_Conv.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int PCO_ConvertCreate(ref IntPtr hdriver, ref PCO_SensorInfo sensorInfo, int iConvertType);
(这样引用对吗?)
上述C++代码是可以正常调用的!请大家指教一下,看看C#的地方怎么弄?总是调用不成功!先谢过了!

[解决办法]
前提是要先把PCO_Conv.dll给注册了
[解决办法]
这个不好说,必须有导出函数的原型才知道。

另外是不是调用约定搞错了,默认是stdcall的,你的C++默认是cdecl的。

贴出具体的错误信息。
[解决办法]
没注意到你是传结构
这样就要修改一下了?

如下?


[StructLayout(LayoutKind.Sequential)]
public struct PCO_SensorInfo
{
...
// 你的定义
}

[DllImport("PCO_Conv.dll", EntryPoint = "PCO_ConvertCreate", CharSet = CharSet.Ansi)]
public static extern int PCO_ConvertCreate(IntPtr hdriver, ref PCO_SensorInfo sensorInfo, int iConvertType);
};

读书人网 >VC

热点推荐