Hi @Venkatesh Naik ,
Thanks for reaching out.
You’re running into a combination of OS behavior differences and a few subtle but important implementation details.
First, the UNC path itself matters. In the failing example, the path is specified as \\192.168.91.10 without a share name. Windows Server 2019 will reject this outright. You must always provide a full UNC path, including the share, for example:
\\192.168.91.10\ShareName
Server 2022 may seem more tolerant in some scenarios, but in practice, a UNC without a share (\\server) usually won’t succeed for connection calls. Always use the full \\server\share path.
Second, there are a couple of API usage details to correct:
- In the
NETRESOURCEstructure, ensure:-
lpRemoteName= full UNC, e.g.,\\192.168.91.10\ShareName -
dwType= numeric constantRESOURCETYPE_DISK(DWORD), not a string
-
- For more explicit and predictable credential handling, consider
WNetAddConnection2orWNetAddConnection3instead ofWNetUseConnection.
For credentials, be explicit. On Server 2019 in particular:
- Use
SERVERNAME\localusernameorDOMAIN\username - Prefer the server’s NetBIOS name over the IP address in the username when possible
Permissions:
- Verify that the account has both share permissions and NTFS permissions on the target folder. Both are required - having only one may still cause the connection to fail.
From an environment perspective, double-check the basics:
- Firewall: ensure File and Printer Sharing (SMB-In) on port 445 is enabled
- Services: confirm the Server service is running on the host and Workstation is running on the client
On the SMB and authentication side, Server 2019 enforces stricter defaults:
- NTLMv2 is required; guest or anonymous access is typically blocked
- If security baselines are applied, SMB signing and/or encryption may be required, and the client must negotiate them
- Do not enable SMB1
Cached sessions can also cause repeated failures: if there is an existing cached connection to the same \\server\share with different credentials, clear it first (net use \\server\share /delete or WNetCancelConnection2) before retrying with the correct credentials.
For troubleshooting, use:
- Validate access outside of code via the command line:
net use \\192.168.91.10\ShareName /user:SERVERNAME\localusername password - Retrieve precise error messages using
WNetGetLastError(preferred for WNet* APIs) orFormatMessage. This will help pinpoint whether the failure is due to username format, permissions, policy, or path issues.
Hope this helps! If my answer was helpful - kindly follow the instructions here so others with the same problem can benefit as well.