none
VB.Net 2005如何寫網路磁碟機 RRS feed

  • 問題

  • 請問VB.Net2005要如何寫程式來判斷網路磁碟機連線是否正常?與連線網路磁碟機呢?
    2007年3月13日 上午 02:33

所有回覆

  • 參考了幾種連線網路磁碟機的作法,我得到錯誤如下:

    「"Unable to find an entry point named 'WNetAddConnection3' in DLL 'Mpr.dll'."」

    這是什麼意思呢?

    我在2005的宣告如下:

    「   Declare Function WNetAddConnection3A Lib "Mpr.dll" Alias "WNetAddConnection3" (ByVal hwndowner As String, ByVal lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As String) As Long

    Public Const RESOURCE_PUBLICNET = &H2
        Public Const RESOURCETYPE_ANY = &H0
        Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
        Public Const RESOURCEUSAGE_CONNECTABLE = &H1
        Public Const CONNECT_UPDATE_PROFILE = &H1」

    麻煩給予協助~,謝謝。

    2007年3月14日 上午 01:54
  • 你寫反了吧?

    Declare Function WNetAddConnection3 Lib "Mpr.dll" Alias "WNetAddConnection3A"

    別名是放後面。

    若有日韓簡罕的路徑名或檔名時,最好改用 W 字尾的,不過 W 字尾的不支援 Win9x 。

    2007年3月14日 上午 03:12
    版主
  • 在修正之後,卻變成「PInvoke」的錯誤了

    PInvokeStackImbalance was detected


    Message: A call to PInvoke function 'SmallSize_DefectXferLoader!SmallSize_DefectXferLoader.DefectXfr::WNetAddConnection2'
    has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target
    signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged
    signature.

    .Net2005在使用API上有什麼改變嗎?

    2007年3月14日 上午 05:12
  • 我是這樣宣告的,參考看看

    Declare Auto Function WNetAddConnection2 Lib "mpr.dll" Alias _
                                        "WNetAddConnection2W" (ByVal objNetResource As NETRESOURCE _
                                        , ByVal strPassword As String _
                                        , ByVal lngUserName As String _
                                        , ByVal lngFlags As Int32) As Integer

    2007年3月14日 上午 05:37
  • 還是出現一樣的錯誤耶~~頭暈~~

    Declare Auto Function WNetAddConnection2 Lib "mpr.dll" Alias _
                                        "WNetAddConnection2W" (ByVal objNetResource As NETRESOURCE _
                                        , ByVal strPassword As String _
                                        , ByVal lngUserName As String _
                                        , ByVal lngFlags As Int32) As Integer


        Public Const RESOURCE_PUBLICNET = &H2
        Public Const RESOURCETYPE_ANY = &H0
        Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
        Public Const RESOURCEUSAGE_CONNECTABLE = &H1
        Public Const CONNECT_UPDATE_PROFILE = &H1

        Private WithEvents MQiAgent As clsMQiAgent

        Structure NETRESOURCE
            Dim dwType As Integer
            Dim lpLocalName As String
            Dim lpRemoteName As String
            Dim lpProvider As Integer
        End Structure

     

        Public Function AddConnection(ByVal RemoteLocation As String, ByVal LocalDriver As String, ByVal Passwd As String, ByVal UserName As String) As Boolean

            Dim ne As NETRESOURCE, i As Integer
            Dim errstr As String, errpriv As String

            ne.dwType = RESOURCETYPE_ANY
            ne.lpLocalName = LocalDriver
            ne.lpProvider = vbNullString
            ne.lpRemoteName = RemoteLocation

            i = WNetAddConnection2(ne, Passwd, UserName, 0)
            If i = 0 Then
                AddConnection = True
            Else
                AddConnection = False
                errstr = Str(256)
                errpriv = Str(256)
            End If
        End Function

    ==========================================

    能否幫我看一下我寫的部份~謝謝。

    2007年3月14日 上午 06:01
  • 嗯,其實Win32 API我也搞不是很懂,我都是一直試一直試,算是亂槍打鳥型的吧...

    NETRESOURCE我是這樣宣告,參考看看

       <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Public Class NETRESOURCE
            Public dwScope As Int32
            Public dwType As Int32
            Public dwDisplayType As Int32
            Public dwUsage As Int32
            Public lpLocalName As String
            Public lpRemoteName As String
            Public lpComment As String
            Public lpProvider As String
        End Class

    2007年3月14日 上午 06:11
  • 我將設定修正之後,終於沒有再出現PInvoke的錯誤了,但仍無法連線,可以再幫我看一下嗎?

    Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2W" (ByVal lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer

        <StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Unicode)> Public Class NETRESOURCE
            'Public dwScope As Int32
            Public dwType As Int32
            'Public dwDisplayType As Int32
            'Public dwUsage As Int32
            Public lpLocalName As String
            Public lpRemoteName As String
            'Public lpComment As String
            Public lpProvider As String
        End Class

        Public Function AddConnection(ByVal RemoteLocation As String, ByVal LocalDriver As String, ByVal Passwd As String, ByVal UserName As String) As Boolean

            Dim ne As New NETRESOURCE, i As Integer
            Dim errstr As String, errpriv As String, erno As Integer

            'ne.dwDisplayType = RESOURCEDISPLAYTYPE_GENERIC
            'ne.dwScope = RESOURCE_PUBLICNET
            ne.dwType = RESOURCETYPE_ANY
            'ne.dwUsage = RESOURCEUSAGE_CONNECTABLE
            'ne.lpComment = vbNullString
            ne.lpLocalName = LocalDriver
            ne.lpProvider = vbNullString
            ne.lpRemoteName = RemoteLocation

            i = WNetAddConnection2(ne, Passwd, UserName, 0)
            If i = 0 Then
                AddConnection = True
            Else
                AddConnection = False
                errstr = Str(256)
                errpriv = Str(256)
                i = WNetGetLastError(erno, errstr, 256, errpriv, 256)
                MsgBox(WNetAddConnection2(ne, Passwd, UserName, 0))
            End If
        End Function

    執行「WNetAddConnection2(ne, Passwd, UserName, 0)」之後得到錯誤為「487」,請問這是什麼意思呢?

    2007年3月14日 上午 09:15
  • 487 = "企圖存取不正確的位址",有可能是 UNC 路徑有問題。

    我直接引用 http://www.codeproject.com/dotnet/WNetAPI.asp 的 Windows Networking API 宣告,然後寫了一個 NetworkDrive 類別,其中有部份程式碼是這樣子:

    public void Connect(string UserName, string Password)
    {
        Win32.WNetAPI.NETRESOURCE netRes = new Win32.WNetAPI.NETRESOURCE();
        int resultCode = -1;

        // set network drive properties.
        netRes.LocalName = this._driveLetter;
        netRes.RemoteName = this._networkUNCPath;
        netRes.RType = (int)Win32.WNetAPI.DEFINES.RESOURCETYPE_DISK;

        resultCode = Win32.WNetAPI.WNetAddConnection2(
            netRes, Password, UserName, (int)Win32.WNetAPI.DEFINES.CONNECT_REDIRECT);

        if (resultCode != 0)
        {
            this.GetLastError(resultCode);
            this._connected = false;
        }
        else
            this._connected = true;
    }

    (抱歉,我寫的是 C#,不是 VB)

    2007年3月14日 上午 09:25
    版主
  • 487 : ERROR_INVALID_ADDRESS,

    您確定用Windows是可以正常連接到網路磁碟的嗎?

    Attempt to access invalid address.
    2007年3月14日 上午 09:31
  • 我讀入「WNetAddConnection2」的參數來源是文字檔,所要連的路徑有試連過了,是可以正常連線的。我再檢查一下

    是否與文字檔案入時有關?!

    我可以去哪裡查到Error Code相對應的意思呢?

    2007年3月14日 上午 09:46
  • 謝謝各位先進的協助,我已經可以連線了~~謝謝
    2007年3月15日 上午 12:27