Determine the shared device type in Windows system C program example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows Xp Pro SP2
Target platform: none, just for learning and fun
Header file: Standard and Windows
Additional library:Windows Platform SDK
Additional project setting: Set project to be compiled as C
Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)
Other info: non-CLR or unmanaged. Need to add netapi32.lib (netapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.
To do: Determine the Windows shared device type programmatically
To show: The various Windows network management functions - sharing resources
// Link to the netapi32.lib...
// The return value of share functions code can be found in Network Management Error Codes in MSDN
// For Win Xp Pro, adjust accordingly for other Windows version
#define _WIN32_WINNT 0x0501
// #define UNICODE - already unicode set through compiler
#include <windows.h>
#include <stdio.h>
#include <lm.h>
int wmain(int argc, TCHAR *argv[])
{
NET_API_STATUS res;
DWORD devType = 0;
size_t sizeInWords = 50;
if(argc < 3)
{
// %ls or %S is OK...
printf("Usage: %ls <server_name> <device_name>\n", argv[0]);
printf("Usage: %ls mypersonal \"E:\\testshare\"\n", argv[0]);
}
else
{
// Call the NetShareCheck() function.
// res = NetShareCheck(argv[1], _wcsupr(argv[2]), &devType);
res = NetShareCheck(argv[1], (LPWSTR)argv[2], &devType);
// If the function succeeds, inform the user.
printf("The res return value is: %u\n", res);
if(res == NERR_Success)
switch(devType)
{
case 0 : printf("Device is shared as type STYPE_DISKTREE - disk drive/folder.");
break;
case 1 : printf("Device is shared as type STYPE_PRINTQ - Print queue.");
break;
case 2 : printf("Device is shared as type STYPE_DEVICE - Communication device.");
break;
case 3 : printf("Device is shared as type STYPE_IPC - Interprocess communication (IPC).");
break;
case 4 : printf("Device is shared as type STYPE_SPECIAL - Special share reserved \
for interprocess communication (IPC$) or \
remote administration of the server (ADMIN$). \
Can also refer to administrative shares such as C$, D$, E$, and so forth.");
break;
case 5 : printf("Device is shared as type STYPE_TEMPORARY - A temporary share.");
break;
}
// Otherwise, print the return error code.
else
{
printf("Something wrong, error: %u\n", res);
perror("The error string ");
}
}
return 0;
}
Output example:
(This program run at the command prompt)
F:\vc2005project\cplus\debug>cplus
Usage: cplus <server_name> <device_name>
Usage: cplus mypersonal "E:\testshare"
F:\vc2005project\cplus\debug>cplus mypersonal C:\
The res return value is: 0
Device is shared as type STYPE_DISKTREE - disk drive/folder.
F:\vc2005project\cplus\debug>cplus mypersonal E:\
The res return value is: 0
Device is shared as type STYPE_DISKTREE - disk drive/folder.
F:\vc2005project\cplus\debug>cplus mypersonal E:\mytestfolder
The res return value is: 0
Device is shared as type STYPE_DISKTREE - disk drive/folder.
F:\vc2005project\cplus\debug>cplus mypersonal E:\mytestfolder\testfile.txt
The res return value is: 0
Device is shared as type STYPE_DISKTREE - disk drive/folder.
F:\vc2005project\cplus\debug>cplus mypersonal C:\inetpub\wwwroot
The res return value is: 0
Device is shared as type STYPE_DISKTREE - disk drive/folder.
F:\vc2005project\cplus\debug>