Link to home
Start Free TrialLog in
Avatar of fpoyavo
fpoyavoFlag for United States of America

asked on

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.
Avatar of fpoyavo
fpoyavo
Flag of United States of America image

ASKER

note: Please do not use Wshells or ActiveX.
Avatar of Axter
"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
Example C/C++ code:

system("net use t: \\\\server\\share");
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
                                   );

You can use WNetGetConnection and WNetGetResourceInformation to check mapped drives.
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
>>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
                  }
            }

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);
}
jkr,
Just out of curiousity, is there a reason why you used UNICODE for the above code?
>> 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*)
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.
>>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.
>>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.
>>Sure but if you map the drive after the check for valid directory

This is not done. Only existing mappings are checked.
Avatar of fpoyavo

ASKER

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.



ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial