0

I try to connect to the c$ share on an old Windows NT4 SP6 Machine via WNetAddConnection2. This is the Code i used:

int main() {
    NETRESOURCE nr;
    std::string remote{"\\\\192.168.178.22\\c$"}
    nr.dwType = RESOURCETYPE_ANY;
    nr.lpLocalName = NULL;
    nr.lpRemoteName = remote.data();
    nr.lpProvider = NULL;

    auto res = WNetAddConnection2(&nr, "test", "User", NULL);

    if (res == NO_ERROR) {
        std::cout << "Connected\n";
    } else {
        std::cout << res << '\n';
    }
}

This just give me the Error Code 86 which stands for:

The specified network password is not correct.

If i try to mount the Share via net use like so:

net use \\192.168.178.22\c$ /user:User

And give the same Password when prompted for it the connection is successfully made.

Is there something wrong with my Code? Or do i miss something? Because for me it's strange that the Connection works with net use but not with the WNetAddConnection2 Function.

Kevin
  • 785
  • 2
  • 10
  • 32
  • You should replace `argv[2]` with `const_cast(remote.c_str())` or `&remote[0]`, or `remote.data()` in C++17 and later. And also, you should zero out any fields of the `NETRESOURCE` that you are not using. – Remy Lebeau Jul 14 '21 at 16:57
  • @RemyLebeau sorry copy paste error =( i also assigned the `remote.data()` to the `lpRemoteName` fixed it in the question – Kevin Jul 14 '21 at 18:32
  • `\c$` needs to be escaped as `\\c$` in your `remote` string literal – Remy Lebeau Jul 14 '21 at 18:38
  • @RemyLebeau have corrected it thanks again – Kevin Jul 14 '21 at 18:42
  • But for me it is strange that `net use` works and `WNetAddConnection2` gives me the error with the invalid password. – Kevin Jul 14 '21 at 18:44
  • You already stated that in the question, you don't need to repeat yourself. – Remy Lebeau Jul 14 '21 at 18:45

0 Answers0