10

Possible Duplicate:
How can I tell if a user is using a laptop

I'm trying to find out if the application is running on a laptop or on a desktop, any ideas on how to achieve this?

Note: I'm interested only in API's written in Delphi and/or C++.

EDIT: my target platform is Windows XP+, even Windows 7 only is OK.

wovano
  • 4,543
  • 5
  • 22
  • 49
  • 8
    Good question, I'm not sure there's a definitive answer hence a comment, but you might find the battery/power API's useful? – Lloyd Jan 31 '11 at 09:50
  • 3
    check if computer where app is installed/runned have battery options? :P something like that? :D – FeRtoll Jan 31 '11 at 09:51
  • I'm assuming you mean in Windows. In OS X you can read the system type and know. – Kludge Jan 31 '11 at 09:53
  • @all I've modified the question, sorry I haven't specified the platformn(tired). @Lloyd @FeRtoll good ideas, gotta check that. –  Jan 31 '11 at 09:55
  • 1
    Dialogs make me shiver.. "Don't make me think" :) – cwap Jan 31 '11 at 09:58
  • @sarnold nope, that won't work, see, most of the times clients expect out of the box solutions for issues, you can't just annoy them with messages unless absolutely necessary. –  Jan 31 '11 at 09:58
  • 1
    @Dorin Duminica, I'm glad to hear that! :D – sarnold Jan 31 '11 at 10:00
  • 3
    Note that a lot of UPS systems connected through a USB or serial port manifests themselves to Windows as a 'battery', juts like a laptop with battery does. – Jeroen Wiert Pluimers Jan 31 '11 at 10:55
  • 4
    Why would you want to know that? – CodesInChaos Jan 31 '11 at 10:56
  • Dialog possibility is most reliable. – Free Consulting Jan 31 '11 at 11:18
  • 1
    @Jeroen Pluimers: simple solution: read battery levels, run a loop for XXX seconds consuming 100% cpu and do something with the gpu. After that compare battery levels... :P – The_Fox Jan 31 '11 at 12:39

6 Answers6

18

Use this struct : SYSTEM_POWER_STATUS, and check the value of ACLineStatus field.

  • ACLineStatus = 0 => The system is not using AC power > Laptop + battery
  • ACLineStatus = 1 => The system is using AC power => Laptop + AC
  • ACLineStatus = 255 => AC power status is unknown => Desktop

Disclaimer : Try experimenting with these. I'm not claiming if they're reliable. But they're almost correct.

--

EDIT:

Use GetSystemPowerStatus to get the value of the above mentioned structure.

By the way, you can also experiment with the other fields of the structure; maybe you can find some useful pattern, giving you some combination of values of different fields to help you reliably detect if application is running on laptop or not.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 7
    FWIW, I plugged a new USB connected UPS into my Windows 2008 server last week, and it auto-detected a "battery", and now shows all the same battery options as a laptop does. I do not think a battery is in any way a reliable indicator of laptop-ness. – mj2008 Jan 31 '11 at 10:47
  • @mj2008: everyone is saying the same thing that *" I do not think a battery is in any way a reliable indicator of laptop-ness"*, and I don't disagree with them. If there is no standard way one can do this job, then one can only experiment with different values of system information, battery information, and their combination of different values, etc to reliably detect (to some degree) if application is running on laptop or not. – Nawaz Jan 31 '11 at 10:56
  • As per https://msdn.microsoft.com/en-us/library/aa373232(v=VS.85).aspx "no battery" should be BatteryFlag == 128 – Giampaolo Rodolà Feb 01 '17 at 16:54
9

I don't think there's a standard way to check whether the application runs on a laptop, but I think there's also no standard rationale for needing to know this.

The point is also that a laptop is no so much different from a desktop: there are laptops that get never moved, and I guess it would be possible to build a desktop with an embedded UPS (seen as a battery?)

I think you should find out if it's a laptop using the features you need to check in a laptop:

  • Do you want to know if it's a laptop because your application needs to behave differently if the computer may be moved around? Then check if it has got a battery plugged in.

  • Do you want to know if it's a laptop in order to see whether hardware can be modified? In this case check the motherboard model or ask with a dialog box.

  • Do you need to check it in order to know if it will burn to death if used too intensively for too long? Just monitor the temperature...

