Announcement

Collapse
No announcement yet.

Network question...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pierre Bellisle
    replied
    Adam,
    on my side, I see it that way...

    Let say someone ask you for a buffer of 50 bytes,
    in this buffer you have to put 3 names.
    The buffer address in memory is 100.

    (Let's replace CHR$(0) in buffer with "." so it's easier to read)
    Code:
    Buffer = "011201170125Adam.William.Bellisle................"
     
    First  DWord is 0112 and it is the address of the first name
    Second DWord is 0117 and it is the address of the second name
    Third  DWord is 0125 and it is the address of the third name
    Pierre



    [This message has been edited by Pierre Bellisle (edited February 04, 2005).]

    Leave a comment:


  • Adam J. Drake
    replied
    I still don't understand it really (it's about to make me go cross-eyed), but I appreciate the info. Have updated my code to reflect these suggested changes.

    ------------------

    Leave a comment:


  • Pierre Bellisle
    replied
    Adam,
    I think William is right,
    try the following code and see how szBuffer is filled,
    4 first bytes is the address of UniversalName
    and then, just following the address, we have our UniversalName...

    This tell us that dwBufferSize should be %MAX_PATH + 4 + 1,
    %MAX_PATH to be able to accept the maximum path lenght set by Windows,
    + 4 for the sise of the DWord address,
    + 1 for a terminating zero...

    [Added]
    To proove, set dwBufferSize to the
    len of the UniversalName + 4 + 1,
    the function will succeed but if you set
    dwBufferSize smaller than that, the function will fail...

    Pierre

    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WIN32API.INC"
     
    FUNCTION PBMAIN() AS LONG
     LOCAL UniPointer    AS UNIVERSAL_NAME_INFO POINTER
     LOCAL szLocalPath   AS ASCIIZ * 3
     LOCAL szBuffer      AS ASCIIZ * %MAX_PATH + 5 'Add 5 becose DWord address and terminating zero
     LOCAL dwBufferSize  AS DWORD
     LOCAL dwInfoLevel   AS DWORD
     
     szLocalPath  = "P:" 'Put a valid network drive, ex: "P:"
     dwInfoLevel  = %UNIVERSAL_NAME_INFO_LEVEL
     dwBufferSize = SIZEOF(szBuffer)
     UniPointer   = VARPTR(szBuffer)
     
     IF WNetGetUniversalName(szLocalPath, dwInfoLevel, szBuffer, dwBufferSize) = %NO_ERROR THEN
       MSGBOX "Success : " & @[email protected] & $CRLF & _
              "Len =" & STR$(LEN(@[email protected]iversalName))
     ELSE
       MSGBOX "Error Occurred !"
     END IF
     
     LOCAL sBuffer AS STRING
     sBuffer = PEEK$(UniPointer, dwBufferSize)
     REPLACE $NUL WITH "." IN sBuffer
     MSGBOX sBuffer
     
    END FUNCTION


    [This message has been edited by Pierre Bellisle (edited February 03, 2005).]

    Leave a comment:


  • Michael Mattias
    replied
    > lpBufferSize as %MAX_PATH, because that's the size of the buffer pointed to by the structure.

    Code:
      lpBufferSize  = SIZEOF(lpBuffer) -1   ' subtract one for the null terminator
    This way if you change the size of lpbuffer, you don't have to also change your size thing.

    Leave a comment:


  • Adam J. Drake
    replied
    I don't understand why this would not be safe...

    I didn't post the entire call, (and don't have it handy at home) but I am sending all that's required according to the documentation: WNetGetUniversalName MSDN Documentation

    Specifying UNIVERSAL_NAME_INFO_LEVEL for dwInfoLevel

    And at least as I'm reading it:

    lpBuffer
    [out] Pointer to a buffer that receives the structure specified by the dwInfoLevel parameter.
    The UNIVERSAL_NAME_INFO structure only has one element, and ASCIIZ PTR, which I am creating, and specifying lpBufferSize as %MAX_PATH, because that's the size of the buffer pointed to by the structure.

    Please don't get me wrong, I don't take offense at the comments, I'm just trying to understand what's now unsafe about the code...

    ------------------

    Leave a comment:


  • William Burns
    replied
    I now set up the buffer like this and it seems to work:


    LOCAL lpBuffer AS UNIVERSAL_NAME_INFO
    LOCAL lpBufferSize AS DWORD
    LOCAL tmppath AS ASCIIZ*%MAX_PATH
    lpBufferSize = %MAX_PATH
    lpBuffer.lpUniversalName=VARPTR(tmppath)
    Is it safe?
    Without seeing the way you are calling the WNetGetUniversalName() function, I can't say for
    sure, but... that does not look safe either. Assuming you are calling the function using the
    lpBuffer for the parameter. Because this is still trying to place the data in the lpBuffer
    variable which is still only a single DWORD that holds a pointer. To make that work you will
    need to pass the pointer to the tmppath variable so it will store the data there.

    ------------------
    "I haven't lost my mind... its backed up on tape... I think??"



    [This message has been edited by William Burns (edited February 03, 2005).]

    Leave a comment:


  • Adam J. Drake
    replied
    William, Thanks for the reply.

    I now set up the buffer like this and it seems to work:

    Code:
        LOCAL lpBuffer      AS UNIVERSAL_NAME_INFO
        LOCAL lpBufferSize  AS DWORD              
        LOCAL tmppath       AS ASCIIZ*%MAX_PATH
    
        lpBufferSize = %MAX_PATH
        lpBuffer.lpUniversalName=VARPTR(tmppath)
    Is it safe?

    ------------------

    Leave a comment:


  • William Burns
    replied

    I guess I'm not understanding the whole pointer thing.

    Most of the time things like that require me to create a buffer variable to receive the data, and set it to VARPTR(szBuf).

    Why is this one different?
    Adam this is not different. You need to provide a buffer before you call the function. The above example works because
    it is writing over the neighbors memory space, which is sometimes empty. (this is dangerous, and will eventually bite you)
    There are a few windows functions that will create their own buffer space and return a pointer to it. If they do, they
    will generally rely on you releasing the memory after you are done. But the Win32API documents will tell you that.
    But in this case, you are on your own to create a buffer ahead of time.

    You should do something similar to this:
    Code:
    '=========================================================
    #Compile Exe
    #Dim All
    #Include "WIN32API.INC"
    
    '=========================================================
    Function GetUNC(ByVal sDriveLetter As String) As String
       Local iRet           As Long
       Local zLocalDrive    As Asciiz * 10
       Local UNI            As UNIVERSAL_NAME_INFO Ptr
       Local zBuff          As Asciiz * %MAX_PATH
       Local dBufSize       As Dword
       If Len(sDriveLetter) <> 2 Or Mid$(sDriveLetter,2,1) <> ":" Then Exit Function
       zLocalDrive = sDriveLetter
       UNI = VarPtr(zBuff)
       dBufSize = %MAX_PATH
       If WNetGetUniversalName(zLocalDrive, ByVal %UNIVERSAL_NAME_INFO_LEVEL, zBuff, dBufSize) = %NO_ERROR Then
          Function = @[email protected]
       Else
          iRet = GetLastError()
          If iRet = 2250 Then 'Drive mapping does not exist so report nothing back for answer
          Else  'unknown error so report it 
             Function = "Error" + Str$(iRet)
          End If
       End If
    End Function
    '=========================================================
    
    Function PbMain()
       Local sLetter As String
       sLetter = InputBox$("Type the Drive letter to get the UNC name for:  (Ex: J    [img]http://www.powerbasic.com/support/forums/smile.gif[/img]",,"F:")
       MsgBox GetUNC(sLetter),,sLetter
    End Function
    '=========================================================

    ------------------
    "I haven't lost my mind... its backed up on tape... I think??"



    [This message has been edited by William Burns (edited February 02, 2005).]

    Leave a comment:


  • Adam J. Drake
    replied
    Ok, cool.

    I guess I'm not understanding the whole pointer thing.

    Most of the time things like that require me to create a buffer variable to receive the data, and set it to VARPTR(szBuf).

    Why is this one different?

    ------------------

    Leave a comment:


  • Pierre Bellisle
    replied
    Adam,
    about %REMOTE_NAME_INFO_LEVEL,
    I forgot to remove it when I posted the code,
    it is now remed,
    it worked becose in both type, the first element is the same name and type...
    see...

    Code:
    TYPE REMOTE_NAME_INFO
      lpUniversalName  AS ASCIIZ PTR '<<<
      lpConnectionName AS ASCIIZ PTR
      lpRemainingPath  AS ASCIIZ PTR
    END TYPE
    
    TYPE UNIVERSAL_NAME_INFO
      lpUniversalName AS ASCIIZ PTR '<<<
    END TYPE
    Pierre

    Leave a comment:


  • Adam J. Drake
    replied
    Pierre,

    Documentation says %REMOTE_NAME_INFO_LEVEL returns a REMOTE_NAME_INFO structure, though I am using a UNIVERSAL_NAME_INFO structure, which only has 1 ASCIIZ PTR var instead of 3 in REMOTE_NAME_INFO. I am curious, why does this work?

    ------------------

    Leave a comment:


  • Adam J. Drake
    replied
    Works beautifully, thank you for the help.


    ------------------

    Leave a comment:


  • Pierre Bellisle
    replied
    Adam,
    try the following...

    Pierre

    Code:
    #COMPILE EXE
    #DIM ALL
    #INCLUDE "WIN32API.INC"
    FUNCTION PBMAIN() AS LONG
     LOCAL lpLocalPath   AS ASCIIZ * %MAX_PATH
     LOCAL lpBuffer      AS UNIVERSAL_NAME_INFO
     LOCAL dwInfoLevel   AS DWORD
     LOCAL lpBufferSize  AS DWORD
     
     dwInfoLevel  = %UNIVERSAL_NAME_INFO_LEVEL
     'dwInfoLevel = %REMOTE_NAME_INFO_LEVEL
     lpBufferSize = %MAX_PATH
     lpLocalPath  = "F:"
     
     IF WNetGetUniversalName(lpLocalPath, dwInfoLevel, lpBuffer, lpBufferSize) = %NO_ERROR THEN
       MSGBOX "Success : " & [email protected]
     ELSE
       MSGBOX "Error Occurred."
     END IF
     
    END FUNCTION


    [This message has been edited by Pierre Bellisle (edited January 19, 2005).]

    Leave a comment:


  • Adam J. Drake
    replied
    I'm only wanting to get one particular UNC path, but no matter what drive letter I try here, it returns success, with no info.

    What am I doing wrong?

    Code:
    #INCLUDE "WIN32API.INC"
    
    FUNCTION PBMAIN() AS LONG
    
        LOCAL lpLocalPath   AS ASCIIZ*%MAX_PATH
        LOCAL dwInfoLevel   AS DWORD
        LOCAL lpBuffer      AS UNIVERSAL_NAME_INFO
        LOCAL lpBufferSize  AS DWORD
        LOCAL szBuf         AS ASCIIZ*%MAX_PATH
        
        dwInfoLevel=%UNIVERSAL_NAME_INFO_LEVEL
        lpBufferSize=%MAX_PATH
        lpBuffer.lpUniversalName=VARPTR(szBuf)
        
        lpLocalPath="F:"
        
        IF WNetGetUniversalName(lpLocalPath,dwInfoLevel,lpBuffer,lpBufferSize)<>0 THEN
            MSGBOX "Error Occurred."
        ELSE
            MSGBOX "Success. "+szBuf
        END IF
    
    END FUNCTION
    ------------------

    Leave a comment:


  • Adam J. Drake
    replied
    Pierre, thanks for the info, exactly what I needed.

    ------------------

    Leave a comment:


  • Pierre Bellisle
    replied
    adam,
    some stuff by william burns should get you started...
    http://www.powerbasic.com/support/pb...ad.php?t=20906

    Leave a comment:


  • Adam J. Drake
    replied
    Well, I have an in-house utility that the shortcut runs an auto-updater for that utility every time it's ran, then based on whether or not they have that drive letter available, I launch either a version that accesses that drive directly, or accesses it via in-house web servers with CGI stuff.

    The problem is some people map the "real" drive as something else sometimes.

    What I'd like to do is check the mapping of that drive (if it's there), then launch the correct version of the utility.

    I currently have no code as I don't have a clue how to do this.

    ------------------




    [This message has been edited by Adam J. Drake (edited January 15, 2005).]

    Leave a comment:


  • Donald Darden
    replied
    Can you give me an example of how you want to use this? Have you
    tried it?

    ------------------
    Old Navy Chief, Systems Engineer, Systems Analyst, now semi-retired

    Leave a comment:


  • Adam J. Drake
    started a topic Network question...

    Network question...

    Is it possible to get a string representation of a network drive mapping in a program?

    (ie: F: "\\SERVER1\VOL_INFO")

    ------------------
Working...
X