Page 1 of 3

Is this a laptop or Pc? (detection)

Posted: Sun Aug 02, 2009 4:20 pm
by Alireza
Can somebody detect the Application is runed in a pc or laptop?
easiest way please

Posted: Sun Aug 02, 2009 4:32 pm
by va!n
you can check the batterie status using api for example...

Posted: Sun Aug 02, 2009 4:54 pm
by Alireza
I don't have enough experience , i search in forum, there is not topic about battery or laptop.
help me if you've experience about this case.
Thanks

Posted: Sun Aug 02, 2009 4:54 pm
by SFSxOI
check the battery as suggested or if you want to use WMI use srod's Comate like this:

Code: Select all

XIncludeFile "COMate.pbi"

Procedure CompSystem_Info() 

Define.COMateObject objWMIService, CompSysInfo 
colCompSysInfo.COMateEnumObject 
strComputer.s = "." 

objWMIService = COMate_GetObject("winmgmts:\\" + strComputer + "\root\cimv2", "") 
If objWMIService 
  colCompSysInfo = objWMIService\CreateEnumeration("ExecQuery('Select * from Win32_ComputerSystem')") 
 
  If colCompSysInfo 
    CompSysInfo = colCompSysInfo\GetNextObject() 
    While CompSysInfo 
    
    
      Select CompSysInfo\GetIntegerProperty("PCSystemType") 
        Case 0 
          COMSYS_PCSystemType$ = "Unspecified" 
        Case 1 
          COMSYS_PCSystemType$ = "Desktop" 
        Case 2 
          COMSYS_PCSystemType$ = "Mobile" ; laptop
        Case 3 
          COMSYS_PCSystemType$ = "Workstation" 
        Case 4 
          COMSYS_PCSystemType$ = "Enterprise Server" 
        Case 5 
          COMSYS_PCSystemType$ = "Small Office and Home Office (SOHO) Server" 
        Case 6 
          COMSYS_PCSystemType$ = "Appliance PC" 
        Case 7 
          COMSYS_PCSystemType$ = "Performance Server" 
        Case 8 
          COMSYS_PCSystemType$ = "Maximum" 
        Default 
          COMSYS_PCSystemType$ = "Unknown or Unspecified" 
      EndSelect
      
      CompSysInfo\Release() 
      CompSysInfo = colCompSysInfo\GetNextObject() 
    Wend 
    colCompSysInfo\Release() 
  EndIf 
  objWMIService\Release() 
  Else 
      MessageRequester("Error", "COMSYSInfo")  
EndIf 
Debug COMSYS_PCSystemType$ ; if "Mobile" then is laptop most likely
EndProcedure
If you want to go the battery route:

http://msdn.microsoft.com/en-us/library ... S.85).aspx
http://msdn.microsoft.com/en-us/library ... S.85).aspx

Code: Select all


battery.SYSTEM_POWER_STATUS

GetSystemPowerStatus_(@battery)

If battery\BatteryFlag = 128 
Debug "This is either a laptop or the system is running on a UPS" 
Else 
Debug "Not a laptop" 
EndIf


or.... use the below if:endif

If battery\BatteryFlag = -128 ; desltops return -128
Debug "Not a laptop"
Else
Debug "This is either a laptop or the system is running on a UPS" 
EndIf


Posted: Sun Aug 02, 2009 5:28 pm
by Alireza
Thanks SFSxOI

this work for me only:

Code: Select all

If battery\BatteryFlag = -128 ; desltops return -128 
Debug "Not a laptop" 
Else 
Debug "This is either a laptop or the system is running on a UPS" 
EndIf
others no, Comate way in pc and laptop send "Unspecified".

above code is nice but i think it's not enough safe way because UPS is same as Laptop in detection .
Have you a better and safe way?

above code is very small and i interest , alack it's not safe as completely
Thanks

Posted: Sun Aug 02, 2009 6:50 pm
by RASHAD
Run as Administrator
If you find "Mobile" in the Processor Name String you are running a laptop
Tested for Intel on XP ,Vista,Windows 7 x86/x64 I do not know @ AMD

Code: Select all

lRetVal.i
sRemMachName.s
lTopLevelKey.i
lHKeyhandle.i
sKeyName.s 
lhkey.i
sValueName.s
vValue.s
msg.s ; 
#ERROR_NONE = 0 

