Search code examples
vb.netwindows-ceimpersonation

How to copy file from network share in VB for an windows ce application


I am doing an application using vb.net.

What I need is access my computer (win 8) from a windows ce device and copy one file to the windows ce device. I already did that but what I need now is an way to pass the user, password and domain. I have researched about and found some solutions using System.Security.WindowsImpersonationContext So I think Something similar to that that will work in a windows ce application.

SOrry If you don't get something I have said but I am new on programming and english is not my home language.

Thanks in advance for your help


Solution

  • You could use the MapDrive method of the Network class from the SDF. Since it's pretty straightforward, the source for that method is as follows (I leave it to you to get it to VB):

    public static void MapDrive(IntPtr hwnd, string netRes, string shareName, string userName, string password)
    {
        NETRESOURCE NetRes = new NETRESOURCE();         
        NetRes.dwScope = RESOURCE_GLOBALNET | RESOURCE_REMEMBERED;
        NetRes.dwType = RESOURCETYPE_DISK;
        NetRes.dwDisplayType = RESOURCEDISPLAYTYPE_SHARE;
        NetRes.dwUsage = RESOURCEUSAGE_CONNECTABLE;
        NetRes.lpRemoteName = Marshal2.StringToHGlobalUni(netRes);
        NetRes.lpLocalName = Marshal2.StringToHGlobalUni(shareName);
        NetRes.lpComment = IntPtr.Zero;
        NetRes.lpProvider = IntPtr.Zero;
    
        int ret = WNetAddConnection3(hwnd, NetRes, password, userName, 1);
    
        if (ret != 0)
        {
            throw new System.ComponentModel.Win32Exception(ret, ((NetworkErrors)ret).ToString());
        }
    
    }
    
    private class NETRESOURCE
    {
        public int dwScope;
        public int dwType;
        public int dwDisplayType;
        public int dwUsage;
        public IntPtr lpLocalName;
        public IntPtr lpRemoteName;
        public IntPtr lpComment;
        public IntPtr lpProvider;
    }
    
    [DllImport("coredll.dll")]
    private static extern int WNetAddConnection3(
        IntPtr hwndOwner, 
        NETRESOURCE lpNetResource, 
        string lpPassword, 
        string lpUserName, 
        int dwFlags);
    
    const int RESOURCE_GLOBALNET  =    0x00000002;
    const int RESOURCE_REMEMBERED =    0x00000003;
    const int RESOURCETYPE_DISK  =     0x00000001;
    const int RESOURCEDISPLAYTYPE_SHARE     =     0x00000003;
    const int  RESOURCEUSAGE_CONNECTABLE =  0x00000001;