Author Topic: Test for available function  (Read 2232 times)

0 Members and 1 Guest are viewing this topic.

Arnold

  • Guest
Test for available function
« on: May 08, 2019, 12:00:11 AM »
Hi Charles,

I try to test if function IsWow64Process is available on my system and if yes then call the function. The following code snippet will run and can be tested, but I noticed that I have to use call IsWow64Func, IsWow64Func alone seems not work. Is there an error in my code?

Roland

Code: [Select]
! GetCurrentProcess lib "kernel32.dll"
! GetModuleHandle lib "kernel32.dll" alias "GetModuleHandleA"

sys isWow64Func = GetProcAddress(GetModuleHandle("Kernel32"), "IsWow64Process")
if isWoW64Func then
   'bool b = IsWow64Func(GetCurrentProcess(), @b) 'does not work?
   bool b = call IsWow64Func(GetCurrentProcess(), @b)     
   print "b = " b ", IsWow64Process function exists"
else
   print "b = " b ", IsWow64Process function does not exist"   
end if   

Arnold

  • Guest
Re: Test for available function
« Reply #1 on: May 08, 2019, 06:08:47 AM »
This is an example of using IsWow64Process, GetNativeSystemInfo and GetSystemInfo. These functions must be treated differently in Win 32-bit and Win 64-bit. I had to pick my old computer to test the app in 32-bit. But everything seems to work as expected.

Code: OxygenBasic
  1. deleted
  2.  

« Last Edit: May 09, 2019, 07:33:28 AM by Arnold »

Charles Pegge

  • Guest
Re: Test for available function
« Reply #2 on: May 08, 2019, 07:18:09 AM »
Hi Roland,

I would make a slight alteration, otherwise b will only indicate that there was no error:

bool b = call IsWow64Func(GetCurrentProcess(), @b) 
   
bool b
call IsWow64Func(GetCurrentProcess(), @b)     


Code: [Select]
! GetCurrentProcess lib "kernel32.dll"
! GetModuleHandle lib "kernel32.dll" alias "GetModuleHandleA"

sys isWow64Func = GetProcAddress(GetModuleHandle("Kernel32"), "IsWow64Process")
if isWoW64Func then
   'bool b = IsWow64Func(GetCurrentProcess(), @b) 'does not work?
   bool b = call IsWow64Func(GetCurrentProcess(), @b)     
   print "b = " b ", IsWow64Process function exists"
else
   print "b = " b ", IsWow64Process function does not exist"   
end if

Arnold

  • Guest
Re: Test for available function
« Reply #3 on: May 08, 2019, 07:55:32 AM »
Thank you, Charles. I (unconsciously) took that into account with my previous example. As GetNativeSystemInfo is not available in 32-bit systems (according to MSDN documentation), I am rather sure that delayed functions will work correctly with Oxygenbasic.

Roland

Arnold

  • Guest
Re: Test for available function
« Reply #4 on: May 09, 2019, 07:31:56 AM »
Contrary to my assumption my old Win32 Vista notebook can call the GetNativeSystemInfo function. I must have read a verry very old info. Therefore I had to change the code for Systeminfo.o2bas a bit. The purpose of the code is to check if a 32-bit app is running in Win64. There is also the improved function IsWow64Process2, which unfortunately needs a bit more work for implementation.

