Tuesday, January 25, 2011

How to write log files??





Why Log file needed ?

: Log files are needed for developer to understand flow of code/applications and most important to track/trace the errors in application. If we consider the case of execptions/errors in code sometimes we are unable to trace them at run time. So writing log file really help to overcome for tracking error locations.

Here is sample code written in Visual C-sharp  :


See the Class below:

public class YourLogger
    {
       public static void WriteLogToFile(string yourlogMsg)
        {
            public static string logFile = @"Path/LogFileName.txt";
            try
            {

                FileStream myFileStream = null;
                //creates file if not exist

                if (File.Exists(LogFileName) == false)
                {
                   FileStream fs = new FileStream(LogFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                    fs.Close();
                }
                else
                {
                    myFileStream = new FileStream(LogFileName, FileMode.Append, FileAccess.Write);
                }
                StreamWriter myStreamWriter = new StreamWriter(myFileStream);
                myStreamWriter.WriteLine(DateTime.Now.ToString() + "\t" + yourlogMsg);
                myStreamWriter.Flush();
                myStreamWriter.Close();
                myFileStream.Close();
            }
            catch
            {

                 //Do your exception related operations
            }
      
}
}


Now call  WriteLogToFile(string yourlogMsg) method your application specifically 

in  Catch block to logg the exception details .

Code to call :

YourLogger.WriteLogToFile(exceptionObject.Message);



Hope this might be helpful for new developer.



Thursday, January 20, 2011

How to Play Audio Files in Windows Mobile?

The intension behind this post is to share the code snippet for playing Audio files programatically for Windows Mobile devices.

What you need to is 
1. Create one class ( say class name as Sound ) file.
2. Copy and paste this code 
3. Need to create the object of Sound class & call  play() & StopPlay() accordingly  :
e.g . Sound objSound = new Sound(@"fileName");
      objSound.Play();


Code Snippet is as below :

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace YourNameSpace
{
    public class Sound
    {
        private byte[] m_soundBytes;
        private string m_fileName;

        public static bool isLooping = false;

        private enum Flags
        {
            SND_SYNC = 0x0000,  /* play synchronously (default) */
            SND_ASYNC = 0x0001,  /* play asynchronously */
            SND_NODEFAULT = 0x0002,  /* silence (!default) if sound not found */
            SND_MEMORY = 0x0004,  /* pszSound points to a memory file */
            SND_LOOP = 0x0008,  /* loop the sound until next sndPlaySound */
            SND_NOSTOP = 0x0010,  /* don't stop any currently playing sound */
            SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
            SND_ALIAS = 0x00010000, /* name is a registry alias */
            SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
            SND_FILENAME = 0x00020000, /* name is file name */
            SND_RESOURCE = 0x00040004 /* name is resource name or atom */
            // SND_HIGH = 0xFFFF
        }

        [DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        private extern static int WCE_PlaySound(string szSound, IntPtr hMod, int flags);

        [DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
        private extern static int WCE_PlaySoundBytes(byte[] szSound, IntPtr hMod, int flags);

        public enum Volumes : int
        {
            OFF = 0,
            LOW = 858993459,
            NORMAL = 1717986918,
            MEDIUM = -1717986919,
            HIGH = -858993460,
            VERY_HIGH = -1
        }

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int waveOutSetVolume(IntPtr device, int volume);

        [DllImport("coredll.dll", SetLastError = true)]
        public static extern int waveOutGetVolume(IntPtr device, ref int volume);

        public static Volumes Volume
        {
            get
            {
                int v = (int)0;
                waveOutGetVolume(IntPtr.Zero, ref v);
                switch (v)
                {
                    case (int)Volumes.OFF: return Volumes.OFF;
                    case (int)Volumes.LOW: return Volumes.LOW;
                    case (int)Volumes.NORMAL: return Volumes.NORMAL;
                    case (int)Volumes.MEDIUM: return Volumes.MEDIUM;
                    case (int)Volumes.HIGH: return Volumes.HIGH;
                    case (int)Volumes.VERY_HIGH: return Volumes.VERY_HIGH;

                    default: return Volumes.OFF;
                }
            }
            set { waveOutSetVolume(IntPtr.Zero, (int)value); }
        }

        /// <summary>
        /// Construct the Sound object to play sound data from the specified file.
        /// </summary>
        public Sound(string fileName)
        {
            m_fileName = fileName;
        }

        /// <summary>
        /// Construct the Sound object to play sound data from the specified stream.
        /// </summary>
        public Sound(Stream stream)
        {
            // read the data from the stream
            m_soundBytes = new byte[stream.Length];
            stream.Read(m_soundBytes, 0, (int)stream.Length);
        }

        /// <summary>
        /// Play the sound
        /// </summary>
        public void Play(bool isLoop)
        {
            // if a file name has been registered, call WCE_PlaySound,
            //  otherwise call WCE_PlaySoundBytes
            try
            {
                //waveOutSetVolume(IntPtr.Zero, (int)Volumes.NORMAL);
                if (isLoop) isLooping = true;

                if (isLooping == false || isLoop == true)
                    if (m_fileName != null)
                     {
                                 if (isLoop)
                            WCE_PlaySound(m_fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC |                                                  Flags.SND_LOOP | Flags.SND_FILENAME));
                                else
                            WCE_PlaySound(m_fileName, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
                       }
                    else
                      {
              WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY));
                      }
            }
            catch
            {
                throw;
            }
        }

  public void StopPlay()
    {   // if a file name has been registered, call WCE_PlaySound,
        //  otherwise call WCE_PlaySoundBytes
         try
         {
            if (isLooping == false)
             {
               //waveOutSetVolume(IntPtr.Zero, (int)Volumes.OFF);
              if (m_fileName != null)
               WCE_PlaySound(m_fileName, IntPtr.Zero, (int)(Flags.SND_SYNC | Flags.SND_FILENAME));
             else
 WCE_PlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_NODEFAULT | Flags.SND_MEMORY));
             }
         }
         catch
         {
                throw;
         }
        }
    }
}


