0

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, ResourceType and DisplayType parameters, 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 net commands. 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.

4
  • 1
    The explanation from Microsoft does not help? The value specified by the lpRemoteName member is not acceptable to any network resource provider because the resource name is invalid, or because the named resource cannot be located. Commented Oct 27 at 15:49
  • @AxelKemper I guess I should ask for help fixing the problem instead of asking why it's not working, since as per my post: 1) I believe I have done everything correctly and 2) I can connect manually. Would that improve the question? Furthermore, I was under the impression the lpRemoteName could follow either a named address or standard UNC path rules. Is that not the case? As the MS docs do not make that clear. Commented Oct 27 at 15:57
  • \\10.0.0.143 is not correct name, try to use just ip, ie 10.0.0.143 Commented Oct 27 at 16:38
  • @IłyaBursov that was tried to no success Commented Oct 28 at 8:20

1 Answer 1

0

The reason this doesn't work is because the app is running as a service. Running it from the .exe works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.