Is it possible to use Microsoft's Offline File

2019-07-28 05:02发布

问题:

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:

Well, it says

Create this object as an in-proc COM server using the class ID CLSID_OfflineFilesCache.

From C or C++, that'd be a call to CoCreateObject. Just use the .NET replacement for CoCreateObject. 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:

Unfortunately, you cannot create a MobSync.dll interop assembly by adding a reference to this COM component in the Visual Studio Solution Explorer. Attempting to do so produces an error. This occurs because MobSync.dll, unlike many COM component DLLs, doesn't have type library (TLB) information to support COM interop embedded inside it.

.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#.



标签: c# com