[SNIPPET] IsOS64bit - Saber si un sistema es de 64 bit

Iniciado por raul338, 7 Octubre 2010, 19:01 PM

0 Miembros y 1 Visitante están viendo este tema.

raul338

Buenas  :D

Tengo una aplicacion que se cuando se inicia se lanza el servidor mysql, el problema es que hay un servidor para cada tipo de SO (32 y 64 bits) entonces.... buscando (luego de intentos fallidos a mano) logre dar con esta funcion :)

En un modulo (o en un form como quieran :xD)
Código (vb) [Seleccionar]

Option Explicit
' Extracto
' http://www.vbmonster.com/Uwe/Forum.aspx/vb-winapi/5690/API-to-determine-if-OS-is-32bit-or-64-bit
Private Declare Function GetVersion Lib "kernel32" () As Long
Private Declare Sub GetNativeSystemInfo Lib "kernel32" (lpSystemInfo As SYSTEM_INFO)
Private Type SYSTEM_INFO
wProcessorArchitecture        As Integer
wReserved                     As Integer
dwPageSize                    As Long
lpMinimumApplicationAddress   As Long
lpMaximumApplicationAddress   As Long
dwActiveProcessorMask         As Long
dwNumberOfProcessors          As Long
dwProcessorType               As Long
dwAllocationGranularity       As Long
wProcessorLevel               As Integer
wProcessorRevision            As Integer
End Type
Private Const PROCESSOR_ARCHITECTURE_IA64 = 6
Private Const PROCESSOR_ARCHITECTURE_AMD64 = 9

Public Function IsOS64Bit() As Boolean
   ' Llamar desde Win2k en adelante
   ' La verdad dudo de la existencia de Win2k 64 bits...
   ' pero en el msdn decia que a partir de win2k se puede llamar a la funcion xD
   If LoByte(LoWord(GetVersion())) >= 5 Then
       Dim si As SYSTEM_INFO
       Call GetNativeSystemInfo(si)
       IsOS64Bit = _
           (si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64) Or _
           (si.wProcessorArchitecture = PROCESSOR_ARCHITECTURE_IA64)
   End If
End Function

Private Function LoWord(ByVal Numero As Long) As Long
   ' Devuelve el LoWord del número pasado como parámetro
   LoWord = Numero And &HFFFF&
End Function

Private Function LoByte(ByVal Numero As Integer) As Integer
   ' Devuelve el LoByte del número pasado como parámetro
   LoByte = Numero And &HFF
End Function


Y para probarlo en el form load
Código (vb) [Seleccionar]

Private Sub Form_Load()
   If IsOS64Bit Then
       MsgBox "Tienes un SO de 64 bits"
   Else
       MsgBox "Tienes un SO de 32 bits"
   End If
End Sub


No me habia dado cuenta que 0 representaba un procesador de 32bit.... y yo decia que no me devolvia nada :xD

Alguien que tenga un so de 64 bits que lo pruebe por favor :)

Saludos!

::) Mi mensaje 888 ._.

ssccaann43 ©

Creeme que estuve mucho tiempo preguntandome esto...! Gracias
- Miguel Núñez
Todos tenemos derechos a ser estupidos, pero algunos abusan de ese privilegio...
"I like ^TiFa^"

Dessa

#2
Cita de: raul338 en  7 Octubre 2010, 19:01 PM
No me habia dado cuenta que 0 representaba un procesador de 32bit.... y yo decia que no me devolvia nada :xD

Si, cero para 32 Bits  y cuando el retorno de "si.wProcessorArchitecture" es (-1) el procesador es "Unknown architecture"




Cita de: raul338 en  7 Octubre 2010, 19:01 PM
Alguien que tenga un S.O de 64 bits que lo pruebe por favor :)

+1








Adrian Desanti

Karcrack

Código (vb) [Seleccionar]
'KERNEL32
Private Declare Function IsWow64Process Lib "KERNEL32" (ByVal hProcess As Long, ByRef Wow64Process As Boolean) As Long
Private Const CurrentProcess        As Long = -1

Public Function Is64() As Boolean
    On Error Resume Next
    Call IsWow64Process(CurrentProcess, Is64)
End Function

Esto deberia funcionar...

raul338

Wow.... Karcrack dicen los rumores (lei por ahi) que si pones en modo compatibilidad se puede "saltar" eso :xD pero creo que ahi lo evitas con current Process

Karcrack

Dejo una version mas molona :xD
Código (vb) [Seleccionar]
Private Type SYSTEM_PROCESSOR_INFORMATION
   ProcessorArchitecture       As Integer
   ProcessorLevel              As Integer
   ProcessorRevision           As Integer
   Unknown                     As Integer
   FeatureBits                 As Long
End Type
Private Const SystemProcessorInformation    As Long = 1

'NTDLL
Private Declare Function NtQuerySystemInformation Lib "NTDLL" (ByVal SystemInformationClass As Long, ByRef SystemInformation As Any, ByVal SystemInformationLength As Long, ByRef ReturnLength As Long) As Long

Public Function x64() As Boolean
   Dim tSPI                    As SYSTEM_PROCESSOR_INFORMATION
   
   If NtQuerySystemInformation(SystemProcessorInformation, tSPI, &HC, ByVal 0&) >= 0 Then
       x64 = ((tSPI.ProcessorArchitecture = 6) Or (tSPI.ProcessorArchitecture = 9))
   End If
End Function

Reducido:
'NTDLL
Private Declare Function NtQuerySystemInformation Lib "NTDLL" (ByVal SystemInformationClass As Long, ByRef SystemInformation As Any, ByVal SystemInformationLength As Long, ByRef ReturnLength As Long) As Long

Public Function x64() As Boolean
    Dim wProcArch   As Integer
   
    If NtQuerySystemInformation(1, wProcArch, &HC, ByVal 0&) >= 0 Then
        x64 = ((wProcArch = 6) Or (wProcArch = 9))
    End If
End Function

raul338

[OFFTOPIC]
Porque siempre que posteo algo siempre encuentran una forma de optimizarlo? :rolleyes:

Karcrack

#7
Todo se puede mejorar :)



Por cierto, el codigo de Wow permite saber si estamos en un SO de 64 bits, en cambio el resto simplemente miran si el procesador lo es... es decir, yo puedo tener instalado un W$ de 32bits con un procesador de 64bits...

raul338

Cita de: Karcrack en  8 Octubre 2010, 16:42 PM
Por cierto, el codigo de Wow permite saber si estamos en un SO de 64 bits, en cambio el resto simplemente miran si el procesador lo es... es decir, yo puedo tener instalado un W$ de 32bits con un procesador de 64bits...

Igual me sirve, ya que necesito lanzar cosas de 32 o 64 bits segun sea el caso :xD