我有一个名为MT7Trace.dll的库和它的标题MT7Trace.h。我希望能够在c#代码中使用此dll中包含的函数,但我无法正确导入它们。以这两个函数为例:
MT7TRACE MT7TraceEnum MT7TraceCreateEnum (void);
MT7TRACE MT7TraceSensor MT7TraceGetNextSensor (MT7TraceEnum hEnumerator);
其中:
typedef struct tagMT7TraceEnum * MT7TraceEnum;
到目前为止我的天真尝试如下:
[DllImport("MT7Trace.dll")]
public static extern IntPtr MT7TraceCreateEnum(); //MT7TRACE MT7TraceEnum T7TraceCreateEnum (void);
[DllImport("MT7Trace.dll")]
public static extern IntPtr MT7TraceGetNextSensor (IntPtr hEnumerator);//MT7TRACE MT7TraceSensor MT7TraceGetNextSensor (MT7TraceEnum hEnumerator);
但是在测试这个导入的函数时:
private bool InitializeOneSensor()
{
var hEnum = MT7TraceCreateEnum();
var sensorCtr = 0;
MT7TraceSensor hSensor;
while ((hSensor = MT7TraceGetNextSensor(hEnum)) != IntPtr.Zero)
{
// Do something else ..
DllTraceReleaseSensor(hSensor);
sensorCtr++;
}
DllTraceReleaseEnum(hEnum);
return sensorCtr ==1;
}
,其中
using MT7TraceEnum = System.IntPtr;
using MT7TraceSensor = System.IntPtr;
我有一个例外:
调用PInvoke函数 'Atracsys.Framework!Atracsys.MicroTouchDevice :: MT7TraceGetNextSensor' 堆栈不平衡。这可能是因为托管PInvoke 签名与非托管目标签名不匹配。检查一下 调用约定和PInvoke签名匹配的参数 目标非托管签名。
我已经看到一些例子解释了如何创建/重新创建一个结构来用它来调用一个dll函数,但是那些c struct定义在某个地方可用:
struct XXX{
type1 fiel1;
type1 fiel1;
...
};
因此,为了调用dll函数,创建了一个等效的c sharp结构并用它来调用它。
在我的情况下,我没有结构定义(你可以看到下面的MT7Trace.h)
有什么建议吗?
这是MT7Trace.h代码的内容:
/*******************************************************************************
Copyright 2009 3M. All rights reserved.
Program Name: MT7Trace
Module Name: MT7Trace.h
Summary: Application interface for MT 7 Software Trace library
Description:
This header file defines the application interface for the MT 7 Software
Trace library. This interface allows an application to enumerate touch
sensors and obtain traces from the sensors.
Notes:
*******************************************************************************/
#ifndef __MT7TRACE_H__
#define __MT7TRACE_H__
#ifdef __cplusplus
extern "C"
{
#endif
/******************************************************************************/
/* I N C L U D E F I L E S */
/* Global Includes */
/* Project Includes */
/* Local Includes */
/******************************************************************************/
/* C O N S T A N T S */
#ifndef MT7TRACE
#if defined (WIN32) || defined (UNDER_CE)
#define MT7TRACE __declspec (dllimport)
#else
#define MT7TRACE
#endif
#endif
/******************************************************************************/
/* G L O B A L D A T A T Y P E S */
typedef enum
{
MT7TraceReason_Arrival = 1,
MT7TraceReason_Departure = 2,
MT7TraceReason_NewTrace = 3,
MT7TraceReason_NewEvent = 4,
} MT7TraceReason;
typedef void (* MT7TraceCallback) (MT7TraceReason eReason, void * hItem,
void * pvUserCode);
typedef enum
{
MT7TraceEvent_Touchdown = 0,
MT7TraceEvent_Drag = 1,
MT7TraceEvent_Liftoff = 2,
} MT7TraceEventType;
typedef struct tagMT7TraceEnum * MT7TraceEnum;
typedef struct tagMT7TraceSensor * MT7TraceSensor;
typedef struct tagMT7TraceTrace * MT7TraceTrace;
typedef struct tagMT7TraceEvent * MT7TraceEvent;
/******************************************************************************/
/* P U B L I C D A T A */
/******************************************************************************/
/* G L O B A L D A T A */
/******************************************************************************/
/* G L O B A L F U N C T I O N P R O T O T Y P E S */
// Enumeration API
MT7TRACE MT7TraceEnum MT7TraceCreateEnum (void);
MT7TRACE unsigned int MT7TraceGetEnumID (MT7TraceEnum hEnumerator);
MT7TRACE MT7TraceEnum MT7TraceCloneEnum (MT7TraceEnum hEnumerator);
MT7TRACE MT7TraceSensor MT7TraceGetNextSensor (MT7TraceEnum hEnumerator);
MT7TRACE void MT7TraceReleaseEnum (MT7TraceEnum hEnumerator);
// Sensor API
MT7TRACE unsigned int MT7TraceGetSensorID (MT7TraceSensor hSensor);
MT7TRACE const char * MT7TraceGetSensorName (MT7TraceSensor hSensor);
MT7TRACE MT7TraceSensor MT7TraceCloneSensor (MT7TraceSensor hSensor);
MT7TRACE MT7TraceTrace MT7TraceGetNextTrace (MT7TraceSensor hSensor);
MT7TRACE void MT7TraceSetSensorCallback (MT7TraceSensor hSensor,MT7TraceCallback fnCallback,void * pvUserCode);
MT7TRACE void MT7TraceReleaseSensor (MT7TraceSensor hSensor);
// Trace API
MT7TRACE unsigned int MT7TraceGetTraceID (MT7TraceTrace hTrace);
MT7TRACE MT7TraceTrace MT7TraceCloneTrace (MT7TraceTrace hTrace);
MT7TRACE MT7TraceEvent MT7TraceGetNextEvent (MT7TraceTrace hTrace);
MT7TRACE MT7TraceEvent MT7TraceGetPriorEvent (MT7TraceTrace hTrace,unsigned int nOffset);
MT7TRACE void MT7TraceSetNextEvent (MT7TraceTrace hTrace,unsigned int nIndex);
MT7TRACE unsigned int MT7TraceGetEventCount (MT7TraceTrace hTrace);
MT7TRACE MT7TraceEvent MT7TraceGetEvent (MT7TraceTrace hTrace,unsigned int nIndex);
MT7TRACE MT7TraceSensor MT7TraceGetTraceSensor (MT7TraceTrace hTrace);
MT7TRACE void MT7TraceSetTraceCallback (MT7TraceTrace hTrace,MT7TraceCallback fnCallback,void * pvUserCode);
MT7TRACE void MT7TraceReleaseTrace (MT7TraceTrace hTrace);
// Event API
MT7TRACE unsigned int MT7TraceGetEventID (MT7TraceEvent hEvent);
MT7TRACE MT7TraceEvent MT7TraceCloneEvent (MT7TraceEvent hEvent);
MT7TRACE MT7TraceEventType MT7TraceGetEventType (MT7TraceEvent hEvent);
MT7TRACE float MT7TraceGetEventX (MT7TraceEvent hEvent);
MT7TRACE float MT7TraceGetEventY (MT7TraceEvent hEvent);
MT7TRACE unsigned int MT7TraceGetEventTime (MT7TraceEvent hEvent);
MT7TRACE MT7TraceTrace MT7TraceGetEventTrace (MT7TraceEvent hEvent);
MT7TRACE void MT7TraceReleaseEvent (MT7TraceEvent hEvent);
// Monitor/Poll API
MT7TRACE int MT7TracePoll (void);
MT7TRACE int MT7TraceStartMonitor (void);
MT7TRACE void MT7TraceStopMonitor (void);
MT7TRACE void MT7TraceSetArrivalCallback (MT7TraceCallback fnCallback,void * pvUserCode);
MT7TRACE void MT7TraceSetUniversalCallback(MT7TraceCallback fnCallback,void * pvUserCode);
/******************************************************************************/
/* G L O B A L M A C R O S */
#ifdef __cplusplus
}
#endif
#endif /* ifndef __MT7TRACE_H__ */
答案 0 :(得分:1)
很可能是不平衡堆栈是由使用错误的调用约定引起的。更改您的DllImport
属性:
[DllImport("MT7Trace.dll", CallingConvention=CallingConvention.Cdecl)]