Network drive mapping

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.
LVL 1
fpoyavoAsked:
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.

fpoyavoAuthor Commented:
note: Please do not use Wshells or ActiveX.
0
AxterCommented:
"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
AxterCommented:
Example C/C++ code:

system("net use t: \\\\server\\share");
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

AxterCommented:
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
AxterCommented:
You can use WNetGetConnection and WNetGetResourceInformation to check mapped drives.
0
AxterCommented:
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
jkrCommented:
>>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
jkrCommented:
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
AxterCommented:
jkr,
Just out of curiousity, is there a reason why you used UNICODE for the above code?
0
jkrCommented:
>> 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
George TokasCommented:
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
jkrCommented:
>>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
George TokasCommented:
>>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
jkrCommented:
>>Sure but if you map the drive after the check for valid directory

This is not done. Only existing mappings are checked.
0
fpoyavoAuthor Commented:
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
jkrCommented:
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

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

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.