Context
I have a function in my app's code that connects (via WNetUseConnection) to a windows fileshare on specified devices on the network the machine that is running the app is connected to. This is for the purposes of programmatically uploading/creating backups for files on the devices, but that's not important since I am failing at the "connect" stage. I will describe the problem with just one device to try avoid confusion.
I have read the MS Docs on how to use WNetUseConnection and what the errors mean. A better question for my title could be: what am I doing wrong when using WNetUseConnection?
Edit: The app is run as a service. Does this prevent file I/O outside of its working directory?
The problem and attempts to fix
Whenever the WNetUseConnection function is used, I get ERROR_BAD_NET_NAME printing to the console. From what I can tell I have done everything correct though. My code for connecting is a copy of this code here, and the exact section looks as such:
public string ConnectToShare(string uri, string username, string password)
{
var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = uri,
};
_networkName = uri;
_logger.LogWarning($"Attempting to connect to share with u/{username} p/{password} on uri: {_networkName}");
int ret = WNetUseConnection(IntPtr.Zero, netResource, password, username, 0, null, null, null);
if (ret == NO_ERROR)
return null;
else
{
var err = GetError(ret);
_logger.LogError($"Failed to connect to share: {err}");
return err;
}
}
and the bit of the logs this ...logs... to looks like this:
[<DateTime> WRN Application.DeviceMonitor.NetworkConnection] Attempting to connect to share with u/.\user p/password on uri: \\10.0.0.143
- The path itself is a standard IPv4 address, and I know the connection details (see above). I have tried various configurations of the uri and username (with/out leading back-slashes and period) and tried including the primary drive being accessed from the share (
\\10.0.0.143\d). - I have tried other Enum values for the
Scope,ResourceTypeandDisplayTypeparameters, in case I had them wrong, to no success. - I can connect to this fileshare manually, through windows explorer and both command prompt and powershell using the aforementioned details and appropriate
netcommands. I can do this from my development PC as well as the VM where the app runs. - Firewall is open, and the device talks to the app in other ways (mostly api commands).
- NTLM being en/disabled does not have an effect one way or another1.
- Group policy stuff does not have an effect1.
The only other potentially relevant information I can think of is that both VM and device are running the following Windows 10 packages:
- My PC: Pro, v10.0.19045
- VM: Server 2019 Standard, v10.0.17763
- Device: Enterprise LTSC, v10.0.17763
1 These things were tried after reading down this call for help.
\\10.0.0.143is not correct name, try to use just ip, ie10.0.0.143