简体   繁体   中英

WNetAddConnection2 fails but net use succeeds

I am trying to Connect to a network resource using WNetAddConnection2 but its failing with error code ERROR_BAD_NET_NAME (Error Code 67).

But if use "net use" command with the same user name and password its succeeding though.

Any clues ?

public class NETRESOURCE
    {
        public int dwScope;
        public int dwType;
        public int dwDisplayType;
        public int dwUsage;
        public string LocalName;
        public string RemoteName;
        public string Comment;
        public string Provider;
    }
    [DllImport("mpr.dll")]
    public static extern int WNetAddConnection2(NETRESOURCE netResource, string password, string username, int flags);

    public class ServerConnect
    {
        private string serverName;
        private string userName;
        private string password;
        public int nResult;

        public ServerConnect()
        {
            serverName = "";
            userName = "";
            password = "";
            nResult = -1;
        }
        public void SetConnectionParam(string serName, string uName, string pwd)
        {
            serverName = serName;
            userName = uName;
            password = pwd;
        }

        public void Connect()
        {
            NETRESOURCE myResource = new NETRESOURCE();
            myResource.dwScope = 0;
            myResource.dwType = 0x00000001; //RESOURCETYPE_DISK
            myResource.dwDisplayType = 0;
            myResource.LocalName = "";
            myResource.RemoteName = serverName;
            myResource.dwUsage = 0;
            myResource.Comment = "";
            myResource.Provider = "";
            nResult = WNetAddConnection2(myResource, password, userName, 0);     


        }
    };

    public void ConnectToDataServer(string serverName)
    {
        ServerConnect oConnect = new ServerConnect();
        oConnect.SetConnectionParam(serverName, @"Domain\username", @"password");
        Thread connectionThread = new Thread(new ThreadStart(oConnect.Connect));
        connectionThread.Start();
        while (!connectionThread.IsAlive) ;// Wait till thread starts and Alive
        int nCount = 0;
        while (connectionThread.IsAlive)
        {
            Thread.Sleep(500);
            nCount++;
            if (nCount == 10) // wait for 5 secs
            {
                //WriteLine(this, "Failed to Connect to to server " + serverName , LogStatus.Error);
                connectionThread.Abort();
                Thread.Sleep(1000);
            }
        }
        //WriteLine(this, oConnect.nResult.ToString(), LogStatus.Success);
    }
    public void ConnectToServer()
    {

        ConnectToDataServer(@"\\ServerName");
}

For one, we would need to see your code, as WNetAddConnection2 is a Windows functions and thus P/Invoked, and P/Invoke operations are always very, very hairy.

On the note of assuming you've invoked correctly, there could be permissions in the way (this is especially true if you're running Windows 8). Try making sure to Run as Administrator when you luanch VS, as this will usually extend the same credentials to executing applications, whereas the console often has different permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM