Solved

Network drive mapping

Posted on 2003-10-28
16
713 Views
Last Modified: 2007-12-19
Hi Experts,

I need to write simple C++ application to :

1 Find out if PC is mapped to ANY from F to Z drives to directory
  "AAA"

2 If yes then start .MDE file from there and end C++ execution
3 If not then map into any available drive letter
   and start .MDE file from newly mapped one and end C++ execution

Thank you in advance.
0
Comment
Question by:fpoyavo
  • 6
  • 6
  • 2
  • +1
16 Comments
 
LVL 1

Author Comment

by:fpoyavo
ID: 9637628
note: Please do not use Wshells or ActiveX.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9638056
"net use" command can be used to map a network path to a drive letter.
net use F: /DELETE
net use F: \\chastity.scsnet.csc.com\dmaisona /user:username mypassword /PERSISTENT:NO
0
 
LVL 30

Expert Comment

by:Axter
ID: 9638070
Example C/C++ code:

system("net use t: \\\\server\\share");
0
 
LVL 30

Expert Comment

by:Axter
ID: 9638084
Here's another method for mapping a drive using Windows API functions:

 NETRESOURCE         nr;

   ZeroMemory  (   &nr,    sizeof  (   NETRESOURCE));

   nr.dwType           =   RESOURCETYPE_DISK;
   nr.lpLocalName      =   "S:";
   nr.lpRemoteName     =   "\\\\server\\sharename;

   //  map server share to local drive using the remote user info
   dwRes   =   WNetAddConnection2  (   &nr,
                                       "Username",
                                       "Password",
                                       0
                                   );

0
 
LVL 30

Expert Comment

by:Axter
ID: 9638159
You can use WNetGetConnection and WNetGetResourceInformation to check mapped drives.
0
 
LVL 30

Expert Comment

by:Axter
ID: 9638175
You can also use 'WNetOpenEnum()' to start the enumeration and 'WNetEnumResource()' to subsequently retrieve the information of all shares of the desired type.
Check out following link:
http://msdn.microsoft.com/library/sdkdoc/network/networks_77sj.htm
0
 
LVL 86

Expert Comment

by:jkr
ID: 9638776
>>Find out if PC is mapped to ANY from F to Z drives to directory "AAA"

For that, you need to know the local name of the share on the server that shares it. To do so, use 'WNetGetConnection()' (which Axter already mentioned) to get the share name form a drive letter and the resolve the server's local name:


      UINT      uDriveType;
      wchar_t      cDrive;

      for      (      cDrive      =      0;      cDrive      <      26;      cDrive++)
            {
                  wsprintf      (      acDriveName,      L"%c:\\",      cDrive      +      L'A');

                  uDriveType      =      GetDriveTypeW (      acDriveName);

                  if ( DRIVE_REMOTE == uDriveType)
                  {

                        wchar_t acShareName [ MAX_PATH];
                        wchar_t acShareLocalName [ MAX_PATH];

                        WNetGetConnectionW ( acDriveName, acShareName, sizeof ( acShareName));

                        GetShareLocalName ( acShareName, acShareLocalName, sizeof ( acShareLocalName));

                        // now, check whether the mapping to the directory is correct
                  }
            }

0
 
LVL 86

Expert Comment

by:jkr
ID: 9638788
Ooops, forgot something:

#include <lmcons.h>
#include <lmerr.h>
#include <lmshare.h>
#include <lmaccess.h>
#include <lmapibuf.h>

NET_API_STATUS  GetShareLocalName   (   wchar_t*    pwszShareName,
                                        wchar_t*    pwszLocalName,
                                        DWORD       dwSize
                                    )
{
    SHARE_INFO_2*   psi2    =   NULL;
    NET_API_STATUS  nas     =   0;

    wchar_t         awcServer   [   LM20_CNLEN] =   L"";
    wchar_t         awcShare    [   LM20_NNLEN];

    wchar_t*        pwc;
    wchar_t*        pwcBuf;

    if  (   pwc =   wcsstr  (   pwszShareName,  L"\\\\"))
        {
            pwcBuf  =   new wchar_t [   wcslen  (   pwszShareName)];

            wcscpy  (   pwcBuf, pwszShareName);

            if  (   !(  pwc =   wcsstr  (   pwcBuf  +   2,  L"\\")))
                {
                    delete  []  pwcBuf;
                   
                    return  (   NERR_BadComponent);
                }
             else   *pwc    =   L'\0',  pwc++;

            wcscpy  (   awcServer,  pwcBuf);
            wcscpy  (   awcShare,   pwc);

            delete  (   pwcBuf);

#ifdef  _DEBUG
            wprintf (   L"\nServer %s Share %s\n",  awcServer,  awcShare);
#endif

        }
     else
        {
            wcscpy  (   awcShare,   pwszShareName);
        }


    if  (   NERR_Success    ==  (   nas =   NetShareGetInfo (   awcServer,
                                                                awcShare,
                                                                2,
                                                                ( UCHAR**) &psi2
                                                            )
                                )
        )
        {
            wcscpn  (   pwszLocalName,  psi2->shi2_path,    dwSize);

            NetApiBufferFree    (   psi2);

            return  (   nas);
        }

    return  (   nas);
}
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Expert Comment

by:Axter
ID: 9638870
jkr,
Just out of curiousity, is there a reason why you used UNICODE for the above code?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9638929
>> is there a reason why you used UNICODE for the above code?

The simple reason is that the code I've cut it out from is UNICODE :o)

(BTW, our company is close to drop ANSI strings in their coding guidlines anyway - hum, I should mention that I am one of the owners, though *duck*)
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 9640995
Hello there..
To both jkr and Axter.
I think you forget to mention the destroying of the network drive after the job is finnished...:-)
   char      *Name;
   Name = // "F" to "Z"
   WNetCancelConnection2(Name,0,true);
Anyway the approach you proposed both is way more detailed than the one I had to write..:-)

Regards.
gtokas.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9642178
>>I think you forget to mention the destroying of the network drive after the job is finnished

No. The suggested code does not map a network drive, so disconnecting would be inherently dangerous.
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 9642790
>>No. The suggested code does not map a network drive, so disconnecting would be inherently dangerous.

Sure but if you map the drive after the check for valid directory at the end of the work the file made (at destroy) if you do not disconnect the drives assigned the target PC will have too many network drives installed.
gtokas.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9642807
>>Sure but if you map the drive after the check for valid directory

This is not done. Only existing mappings are checked.
0
 
LVL 1

Author Comment

by:fpoyavo
ID: 9643591
Hi Jkr,

This is sounds like "Scary movie 4" :)) I've got some questions.
I am using VC++ and cannot GET clean compile.

I am getting a bunch of errors on :

#include <lmshare.h>
#include <lmaccess.h>
#include <lmapibuf.h>

Here are some messages :

error C2143: syntax error : missing ';' before '__stdcall'
error C2501: 'DWORD' : missing storage-class or type specifiers
error C2065: 'LPTSTR' : undeclared identifier
error C2146: syntax error : missing ')' before identifier 'servername'
error C2501: 'NetShareAdd' : missing storage-class or type specifiers
error C2501: 'NetShareSetInfo' : missing storage-class or type specifiers

There are much more....but they are similar.
Hundreds of them.

Is anything missing on my system ?

Please advise.



0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 9644007
Try


#define UNICODE
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <lmcons.h>
#include <lmerr.h>
#include <lmshare.h>
#include <lmaccess.h>
#include <lmapibuf.h>

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

#ifdef __cplusplus
extern "C"
{
#endif

int __cdecl         wmain                   (   int             argc,  
                                                wchar_t**       argv
                                            );

#ifdef __cplusplus
}
#endif

NET_API_STATUS  GetShareLocalName   (   wchar_t*    pwszShareName,
                                        wchar_t*    pwszLocalName,
                                        DWORD       dwSize
                                    )
{
    SHARE_INFO_2*   psi2    =   NULL;
    NET_API_STATUS  nas     =   0;

    wchar_t         awcServer   [   MAX_PATH]   =   L"";
    wchar_t         awcShare    [   MAX_PATH];

    wchar_t*        pwc;
    wchar_t*        pwcBuf;

    if  (   pwc =   wcsstr  (   pwszShareName,  L"\\\\"))
        {
            pwcBuf  =   new wchar_t [   wcslen  (   pwszShareName)];

            wcscpy  (   pwcBuf, pwszShareName);

            if  (   !(  pwc =   wcsstr  (   pwcBuf  +   2,  L"\\")))
                {
                    delete  []  pwcBuf;
                   
                    return  (   NERR_BadComponent);
                }
             else   *pwc    =   L'\0',  pwc++;

            wcscpy  (   awcServer,  pwcBuf);
            wcscpy  (   awcShare,   pwc);

            delete  (   pwcBuf);

#ifdef  _DEBUG
            wprintf (   L"\nServer %s Share %s\n",  awcServer,  awcShare);
#endif

        }
     else
        {
            wcscpy  (   awcShare,   pwszShareName);
        }


    if  (   NERR_Success    ==  (   nas =   NetShareGetInfo (   awcServer,
                                                                awcShare,
                                                                2,
                                                                ( UCHAR**) &psi2
                                                            )
                                )
        )
        {
            wcsncpy (   pwszLocalName,  psi2->shi2_path,    dwSize);

            NetApiBufferFree    (   psi2);

            return  (   nas);
        }

    return  (   nas);
}

int wmain   (   int         argc,  
                wchar_t**   argv
            )
{
    NET_API_STATUS  nas;

    UINT     uDriveType;
    wchar_t     cDrive;
    wchar_t     acDriveName [ 4];
    wchar_t acShareName [ MAX_PATH];
    wchar_t acShareLocalName [ MAX_PATH] = L"\0";

    for  (     cDrive     =     L'A';     cDrive     <     L'Z';     cDrive++)
         {
              wsprintf     (     acDriveName,     L"%c:",     cDrive);

              uDriveType     =     GetDriveTypeW (     acDriveName);

              wprintf ( L"checking %s\n", acDriveName);

              if ( DRIVE_REMOTE == uDriveType)
               {

                   wprintf ( L"%s is 'remote'\n", acDriveName);

                   DWORD dwLen = sizeof ( acShareName);

                   DWORD dwErr = WNetGetConnectionW ( acDriveName, acShareName, &dwLen);

                   if ( dwErr) { wprintf ( L"WNetGetConnectionW() failed with %d\n", dwErr); break;}

                   wprintf ( L"%s is the share name\n", acShareName);

                   nas = GetShareLocalName ( acShareName, acShareLocalName, MAX_PATH);

                   if ( NERR_Success == nas)
                   {

                        wprintf ( L"Found '%s'\n", acShareLocalName);

                   // now, check whether the mapping to the directory is correct

                   }
              }
         }

    return  (   0);
}

:o)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now