WNetUseConnection returns 1312 on 2nd time connect

0

I have a window service which access files from shared folder on other server (with username and password). On the first start of the service, there is no problem on connecting on server but when it loop or stop and start the window service, there WNetUseConnection always returns Error: 1231. What seems to be the problem?

Below is the calling of the connection:

NetworkShare.ConnectToShare(@\\192.168.XX.XXX\images, *username*, *pw*);

NetworkShare.DisconnectFromShare(@\\192.168.XX.XXX\images, true);

Below is code for the connection:

/// <summary>
/// ネットワーク接続クラス
/// </summary>
public class NetworkShare
{
    /// <summary>
    /// リモート共有に接続します
    /// </summary>
    /// <returns>成功した場合はnull、それ以外の場合はエラーメッセージ。</returns>
    public static string ConnectToShare(string uri, string username, string password)
    {
        //ネットリソースを作成し、共有に向けます
        NETRESOURCE nr = new NETRESOURCE();
        nr.dwType = RESOURCETYPE_DISK;
        nr.lpRemoteName = uri;

        //共有を作成する
        int ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);

        //エラーをチェックする
        if (ret == NO_ERROR)
            return null;
        else
            return GetError(ret);
    }

    /// <summary>
    /// キャッシュから共有を削除します。
    /// </summary>
    /// <returns>成功した場合はnull、それ以外の場合はエラーメッセージ。</returns>
    public static string DisconnectFromShare(string uri, bool force)
    {
        //共有を削除する
        int ret = WNetCancelConnection(uri, force);

        //エラーをチェックする
        if (ret == NO_ERROR)
            return null;
        else
            return GetError(ret);
    }

    #region P/Invoke Stuff
    /// <summary>
    /// 共有を作成する
    /// </summary>
    [DllImport("Mpr.dll")]
    private static extern int WNetUseConnection(
        IntPtr hwndOwner,
        NETRESOURCE lpNetResource,
        string lpPassword,
        string lpUserID,
        int dwFlags,
        string lpAccessName,
        string lpBufferSize,
        string lpResult
        );

    /// <summary>
    /// 共有を削除する
    /// </summary>
    [DllImport("Mpr.dll")]
    private static extern int WNetCancelConnection(
        string lpName,
        bool fForce
        );

    /// <summary>
    /// ネットリソース
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    private class NETRESOURCE
    {
        public int dwScope = 0;
        public int dwType = 0;
        public int dwDisplayType = 0;
        public int dwUsage = 0;
        public string lpLocalName = "";
        public string lpRemoteName = "";
        public string lpComment = "";
        public string lpProvider = "";
    }

    #region 定数
    /// <summary>
    /// リソースタイプディスク
    /// </summary>
    const int RESOURCETYPE_DISK = 0x00000001;

    /// <summary>
    /// 接続更新プロファイル
    /// </summary>
    const int CONNECT_UPDATE_PROFILE = 0x00000001;
    #endregion

    #region エラー
    /// <summary>
    /// エラーなし
    /// </summary>
    const int NO_ERROR = 0;

    /// <summary>
    /// アクセスが拒否されました
    /// </summary>
    const int ERROR_ACCESS_DENIED = 5;

    /// <summary>
    /// 割り当て済み
    /// </summary>
    const int ERROR_ALREADY_ASSIGNED = 85;

    /// <summary>
    /// 悪いデバイス
    /// </summary>
    const int ERROR_BAD_DEVICE = 1200;

    /// <summary>
    /// 不正なネットワーク名
    /// </summary>
    const int ERROR_BAD_NET_NAME = 67;

    /// <summary>
    /// 悪いプロバイダー
    /// </summary>
    const int ERROR_BAD_PROVIDER = 1204;

    /// <summary>
    /// キャンセル
    /// </summary>
    const int ERROR_CANCELLED = 1223;

    /// <summary>
    /// 拡張エラー
    /// </summary>
    const int ERROR_EXTENDED_ERROR = 1208;

    /// <summary>
    /// 無効なアドレス
    /// </summary>
    const int ERROR_INVALID_ADDRESS = 487;

    /// <summary>
    /// 無効なパラメーター
    /// </summary>
    const int ERROR_INVALID_PARAMETER = 87;

    /// <summary>
    /// 無効なパスワード
    /// </summary>
    const int ERROR_INVALID_PASSWORD = 1216;

    /// <summary>
    /// より多くのデータ
    /// </summary>
    const int ERROR_MORE_DATA = 234;

    /// <summary>
    /// アイテムはありません
    /// </summary>
    const int ERROR_NO_MORE_ITEMS = 259;

    /// <summary>
    /// ネットがないか悪いパス
    /// </summary>
    const int ERROR_NO_NET_OR_BAD_PATH = 1203;

    /// <summary>
    /// ネットワークなし
    /// </summary>
    const int ERROR_NO_NETWORK = 1222;

    /// <summary>
    /// セッション機密の競合
    /// </summary>
    const int ERROR_SESSION_CREDENTIAL_CONFLICT = 1219;

    /// <summary>
    /// 悪いプロファイル
    /// </summary>
    const int ERROR_BAD_PROFILE = 1206;

    /// <summary>
    /// プロファイルを開けません
    /// </summary>
    const int ERROR_CANNOT_OPEN_PROFILE = 1205;

    /// <summary>
    /// 使用中のデバイス
    /// </summary>
    const int ERROR_DEVICE_IN_USE = 2404;

    /// <summary>
    /// 接続されていません
    /// </summary>
    const int ERROR_NOT_CONNECTED = 2250;

    /// <summary>
    /// エラーファイルを開く
    /// </summary>
    const int ERROR_OPEN_FILES = 2401;

    /// <summary>
    /// エラークラス
    /// </summary>
    private struct ErrorClass
    {
        public int num;
        public string message;
        public ErrorClass(int num, string message)
        {
            this.num = num;
            this.message = message;
        }
    }

    /// <summary>
    /// エラーメッセージリスト
    /// </summary>
    private static ErrorClass[] ERROR_LIST = new ErrorClass[] {
        new ErrorClass(ERROR_ACCESS_DENIED, "エラー:アクセスが拒否されました"), 
        new ErrorClass(ERROR_ALREADY_ASSIGNED, "エラー:割り当て済み"), 
        new ErrorClass(ERROR_BAD_DEVICE, "エラー:不正なデバイス"), 
        new ErrorClass(ERROR_BAD_NET_NAME, "エラー:不正なネット名"), 
        new ErrorClass(ERROR_BAD_PROVIDER, "エラー:不正なプロバイダー"), 
        new ErrorClass(ERROR_CANCELLED, "エラー:キャンセルされました"), 
        new ErrorClass(ERROR_EXTENDED_ERROR, "エラー:拡張エラー"), 
        new ErrorClass(ERROR_INVALID_ADDRESS, "エラー:無効なアドレス"), 
        new ErrorClass(ERROR_INVALID_PARAMETER, "エラー:無効なパラメーター"), 
        new ErrorClass(ERROR_INVALID_PASSWORD, "エラー:無効なパスワード"), 
        new ErrorClass(ERROR_MORE_DATA, "エラー:追加データ"), 
        new ErrorClass(ERROR_NO_MORE_ITEMS, "エラー:これ以上アイテムはありません"), 
        new ErrorClass(ERROR_NO_NET_OR_BAD_PATH, "エラー:ネットなしまたは不良パス"), 
        new ErrorClass(ERROR_NO_NETWORK, "エラー:ネットワークなし"), 
        new ErrorClass(ERROR_BAD_PROFILE, "エラー:不正なプロファイル"), 
        new ErrorClass(ERROR_CANNOT_OPEN_PROFILE, "エラー:プロファイルを開けません"), 
        new ErrorClass(ERROR_DEVICE_IN_USE, "エラー:使用中のデバイス"), 
        new ErrorClass(ERROR_NOT_CONNECTED, "エラー:接続されていません"), 
        new ErrorClass(ERROR_OPEN_FILES, "エラー:ファイルを開く"), 
        new ErrorClass(ERROR_SESSION_CREDENTIAL_CONFLICT, "エラー:資格情報の競合"),
    };

    /// <summary>
    /// エラーリストでエラーを取得する
    /// </summary>
    /// <param name="errNum">エラーコード</param>
    /// <returns>エラーメッセージ</returns>
    private static string GetError(int errNum)
    {
        foreach (ErrorClass er in ERROR_LIST)
        {
            if (er.num == errNum) return er.message;
        }
        return "エラー:不明, " + errNum;
    }
c#
asp.net
file
connection
remote-access
asked on Stack Overflow Sep 10, 2020 by Akino • edited Sep 10, 2020 by Akino

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0