Results 1 to 16 of 16

Thread: This code doesn't wok in 64bit

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2022
    Posts
    239

    This code doesn't wok in 64bit

    I use the following code to run a callback procedure that is self-contained in a class module (EnumWindows callback). The code works ok in x32bit.

    However, the code doesn't work in x64bit ... Since the memory layout is different in x64bit processes, I presume the bvASM byte array should also contain different values.

    Can anyone update the code in the GetAdressOfCallbackProc function so it works in x64 ? Thanks.

    Class code:
    Code:
    Option Explicit
    
    Private Declare Sub CopyMemory Lib "kernel32" _
        Alias "RtlMoveMemory" _
        (ByRef Destination As Any, _
        ByRef Source As Any, _
        ByVal Length As Long)
    
    Private Declare Function EnumWindows Lib "user32" _
        (ByVal lpEnumFunc As Long, _
        ByVal lParam As Long) As Long
    
    Private Declare Function GetClassName Lib "user32" _
        Alias "GetClassNameA" _
        (ByVal hwnd As Long, _
        ByVal lpClassName As String, _
        ByVal nMaxCount As Long) As Long
    
    
    
    Public Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
        Dim sClassName As String * 256, lRet As Long
        lRet = GetClassName(hwnd, sClassName, 256)
        If Left(sClassName, lRet) = "#3215" Then
            EnumProc = 0
            Exit Function
        End If
        EnumProc = 1
    End Function
    
    
    Private Sub Class_Initialize()
        Dim lp As Long
        lp = GetAdressOfCallbackProc(Me)
        Call EnumWindows(ByVal lp, 0)
    End Sub
    
    
    
    Private Function GetAdressOfCallbackProc(obj As Object) As Long
        Dim WindowProcAddress As Long
        Dim pObj As Long
        Dim pVar As Long
        Dim bvASM(40)  As Byte
        Dim i As Long
     
        For i = 0 To 40
            bvASM(i) = Choose(i + 1, &H55, &H8B, &HEC, &H83, &HC4, &HFC, &H8D, &H45, &HFC, &H50, &HFF, &H75, &H14, _
                                     &HFF, &H75, &H10, &HFF, &H75, &HC, &HFF, &H75, &H8, &H68, &H0, &H0, &H0, &H0, _
                                     &HB8, &H0, &H0, &H0, &H0, &HFF, &HD0, &H8B, &H45, &HFC, &HC9, &HC2, &H10, &H0)
        Next i
        
        pObj = ObjPtr(obj)
        Call CopyMemory(pVar, ByVal pObj, 4)
        Call CopyMemory(WindowProcAddress, ByVal (pVar + 28), 4)
        Call LongToByte(pObj, bvASM, 23)
        Call LongToByte(WindowProcAddress, bvASM, 28)
        GetAdressOfCallbackProc = VarPtr(bvASM(0))
    End Function
    
    
    Private Sub LongToByte(ByVal lLong As Long, ByRef bReturn() As Byte, Optional i As Integer = 0)
        bReturn(i) = lLong And &HFF
        bReturn(i + 1) = (lLong And 65280) / &H100
        bReturn(i + 2) = (lLong And &HFF0000) / &H10000
        bReturn(i + 3) = ((lLong And &HFF000000) \ &H1000000) And &HFF
    End Sub
    Last edited by AngelV; Sep 21st, 2022 at 02:52 AM.

  2. #2
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,121

    Re: This code doesn't wok in 64bit

    Quote Originally Posted by AngelV View Post
    Can anyone update the code in the GetAdressOfCallbackProc function so it works in x64 ?
    Now that's an innocent looking request :-))

    cheers,
    </wqw>

  3. #3
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: This code doesn't wok in 64bit

    Should be easy and if it helps, I think it has something to do with a Chinese restaurant.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: This code doesn't wok in 64bit

    Never seen so much bizarre questions just to program in Excel VBA instead of using a decent programming environment and using normal storage instead of XLSX data.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2022
    Posts
    239

    Re: This code doesn't wok in 64bit

    Sorry guys if I have sounded cheeky by asking people here to update the code for me. (English is not my mother language so I don't know its nuances hence I am bound to make some slip ups)

    But now that i am looking back at the question, It's so damn cheeky

    Maybe I should have rephrased my request to sound less demanding such as : "Does anyone know how ...." or something like that.

    Honestly, I am not being lazy and my intention is never to expect forum members to do homework for me. I just need some pointers from experienced people and I will do the rest.

  6. #6
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: This code doesn't wok in 64bit

    Perhaps you should start by telling us what the code is for exactly, what it is meant to do on a 32bit system, what it actually does on a 64bit system and what you are trying to achieve? Any pointers, errors?

    When you go to the Doctor you might be a little more precise so that the Doctor actually knows what has been going on and can treat you without killing you with the wrong medicine.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jun 2022
    Posts
    239

    Re: This code doesn't wok in 64bit

    Quote Originally Posted by yereverluvinuncleber View Post
    Perhaps you should start by telling us what the code is for exactly, what it is meant to do on a 32bit system, what it actually does on a 64bit system and what you are trying to achieve? Any pointers, errors?

    When you go to the Doctor you might be a little more precise so that the Doctor actually knows what has been going on and can treat you without killing you with the wrong medicine.
    fair enough. Let me explain:

    The code simply iterates all top level windows and looks at their respective class names. When it finds the window whose class I am searching for, it stores its hwnd and exits.

    The code makes use of the EnumWindows api to accomplish the above task.

    Now this api expects to be passed the address of a callback function (the one that carries out the above mentioned iteration) in its first argument... I would normally use the AddressOf keyword to obtain the callback function address and then pass the address to the EnumWindows api. However, in order for this to work, the callback function must reside in a bas module.

    What is special about the code I posted is that it allows for the callback function to be located in the class module itself (instead of a separate bas module) . It uses ASM thunks which I don't understand. Of course, I am not revealing anything new here. I have seen this technique used may times before.

    Problem is, the code works in x32bit but fails in x64bit (crashes) presumably since the memory layout is different in x64bit.

    The x32 code I posted is easy to follow, EXCEPT, of course, the magic values in the bvASM bytes array.

    So to summarize; I was just hoping that somenone would know the answer and would be willing to share it as it would very useful and interesting. BTW, I asked this question for educational purpose only.
    Last edited by AngelV; Sep 21st, 2022 at 06:13 AM.

  8. #8
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: This code doesn't wok in 64bit

    But why do you need to perform all this window / api magic from Excel?

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Jun 2022
    Posts
    239

    Re: This code doesn't wok in 64bit

    Quote Originally Posted by Arnoutdv View Post
    But why do you need to perform all this window / api magic from Excel?
    I don't need to... Like in this particular scenario, I could simply use AddressOf and get the job done. I guess, I am just piqued by curiosity and wanting to know more.

    Also, since vba is very limited, I often find myself having to resort to the win32 api to get some more advanced tasks done.

    And since vba is close to vb6, I visit forums, like this one, where I can learn some fairly advanced stuff in windows programming and then, whenever possible, port what I have learnt to vba.

    Never seen so much bizarre questions just to program in Excel VBA instead of using a decent programming environment and using normal storage instead of XLSX data.
    Unfortunately, vba is the only coding I am familiar with and It looks like I have ran out of time to start anything new. Furthermore, playing with code is just a hobby for me, I don't work in IT... So I guess, I am stuck with vba for good.

    Regards.

  10. #10
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: This code doesn't wok in 64bit

    But what for what kind of things do you all these API fiddling?
    What kind of project/tool are you creating using VBA?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2022
    Posts
    239

    Re: This code doesn't wok in 64bit

    Quote Originally Posted by Arnoutdv View Post
    But what for what kind of things do you all these API fiddling?
    What kind of project/tool are you creating using VBA?
    Nothing major, just some small, albeit useful projects to make up for the lack of some available *tools* vb6 programmers are familiar with such as Screen, clipboard, printer objects, windowed controls, DCs, timers, many events and so on.

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: This code doesn't wok in 64bit

    Thread moved to office development.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  13. #13
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,418

    Re: This code doesn't wok in 64bit

    You could start with reading the documentation:
    Most important: The compile Constants VBA7 and WIN64 --> Nowadays you only need WIN64 to check if your Office is 32- or 64-bit
    https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview
    https://jkp-ads.com/articles/apideclarations.asp
    Last edited by Zvoni; Sep 21st, 2022 at 08:29 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jun 2022
    Posts
    239

    Re: This code doesn't wok in 64bit

    Quote Originally Posted by Zvoni View Post
    You could start with reading the documentation:
    Most important: The compile Constants VBA7 and WIN64 --> Nowadays you only need WIN64 to check if your Office is 32- or 64-bit
    https://learn.microsoft.com/en-us/office/vba/language/concepts/getting-started/64-bit-visual-basic-for-applications-overview
    https://jkp-ads.com/articles/apideclarations.asp
    Thanks alot for the link Zvoni.

    I actually understand very well those compile constants and how to differentiate between x32 and x64 code including object handles, Window handles etc. So that won't help much in this case.

    The original question is quite advanced and win32-oriented. It has more to do with windows programming than with anything else, that's why I posted it in the VB6 section. Having moved this thread to the "Office Developement" section by the moderators will sure ruin it, I am afraid.

  15. #15
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,652

    Re: This code doesn't wok in 64bit

    What do you mean it's not working in 64bit then?

    That code isn't written to run under 64bit Office versions. The VB part. Presumably asm too as you'd need to use x86-64 asm.

    Are you saying you're using VB6 or 32bit Office and that code isn't working on 64bit Windows?

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,121

    Re: This code doesn't wok in 64bit

    Quote Originally Posted by fafalone View Post
    Presumably asm too as you'd need to use x86-64 asm.
    The funny part is that ASM source is missing and you are supposed to disassemble the x86 opcodes on your own *before* answering if rewriting the thunk in x64 is possible at all. Nice bait!

    cheers,
    </wqw>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width