Archive for October, 2009


Windows Privileges Issues !!

A very interesting tip..
For my current project, I need to set the system time with some reference time. Quite easy!!. The SetSytemTime (). The syntax is like this
BOOL WINAPI SetSystemTime(
__in          const SYSTEMTIME* lpSystemTime
);
The time must be in the form of SYSTEMTIME struct.  And I implement it in my code as
SYSTEMTIME st = NewTime;
bSuccess = SetSystemTime(&st);
:). The problem is on the way.
The SetSystemTime function enables the SE_SYSTEMTIME_NAME privilege before changing the system time. This privilege is disabled by default. (©MSDN)
for changing the privileges, we need to implement some authorization functions. In the following order
1-  OpenProcessToken() – opens the access token associated with a process
2-  LookupPrivilegeValue() – Retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name
3-   AdjustTokenPrivileges()-enables or disables privileges in the specified access token.
After that, change the system time with SetSystemTime() method.
Then again reset the privileges by using the function AdjustTokenPrivileges().
It works well in WindowsXP. No problems for setting the new time.  J
But , the problem comes from Windows 7 and Windows Vista.
Then, I start debugging my code in Windows 7. All authorization functions works fine for me.  Like OpenProcessToken(),LookupPrivilegeValue() and AdjustTokenPrivileges(). But I felt something bad about the execution of the last method. AdjustTokenPrivileges()..
I checked the GetLastError() return value. Instead of ERROR_SUCCESS
, it returns ERROR_NOT_ALL_ASSIGNED J
So my investigation is now in that way. How to handle that? Its all because of UAC (User Access Control) problems. Because the SE_SYSTEMTIME_NAME require administrative privilege.
My colleague gave me a tip about Manifests.
The description from MSDN is like Manifests are XML files that accompany and describe side-by-side assemblies or isolated applications. Manifests uniquely identify the assembly through the assembly’s <assemblyIdentity> element. They contain information used for binding and activation, such as COM classes, interfaces, and type libraries, that has traditionally been stored in the registry. Manifests also specify the files that make up the assembly and may include Windows classes if the assembly author wants them to be versioned. Side-by-side assemblies are not registered on the system, but are available to applications and other assemblies on the system that specify dependencies in manifest files.
Manifest files enable administrators and applications to manage side-by-side assembly versions after deployment. Every side-by-side assembly must have a manifest associated with it. The installation of Windows XP installs the supported Microsoft side-by-side assemblies with their manifests. If you develop your own side-by-side assemblies, you must also install manifest files
I turn on the manifests for my application also. Its very simple.
Project Settings > Linker > Manifest File.
And make the options as indicated in the figure.
Including manifest for project
The Rebuild All  and Execute..
🙂
BijU
Gravity is not responsible for people to fall in love. It just happens.

A very interesting tip..

For my current project, I need to set the system time with some reference time. Quite easy!!. Using SetSytemTime (). The syntax is like this

BOOL WINAPI SetSystemTime(

__in          const SYSTEMTIME* lpSystemTime

);

The time must be in the form of SYSTEMTIME struct.  And I implement it in my code as

SYSTEMTIME st = NewTime;

bSuccess = SetSystemTime(&st);

:). The problem is on the way.

The SetSystemTime function enables the SE_SYSTEMTIME_NAME privilege before changing the system time. This privilege is disabled by default. (©MSDN)

for changing the privileges, we need to implement some authorization functions. In the following order

1-  OpenProcessToken() – opens the access token associated with a process

2-  LookupPrivilegeValue() – Retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name

3-   AdjustTokenPrivileges()-enables or disables privileges in the specified access token.

After that, change the system time with SetSystemTime() method.

Then again reset the privileges by using the function AdjustTokenPrivileges().

It works well in WindowsXP. No problems for setting the new time.  🙂 But , the problem comes from Windows 7 and Windows Vista.

Then, I start debugging my code in Windows 7. All authorization functions works fine for me.  Like OpenProcessToken(),LookupPrivilegeValue() and AdjustTokenPrivileges(). But I felt something bad about the execution of the last method. AdjustTokenPrivileges()..

I checked the GetLastError() return value. Instead of ERROR_SUCCESS, it returns ERROR_NOT_ALL_ASSIGNED J

So my investigation is now in that way. How to handle that? Its all because of UAC (User Access Control) problems. Because the SE_SYSTEMTIME_NAME require administrative privilege.

My colleague gave me a tip about Manifests.