peoro
  • 25,562
  • 20
  • 98
  • 150
  • @peoro good answer, believe me there are quite a few reasons to check if it's running on laptop or not, for example the most important is "avoiding application execution/functionality in case it's ran on a laptop", reason? performance, people forget that desktops are way faster(in most cases) than their laptops(consumer ones) so they tend to run a lot of applications at once, now the real issue is that I have no clue if it's running on a server, desktop or on a laptop... –  Jan 31 '11 at 10:05
  • 5
    @Dorin Duminica: then check your machine performance: check machine CPU or run a tiny embedded benchmark of few seconds. This way you'll be able to give the same features to a brand new laptop and an older desktop which have the same actual performance (and so on). – peoro Jan 31 '11 at 10:08
  • 1
    @Dorin Duminica: So you decide what may or may not be done on a client's computer? Based on whether it's a laptop (which might be a 2.3 GHz quad-core i7 connected to the power grid and a GBit ethernet connection) or a desktop (which might be a 500 MHz PIII running on feeble UPS power and a 11 MBit WLAN)? I say let the user decide what his system is capable of. – DevSolar Jan 31 '11 at 10:16
  • @DevSolar yes that's pretty much what I'm going to do, the desktops are all dual cores running at 2.8 GHz+ with full HD LCD's, 2GB+ RAM, etc. my concern is with the management which uses laptops that take over 2 minutes to start(too many apps. runing in the background...) at any rate, the question is pretty clear if I'm not mistaken... –  Jan 31 '11 at 10:19
  • 2
    @Dorin Duminica: I would be really pissed if I had an application that decides to cut down in functionality because it runs on a laptop. Now.. you don't want to piss off management, do you? – The_Fox Jan 31 '11 at 12:48
  • @The_Fox: Maybe they are a special case management who don't mind... Anyway, I can see this as a specific task for a specific set of hardware in a specific company. Why should one worry? – Andriy M Jan 31 '11 at 14:26
  • If it is a special one-company setup, simply have all laptops set an environment variable to a defined value. Problem solved. – DevSolar Jan 31 '11 at 14:45
  • @The_Fox they don't really know what's good for them, therefore it's up to me to decide what functionality I want to allow on that, for example one of the options they have is to retrieve all data from database or a certain number of records, NOW we wouldn't want that, we want to restrict or at least give them a warning about what they are going to do and if they chose to do it it's fine by me, but I want to make sure I take a few precautions. –  Feb 01 '11 at 00:08
4

I don't believe there is a reliable way to detect this.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • @Dorin You can try WMI, but that doesn't work all the time, or indeed very often in my experience. You can look for batteries, but some laptops can run without batteries. We might be able to suggest an alternative approach if you were able to tell us how you are going to use the information. – David Heffernan Jan 31 '11 at 10:04
  • the idea is that most of the times I want to be at least 70%+ sure that I know if app. is running on laptop or (desktop or server) so that I can act apropriately by turning features off and so on... nothing "very" specific, but still. –  Jan 31 '11 at 10:11
3

Apparently, the hidden problem is that the company laptops typically have not enough memory, but the company desktops do. To address this specific problem, compare memory used against memory installed: EnumProcesses() and GetProcessMemoryInfo tells you the first; GetPhysicallyInstalledSystemMemory() tells you the second.

If they're too close, you can inform the user that there are 73 running processes using 2.5 GB, but only 2GB RAM is present. This is a valid reason for your program not to start.

MSalters
  • 173,980
  • 10
  • 155
  • 350
2

The MSDN discusses API for Power and Device Aware applications here

You could also check other things like:

  • Is battery connected
  • Is track pad connected
  • Is PC Card installed Is
  • Has a certain type of CPU (low power, Atom, etc)
  • Has a screen unique to laptop.
  • laptop hardrive is 3.5"

If a certain number of the above is true then you can assume laptop.

You could also just ask the user at installation....

T33C
  • 4,341
  • 2
  • 20
  • 42
  • user can copy application from one system to another without any issue, so unless the app. is capable of detecting the system, there is no real way of checking where is it ran, it might as well be ran from a VM at any time... –  Jan 31 '11 at 10:13
1

Here's a bunch of other answers and links you might find useful for this question:

How to detect when the laptop is running on batteries?

How can I tell if a user is using a laptop

The latter also discusses WMI, while the answer is centred around .NET you can use WMI from Delphi.

Community
  • 1
  • 1
Lloyd
  • 29,197
  • 4
  • 84
  • 98