首页 > 解决方案 > 来自 C# 的 DLL 调用在较新的 .Net 框架中不起作用(3.5 与 4.6.1)

问题描述

我有以下代码:

[DllImport("ftcjtag.dll", CallingConvention = CallingConvention.Cdecl)]
static extern uint JTAG_OpenHiSpeedDevice(string DeviceName, uint locationID, string channel, ref IntPtr pftHandle);

[DllImport("ftcjtag.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern uint JTAG_InitDevice(IntPtr ftHandle, uint clockDivisor);


IntPtr ftHandle = IntPtr.Zero;
ftStatus = JTAG_OpenHiSpeedDevice(hiSpeedDeviceName, locationId, hiSpeedChannel, ref ftHandle);
if (ftStatus != FTC_SUCCESS)
{
      throw new JTAGApiException("An error occurred while calling JTAG_OpenHiSpeedDevice for device:" + hiSpeedDeviceName + ". API Error:" + GetError(ftStatus));
}


ftStatus = JTAG_InitDevice(ftHandle, 1);

if (ftStatus != FTC_SUCCESS)
{
    throw new Exception("An error occurred during init jtag device " + HiSpeedDevice.DeviceName);
}

当我使用 ,Net 3.5 运行它时,它运行良好。

当我切换到 4.6.1 时,JTAG_InitDevice 返回一个错误代码,指出提供的句柄无效。JTAG_OpenHiSpeedDevice 是我得到句柄的地方。我看不出为什么在 3.5 中句柄是有效的,而在 4.6.1 中它是无效的。

谢谢

标签: c#pinvoke

解决方案


推荐阅读