Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 38

Thread: Shell and Wait - 32 bit and 64 bit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1

    Shell and Wait - 32 bit and 64 bit

    I am using a shell and wait routine that looks like the following. When it says "kernel32" what are the possible limitations? For example, will this not work on OS's that are 64 bit or Office versions that are 64 bit? Or should it work for both. It seems that it works for a 64 bit OS, but not sure about a 64 bit Office version since I have not test bench for that at the moment.


    -----

    'Libraries (DLL's)
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    
    'Function MeShellAndWait(szCommandLine As String, Optional iWindowState As Integer = vbHide) As Boolean
    'VBA Shell Options
    Function MeShellAndWait(szCommandLine As String, Optional iWindowState As Integer = vbNormalFocus) As Boolean

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello ronjon65,

    Windows Vista, 7, 8, and 10 support 32 and 64 bit programs. The 32-bit and 64-bit versions of Office programs aren’t compatible, so you can’t install both on the same computer.

    Office 2010 introduced a new version of VBA known as VBA 7.0. This works with both 32 and 64 bit machines. There is compilation constant VBA7 used to test for this new version.

    Here are the 32 and 64 bit versions of the two API calls you are using. The 64 bit API will only work if the 32 bit DLL is loaded on the 64 bit machine.
    This macro will load the correct API calls for machines using 64 bits and VBA 7.0 using conditional compilation statements.

    #IF VBA7 = True Then
        ' 32 Bit API
        Private Declare PtrSafe Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPtr
        Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" Alias "GetExitCodeProcess" (ByVal hProcess As LongPtr, lpExitCode As Long) As Long 
    #ELSE 
        ' 64 bit API 
        Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long      Private Declare Function 
        GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long 
    #End If
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Quote Originally Posted by Leith Ross View Post
    The 32-bit and 64-bit versions of Office programs aren’t compatible, so you can’t install both on the same computer.
    FYI, you can in fact have both 32 and 64bit Office on the same computer, just not of the same version. So you can have say 32 bit 2010 and 64 bit 2016, but not 32 and 64bit of 2010.
    Be as you wish to seem

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,635
    @Leith

    I think you reversed ' 32 Bit & ' 64

  5. #5
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Thanks for catching that snb. The code is correct but the labels were reversed.

    #If VBA7 = True Then
        ' 64 Bit API
        Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPtr
        Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As LongPtr, lpExitCode As Long) As Long
    #Else
        ' 32 bit API
        Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
        Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    #End If
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  6. #6
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello Aflatoon,

    Thanks for the correction. I quoted what Microsoft currently has on it's MSDN knowledge base. Personally, I think it would be a very unstable environment to have 32 and 64 bit Office versions installed on the same machine, especially if you are doiing code development.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  7. #7
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Quote Originally Posted by Leith Ross View Post
    Personally, I think it would be a very unstable environment to have 32 and 64 bit Office versions installed on the same machine, especially if you are doiing code development.
    I ran such a setup for the best part of a year with no issues. The only drawbacks for me were that my favourite add-ins didn't have 64bit versions so I tended to stick to 32bit for development anyway, but I had no stability problems. I can appreciate that most people would probably prefer to use VMs for this rather than go through what is a slightly fiddly installation process though.
    Be as you wish to seem

  8. #8
    Hey Leith...so what can be done about this? I really want just one file to cover both scenarios, but I suppose it may need to be one file for 32 bit and one for 64 bit systems? That is rather clunky and unfortunate though.

    If two files is the only option, is there a way for each file to indicate that they have the "wrong" version via message box?

  9. #9
    Great stuff guys

    Curious, was there no 64 bit version before 2010? In other words, any issues with 2003?

    Looking forward to trying this out soon. Yeah, I need a new machine for testing the 64 bit version. It can be tricky with all the various versions to get things to be solid for general use by anyone.

  10. #10
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello ronjon65,

    Windows 2003 is a 32 bit platform. Vista was the first version of Windows to run on a 64 bit platform. Until the release of Office 2010, there were no 64 bit Office programs.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  11. #11
    Leith,

    "if the operating system does not support 64 bits then the API code for the 64 bit environment will error". Do you mean the office/VBA version? I have a 64 bit OS and it shows red. But Excel is 2003 and 32 bit.

    But it still runs with the code you have. So should code work for all Excel 2003 and later and both 32bit and 64 bit office versions? I guessing that it may but I'm out of town and can't try with later Office versions right now.

    I am going to get a cheap laptop and put a 64bit Office on it as well to test things out. Lots of combinations to consider.

  12. #12
    Couple questions:

    - Why is the # needed? I have never used that before.

    - Using Excel 2003 32 bit on Win10 64 bit and the following two lines appear red. It still runs OK, but not sure what that means?

    Private  Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal  dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId  As Long) As LongPtr
    
        Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As LongPtr, lpExitCode As Long) As Long

  13. #13
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello ronjon65,

    The # (pound sign) is used in VBA to signal the statement is conditionally compiled. It will execute during compilation but not during run-time.

    As I mentioned, if the operating system does not support 64 bits then the API code for the 64 bit environment will error. The reason is statements like PtrSafe and LongPtr do not exist in VBA6 and earlier. VBA7 was released with Windows 7 and supports both.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  14. #14
    Leith,

    Exactly what happens when you have lines that do not compile? For general cross platform compatibility, I want to use the if/else structure you originally stated. In Excel 2003, it shows the 2 lines that do not compile (as expected), but everything works fine and I don't get an error. I guess because the if/else catches it so I can just ignore the fact that it does not compile? Or could that have other undesirable consequences that I am not aware of?

  15. #15
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello ronjon65,

    If your running VBA in Office 2003, which is VBA6, then you only need the code below:
         ' 32 bit API
        Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long 
        Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
    These lines of code will only compile in VBA7:
    Note: The following words PtrSafe and LongPtr are part of VBA7 only and will not compile in earlier versions of VBA.
         ' 64 Bit API
        Private Declare PtrSafe Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPtr
        Private Declare PtrSafe Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As LongPtr, lpExitCode As Long) As Long
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  16. #16
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    You can simply ignore the fact that the lines are highlighted in red by syntax checking. It will not affect the actual run time execution as you have found.
    Be as you wish to seem

  17. #17
    I finally got a 64 bit system to test on and added the code as in post #4 (works on 32 bit office). But it throws an error and highlights the word Function after the ' 32 bit API comment line

    "Compile error: The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute."

    So what gives?

  18. #18
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello ronjon65,

    An operating system that is 64 bit only will not compile 32 bit system API calls, even if they are in a conditional compilation statement.
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  19. #19
    Right.

    Office 2013 64bit + Win 10 64bit gives error.
    Office 2003 32bit + Win 10 64bit does not give error.

  20. #20
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Are you saying you ran the code in 64 bit office and got an error?
    Be as you wish to seem

Posting Permissions

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