Code: OxygenBasic
  1. 'https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/ns-sysinfoapi-_system_info
  2. 'https://docs.microsoft.com/de-de/windows/desktop/api/sysinfoapi/nf-sysinfoapi-globalmemorystatusex
  3. $ filename "SystemInfo.exe"
  4. 'uses rtl32
  5. 'uses rtl64
  6.  
  7. uses corewin
  8. uses console
  9.  
  10. SetConsoleTitle "Return Information about the current System"
  11.  
  12.  
  13. type SYSTEM_INFO
  14.     union dn {
  15.     word wProcessorArchitecture    
  16.     word wReserved
  17.     =
  18.     dword dwOemId  'union
  19.    }    
  20.     dword dwPageSize
  21.     sys   lpMinimumApplicationAddress 'lpvoid
  22.    sys   lpMaximumApplicationAddress 'lpvoid
  23.    sys   dwActiveProcessorMask       'dword_ptr
  24.    dword dwNumberOfProcessors
  25.     dword dwProcessorType
  26.     dword dwAllocationGranularity
  27.     word  wProcessorLevel
  28.     word  wProcessorRevision      
  29. end type
  30.  
  31. type PROCCESSOR_ARCHITECTURE
  32.     int value
  33.     string kind
  34. end type
  35.  
  36. PROCCESSOR_ARCHITECTURE p_a = {
  37. {9,"x64 (AMD or Intel)"},
  38. {5,"ARM"},
  39. {12,"ARM64"},
  40. {6,"Intel Itanium-based"},
  41. {0,"x86"},
  42. {65535,"Unknown architecture"}
  43. }
  44.  
  45. type MEMORYSTATUSEX
  46.   dword dwLength
  47.   dword dwMemoryLoad
  48.   quad   ullTotalPhys
  49.   quad   ullAvailPhys              'DWORDLONG
  50.  quad   ullTotalPageFile          'DWORDLONG
  51.  quad   ullAvailPageFile          'DWORDLONG
  52.  quad   ullTotalVirtual           'DWORDLONG
  53.  quad   ullAvailVirtual           'DWORDLONG
  54.  quad   ullAvailExtendedVirtual   'DWORDLONG
  55. end type
  56.  
  57.  
  58. sub systeminfo(SYSTEM_INFO *si)
  59.    printl "Number of Processors "  si.dwNumberOfProcessors
  60.    int x
  61.    for x=1 to countof(p_a)
  62.       if (p_a[x].value = si.dn.wProcessorArchitecture) then  
  63.         printl "Processor Architecture: " p_a[x].value & " ==> " p_a[x].kind  : exit for
  64.       end if
  65.    next
  66.    printl "Level: " si.wProcessorLevel ", Revision: " si.wProcessorRevision
  67.    printl
  68.    printl "Page size = " si.dwPageSize    
  69.    printl "lowest memory address accessible:  0x" hex si.lpMinimumApplicationAddress
  70.    printl "highest memory address accessible: 0x" hex si.lpMaximumApplicationAddress
  71.    printl "ActiveProcessorMask:  " si.dwActiveProcessorMask
  72.    printl "Starting address at which virtual memory can be allocated: 0x" hex si.dwAllocationGranularity      
  73. end sub
  74.  
  75. SYSTEM_INFO si
  76.  
  77. sys GetNativeInfoFunc = GetProcAddress(GetModuleHandle("Kernel32"), "GetNativeSystemInfo")
  78. sys isWow64Func = GetProcAddress(GetModuleHandle("Kernel32"), "IsWow64Process")
  79.  
  80. if isWoW64Func!=0 then
  81.    bool buff
  82.    call IsWow64Func(GetCurrentProcess(), @buff)
  83.    if buff then
  84.       print "Running 32-bit application under WOW64"
  85.       printl
  86.       call GetNativeInfoFunc(si)
  87.       printl "Using GetNativeSystemInfo:"
  88.       systeminfo(si)      
  89.    else
  90. #ifdef mode64bit
  91.       print "Running 64-bit application"  
  92.       printl
  93.       GetSystemInfo (si)
  94.       printl "Using GetSystemInfo:"
  95.       systeminfo(si)  
  96. #else
  97.       print "Running 32-bit application"    
  98.       printl
  99.       if GetNativeInfoFunc!=0 then
  100.         call GetNativeInfoFunc(si)
  101.         printl "Using GetNativeSystemInfo:"
  102.         systeminfo(si)
  103.       else  
  104.         GetSystemInfo (si)
  105.         printl "Using GetSystemInfo:"
  106.         systeminfo(si)
  107.       end if    
  108. #endif
  109.    end if
  110. else
  111.    'must be 32-bit anyway
  112.   print "Running 32-bit application"    
  113.    printl
  114.    GetSystemInfo (si)
  115.    printl "Using GetSystemInfo:"
  116.    systeminfo(si)  
  117. end if  
  118.  
  119.  
  120.  
  121. MEMORYSTATUSEX mse
  122. mse.dwLength = sizeof(MEMORYSTATUSEX)
  123. bool ret = GlobalMemoryStatusEx(Mse)
  124.  
  125. printl
  126. printl "Memory used: " mse.dwMemoryLoad "%"
  127. printl "Total physical memory: " mse.ullTotalPhys/1024 " KB"
  128. printl "Free physical mmemory: " mse.ullAvailPhys/1024 " KB"
  129. printl "Size of total paging file: " mse.ullTotalPageFile/1024 " KB"
  130. printl "Size of free pageing file: " mse.ullAvailPageFile/1024 " KB"
  131. printl "Total virtual memory: " mse.ullTotalVirtual/1024 " KB"
  132. printl "Free virtual memory:  " mse.ullAvailVirtual/1024 " KB"
  133. printl "Free extended virtual memory: "mse.ullAvailExtendedVirtual/1024 " KB"
  134. printl
  135.  
  136.  
  137. printl "Enter ..."
  138. waitkey
  139.