Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 329
  • Last Modified:

Code to find local machine name and enumerate network shares

I'm looking for some sample code that will find the local machine name and then list any shares with both the UNC and local path.

Something like :

Share                                    LocalPath
\\machinename\sharename1                 c:\data\directoryname1
\\machinename\sharename2                 c:\otherstuff\durectoryname2
etc
etc

Ideally I want either code that will work on 98/nt/2000/xp or two separate lots of code one for 98 and one for nt/2000/xp.

Will increase points for a quick and accurate answer.
0
ozymandias
Asked:
ozymandias
  • 4
  • 2
1 Solution
 
SalteCommented:
Try GetComputerName() to get the name of your computer. Type GetComputerName in your edit window and press F1 to get information about arguments, return value and how to call it etc etc.

To enumerate the network shares, I am not so sure, I believe it is possible to do but I don't know the exact function, have you tried to search in the index of your Win32 help file?

Alf
0
 
jkrCommented:
Once you got the machine name, use 'NetShareEnum()' - see the sample code at http://www.mvps.org/win32/network/nshe.cpp

#include <windows.h>
#include <lm.h>
#include <stdio.h>
#include <stdlib.h>
#pragma hdrstop



#pragma comment( lib, "netapi32.lib" )



#define MAXLEN 256



int main( int argc, char *argv[] )
{
     SHARE_INFO_2 *buf, *cur;
     DWORD read, total, resumeh, rc, i;
     wchar_t server[MAXLEN];
     char tempstr[20], *type;
     static char *types[] = { "disk", "printq", "device", "ipc" };

     if ( argc > 2 )
     {
          puts( "usage: nshe [\\\\server]" );
          return 1;
     }

     if ( argc >= 2 )
          mbstowcs( server, argv[1], MAXLEN );
     else
          server[0] = L'\0';

     resumeh = 0;
     do
     {
          buf = NULL;
          rc = NetShareEnum( argc < 2? NULL: server,
               2, (BYTE **) &buf, 8192, &read, &total, &resumeh );

          if ( rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS )
               break;

          printf( "\ngot %lu entries out of %lu\n", read, total );
          for ( i = 0, cur = buf; i < read; ++ i, ++ cur )
          {
               // Note: the capital S in the format string will expect Unicode
               // strings, as this is a program written/compiled for ANSI.

               if ( ( cur->shi2_type & ~STYPE_SPECIAL ) >= 0 &&
                    ( cur->shi2_type & ~STYPE_SPECIAL ) <= STYPE_IPC )
                    type = types[( cur->shi2_type & ~STYPE_SPECIAL )];
               else
                    sprintf( type = tempstr, "%08lx", cur->shi2_type );

               printf( "%-20.20S     %-8.8s%c     %-.40S\n",
                    cur->shi2_netname, type, ( cur->shi2_type & STYPE_SPECIAL )? '*': ' ',
                    cur->shi2_path );
          }

          if ( buf != NULL )
               NetApiBufferFree( buf );

     } while ( rc == ERROR_MORE_DATA );

     if ( rc != ERROR_SUCCESS )
          printf( "NShE() returned %lu\n", rc );

     return 0;
}

0
 
ozymandiasAuthor Commented:
Thanks guys...this looks good.

jkr :

error C2664: 'NetShareEnum' : cannot convert parameter 1 from 'unsigned short *' to 'char *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jkrCommented:
Are you on Win9x/ME?
0
 
jkrCommented:
0
 
ozymandiasAuthor Commented:
jkr : no. I'm running on XP and 2000.

In the end I jts changed the argument to NULL because I only want to check locally anyway, so it works fine.

BTW, it does not work on 95/98/ME and neither do the MS examples.

There seem to be a lot of MS exmaples that say you can use the NetShareEnum method in netapi32, including the one posted by jkr, which I had already tried, but it wouldn't run on 95/98/ME.

The fact is you can't. On 95/95/ME that method does not exist, or certainly isn't exported. That was what was bugging me until I found this :

http://www.mvps.org/win32/network/net_win9x.html

I tried doing exactly what it ways on this website and it works fine.

Anyway, thanks for your help and points to jkr for the code and the article pointer.
0
 
jkrCommented:
Aaah, now I got the problem - you were not using the UNICODE version of 'GetComputerName()' :o)

Thanx :o)
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now