我有一个c#库,它提供了一些将数据上传到连接(android)设备上的功能。dll本身通过UnmangedExports导出,供delphi应用程序使用。
下面是delphi应用程序调用的函数:
[DllExport]
[return: MarshalAs(UnmanagedType.LPStr)]
public static string getDevices()
{
try
{
var devices = string.Empty;
var collection = new PortableDeviceCollection();
collection.Refresh();
foreach (var device in collection)
{
device.Connect();
if (devices != string.Empty)
{
devices += ";";
}
devices += device.FriendlyName;
device.Disconnect();
}
return devices;
}
catch (Exception e)
{
SomeClass.WriteErrorToLogFile(e);
return "ERROR";
}
}
下面是类PortableDeviceCollection:
public class PortableDeviceCollection : Collection<PortableDevice>
{
private readonly PortableDeviceApiLib.PortableDeviceManagerClass _deviceManager;
public PortableDeviceCollection()
{
this._deviceManager = new PortableDeviceApiLib.PortableDeviceManagerClass();
}
public bool Refresh()
{
this._deviceManager.RefreshDeviceList();
// Determine how many WPD devices are connected
var deviceIds = new string[1];
uint count = 1;
this._deviceManager.GetDevices(null, ref count);
if (count > 0)
{
// Retrieve the device id for each connected device
deviceIds = new string[count];
this._deviceManager.GetDevices(deviceIds, ref count);
foreach (var deviceId in deviceIds)
{
Add(new PortableDevice(deviceId));
}
return true;
}
else
return false;
}
}
我可以用visual创建dll,并在delphi应用程序中使用它。当delphi应用程序调用getDevices()
函数时,在实例化PortableDeviceCollection类时会出现一个错误:
没有找到文件或程序集"Interop.PortableDeviceApiLib,Version = 1.0.0.0,区域性=中性,PublicKeyToken = null“或它的依赖项。程序集是由比当前加载的运行库更近期且无法加载的运行时创建的。 ProductXY.PortableDeviceCollection..ctor() ProductXY.ProductXYMain.getDevices()
c#项目的目标框架设置为.Net Framework 4
。使用任何较低的版本,当我试图编译该项目时,都会得到一个错误:
无法解析主引用"Interop.PortableDeviceApiLib“,因为它间接依赖于.NET框架程序集"mscorlib,version = 4.0.0.0,区域性=中性,PublicKeyToken = b77a5c561934e089",该版本比当前目标框架中的版本2.0.0.0高。
请注意。我既没有编写c#库,也没有编写delphi应用程序。两人一起工作多年了。现在,我必须向c#库添加一个功能。我还没有在项目中添加任何代码。我只是尝试再次编译它并使用dll。我所做的唯一的事情就是通过RGiesecke.DLLExport.Metadata包管理器更新NuGet。在没有更新的情况下我得到了一个错误
"Microsoft.Build.Utilities.ToolLocationHelper找不到ildasm.exe“
我知道这个枚举C#中的Windows便携设备问题。但是,我的错误会在被问题处理的代码到达之前抛出。我仍然尝试了这个问题的解决方案,但是在answere中描述的操作(在dll中找到和替换)已经完成(否则我的代码就不会编译)。
这条错误信息对我来说毫无意义。Interop.PortableDeviceApiLib
是一个COM-Lib,在不同的框架版本中是不可下载的。我想我漏掉了什么东西。
有谁可以帮我?
发布于 2019-10-16 10:38:54
我终于解决了这个问题。老实说,我不知道是什么最终解决了这件事。对于每一个偶然发现这个问题的人,下面是我试图解决这个问题的一些事情。它们没有具体的顺序(因为我多次尝试了所有的东西):
Interop.PortableDeviceApiLib
like in 这问题( Christophe和Bruno的回答)Interop.PortableDeviceApiLib
的引用Interop.PortableDeviceTypesLib
的引用Interop.PortableDeviceApiLib
的引用Interop.PortableDeviceTypesLib
的引用至少这对我有帮助。
https://stackoverflow.com/questions/58396049
复制相似问题