No announcement yet.

Pointer error with union'ed ASCIIZ pointers?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Pointer error with union'ed ASCIIZ pointers?

    This is most curious. Bear in mind that I've solved my initial problem with using ASCIIZ pointers into a union'ed structure, but the results are most curious as to WHY this behavior exists.

    The function (Winsock is initialized):
    SUB GenerateIpPort(hDialog AS DWORD, hostToIP AS STRING)
    LOCAL Work          AS STRING
    LOCAL sockin        AS sockaddr_in
    LOCAL dwHost        AS hostentStru PTR
    LOCAL connectHost   AS ASCIIZ * 256
    LOCAL zStr          AS ASCIIZ PTR * 5
    LOCAL zWord         AS DWORD PTR
        sockin.sin_port = htons(80)
        connectHost = hostToIp
        dwHost = GetHostByName(connectHost)
        IF dwHost = %NULL THEN
            DisplayMessage hostToIp &" = Unresolved / Bad Host Name"
            Work = @[email protected]_name
            zWord = @dwHost.h_list
            zStr = @zWord
            Work = Work &" = "& USING$("#_.#_.#_.#", _
                ASC(MID$(@zStr[0],1,1)),  ASC(MID$(@zStr[0],2,1)), _
                    ASC(MID$(@zStr[0],3,1)), ASC(MID$(@zStr[0],4,1)))
            Work = Work &" / "& HEX$(@dwHost.h_addr, 4)
            DisplayMessage Work
            ' this is what i was using first to get at the packed IP
            ' sockin.sin_addr.s = @zStr[0]
            sockin.sin_addr.s_addr = @@zWord
            zStr = inet_ntoa(sockin.sin_addr.s_addr)
            Work = @zStr
            Work = Work &" = "& USING$("#_.#_.#_.#", _
                sockin.sin_addr.S_un_b.s_b1, sockin.sin_addr.S_un_b.s_b2, _
                    sockin.sin_addr.S_un_b.s_b3, sockin.sin_addr.S_un_b.s_b4) _
                        &" / "& HEX$(sockin.sin_addr.s_addr, 8)
            DisplayMessage Work
        END IF
    No, it's not a function yet, this is just a test to figure things out.

    When called with various host names as the hostToIP string, I get the following results:
    ' should return the proper IP address.
    GenerateIpPort CB.HNDL, ""
    ' = / 0002
    ' = / E0729345
    ' why yes it does.
    ' should generate a failure to resolve.
    GenerateIpPort CB.HNDL, ""
    ' = Unresolved / Bad Host Name
    ' yep.
    ' this is blocked via my HOSTS file.
    GenerateIpPort CB.HNDL, ""
    ' = 127.-1.-1.-1 / 0002
    ' = / 0100007F
    ' ok, the ASC is not returning character codes below 32?
    And it all works, but the results from the third "lookup" is most curious when you access the 4 bytes of the return via a ASCIIZ string pointer, it's returning the 0's and the 1 as -1.

    So, is ASC failing here? And if so, how?

    P.S. Title of the post was hard to come up with, ...
    Furcadia, an interesting online MMORPG in which you can create and program your own content.

  • #2
    There was quite a discussion on ASC, MID$ and other statements recently. It looks like you are trying to use the string commands on a DWORD (zWord is pointer to a DWORD, zStr is a pointer to a 5 character string and you are setting its address to the address of zWord). If a character in a string is ASCII value 0, it is considered null so the MID$ of the second and third bytes of will return a null string and ASC of a null string returns -1. I'm not sure why the fourth byte is giving you a -1.

    Have you considered using PEEK instead? Instead of all the ASC(MID$(...)) in the USING$ statement, use PEEK(BYTE, @zWord), PEEK(BYTE, @zWord + 1), PEEK(BYTE, @zWord + 2), PEEK(BYTE, @zWord + 3). At least, I think that is the way it should look.
    Jeff Blakeney