Note : If you have any query please let me know.

Thanks

Wednesday, January 19, 2011

How to use TATA DOCOMO 3G 21 mbps for windows mobile?









Nowadays I am frequently watching advertisement from Tata Docomo  3G  21mbps. 
Anybody knows how to utilise the same for windows mobile or Windows Phone?
I mean to say , am looking for any ideas that can we implement using this, some samples or how to give exposure to 3G technology for windows based devices.


Ideas,help,suggestions will be appreciated.

Thursday, January 6, 2011

Brain Vs Face





I think everyone must experieced , When we meet to any new person our mind starts thinking about his/her personality by looking his face only. 
Mind starts to take different decisions like, he/she is Clever,Talented, Short tempered, Stupid, froudy, funny, naughty etc. etc.
Is anyone observed or thinked on this ? Why our mind play such game. Eventhough I also don't know , but really it is amazing.

I read some information about describing/identifying  a human personality by recongising face only, however facts is that those decision are taken up  depending on physical factors like Hair,Nose,Ear,Forehead etc. So I am wondering how does mind works ?
Any replies will be appriciated!!!

Tuesday, January 4, 2011

One experience with MSDN forums for Windows Mobile development.

Almost 1 year back, being a part of team member I was working as software developer in one of project. That project is nothing but all about PIM synchronization includes Caledar,Contacts,tasks and media contents.
In development phase I got stuck with one problem in retrieving a field from contacts. I did all possible efforts in technical point of view however problem remains the same. So thought why not to go with MSDN forums and raise the query?  I am damn sure that I will get any feedback , suggestions from forum members including  MVP's and Microsofts Moderators. But unfortunatly result is same. After that I updates the post by saying that I wanna report this to Microsofts Windows Mobile Team and I need help for reporting the same. But no replies I was observing this for long time, just can able to see the numbers count of visitors and  nothing else.


If Microsoft people started these forums then its there responsibility to help developers to resolve their queries (either with positive outcome or negative outcome)by accepting facts. What does it mean about Social Networking ? 


Posting this blog doesn't mean that I am complaining about MicroSoft guys, just wanna inform them atleast they come with YES / NO for the raised query. 


For more details I am adding the unanswered link below i.e thread in MSDN forum,


http://social.msdn.microsoft.com/Forums/en-US/windowsmobiledev/thread/dbe34c19-deb0-4193-ae7b-a8ba5dca3c24


Finally wanna say few words : The True Pleasure is to understand Human Values & serve them in needs. !!!




I hope if any MS forum buddy will read this , he/she will come with some output. 


Though it was not serious problem; so somehow we convenced to our project client and Happily delivered the project.