Procedure.l QueryValueEx(lhkey.i, szValueName.s) 
    Define.l cch, lrc, lType, lValue 
    Define.s sValue
    Shared vValue 
    cch = 255 
    sValue = Space(255)
            lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue, @cch) 
            If lrc = #ERROR_NONE 
                vValue = Left(sValue, cch-1) 
            Else 
                vValue = "Empty" 
            EndIf
    ProcedureReturn lrc 
EndProcedure

      lTopLevelKey = #HKEY_LOCAL_MACHINE 
      sRemMachName = ""
      sKeyName = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
      sValueName = "ProcessorNameString"
      
      lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey, @lHKeyhandle) 
      lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey) 
      lRetVal = QueryValueEx(lhkey, sValueName) 
      RegCloseKey_(lhkey) 
      
      If lRetVal = 0
        MessageRequester("CPU Name",vValue,#MB_ICONINFORMATION) 
      Else 
        msg = "An Error occured, Return value = " + Str(lRetVal)
       MessageRequester("Error",msg, #MB_ICONERROR) 
      EndIf

Posted: Sun Aug 02, 2009 6:52 pm
by Marco2007
My Laptop has no battery anymore and I will not buy one for more than 100€....

others no, Comate way in pc and laptop send "Unspecified". Here, too.

@Rashad:
Laptop wrote:---------------------------
CPU Name
---------------------------
Intel(R) Celeron(R) M processor 1.60GHz
---------------------------
OK
---------------------------
M=Mobile?

Posted: Sun Aug 02, 2009 7:08 pm
by RASHAD
@Marco2007
hello Marco
Yes M for Mobile
Ali should spend some time to know the designation for Intel & AMD
mobile cpu line of production

Posted: Sun Aug 02, 2009 7:12 pm
by ts-soft
RASHAD wrote:Run as Administrator
or change to this:

Code: Select all

lRetVal.i
sRemMachName.s
lTopLevelKey.i
lHKeyhandle.i
sKeyName.s
lhkey.i
sValueName.s
vValue.s
msg.s ;
#ERROR_NONE = 0

Procedure.l QueryValueEx(lhkey.i, szValueName.s)
  Define.l cch, lrc, lType, lValue
  Define.s sValue
  Shared vValue
  cch    = 255
  sValue = Space(255)
  lrc    = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue, @cch)
  If lrc = #ERROR_NONE
    vValue = Left(sValue, cch - 1)
  Else
    vValue = "Empty"
  EndIf
  ProcedureReturn lrc
EndProcedure

lTopLevelKey = #HKEY_LOCAL_MACHINE
sRemMachName = ""
sKeyName     = "HARDWARE\DESCRIPTION\System\CentralProcessor\0"
sValueName   = "ProcessorNameString"

lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_READ, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)

If lRetVal = 0
  MessageRequester("CPU Name", vValue, #MB_ICONINFORMATION)
Else
  msg = "An Error occured, Return value = " + Str(lRetVal)
  MessageRequester("Error", msg, #MB_ICONERROR)
EndIf
an run as pb-user :wink:

Posted: Sun Aug 02, 2009 7:50 pm
by RASHAD
@ts-soft
Hello ts-soft
I did not get it
What do you mean?
If you ment that this code is yours
The answer is no
It is from Codearchiv
BTW:Srod did a fine job based on yours and you was asking him to add
#REG_BINARY and #REG_MULTI_SZ
#REG_BINARY in codearchiv
#REG_MULTI_SZ
I did that long time back check my post Unrated
http://www.purebasic.fr/english/viewtopic.php?t=37138

Have a good day
RASHAD

Posted: Sun Aug 02, 2009 8:01 pm
by ts-soft
i mean i have changed one line to run without adminprivilege :wink:

Posted: Sun Aug 02, 2009 8:07 pm
by RASHAD
@ts-soft
I am sorry my friend
In that case I will copy your version instead to my lib

Have a good day
RASHAD

Posted: Mon Aug 03, 2009 2:02 pm
by Alireza
The Result in PC:
Intel(R) Pentium(R) 4 CPU 2.00 GHz


The Result in Laptop:
Intel(R) Core(TM)2 Duo CPU T6400 @ 2.00GHz

How can i detect which one is PC and which one is Laptop?
Thanks

Posted: Mon Aug 03, 2009 3:18 pm
by RASHAD

Posted: Mon Aug 03, 2009 3:32 pm
by Alireza
omg, you're right
not easy way