我试图弄清楚如何使用 离线文件 API(如果可能)。我相信,如果 API 是 COM API,那么理论上我应该能够使用以下方法从 C# 调用它 此处。
不幸的是,我不知道它是否是 COM API,也不知道如何判断。 (作为更一般的旁注,谁能告诉我如何判断 API 是否与 COM 兼容?)
我已成功从 API 调用 OfflineFilesQueryStatus
函数,声明如下
[DllImport("cscapi.dll")]
public static extern int OfflineFilesQueryStatus(out bool pbActive, out bool pbEnabled);
:不确定如何使用接口(即,我不知道如何创建 IOfflineFilesCache 对象。)
任何人都可以帮我解释一下吗?
I'm trying to figure out how to use the Offline Files API from C# (if possible). I believe that if the API is a COM API, then I should theoretically be able to call it from C# using the methods here.
Unfortunately, I have no idea if it's a COM API or not, or how to tell. (As a more general side note, can anyone tell me how to tell whether an API is COM compatible?)
I've successfully called the OfflineFilesQueryStatus
function from the API by declaring it like:
[DllImport("cscapi.dll")]
public static extern int OfflineFilesQueryStatus(out bool pbActive, out bool pbEnabled);
But I'm not sure how to use the interfaces (i.e., I don't know how to create an IOfflineFilesCache
object.)
Can anyone help explain this to me?
发布评论
评论(1)
嗯,它说
在 C 或 C++ 中,这将是对 CoCreateObject 的调用。只需使用
CoCreateObject
的 .NET 替代品即可。我认为运行时可调用包装器/主互操作程序集应该发出元数据,让您可以使用普通的 C#new
语法来创建对象。对于 COM 对象,使用 .NET 中的脱机文件 API 比平常更困难,MSDN 解释了原因:
.NET 依赖类型库进行互操作,但 Microsoft 仅为这些接口提供了 C/C++ 标头。通过一些 IDL 黑客攻击,您可以让它工作,请参阅创建自定义同步管理器处理程序MSDN 上的示例,其中包含“修复”
mobsync.dll
的方法。您需要执行相同的步骤,但使用 cscapi/cscobj IDL。但到目前为止,最简单且最受支持的方法是使用 C++/CLI。在那里,您可以直接
#include
SDK 标头,还可以定义从 C# 轻松使用的托管类型。Well, it says
From C or C++, that'd be a call to
CoCreateObject
. Just use the .NET replacement forCoCreateObject
. I thought the runtime callable wrapper / primary interop assembly was supposed to emit metadata that would let you do that using the normal C#new
syntax for creating objects.Using the Offline Files API from .NET is more difficult than usual for COM objects, MSDN explains why:
.NET relies on the type library for interop, but Microsoft only provided the C/C++ header for these interfaces. With some IDL hacking, you can get it to work, see Creating a Custom Synchronization Manager Handler sample on MSDN, which has a recipe for "fixing"
mobsync.dll
. You'd need to perform the same steps, but with the cscapi/cscobj IDL.But by far the easiest and best-supported approach would be to use C++/CLI. There you can directly
#include
the SDK headers, and you can also define managed types easily used from C#.