#include "net.h"
+#define COUNT_OF(a) (sizeof(a) / sizeof(a[0]))
+
static
DWORD
EnumerateConnections(LPCWSTR Local)
DWORD dCount = -1;
LPNETRESOURCE lpCur;
- printf("%S\t\t\t%S\t\t\t\t%S\n", L"Local", L"Remote", L"Provider");
+ ConPrintf(StdOut, L"%s\t\t\t%s\t\t\t\t%s\n", L"Local", L"Remote", L"Provider");
dRet = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_DISK, 0, NULL, &hEnum);
if (dRet != WN_SUCCESS)
{
if (!Local || wcsicmp(lpCur->lpLocalName, Local) == 0)
{
- printf("%S\t\t\t%S\t\t%S\n", lpCur->lpLocalName, lpCur->lpRemoteName, lpCur->lpProvider);
+ ConPrintf(StdOut, L"%s\t\t\t%s\t\t%s\n", lpCur->lpLocalName, lpCur->lpRemoteName, lpCur->lpProvider);
}
lpCur++;
if (argc == 2)
{
Status = EnumerateConnections(NULL);
- printf("Status: %lu\n", Status);
+ ConPrintf(StdOut, L"Status: %lu\n", Status);
return 0;
}
else if (argc == 3)
Len = wcslen(argv[2]);
if (Len != 2)
{
- PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
+ ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
return 1;
}
if (!iswalpha(argv[2][0]) || argv[2][1] != L':')
{
- PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
+ ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
return 1;
}
Status = EnumerateConnections(argv[2]);
- printf("Status: %lu\n", Status);
+ ConPrintf(StdOut, L"Status: %lu\n", Status);
return 0;
}
Len = wcslen(argv[2]);
if (Len != 1 && Len != 2)
{
- PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
- printf("Len: %lu\n", Len);
+ ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
+ ConPrintf(StdOut, L"Len: %lu\n", Len);
return 1;
}
if (Len == 2 && argv[2][1] != L':')
{
- PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
+ ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
return 1;
}
if (argv[2][0] != L'*' && !iswalpha(argv[2][0]))
{
- PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
+ ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
return 1;
}
{
if (argv[2][0] == L'*')
{
- PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
+ ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
return 1;
}
- return WNetCancelConnection(argv[2], FALSE);
+ return WNetCancelConnection2(argv[2], CONNECT_UPDATE_PROFILE, FALSE);
}
else
{
BOOL Persist = FALSE;
NETRESOURCE lpNet;
+ WCHAR Access[256];
+ DWORD OutFlags = 0, Size = COUNT_OF(Access);
Len = wcslen(argv[3]);
if (Len < 4)
{
- PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"Name");
+ ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"Name");
return 1;
}
if (argv[3][0] != L'\\' || argv[3][1] != L'\\')
{
- PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"Name");
+ ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"Name");
return 1;
}
else
{
HeapFree(GetProcessHeap(), 0, Cpy);
- PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE, L"Persistent");
+ ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"Persistent");
return 1;
}
}
lpNet.lpRemoteName = argv[3];
lpNet.lpProvider = NULL;
- return WNetAddConnection2(&lpNet, NULL, NULL, CONNECT_REDIRECT | (Persist ? CONNECT_UPDATE_PROFILE : 0));
+ Status = WNetUseConnection(NULL, &lpNet, NULL, NULL, CONNECT_REDIRECT | (Persist ? CONNECT_UPDATE_PROFILE : 0), Access, &Size, &OutFlags);
+ if (argv[2][0] == L'*' && Status == NO_ERROR && OutFlags == CONNECT_LOCALDRIVE)
+ ConPrintf(StdOut, L"%s is now connected to %s\n", argv[3], Access);
+ else if (Status != NO_ERROR)
+ {
+ LPWSTR Buffer;
+
+ ConPrintf(StdErr, L"The system error %d happened.\n", Status);
+
+ if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, Status, 0, (LPWSTR)&Buffer, 0, NULL))
+ {
+ ConPrintf(StdErr, L"\n%s\n", Buffer);
+ LocalFree(Buffer);
+ }
+ }
+
+ return Status;
}
}