Thread: MakeDWORD

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

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

    MakeDWORD


    Ok, this is one of my bigger problems with creating my application in Python. Someone tried helping me with this before showing me a way to do this through an array, only I didn't quite understand it. Here's the code in Visual Basic, only I have no clue how Python can do anything like this without it being very complicated. (The following code is in a class module)

    Code:
    Function MakeDWORD(Value As Long) As String
        Dim Result As String * 4
        CopyMemory ByVal Result, Value, 4
        MakeDWORD = Result
    End Function
    I know that there must be a way for Python to do something to this extent. Can anyone 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
    CopyMemory isn't a Visual Basic function, it is a Windows API call, for copying from one area of memory to another; however, in this function, it seems to be used as a way of circumventing the language typing, by converting a VB Long Integer into a DWORD, a type which is used by the Windows API extensively when handling raw (in the sense of typeless) data, by way of a 4-byte string. It is not a cast or conversion, but a bit-for-bit copy, something VB does not normally allow between disparate data types.

    AFAICT, there is no direct equivalent in Python, because the conversion itself has no meaning in the Python object model. What you need to do is kick it up one level of abstraction; that is to say, work out what the DWORD in question was used for - probably for some kind of external function call, as it wouldn't be of any use that I can think of in VB itself - and determine how to do that in Python. It will almost certainly be different from how it was done in VB, because of the differences in how the languages handle system calls.

    In fact, I think you'll find that a literal translation of a VB program - especially one that uses a lot of system calls, which is the case in this sort of Socket programming - is not going to work very well, as the two languages have very different idioms, and interface with the system differently. I think you'd be better off studying the underlying battle.net protocol and working out a new design instead.
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Well, that's my problem. I've only had a battle.net bot written in Visual Basic 6, and that's what the code was like. Every Visual Basic programmer had the same PacketBuffer code. I also know the proper code to making a PacketBuffer (MakeDWORD is in PacketBuffer) in C++. Only when it comes to Python it seems almost impossible because of how different the 2 languages are. I'm still trying to figure out another way to make MakeDWORD work in Python, only I think I'm going to be the first person ever to create a bot for battle.net in Python. No matter how much I look or where I look it seems no one else has created one yet. EVery other bot is made in either C++ or Visual Basic, and about 2 bots are made in Java. (The bot in Java has an open source copy) If I looked at the MakeDWORD code from this bot (I just thought of this right now) would it be even easier to convert to Python?
  6. #4
  7. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    I sitll think you should try to get more information about the protocols themeselves. A quick Google for "battle.net" protocol and a bit of digging turns up a site called "The Disclaimer" which has fora specific to this topic.

    I would be very careful about any of this however; looking this up shows that battleChat is not an open protocol, and there are definite questions about the legality of these bots, even just for chat use. I would go over the Blizzard EULA and related documentation before going forward with this.
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ya, I've been to The Disclaimer, they have programming forums in there, etc. The only problem is they only have a programming section for Visual Basic, C++, ASP, I think Java, and like one other language. There's no Python in there. Some of the greatest programmers of all time run this site (Skywing, Grok, Yoni...). I have no clue on how much most of the members on the site know about Python. That's why I figured I'd come here instead. I'll go over to BotDev (The Disclaimer) and see if anyone can help. Thanks.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    As Schol-R-LEA said, the VB function takes an integer value and packs it into a 4 byte string. The simplest way to do this in Python is to use the struct module. Here is the Python equivalent:

    Code:
    import struct
    
    def makeDWORD(value):
        return struct.pack('i', value)
    Dave - The Developers' Coach
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    A quick google for "battle.net protocol python" threw up this. It may be what you need. Or it may not.

    Dave - The Developers' Coach
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ok, heres the code I now have: (Yes I have struct imported)

    Code:
        def MakeDWORD(value):
            MakeDWORD = MakeDWORD()
            return struct.pack(result, value)
    Now, in the Visual Basic code it had:

    Code:
    Function MakeDWORD(Value As Long) As String
        Dim Result As String * 4
        CopyMemory ByVal Result, Value, 4
        MakeDWORD = Result
    End Function
    I was wondering if the 4 after Value, should be added in my code, for instance:

    Code:
        def MakeDWORD(value):
            MakeDWORD = MakeDWORD()
            return struct.pack(result, value, 4)
    The people in The Disclaimer don't seem to know Python, if there are any good members in there who really know Python, I can't find them. One of them asked me to ask you how to convert a 32 bit interger into a string, maybe that would make the question easier. So for now, all I need to know is if my code will actually work like the Visual Basic code does. Thanks for the help so far.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    See my previous reply - I posted up the function you want. The one you posted will fail with an exception if you try to run it.

    Dave - The Developers' Coach
  18. #10
  19. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ok, thanks for the help DevCoach, I went to that site and download BnetBot. The only problem was that that bot was probably the most confusing code Iv'e ever looked at, plus it has an entirely different set up from what my bot has. BnetBot doesn't have the normal code from a PacketBuffer as my bot does, I'm not sure what that bot had. All I need to know is that for my code, return struct.pack(result, value, 4), result obviously is the string as you used in your example, "i", and value is a long, you put that in the same spot for your example. Now for Battle.net, there is MakeDWORD, and MakeWORD. MakeDWORD used an interger of 4, and MakeWORD uses an interger of a 2. So is return struct.pack(result, value, 4) proper code? Or is that 4 in the wrong spot? If you can help me with this my PacketBuffer will be set. Thanks for all the help so far though.
    Edit:Someone in the BotDev (The Disclaimer) forums has also just told me about the struct module, sicne he knows a lot about battle.net I might also be able to get some help from him.
  20. #11
  21. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Alpharetta, GA
    Posts
    1,806
    Rep Power
    1570
    Yegg, you are going to have to understand what the functions do and why they take the arguments they do, otherwise you'll never accomplish what you're trying to. The kind of blind duplication of the VB code you are using is just going to cause you trouble in the long run, and this is one example of why.

    The reason that the VB CopyMemory() function needs the final size argument is because it is copying raw memory space, and it needs to now how many bytes to copy.

    The purpose of the Python struct.pack() method, on the other hand, is to take the data in the object and 'pack' it into a a contiguous data space. Because the object already knows how large the data is, it does not need, and will not accept, the size argument. thus, the '4' isn't need at all - and adding it will cause the program to fail. Clear?

IMN logo majestic logo threadwatch logo seochat tools logo