?
Solved

Code to find local machine name and enumerate network shares

Posted on 2003-03-28
7
Medium Priority
?
322 Views
Last Modified: 2008-02-01
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
Comment
Question by:ozymandias
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 12

Expert Comment

by:Salte
ID: 8225985
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
 
LVL 86

Accepted Solution

by:
jkr earned 1200 total points
ID: 8226105
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
 
LVL 15

Author Comment

by:ozymandias
ID: 8226297
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:jkr
ID: 8226347
Are you on Win9x/ME?
0
 
LVL 86

Expert Comment

by:jkr
ID: 8226366
0
 
LVL 15

Author Comment

by:ozymandias
ID: 8227263
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
 
LVL 86

Expert Comment

by:jkr
ID: 8227290
Aaah, now I got the problem - you were not using the UNICODE version of 'GetComputerName()' :o)

Thanx :o)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question