I am trying to create a cloud sync provider in C#, using CSWin32 to access the CloudFilter API. However, whenever I call CfRegisterSyncProvider
I get the error code 0x80070057
. When I pass that to Marshal.GetExceptionForHR
it returns System.ArgumentException: Value does not fall within the expected range.
Below is my code:
using Windows.Win32;
using Windows.Win32.Storage.CloudFilters;
using Windows.Win32.Foundation;
using System.Runtime.InteropServices;
namespace PStore;
internal class Install {
public static void Main() {
unsafe {
fixed (char* providerNameP = "PStore", providerVersionP = "1.1.1") {
CF_SYNC_REGISTRATION SyncReg = new() {
ProviderName = new PCWSTR(providerNameP),
ProviderVersion = new PCWSTR(providerVersionP)
};
CF_SYNC_POLICIES SyncPol = new() {
Hydration = new CF_HYDRATION_POLICY { Primary = CF_HYDRATION_POLICY_PRIMARY.CF_HYDRATION_POLICY_FULL, Modifier = CF_HYDRATION_POLICY_MODIFIER.CF_HYDRATION_POLICY_MODIFIER_AUTO_DEHYDRATION_ALLOWED },
Population = new CF_POPULATION_POLICY { Primary = CF_POPULATION_POLICY_PRIMARY.CF_POPULATION_POLICY_FULL, Modifier = CF_POPULATION_POLICY_MODIFIER.CF_POPULATION_POLICY_MODIFIER_NONE },
InSync = CF_INSYNC_POLICY.CF_INSYNC_POLICY_NONE,
HardLink = CF_HARDLINK_POLICY.CF_HARDLINK_POLICY_NONE,
PlaceholderManagement = CF_PLACEHOLDER_MANAGEMENT_POLICY.CF_PLACEHOLDER_MANAGEMENT_POLICY_DEFAULT,
};
HRESULT res = PInvoke.CfRegisterSyncRoot(
@"C:\Users\finne\PStore",
SyncReg,
SyncPol,
CF_REGISTER_FLAGS.CF_REGISTER_FLAG_NONE
);
if (res != HRESULT.S_OK) {
Console.WriteLine($"Failed with {(uint) res:X8}");
throw Marshal.GetExceptionForHR((int) res) ?? new Exception($"Unidentified Error - {(uint) res:X8}");
}
}
}
}
}
I get the output
Failed with 80070057
Unhandled exception. System.ArgumentException: Value does not fall within the expected range.
at PStore.Install.Main() in C:\Users\finne\OneDrive\Documents\0coding\PStore\PStore\Install.cs:line 45
I should see the cloud provider show up in the left navigation menu of File Explorer, but it does not, nor does it show up in the Registry.
I have attempted changing different parameters, adding or removing optional parameters, and using the pointer overload of CfRegisterSyncRoot
, all to no avail.
I have no conflicts with other cloud providers, as OneDrive does not have access to the relevant folder, and OneDrive is the only provider visible in the registry.
This Microsoft page suggests that it is an invalid argument, as does the marshalled exception, but I do not know what argument it could be. I have checked the API docs extensively but do not see where any arguments I've passed would cause an error.
This answer suggests that it could be a corrupted/missing library. I cleared my /bin
folder as it says, but that did not work.
This GitHub issue for a Cloud Sync service says it comes from the fact that some of the code is in different libraries. I do not think that is the case for me, as it still errors with my one-file MRE. However, if that is a possibility, then I could use advice on how to resolve the issue, as I am not sure how to approach that at all.
My Github repository has my full project for reference, but the MRE above demonstrates the problem.
In ProviderRegistrar.cs, there is code that successfully registers a provider using the registry, as suggested in this Microsoft Guide, but that does not seem to work with the rest of the API. If it turns out the registry route is the way to go, I will have to open a new question to figure out why it doesn't work with API placeholders.
I suspect there is something wrong with the arguments I am passing to the function, as there is little documentation and I am going through a C# interface for these libraries. However, even after extensive double-checking and trial and error, I have not found anything.
Please let me know what is causing this error!
According to the suggestion of Jonathan Potter:
You need to initialize the StructSize
parameter in CF_SYNC_REGISTRATION
and CF_SYNC_POLICIES
.
Use Marshal.SizeOf<CF_SYNC_REGISTRATION>()
and Marshal.SizeOf<CF_SYNC_POLICIES>()
for their respective values.