Solved

Open a remote disk drive

Posted on 2007-11-19
7
1,505 Views
Last Modified: 2013-11-20
I want to access a disk drive of a remote computer. is there any way i can do it? I am developing in VS2003 using VC++.  The function  CreateFile() can  open the disk drive of the local machine. I am not sure if the same function works for the remote disk drives too. Is there any function in VC++ that can do the same for the remote disk drives?
0
Comment
Question by:tariq6000
7 Comments
 
LVL 4

Accepted Solution

by:
yuy2002 earned 50 total points
ID: 20318058
Is the remote computer in the same lan network?If so , you can use disk map.
You can map the disk drive of remote computer to you local machine disk just as you have privilage to access the remote computer's disk.Then you could do evething on remote disk like the local disk.  
0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 20318273
Can't you just mount it as a network drive or access it via UNC path?
\\<servername>\<sharename>\<directory>
0
 

Author Comment

by:tariq6000
ID: 20318417
Yes, the remote disk is in the same LAN. But, I don't know how to map the remote disk on to the local machine. Can you please provide me a link to the sample code snippet illustrating on how it can be done?
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 20318455
Use the UNC path...

HANDLE CreateFile(
   "\\remotepc\sharename\myfile.txt",
   ... OTHER OPTIONS
);

...or mount it as a network drive and use it as if it was local: -

http://www.cs.umd.edu/faq/pc/map_network_drive/map_network_drive.html

0
 
LVL 40

Assisted Solution

by:evilrix
evilrix earned 150 total points
ID: 20318461
Oops... I forgot the double slash for C-String...

CreateFile(
    "\\\\remotepc\\sharename\\myfile.txt",
    ... OTHER OPTIONS
);
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 50 total points
ID: 20320159
>>I don't know how to map the remote disk on to the local machine

That is done using 'WNetAddConnection2()', e.g.

#include <winnetwk.h>

    NETRESOURCE nr;

    ZeroMemory  (   &nr,    sizeof  (   NETRESOURCE));

    nr.dwType           =   RESOURCETYPE_DISK;
    nr.lpLocalName      =   "x:";
    nr.lpRemoteName     =   "\\\\theserver\\E$";

    //  authenticate server share using the remote user info
    dwRes   =   WNetAddConnection2  (   &nr,
                                       "SecretPassword",
                                       "UserName",
                                       0
                                   );

The above would map the drive as 'x:' (link with 'mpr.lib')
0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 50 total points
ID: 20321822
You only can have access to a remote disk if there is a shared folder where you have access to.

Then you can map a free drive letter from the commandline by

  net use Z: \\server\share

If you don't have access either to 'server' or 'server\share' you'll get an appropriate error message. To see what driver letters are available you can type

   net use

and
 
  subst

These commands will show the dynamically defined drive letters either defined by 'net use' command or by 'subst' command. You have to add the local disk drive letters to get a full list.

Note, with 'net use' you only can map remote (folders of a) share using the UNC name.  With 'subst' you can map drive letters to any path using a local drive letter but not UNC paths. Also paths using driver letters defined with 'net use' can be used:

    net use K: \\my_server\my_share
    subst L: K:\any_path\any_folder

You can do all that programmatically by using the system function:

#include <process.h>

        int ret;
        if ((ret = system("net use K: \\\\my_server\\my_share")) != 0)
        {
               // error:  ret == 2 if path doesn't exist
        }

        if ((ret = system("subst L: K:\\any_path")) != 0)
        {
               // error:  ret == 2 if path doesn't exist
        }

The subst normally doesn't give an error when using an UNC name at the right side. But it will not work as a valid drive letter doing so.

You can check whether the 'subst' was successful by using the stat function:

#include <sys/stat.h>
#include <errno.h>

        ...
        if ((ret = system("subst L: K:\\any_path")) != 0)
        {
               // error:  ret == 2 if path doesn't exist
        }
         struct stat fs;
         if (stat("L:\\any_file_or_folder", &fs) != 0)
         {
               // errno will show the error code
         }  

Regards, Alex







0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

In this article you will get to know about pros and cons of storage drives HDD, SSD and SSHD.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

828 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