首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PortableDeviceManagerClass在C#中无法初始化

PortableDeviceManagerClass在C#中无法初始化
EN

Stack Overflow用户
提问于 2019-10-15 13:35:37
回答 1查看 356关注 0票数 1

我有一个c#库,它提供了一些将数据上传到连接(android)设备上的功能。dll本身通过UnmangedExports导出,供delphi应用程序使用。

下面是delphi应用程序调用的函数:

代码语言:javascript
运行
复制
[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:

代码语言:javascript
运行
复制
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,在不同的框架版本中是不可下载的。我想我漏掉了什么东西。

有谁可以帮我?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-16 10:38:54

我终于解决了这个问题。老实说,我不知道是什么最终解决了这件事。对于每一个偶然发现这个问题的人,下面是我试图解决这个问题的一些事情。它们没有具体的顺序(因为我多次尝试了所有的东西):

  • 更新RGiesecke.DLLExport数据包
  • 将Konfigurations中的平台更改为x86
  • Interop.PortableDeviceApiLib like in 问题( Christophe和Bruno的回答)
  • 删除对Interop.PortableDeviceApiLib的引用
  • 删除对Interop.PortableDeviceTypesLib的引用
  • 重新添加对Interop.PortableDeviceApiLib的引用
  • 重新添加对Interop.PortableDeviceTypesLib的引用
  • 重建工程
  • 将Interoptyp嵌入设置为false (我找到了各种语句以避免此操作,但项目在获得它时是这样设置的,并且在两个Interop-Lib上都工作了(小心))。

至少这对我有帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58396049

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档