0

I wrote a window to enter my username and password to login. I can't save the credential when I use the following method, what should I do

NETRESOURCEW net_resource {0};
net_resource.dwType = RESOURCETYPE_DISK | RESOURCETYPE_ANY;
TCHAR szRemotePath[MAX_PATH] {0};
_tcscpy_s(szRemotePath, MAX_PATH, remote_path.toStdWString().c_str());
net_resource.lpRemoteName = szRemotePath;
status_code = ::WNetAddConnection2(&net_resource, password.toStdWString().c_str(), user.toStdWString().c_str(),
CONNECT_UPDATE_PROFILE | CONNECT_INTERACTIVE | CONNECT_COMMANDLINE | CONNECT_CMD_SAVECRED);
0ices
  • 35
  • 9
  • 1
    What is `status_code` when `WNetAddConnection2` returns? It's not immediately clear from the question but it sounds like have created a GUI, but also want to use `CONNECT_COMMANDLINE`. Can you show a [mcve]? – IInspectable Oct 20 '21 at 08:05
  • I wrote the GUI window based on Qt and did not use the window that comes with Windows. If the username and password are correct, the return value of WNetAddConnection2 is OK and does not report any error. I just don't understand how I can save the credentials. – 0ices Oct 20 '21 at 08:46
  • Per the documentation for `CONNECT_UPDATE_PROFILE`: "*The operating system remembers only successful connections that redirect local devices. It does not remember connections that are unsuccessful **or deviceless connections**. (A deviceless connection occurs when the lpLocalName member is NULL or points to an empty string.)*" You are setting `lpLocalName` to NULL. – Remy Lebeau Oct 20 '21 at 19:25
  • @RemyLebeau I am making a connection to the network resource, so it is correct not to set `lpLocalName`. – 0ices Oct 21 '21 at 05:14

1 Answers1

2

You need to write the credentials into the credential vault, e.g. with CredWriteDomainCredentials. See my answer in this question for an example (written in Delphi but should be very straightforward to convert to C/C++)

Remko
  • 7,214
  • 2
  • 32
  • 52