I have also discovered from @rocketsarefast's answer that Windows will clear the old network credentials when there is a new login attempt.
However, his net use "\\10.0.0.5\c$" "badpassword" /user:"baduser"
command is way too slow, especially when the client has to wait up to several seconds for the server to respond with a rejection, which is terrible and inconvenient.
For my use case, I was able to use the Win32 API to map the network share \\Server\Share
to the S:\
drive. It disconnects the drive first and then prompts the user with the Windows network login dialog so that the person can logout and login as a different user.
Here is the C code main.c
:
#ifndef UNICODE
#define UNICODE
#endif
#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
#define _WIN32_IE 0x0500
#pragma comment(lib, L"mpr.lib")
#include <windows.h>
//#include <stdio.h>
int wmain(const size_t argc, const wchar_t** argv) {
NETRESOURCE networkResource = {0};
networkResource.dwType = RESOURCETYPE_DISK;
networkResource.lpLocalName = L"S:";
networkResource.lpRemoteName = L"\\\\Server\\Share";
networkResource.lpProvider = NULL;
DWORD result = 0;
result = WNetCancelConnection2(networkResource.lpLocalName, CONNECT_UPDATE_PROFILE, TRUE);
// wprintf(L"WNetCancelConnection2 result: %d\n", result);
result = WNetAddConnection2(&networkResource, NULL, NULL, CONNECT_INTERACTIVE | CONNECT_PROMPT);
// wprintf(L"WNetAddConnection2 result: %d\n", result);
// getchar();
return EXIT_SUCCESS;
}
Here is the CMakeLists.txt
:
cmake_minimum_required(VERSION 3.7)
project(MapNetworkDrive)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11 -g3 -pedantic -Wall -Wextra -O0")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -municode -mwindows")
set(SOURCE_FILES main.c)
add_executable(MapNetworkDrive ${SOURCE_FILES})
target_link_libraries(MapNetworkDrive mpr.lib)
Compile using MinGW-w64 - for 32 and 64 bit Windows:
As an alternative to C, here is an easy C# tutorial on the API: