We help IT Professionals succeed at work.

Calculating CPU load for Windows from VLC Open Source

Last Modified: 2013-12-10
I would like to programmatically measure momentary CPU load on Windows XP.
1. Does anyone have a snippet of code that will achieve this?
2. In particular, I would like to do this from within the VideoLan open source code. How can I achieve this in the Cygwin environment which is typically used to compile the VideoLan package for Windows in a Windows development environment?

Watch Question

I don't think the last link I posted give any examples of to use with Cygwin environment. The next does not either, but it demonstrates using the Win32 api and states that it will work on WinXP.



Hi Anthony,
Thanks for the pointer. This indeed answers my question for Win32.
However, as you say, the second part of the question is still open.
In VLC I need to compile the following snippet
int VLC_GetCPUUsage( int i_object )
    struct tms cpu_usage;
    times( &cpu_usage );
   return cpu_usage.tms_utime;
Do you understand the mechanics of Cygwin? Is there an emulation library for all those UNIX calls, in which I could use your code as implemention?
Thanks a lot

I copied this from a Cygwin FAQ, I believe it sums up what you want to do with regards to using the Win32 api:

How do I use Win32 API calls?

It's pretty simple actually. Cygwin tools require that you explicitly link the import libraries for whatever Win32 API functions that you are going to use, with the exception of kernel32, which is linked automatically (because the startup and/or built-in code uses it).

For example, to use graphics functions (GDI) you must link with gdi32 like this:

gcc -o foo.exe foo.o bar.o -lgdi32

or (compiling and linking in one step):

gcc -o foo.exe foo.c bar.c -lgdi32

The following libraries are available for use in this way:

advapi32 largeint ole32 scrnsave vfw32 cap lz32 oleaut32 shell32 win32spl comctl32 mapi32 oledlg snmp winmm comdlg32 mfcuia32 olepro32 svrapi winserve ctl3d32 mgmtapi opengl32 tapi32 winspool dlcapi mpr penwin32 th32 winstrm gdi32 msacm32 pkpd32 thunk32 wow32 glaux nddeapi rasapi32 url wsock32 glu32 netapi32 rpcdce4 user32 wst icmp odbc32 rpcndr uuid imm32 odbccp32 rpcns4 vdmdbg kernel32 oldnames rpcrt4 version

The regular setup allows you to use the option -mwindows on the command line to include a set of the basic libraries (and also make your program a GUI program instead of a console program), including user32, gdi32 and, IIRC, comdlg32.

Note that you should never include -lkernel32 on your link line unless you are invoking ld directly. Do not include the same import library twice on your link line. Finally, it is a good idea to put import libraries last on your link line, or at least after all the object files and static libraries that reference them.

The first two are related to problems the linker has (as of b18 at least) when import libraries are referenced twice. Tables get messed up and programs crash randomly. The last point has to do with the fact that gcc processes the files listed on the command line in sequence and will only resolve references to libraries if they are given after the file that makes the reference.

********************************** And another quote that you might be interested in:

How do I compile a Win32 executable that doesn't use Cygwin?

The -mno-cygwin flag to gcc makes gcc link against standard Microsoft DLLs instead of Cygwin. This is desirable for native Windows programs that don't need a UNIX emulation layer.

This one is on us!
(Get your first solution completely free - no credit card required)


Hi Anthony,
Thanks a lot for your help.
We will try your recommendations!
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.