我的目的是调用研华的动态库文件Adsapi.32中的若干函数,碰到了很大麻烦,主要是因为它是C++的。研华结构体原型:
typedef struct tagPT_QCounterConfig
{
USHORT counter;
USHORT LatchSrc;
USHORT LatchOverflow;
USHORT ResetOnLatch;
USHORT ResetValue;
} PT_QCounterConfig, FAR * LPT_QCounterConfig;研华函数定义FEXPORT LRESULT FTYPE DRV_QCounterConfig(LONG DriverHandle, LPT_QCounterConfig lpQCounterConfig);研华示例调用函数如下:
static USHORT usResetAfterLatch = 0; /* Reset after latch flag */
static USHORT usFreeRun = 0; /* Free run flag */
static USHORT usResetValue = 0; /* Reset Value */
static USHORT usLatchSource = 0; /* Latch source flag */
static USHORT usInputMode = 0; /* Input mode flag */
static int wChannel = 0; /* Input channel */ptQCounterConfig.counter = wChannel;
ptQCounterConfig.LatchSrc = usLatchSource;
ptQCounterConfig.LatchOverflow = usFreeRun;
ptQCounterConfig.ResetOnLatch = usResetAfterLatch;
ptQCounterConfig.ResetValue = usResetValue;if((ErrCde = DRV_QCounterConfig(DriverHandle, (LPT_QCounterConfig)&ptQCounterConfig)) != 0)
VC中调试成功我在C#中修改如下:
[DllImport("Adsapi32.dll")]
public static extern int DRV_QCounterConfig(long DriverHandle,tagPT_QCounterConfig lpQCounterConfig);
public struct tagPT_QCounterConfig
{
public ushort counter;
public ushort LatchSrc;
public ushort LatchOverflow;
public ushort ResetOnLatch;
public ushort ResetValue;
}
ushort wChannel = 0;
ushort usFreeRun = 0;
ushort usResetAfterLatch = 0;
ushort usResetValue = 0;
ushort usLatchSource = 0;tagPT_QCounterConfig ptQCounterConfig = new tagPT_QCounterConfig();ptQCounterConfig.counter = wChannel;
ptQCounterConfig.LatchSrc = usLatchSource;
ptQCounterConfig.LatchOverflow = usFreeRun;
ptQCounterConfig.ResetOnLatch = usResetAfterLatch;
ptQCounterConfig.ResetValue = usResetValue;dwErrCde = DRV_QCounterConfig(lDriverHandle, ptQCounterConfig);之前首先还有一个打开设备的函数,没有问题,接下来运行到这个DRV_QCounterConfig就出了问题,貌似是ptQCounterConfig这个参数传的不对,请问我该怎么调用这个非托管dll的方法?为什么示例中调用的是(LPT_QCounterConfig)&ptQCounterConfig这种形式?
还有,像LONG far * 这种变量在C#中应该怎么表示?我用了 unsafe long *,好像不行。
多谢关注!
typedef struct tagPT_QCounterConfig
{
USHORT counter;
USHORT LatchSrc;
USHORT LatchOverflow;
USHORT ResetOnLatch;
USHORT ResetValue;
} PT_QCounterConfig, FAR * LPT_QCounterConfig;研华函数定义FEXPORT LRESULT FTYPE DRV_QCounterConfig(LONG DriverHandle, LPT_QCounterConfig lpQCounterConfig);研华示例调用函数如下:
static USHORT usResetAfterLatch = 0; /* Reset after latch flag */
static USHORT usFreeRun = 0; /* Free run flag */
static USHORT usResetValue = 0; /* Reset Value */
static USHORT usLatchSource = 0; /* Latch source flag */
static USHORT usInputMode = 0; /* Input mode flag */
static int wChannel = 0; /* Input channel */ptQCounterConfig.counter = wChannel;
ptQCounterConfig.LatchSrc = usLatchSource;
ptQCounterConfig.LatchOverflow = usFreeRun;
ptQCounterConfig.ResetOnLatch = usResetAfterLatch;
ptQCounterConfig.ResetValue = usResetValue;if((ErrCde = DRV_QCounterConfig(DriverHandle, (LPT_QCounterConfig)&ptQCounterConfig)) != 0)
VC中调试成功我在C#中修改如下:
[DllImport("Adsapi32.dll")]
public static extern int DRV_QCounterConfig(long DriverHandle,tagPT_QCounterConfig lpQCounterConfig);
public struct tagPT_QCounterConfig
{
public ushort counter;
public ushort LatchSrc;
public ushort LatchOverflow;
public ushort ResetOnLatch;
public ushort ResetValue;
}
ushort wChannel = 0;
ushort usFreeRun = 0;
ushort usResetAfterLatch = 0;
ushort usResetValue = 0;
ushort usLatchSource = 0;tagPT_QCounterConfig ptQCounterConfig = new tagPT_QCounterConfig();ptQCounterConfig.counter = wChannel;
ptQCounterConfig.LatchSrc = usLatchSource;
ptQCounterConfig.LatchOverflow = usFreeRun;
ptQCounterConfig.ResetOnLatch = usResetAfterLatch;
ptQCounterConfig.ResetValue = usResetValue;dwErrCde = DRV_QCounterConfig(lDriverHandle, ptQCounterConfig);之前首先还有一个打开设备的函数,没有问题,接下来运行到这个DRV_QCounterConfig就出了问题,貌似是ptQCounterConfig这个参数传的不对,请问我该怎么调用这个非托管dll的方法?为什么示例中调用的是(LPT_QCounterConfig)&ptQCounterConfig这种形式?
还有,像LONG far * 这种变量在C#中应该怎么表示?我用了 unsafe long *,好像不行。
多谢关注!
我修改了声明,把一个tagPT_QCounterConfig指针传进去了,还是不行。关键是这个FAR * LPT_QCounterConfig是什么意思?为什么研华用的是这个而不是tagPT_QCounterConfig
呢?
public static extern int DRV_QCounterConfig(int DriverHandle,ref tagPT_QCounterConfig lpQCounterConfig);它传的是指向tagPT_QCounterConfig的指针。http://www.cnblogs.com/kmsmg/archive/2008/06/01.html
public static extern int DRV_QCounterConfig(long DriverHandle,ref tagPT_QCounterConfig lpQCounterConfig);
调用也加了ref,为什么还不行呢?