#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    CLng() to Int Question


    I think I figured something out. I know that I have to use Int() in Python to replace what the VB6 function CLng() does, but for the following code:
    Code:
    Dim lngProdID As Long
    Dim dblProdID As Double
    lngProdID = CLng(dblProdID)
    I'm not sure what to do. dblProdID isn't given any value at all, it is just declared as a Double. Would the proper Python code be:
    Code:
    lngProdID = int(lngProdID)
    ? If this is wrong, please help me.
  2. #2
  3. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    The original code doesn't seem to make sense; both lngProdID and dblProdID should default to zero, so that assignment should have no effect. Can you post some more of the function it was in for context?
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short • Understanding the C/C++ Preprocessor
    Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Here is the entire function which I'm trying to convert.
    Code:
    Public Function Send_0x51(Data As String)
    Dim Hash As String, MPQName As String, TempData As String
    Dim EXEInfo As String, version As Long, CheckSum As Long, Result As Long
    Dim dblProdID As Double, dblValue1 As Double, dblValue2 As Double
    Dim lngProdID As Long, lngValue1 As Long, lngValue2 As Long
    Dim AccountHash As String, TempString As String, dblKey As Long
    Dim Servers As String
    Token = Mid(Data, 9, 4)
    MPQName = Mid(Data, InStr(Data, "I"), 12)
    Hash = Mid(Mid(Data, 34), InStr(Mid(Data, 34), Chr(0)) + 1, InStr(Mid(Mid(Data, 34), InStr(Data, Chr(0)) + 1), Chr(0)))
    Hash = Replace(Hash, Chr(0), "")
    EXEInfo = Space(256)
    dblKey = GetTickCount()
    If Product = "VD2D" Then
        Result = z(HashPath & Executable, HashPath & "BNClient.DLL", HashPath & "D2Client.DLL", Hash, version, CheckSum, EXEInfo, MPQName)
    Else
        Result = z(HashPath & Executable, HashPath & "Storm.DLL", HashPath & "Battle.SNP", Hash, version, CheckSum, EXEInfo, MPQName)
    End If
    NullTruncString EXEInfo
    DecodeCDKey Form1.txtCDKey, dblProdID, dblValue1, dblValue2
    lngProdID = CLng(dblProdID)
    lngValue1 = CLng(dblValue1)
    lngValue2 = CLng(dblValue2)
    Servers = CLng(Val("&h" & StrToHex(StrReverse(Token))))
    AccountHash = String(5 * 4, vbNullChar)
    TempString = c(AccountHash, Servers, lngProdID, lngValue1, lngValue2, dblKey)
    If Result = 0 Then
        AddC True, True, vbRed, "Hashing Failed"
        Form1.Winsock1.Close
        Exit Function
    End If
    Packet.InsertDWORD dblKey
    Packet.InsertDWORD version
    Packet.InsertDWORD CheckSum
    Packet.InsertDWORD &H1
    Packet.InsertDWORD "&H0"
    Packet.InsertDWORD Len(Form1.txtCDKey)
    Packet.InsertDWORD CLng(dblProdID)
    Packet.InsertDWORD CLng(dblValue1)
    Packet.InsertDWORD &H0
    Packet.InsertNonNTString AccountHash
    Packet.InsertNTString EXEInfo
    Packet.InsertNTString Form1.txtUser
    Packet.SendPacket &H51
    AddC True, True, &HFFFFC0, "Sent Username & Password. "
    End Function
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Originally Posted by †Yegg†
    I think I figured something out. I know that I have to use Int() in Python to replace what the VB6 function CLng() does, but for the following code:
    Code:
    Dim lngProdID As Long
    Dim dblProdID As Double
    lngProdID = CLng(dblProdID)
    I'm not sure what to do. dblProdID isn't given any value at all, it is just declared as a Double. Would the proper Python code be:
    Code:
    lngProdID = int(lngProdID)
    ? If this is wrong, please help me.
    Correct code would be:
    Code:
    lngProdID = int(dblProdID)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    !!!How stupid of me. I wasn't paying attention to that fact that DecodeCdkey was returning the value for dblProdID. Thanks for the help guys.
    Update: Well I'm confused again. DecodeCdkey isn't returning the value for dblProdID (or the two values). I can't seem to figure out what dblProdID equals. It doesn't start or end with any value.
    Last edited by †Yegg†; March 26th, 2005 at 12:27 PM.
  10. #6
  11. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    Could you post the code for DecodeCDKey(), please? Passing by reference is the default in VB 6, so if the subroutine assigns a value to the parameter which you are passing DblProdID as an argument for, then it is changing DblProdID after all.
    Last edited by Schol-R-LEA; March 26th, 2005 at 08:10 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short • Understanding the C/C++ Preprocessor
    Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  12. #7
  13. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Here is the code:
    Code:
    Public Sub DecodeCDKey(ByVal sCDKey As String, ByRef dProductId As Double, ByRef dValue1 As Double, ByRef dValue2 As Double)
    On Error Resume Next
    sCDKey = Replace(sCDKey, "-", "")
    sCDKey = Replace(sCDKey, " ", "")
    sCDKey = KillNull(sCDKey)
    If Len(sCDKey) = 13 Then
        sCDKey = DecodeStarcraftKey(sCDKey)
    ElseIf Len(sCDKey) = 16 Then
        sCDKey = DecodeD2Key(sCDKey)
    Else
        Exit Sub
    End If
    dProductId = Val("&H" & Left$(sCDKey, 2))
    If Len(sCDKey) = 13 Then
        dValue1 = Val(Mid$(sCDKey, 3, 7))
        dValue2 = Val(Mid$(sCDKey, 10, 3))
    ElseIf Len(sCDKey) = 16 Then
        dValue1 = Val("&H" & Mid$(sCDKey, 3, 6))
        dValue2 = Val("&H" & Mid$(sCDKey, 9))
    End If
    End Sub
    Thanks for all the help you've given so far.
  14. #8
  15. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    Ok, just to make sure we are all on the same page about this: as you can see, the three relevant parameters are all explicitly declared as ByRef (bolded below in the code), meaning that they are meant to alter the variables passed to them. Here are the places where dblProdID, dblValue1, and dblValue2, respectively, are assigned:

    Code:
    Public Sub DecodeCDKey(ByVal sCDKey As String, ByRef dProductId As Double, ByRef dValue1 As Double, ByRef dValue2 As Double)
    On Error Resume Next
    sCDKey = Replace(sCDKey, "-", "")
    sCDKey = Replace(sCDKey, " ", "")
    sCDKey = KillNull(sCDKey)
    If Len(sCDKey) = 13 Then
        sCDKey = DecodeStarcraftKey(sCDKey)
    ElseIf Len(sCDKey) = 16 Then
        sCDKey = DecodeD2Key(sCDKey)
    Else
        Exit Sub
    End If
    dProductId = Val("&H" & Left$(sCDKey, 2))
    If Len(sCDKey) = 13 Then
        dValue1 = Val(Mid$(sCDKey, 3, 7))
        dValue2 = Val(Mid$(sCDKey, 10, 3))
    ElseIf Len(sCDKey) = 16 Then
        dValue1 = Val("&H" & Mid$(sCDKey, 3, 6))
        dValue2 = Val("&H" & Mid$(sCDKey, 9))
    End If
    End Sub
    Now, there are some interesting things in this, not the least of which being the Exit Sub in the first if..then statement; this means that the values aren't necessarily being set. The reason this is interesting is because Send_0x51() doesn't check for this; so either a zero value is legitimate value that can only come up in that situation, or the programmer didn't put in the necessary error handling. I also noticed that in Send_0x51(), the expression CLng(dblProdID) is used again later on, even though dblProdID hasn't been changed. This would seem to imply that c() changes lngProdID somehow, but that doesn't follow, since lngProdID never gets used again. Perhaps the side effect is used in other calls, but just not this one? Just something to take note of.

    Since these subroutines are using pass by reference, you may have to change things around a nit in Python. Python uses what could be call 'pass by object-reference'. By this I mean that what gets passed in a function call is a reference to the objects in question. This means that if you have a function

    Code:
    class Squink:
    	def __init__(self, qx):
    		self.quux = qx
    
    def foo(bar):
    	glorp = bar.quux
    	bar.quux = "peas and carrots"
    	bar = None
    	return glorp
    
    baz = Squink("What, me worry?")
    print foo(baz)
    print baz.quux
    The result would be

    Code:
    What, me worry?
    peas and carrots
    This is because while the assignment to bar.quux changes which of the two string objects is referred to by the variable quux (internal to the object which both bar and baz point to), the assignment bar = None only affects to variable bar, by changing which object it refers to. As for glorp, because it still refers to the string which quux referred to originally, it is not effected by the changes in quux.

    Don't worry if this makes little sense yet; the practical upshot of it, though, is that you can't pass numeric variables to a function and expect them to be changed by reassignment within said function.
    Last edited by Schol-R-LEA; March 26th, 2005 at 10:06 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short • Understanding the C/C++ Preprocessor
    Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  16. #9
  17. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I think I can figure a way around this. Thanks for the help, I had no idea that that was what ByRef actually did.
  18. #10
  19. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    Yes. Pass by reference is, in a nutshell, gives the function a pointer to the variable rather than just copying it's value (OK, technically it isn't necessarily a pointer, but that's usually how it is implemented). In other words, if you have a function

    Code:
    sub Blah (byVal A as Integer, byRef B as Integer)
        B = B + A
        A = 17
    end sub
    ' ... some code
    
    Dim x as Integer, Y as Integer
    
    X = 5
    Y = 23
    
    Blah X, Y
    The call to Blah would put the value 5 into A, but would put a pointer to Y in B. Then, when it reassigns B, it changes the value at the memory address B points to - which happens to be the same memory address Y is a name for, so changing B also changes Y. But when it changes A, it only changes the local value of A from 5 to 17 - it has no effect on X.

    I hope this makes things clearer, rather than more Murky (What's he doing here? whacks Stef across head - Get back to your comic strip where you belong!). You don't really need to know all this low-level stuff, but it does help you understand why the languages act the way they do.
    Last edited by Schol-R-LEA; March 27th, 2005 at 04:58 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in Short • Understanding the C/C++ Preprocessor
    Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

IMN logo majestic logo threadwatch logo seochat tools logo