IBlog

February 2, 2010

Ignorance is not bliss. EventWaitHandle.OpenExisting throws WaitHandleCannotBeOpenedException

Filed under: .NET, Windows Plateform — Himanshu @ 12:06 pm

My development computer got upgraded to Windows 7, Windows 7 is good experience.

For one of our client, I needed to setup mechanics such that admin users should be able to initiate a process in Windows Service using GDI based .NET application. We had also developed the windows service in .NET. As both of them are .NET applications, there are more than one ways to IPC between them. As in this case there was only need to give signal to the service, I choose to do it through Windows Event. .NET BCL provides wrapper around Win32 functions to do this through type named EventWaitHandle.

I completed the code like I used to do in my old days, and tried the first use of it. On call to OpenExisting, client application failed with exception “No handle of the given name exists”. My first thought was that this could be because of UAC, but in that case it should say something like access violation. I tried running application in Windows 2003 Server and it worked perfectly. I decided to check with Google and documentation. I found this, which gave me a relieving thought – “I’m not the only one in this world!” :) .

After some time I landed to msdn page for namespaces of kernel objects, which indicates that:

For processes started under a client session, the system uses the session namespace by default. However, these processes can use the global namespace by prepending the “Global\” prefix to the object name

changed my client code to have name prefixed with “Global\”, and everything started behaving the way I wanted it to in Windows 7 as well.

January 10, 2009

Knowing memory usage from Compact Framework application

Filed under: Compact Framework, Programming, Windows Plateform — Himanshu @ 6:50 pm

For any handheld device application, memory usage is an important element to observer. .NET Compact framework have very less code that gives information about physical or virtual memory. But using native libraries – by doing pInvoke, one can retrieve memory usage statistics.

There can be more than one level of report that can be captured. Today, lets start with summary. GlobalMemoryStatus function in coredll can help identifying how much total physical memory available in the device and how much of it is free. Same way, how much of virtual memory available to process and how much of it is free.

Lets see how can we use GlobalMemoryStatus. We will have to define a class which will be passed to function and will be filled by GlobalMemoryStatus function. Layout of it is defined as structure by MS team, but its alright to use class here. By having it as class, we will be able to write default constructor unlike structure in C#.

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    internal class MemoryStatus
    {
        public uint dwLength;
        public uint dwMemoryLoad;
        public uint ullTotalPhys;
        public uint ullAvailPhys;
        public uint ullTotalPageFile;
        public uint ullAvailPageFile;
        public uint ullTotalVirtual;
        public uint ullAvailVirtual;
        public uint ullAvailExtendedVirtual;
        public MemoryStatus()
        {
            this.dwLength = (uint)Marshal.SizeOf(typeof(MemoryStatus));
        }
    }

Now, lets define extern method for GlobalMemoryStatus

        [return: MarshalAs(UnmanagedType.Bool)]
        [DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = true, EntryPoint = "GlobalMemoryStatus")]
        static extern bool GlobalMemoryStatusWinCE([In, Out] MemoryStatus lpBuffer);

And here is how we can call the method

    MemoryStatus internalReport = new MemoryStatus();
    if (GlobalMemoryStatusWinCE(internalReport))
	

Powered by WordPress