The description from MSDN is like Manifests are XML files that accompany and describe side-by-side assemblies or isolated applications. Manifests uniquely identify the assembly through the assembly’s <assemblyIdentity> element. They contain information used for binding and activation, such as COM classes, interfaces, and type libraries, that has traditionally been stored in the registry. Manifests also specify the files that make up the assembly and may include Windows classes if the assembly author wants them to be versioned. Side-by-side assemblies are not registered on the system, but are available to applications and other assemblies on the system that specify dependencies in manifest files.

Manifest files enable administrators and applications to manage side-by-side assembly versions after deployment. Every side-by-side assembly must have a manifest associated with it. The installation of Windows XP installs the supported Microsoft side-by-side assemblies with their manifests. If you develop your own side-by-side assemblies, you must also install manifest files

I turn on the manifests for my application also. Its very simple.

Project Settings > Linker > Manifest File. And make the options as indicated in the figure.

Project Settings>Linker Options > Manifest

Project Settings>Linker Options > Manifest

Including manifest for project

The Rebuild All  and Execute..

🙂

for https://vctipsplusplus.wordpress.com/

BijU

Gravity is not responsible for people to fall in love. It just happens.

Environment variable are variables that can be available to all running programs.  You can access the environment variables using Control Panel > System > Advanced > Environment Variables (Lot of other methods are also available 🙂 )

Environment Variables

Environment Variables

Environment Variables

Environment Variables

In Windows Registry, you can locate them on HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ Control\ Session Manager\ Environment.

Some of the Environment Variables are TEMP, TMP, OS etc.

A number of situations may come during the coding phase to access those variables. For eg: getting the temp folder name, system folder name, lib path, include path etc.

We can access those items from environment variables using the following commands.

ExpandEnvironmentStrings()  – function expands environment-variable strings and replaces them with their defined values.

and ExpandEnvironmentStringsForUser()-function expands the source string by using the environment block established for the specified user.

Like this

CString strErrorMsg = _T(“”);

TCHAR szEnvPath[MAX_PATH];

DWORD dwLen = 0;

dwLen = ::ExpandEnvironmentStrings( _T(“%MSDevDir%”), szEnvPath, MAX_PATH );

if( 0 == dwLen)

{

// ExpandEnvironmentStrings() failed

strErrorMsg = _T(“Cannot Expand using ExpandEnvironmentStrings()”);

}

Now the value for the MSDevDir is in the szEnvPath variable. 🙂

for https://vctipsplusplus.wordpress.com/

BijU

Every day you waste is one you can never make up

Turn Off your Monitor Display !!

On Off Switch

Image Source: http://thumbs.dreamstime.com/thumb_76/1154983417etR5O8.jpg

Nowadays, I’m experimenting with some hardware and Windows :).I’m very happy to work with this. Because, after a lot of reading and googling, doing some coding. Then again, reading a lot of documents 🙂

A very interesting thing in doing this type of coding is that, we don’t get  much information from MSDN :D. Almost all information is available in the MSDN, but it’s up to you to find the best algorithm/class/technology that fit for you.

Yesterday, I tried a lot to get the information from our Desktop Monitor. My aim was to ON/OFF our Desktop Monitor. I have hands on experience with WMI (Windows Management Instrumentation) to query the details like motherboard, disks, Operating System etc. With that confidence I first look into the Win32_DesktopMonitor. And in that a method named SetPowerState() is available. And the information about that method give me immense pleasure.

Its like “The SetPowerState method sets the desired power state for a logical device and when a device should be put into that state. And the power state may have the following values, like(from MSDN)

1 Full power.
2 Power save — low-power mode.
3 Power save — standby.
4 Power save — other.
5 Power cycle.
6 Power off.

I’m very happy after reading this document.  Unfortunately, its not implemented by windows. Due to a lot of technical problems.

So..

But using the very common message like SendMessage can solve the problem so easily.

Just copy the below statement and execute..

SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, (LPARAM)2);

Hope you are familiar with first 2 params. Documentation from the  MSDN for the 3rd param is

SC_MONITORPOWER Sets the state of the display. This command supports devices that have power-saving features, such as a battery-powered personal computer.

And the 4th param may take the values like

-1 – the display is being on

1 – the display is going to low power

2 – the display is being shut off

I think it is so simple. Thanks to Microsoft.. 🙂

for https://vctipsplusplus.wordpress.com/

BijU

God won’t ask about the fancy clothes in your wardrobe, but will ask how many of those clothes helped the needy.