[ROSVBOXMGMT]
[reactos.git] / rosapps / applications / cmdutils / rosvboxmgmt / rosvboxmgmt.c
1 #include <stdio.h>
2 #include <wchar.h>
3 #include <windows.h>
4
5 /* DON'T CHANGE ORDER!!!! */
6 PCWSTR devices[3] = { L"\\\\.\\VBoxMiniRdrDN", L"\\??\\VBoxMiniRdrDN", L"\\Device\\VBoxMiniRdr" };
7
8 /* Taken from VBox header */
9 #define _MRX_MAX_DRIVE_LETTERS 26
10 #define IOCTL_MRX_VBOX_BASE FILE_DEVICE_NETWORK_FILE_SYSTEM
11 #define _MRX_VBOX_CONTROL_CODE(request, method, access) \
12 CTL_CODE(IOCTL_MRX_VBOX_BASE, request, method, access)
13 #define IOCTL_MRX_VBOX_ADDCONN _MRX_VBOX_CONTROL_CODE(100, METHOD_BUFFERED, FILE_ANY_ACCESS)
14 #define IOCTL_MRX_VBOX_GETLIST _MRX_VBOX_CONTROL_CODE(103, METHOD_BUFFERED, FILE_ANY_ACCESS)
15 #define IOCTL_MRX_VBOX_GETGLOBALLIST _MRX_VBOX_CONTROL_CODE(104, METHOD_BUFFERED, FILE_ANY_ACCESS)
16 #define IOCTL_MRX_VBOX_START _MRX_VBOX_CONTROL_CODE(106, METHOD_BUFFERED, FILE_ANY_ACCESS)
17
18 BOOL performDevIoCtl(DWORD dwIoControlCode, LPVOID lpInBuffer, DWORD nInBufferSize, LPVOID lpOutBuffer, DWORD nOutBufferSize)
19 {
20 short i;
21 BOOL ret;
22 DWORD lpBytesReturned;
23 HANDLE dev = INVALID_HANDLE_VALUE;
24
25 wprintf(L"Trying to open a VBoxSRV device\n");
26 for (i = 0; i < 3; ++i)
27 {
28 dev = CreateFile(devices[i],
29 GENERIC_READ | GENERIC_WRITE,
30 FILE_SHARE_READ | FILE_SHARE_WRITE,
31 NULL, OPEN_EXISTING, 0, NULL);
32 if (dev != INVALID_HANDLE_VALUE)
33 {
34 break;
35 }
36 }
37
38 if (dev == INVALID_HANDLE_VALUE)
39 {
40 return FALSE;
41 }
42
43 wprintf(L"%s opened.\n", devices[i]);
44
45 ret = DeviceIoControl(dev, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, &lpBytesReturned, NULL);
46 wprintf(L"Done: it %s with error: %lx\n", (ret != 0 ? L"succeed" : L"failed"), (ret != 0 ? ERROR_SUCCESS : GetLastError()));
47
48 CloseHandle(dev);
49
50 return ret;
51 }
52
53 int startVBoxSrv(void)
54 {
55 return (performDevIoCtl(IOCTL_MRX_VBOX_START, NULL, 0, NULL, 0) == FALSE);
56 }
57
58 int addConn(PCWSTR letter, PCWSTR path)
59 {
60 BOOL ret;
61 PWSTR inputBuffer;
62 DWORD inputBufferSize;
63
64 if (iswalpha(letter[0]) == 0)
65 {
66 wprintf(L"Invalid letter provided\n");
67 return 1;
68 }
69
70 if (wcschr(path, L'\\') != NULL)
71 {
72 wprintf(L"Only give the name of a share\n");
73 return 1;
74 }
75
76 inputBufferSize = (wcslen(path) + wcslen(devices[2]) + wcslen(L"\\;Z:\\vboxsvr\\")) * sizeof(WCHAR) + sizeof(UNICODE_NULL);
77 inputBuffer = malloc(inputBufferSize);
78 if (inputBuffer == NULL)
79 {
80 wprintf(L"Memory failure\n");
81 return 1;
82 }
83
84 swprintf(inputBuffer, L"%s\\;%c:\\vboxsvr\\%s", devices[2], towupper(letter[0]), path);
85 wprintf(L"Will create the following connection: %s\n", inputBuffer);
86 ret = performDevIoCtl(IOCTL_MRX_VBOX_ADDCONN, inputBuffer, inputBufferSize, NULL, 0);
87 free(inputBuffer);
88
89 return (ret == FALSE);
90 }
91
92 int getList(void)
93 {
94 short i;
95 BOOL ret;
96 DWORD outputBufferSize;
97 char outputBuffer[_MRX_MAX_DRIVE_LETTERS];
98
99 outputBufferSize = sizeof(outputBuffer);
100 ret = performDevIoCtl(IOCTL_MRX_VBOX_GETLIST, NULL, 0, &outputBuffer, outputBufferSize);
101 if (ret == FALSE)
102 {
103 return 1;
104 }
105
106 for (i = 0; i < _MRX_MAX_DRIVE_LETTERS; i += 2)
107 {
108 wprintf(L"%c: %s\t%c: %s\n", 'A' + i, (outputBuffer[i] == 0 ? L"FALSE" : L"TRUE"),
109 'A' + (i + 1), (outputBuffer[i + 1] == 0 ? L"FALSE" : L"TRUE"));
110 }
111
112 return 0;
113 }
114
115 int getGlobalList(void)
116 {
117 short i;
118 BOOL ret;
119 DWORD outputBufferSize;
120 char outputBuffer[_MRX_MAX_DRIVE_LETTERS];
121
122 outputBufferSize = sizeof(outputBuffer);
123 memset(outputBuffer, 0, outputBufferSize);
124 ret = performDevIoCtl(IOCTL_MRX_VBOX_GETGLOBALLIST, NULL, 0, &outputBuffer, outputBufferSize);
125 if (ret == FALSE)
126 {
127 return 1;
128 }
129
130 for (i = 0; i < _MRX_MAX_DRIVE_LETTERS; i += 2)
131 {
132 wprintf(L"%c: %s\t%c: %s\n", 'A' + i, (outputBuffer[i] & 0x80 ? L"Active" : L"Inactive"),
133 'A' + (i + 1), (outputBuffer[i + 1] & 0x80 ? L"Active" : L"Inactive"));
134 }
135
136 return 0;
137 }
138
139 void printUsage(void)
140 {
141 wprintf(L"ReactOS VBox Shared Folders Management\n");
142 wprintf(L"\tstart: start the VBox Shared folders (mandatory prior any operation!)\n");
143 wprintf(L"\taddconn <letter> <share name>: add a connection\n");
144 wprintf(L"\tgetlist: list connections\n");
145 wprintf(L"\tgetgloballist: list available shares\n");
146 }
147
148 int wmain(int argc, wchar_t *argv[])
149 {
150 PCWSTR cmd;
151
152 if (argc == 1)
153 {
154 printUsage();
155 return 0;
156 }
157
158 cmd = argv[1];
159
160 if (_wcsicmp(cmd, L"start") == 0)
161 {
162 return startVBoxSrv();
163 }
164 else if (_wcsicmp(cmd, L"addconn") == 0)
165 {
166 if (argc < 4)
167 {
168 printUsage();
169 return 0;
170 }
171
172 return addConn(argv[2], argv[3]);
173 }
174 else if (_wcsicmp(cmd, L"getlist") == 0)
175 {
176 return getList();
177 }
178 else if (_wcsicmp(cmd, L"getgloballist") == 0)
179 {
180 return getGlobalList();
181 }
182 else
183 {
184 printUsage();
185 return 0;
186 }
187 }