?
Solved

Network drive mapping

Posted on 2003-10-28
16
Medium Priority
?
721 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
[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
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

719 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