简体   繁体   English

WNetAddConnection2失败,但是网络使用成功

[英]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). 我正在尝试使用WNetAddConnection2连接到网络资源,但是它失败,错误代码为ERROR_BAD_NET_NAME(错误代码为67)。

But if use "net use" command with the same user name and password its succeeding though. 但是,如果使用具有相同用户名和密码的“ net use”命令,则它会成功。

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. 首先,我们将需要查看您的代码,因为WNetAddConnection2是Windows函数,因此P / Invoked和P / Invoke操作始终非常WNetAddConnection2

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). 注意,假设您已正确调用,则可能会有权限(如果您正在运行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. 在启动VS时,请尝试确保以Run as Administrator身份Run as Administrator ,因为这通常会将相同的凭据扩展到执行应用程序,而控制台通常具有不同的权限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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