Solved

Network drive mapping

Posted on 2003-10-28
16
715 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

815 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

9 Experts available now in Live!

Get 1:1 Help Now