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

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.
LVL 15
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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?

Once you got the machine name, use 'NetShareEnum()' - see the sample code at

#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 );
          server[0] = L'\0';

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

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

          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 )];
                    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;


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Are you on Win9x/ME?
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 :

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.
Aaah, now I got the problem - you were not using the UNICODE version of 'GetComputerName()' :o)

Thanx :o)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.