<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-2219773753775246759</id><updated>2011-08-19T00:05:35.278-07:00</updated><category term='C++'/><category term='Further Data Types'/><category term='Pointers'/><category term='Dynamic Memory Allocation and Dynamic Structures'/><category term='Looping and Iteration'/><category term='Arrays and Strings'/><category term='Conditionals'/><category term='IDE'/><category term='Functions'/><title type='text'>Source Code - Koreng</title><subtitle type='html'>Vb, Delphi, .net, framework, C++, Java, Pascal,Visual Studio, Asm, Ruby, C#, j#, Cs, Html, Php, Perl, Asp, xHtml Get Free Souce Code Here...</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>50</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3973598479534817920</id><published>2009-01-14T09:00:00.000-08:00</published><updated>2009-01-14T09:01:03.368-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Conditionals'/><title type='text'>Other Preprocessor Commands</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION001400000000000000000"&gt; The C Preprocessor&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="ch:preprocess"&gt;&amp;nbsp;&lt;/a&gt; Recall that preprocessing is the first step in the C program compilation stage -- this feature is unique to C compilers.  The preprocessor more or less provides its own language which can be a very powerful tool to the programmer. Recall that all preprocessor directives or commands begin with a #. &lt;br /&gt;Use of the preprocessor is advantageous since it makes: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; programs easier to develop, &lt;/li&gt;&lt;li&gt; easier to read, &lt;/li&gt;&lt;li&gt; easier to modify &lt;/li&gt;&lt;li&gt; C code more transportable between different machine architectures. &lt;/li&gt;&lt;/ul&gt;The preprocessor also lets us customise the language. For example to replace { ... } block statements delimiters by PASCAL like &lt;tt&gt;begin ... end&lt;/tt&gt; we can do: &lt;br /&gt;&lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #define begin {&lt;br /&gt;   #define end  }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;During compilation all occurrences of &lt;tt&gt;begin and &lt;tt&gt;end&lt;/tt&gt; get replaced by corresponding { or } and so the subsequent C compilation stage does not know any difference!!!.  &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Lets look at &lt;tt&gt;#define&lt;/tt&gt; in more detail &lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001410000000000000000"&gt; #define&lt;/a&gt; &lt;/h1&gt;Use this to define constants or any macro substitution. Use as follows: &lt;br /&gt;&lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #define  &lt;macro _moz-userdefined=""&gt; &lt;replacement _moz-userdefined="" name=""&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;em&gt;For Example&lt;/em&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   #define FALSE  0&lt;br /&gt;   #define TRUE  !FALSE &lt;br /&gt;&lt;/replacement&gt;&lt;/macro&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;br /&gt;We can also define small ``functions'' using &lt;tt&gt;#define. For example max. of two variables: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;#define max(A,B) ( (A) &amp;gt; (B) ? (A):(B))&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;? is the ternary operator in C.  &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Note: that this does &lt;u&gt;not&lt;/u&gt; define a proper function &lt;tt&gt;max&lt;/tt&gt;.   &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;All it means that wherever we place &lt;tt&gt;max(C&lt;img align="middle" alt="$\dagger$" border="0" height="30" src="http://www.cs.cf.ac.uk/Dave/C/img25.gif" width="11" /&gt;,D&lt;img align="middle" alt="$\dagger$" border="0" height="30" src="http://www.cs.cf.ac.uk/Dave/C/img25.gif" width="11" /&gt;)&lt;/tt&gt; the text gets replaced by the appropriate definition.  [&lt;img align="middle" alt="$\dagger$" border="0" height="30" src="http://www.cs.cf.ac.uk/Dave/C/img25.gif" width="11" /&gt; = any variable names - not necessarily C and D] &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;So if in our C code we typed something like: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;x = max(q+r,s+t);&lt;/tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;after preprocessing, if we were able to look at the code it would appear like this: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;x = ( (q+r) &amp;gt; (r+s) ? (q+r) : (s+t));&lt;/tt&gt;  &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Other examples of &lt;tt&gt;#define&lt;/tt&gt; could be: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;#define Deg_to_Rad(X) (X*M_PI/180.0) &lt;br /&gt;/* converts degrees to radians, &lt;tt&gt;M_PI&lt;/tt&gt; is the value  &lt;br /&gt;of pi and is defined in math.h library */&lt;br /&gt;&amp;nbsp;&lt;br /&gt;#define LEFT_SHIFT_8 &amp;lt;&amp;lt;8&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;NOTE: The last macro &lt;tt&gt;LEFT_SHIFT_8 is only &lt;br /&gt;valid so long as replacement context is valid &lt;em&gt;i.e.&lt;/em&gt; &lt;br /&gt;&lt;tt&gt;x = y LEFT_SHIFT_8&lt;/tt&gt;. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001420000000000000000"&gt; #undef&lt;/a&gt; &lt;/h1&gt;This commands &lt;u&gt;undefined&lt;/u&gt; a macro. A macro &lt;b&gt;must&lt;/b&gt; be undefined before being redefined to a different value. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001430000000000000000"&gt; #include&lt;/a&gt; &lt;/h1&gt;This directive includes a file into code. &lt;br /&gt;&lt;br /&gt;It has two possible forms: &lt;br /&gt;&lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;#include &lt;file _moz-userdefined=""&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;or&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   #include ``file''&lt;br /&gt;&lt;/file&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;&lt;tt&gt;&lt;file _moz-userdefined=""&gt; tells the compiler to look where system include files are held.  Usually UNIX systems store files in &lt;tt&gt; &lt;em&gt;&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;usr&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;include&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;&lt;/em&gt;&lt;/tt&gt; directory. &lt;br /&gt;&lt;/file&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;``file''&lt;/tt&gt; looks for a file in the current directory (where program was run from)  &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;em&gt;&lt;tt&gt;Include&lt;/tt&gt;d&lt;/em&gt; files usually contain C prototypes and  declarations from header files and &lt;u&gt;not&lt;/u&gt; (algorithmic) C code (SEE next Chapter for reasons) &lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001440000000000000000"&gt; #if -- Conditional inclusion&lt;/a&gt; &lt;/h1&gt;&lt;tt&gt;#if&lt;/tt&gt; evaluates a constant integer expression. You &lt;u&gt;always&lt;/u&gt;  need a &lt;tt&gt;#endif&lt;/tt&gt;  to delimit end of statement. &lt;br /&gt;&lt;br /&gt;We can have &lt;em&gt;else etc.&lt;/em&gt; as well by using &lt;tt&gt;#else&lt;/tt&gt; and &lt;tt&gt;#elif&lt;/tt&gt; -- else if. &lt;br /&gt;&lt;br /&gt;Another common use of &lt;tt&gt;#if&lt;/tt&gt; is with: &lt;br /&gt;&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;&lt;tt&gt;#ifdef&lt;/tt&gt;&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- if defined and  &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;&lt;tt&gt;#ifndef&lt;/tt&gt;&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- if not defined &lt;/dd&gt;&lt;/dl&gt;These are useful for checking if macros are set -- perhaps from different program modules and header files.  &lt;br /&gt;&lt;br /&gt;For example, to set integer size for a portable C program between TurboC (on MSDOS) and Unix (or other) Operating systems. Recall that TurboC uses 16 bits/integer and UNIX 32 bits/integer. &lt;br /&gt;&lt;br /&gt;Assume that if TurboC is running a macro &lt;tt&gt;TURBOC&lt;/tt&gt; will be defined. So we just need to check for this: &lt;br /&gt;&lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;#ifdef TURBOC&lt;br /&gt;   &amp;nbsp; #define INT_SIZE 16&lt;br /&gt;   #else&lt;br /&gt;      #define INT_SIZE  32&lt;br /&gt;   #endif&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt;  &lt;br /&gt;&lt;br /&gt;As another example if running program on MSDOS machine we want to include file msdos.h otherwise a default.h file. A macro &lt;tt&gt;SYSTEM is set (by OS) to type of system so check for this: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;#if SYSTEM == MSDOS&lt;br /&gt;   &amp;nbsp; #include &lt;msdos.h _moz-userdefined=""&gt;&lt;br /&gt;   #else&lt;br /&gt;      #include ``default.h''&lt;br /&gt;  #endif&lt;br /&gt;&lt;/msdos.h&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001450000000000000000"&gt; Preprocessor Compiler Control&lt;/a&gt; &lt;/h1&gt;You can use the &lt;tt&gt;cc&lt;/tt&gt; compiler to control what values are set or defined from the command line. This gives some flexibility in setting customised values and has some other useful functions. The &lt;tt&gt;-D&lt;/tt&gt; compiler option is used. For example: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;cc -DLINELENGTH=80 prog.c -o prog&lt;/tt&gt; &lt;br /&gt;has the same effect as: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;code&gt;#define LINELENGTH 80&lt;/code&gt; &lt;br /&gt;Note that any &lt;code&gt;#define&lt;/code&gt; or &lt;code&gt;#undef&lt;/code&gt; &lt;b&gt;within&lt;/b&gt; the program (&lt;tt&gt;prog.c&lt;/tt&gt; above) &lt;b&gt;override&lt;/b&gt; command line settings. &lt;br /&gt;You can also set a symbol without a value, for example: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;cc -DDEBUG prog.c -o prog&lt;/tt&gt; &lt;br /&gt;Here the value is assumed to be 1. &lt;br /&gt;The setting of such flags is useful, especially for debugging. You can put commands like: &lt;br /&gt;&lt;pre&gt;#ifdef DEBUG&lt;br /&gt;     print("Debugging: Program Version 1\");&lt;br /&gt;#else&lt;br /&gt;     print("Program Version 1 (Production)\");&lt;br /&gt;#endif&lt;br /&gt;&lt;/pre&gt;Also since preprocessor command can be written anywhere in a C program you can filter out variables etc for printing &lt;em&gt;etc.&lt;/em&gt;  when debugging: &lt;br /&gt;&lt;pre&gt;x = y *3;&lt;br /&gt;&lt;br /&gt;#ifdef DEBUG&lt;br /&gt;     print("Debugging: Variables (x,y) = \",x,y);&lt;br /&gt;#endif&lt;br /&gt;&lt;/pre&gt;The &lt;tt&gt;-E&lt;/tt&gt; command line is worth mentioning just for academic reasons. It is not that practical a command. The &lt;tt&gt;-E command&lt;/tt&gt; will force the compiler to stop after the preprocessing stage and output the current state of your program. Apart from being debugging aid for preprocessor commands and also as a useful initial learning tool (try this option out with some of the examples above) it is not that commonly used. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001460000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;There are few other preprocessor directives available: &lt;br /&gt;&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;&lt;tt&gt;#error&lt;/tt&gt;&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;&lt;tt&gt;text of error message&lt;/tt&gt; -- generates an appropriate compiler error message. &lt;em&gt;e.g&lt;/em&gt; &lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;#ifdef OS_MSDOS&lt;br /&gt;   &amp;nbsp; #include &lt;msdos.h _moz-userdefined=""&gt;&lt;br /&gt;   #elifdef OS_UNIX&lt;br /&gt;      #include ``default.h''&lt;br /&gt;  #else&lt;br /&gt;     #error Wrong OS!!&lt;br /&gt;  #endif&lt;br /&gt;&lt;/msdos.h&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;# line&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;&lt;tt&gt;number "string" -- informs the preprocessor that the  &lt;tt&gt;number&lt;/tt&gt; is the next number of line of input. &lt;tt&gt;"string"&lt;/tt&gt; is optional and names the next line of input. This is most often used with programs that translate other languages to C. For example, error messages produced by the C compiler can reference the file name and line numbers of the original source files instead of the intermediate C (translated) source files. &lt;/tt&gt;&lt;/dd&gt;&lt;/dl&gt;&lt;h1&gt;&lt;a href="" name="SECTION001470000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12529&lt;/b&gt;&lt;br /&gt;Define a preprocessor macro &lt;tt&gt;swap(t, x, y)&lt;/tt&gt; that will swap two arguments &lt;tt&gt;x&lt;/tt&gt; and &lt;tt&gt;y&lt;/tt&gt; of a given type &lt;tt&gt;t&lt;/tt&gt;.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Exercise 12531&lt;/b&gt;&lt;br /&gt;Define a preprocessor macro to select: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; the least significant bit from an &lt;tt&gt;unsigned char&lt;/tt&gt; &lt;/li&gt;&lt;li&gt; the &lt;i&gt;n&lt;/i&gt;th (assuming least significant is 0) bit from an &lt;tt&gt;unsigned char&lt;/tt&gt;. &lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3973598479534817920?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3973598479534817920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/other-preprocessor-commands.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3973598479534817920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3973598479534817920'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/other-preprocessor-commands.html' title='Other Preprocessor Commands'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-2091930027073860855</id><published>2009-01-14T08:59:00.000-08:00</published><updated>2009-01-14T09:00:06.662-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Pointers'/><title type='text'>Low Level Operators and Bit Fields</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION001300000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="ch:bits"&gt;&amp;nbsp;&lt;/a&gt; We have seen how pointers give us control over low level memory operations.  Many programs (&lt;em&gt;e.g.&lt;/em&gt; systems type applications) must actually operate at a low level where individual bytes must be operated on. &lt;br /&gt;&lt;b&gt;NOTE:&lt;/b&gt; The combination of pointers and bit-level operators makes C useful for many low level applications and can almost replace assembly code. (Only about 10 % of UNIX is assembly code the rest is C!!.) &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001310000000000000000"&gt; Bitwise Operators&lt;/a&gt; &lt;/h1&gt;The &lt;em&gt;bitwise&lt;/em&gt; operators of C a summarised in the following table: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="center"&gt; &lt;a href="" name="2414"&gt;&amp;nbsp;&lt;/a&gt; &lt;table align="center" border="1" cellpadding="3"&gt;&lt;caption&gt;&lt;strong&gt;Table:&lt;/strong&gt; Bitwise operators&lt;/caption&gt; &lt;tbody&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&amp;amp;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;AND&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&lt;img align="middle" alt="$\mid$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img17.gif" width="8" /&gt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;OR&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&lt;img align="bottom" alt="$\wedge$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img7.gif" width="15" /&gt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;XOR&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&lt;img align="bottom" alt="$\sim$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img6.gif" width="17" /&gt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;One's Compliment&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;&lt;img align="bottom" alt="$0 \rightarrow 1$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img18.gif" width="51" /&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&amp;nbsp;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;&lt;img align="bottom" alt="$1 \rightarrow 0$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img19.gif" width="51" /&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&amp;lt;&amp;lt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;Left shift&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&amp;gt;&amp;gt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;Right Shift&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;DO NOT&lt;/b&gt; confuse &amp;amp; with &amp;amp;&amp;amp;: &amp;amp; is bitwise AND, &amp;amp;&amp;amp; &lt;u&gt;logical&lt;/u&gt; AND. Similarly for &lt;img align="middle" alt="$\mid$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img17.gif" width="8" /&gt; and &lt;img align="middle" alt="$\mid\mid$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img20.gif" width="13" /&gt;. &lt;br /&gt;&lt;br /&gt;&lt;img align="bottom" alt="$\sim$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img6.gif" width="17" /&gt; is a unary operator -- it only operates on one argument to right of the operator.  &lt;br /&gt;&lt;br /&gt;The shift operators perform appropriate shift by operator on the right to the operator on the left. The right operator &lt;u&gt;must&lt;/u&gt; be positive. The vacated bits are filled with zero (&lt;em&gt;i.e.&lt;/em&gt; There is &lt;b&gt;NO&lt;/b&gt; wrap around). &lt;br /&gt;For example: &lt;i&gt;x&lt;/i&gt; &amp;lt;&amp;lt; 2 shifts the bits in &lt;i&gt;x&lt;/i&gt; by 2 places to the left. &lt;br /&gt;&lt;br /&gt;So: &lt;br /&gt;&lt;br /&gt;if &lt;i&gt;x&lt;/i&gt; = 00000010 (binary) or 2 (decimal) &lt;br /&gt;&lt;br /&gt;then:  &lt;br /&gt;&lt;br /&gt;&lt;img align="middle" alt="$x \gt\gt= 2 \Rightarrow x = 00000000$" border="0" height="28" src="http://www.cs.cf.ac.uk/Dave/C/img21.gif" width="217" /&gt; or 0 (decimal) &lt;br /&gt;&lt;br /&gt;Also: if &lt;i&gt;x&lt;/i&gt; = 00000010 (binary) or 2 (decimal) &lt;br /&gt;&lt;br /&gt;&lt;img align="middle" alt="$x &amp;lt;&amp;lt;= 2 \Rightarrow x = 00001000$" border="0" height="28" src="http://www.cs.cf.ac.uk/Dave/C/img22.gif" width="217" /&gt; or 8 (decimal) &lt;br /&gt;&lt;br /&gt;Therefore a shift left is equivalent to a  multiplication by 2. &lt;br /&gt;&lt;br /&gt;Similarly a shift right is equal to division by 2 &lt;br /&gt;&lt;br /&gt;&lt;b&gt;NOTE&lt;/b&gt;: Shifting is much faster than actual  multiplication (*) or division (/) by 2. So if you want fast multiplications or division by 2 &lt;em&gt;use shifts&lt;/em&gt;.  &lt;br /&gt;&lt;br /&gt;To illustrate many points of bitwise operators let us write a function, &lt;tt&gt;Bitcount&lt;/tt&gt;, that counts bits set to 1 in an 8 bit number (&lt;tt&gt;unsigned char&lt;/tt&gt;) passed as an argument to the function. &lt;br /&gt;&lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;int bitcount(unsigned char x) &lt;br /&gt;&amp;nbsp;&lt;br /&gt;   {&amp;nbsp;int count;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     for (count=0; x != 0; x&amp;gt;&amp;gt;=1);&lt;br /&gt;       if ( x &amp;amp; 01)&lt;br /&gt;         count++;&lt;br /&gt;     return count;&lt;br /&gt;   }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This function illustrates many C program points: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; &lt;tt&gt;for loop &lt;u&gt;not&lt;/u&gt; used for simple counting operation &lt;/tt&gt;&lt;/li&gt;&lt;li&gt;&lt;tt&gt; &lt;tt&gt;x&lt;img align="middle" alt="$\gt\gt=1 \Rightarrow$" border="0" height="28" src="http://www.cs.cf.ac.uk/Dave/C/img23.gif" width="85" /&gt; x = x &amp;gt;&amp;gt; 1&lt;/tt&gt; &lt;/tt&gt;&lt;/li&gt;&lt;li&gt;&lt;tt&gt; for loop will repeatedly shift right &lt;tt&gt;x&lt;/tt&gt; until &lt;tt&gt;x&lt;/tt&gt; becomes 0 &lt;/tt&gt;&lt;/li&gt;&lt;li&gt;&lt;tt&gt; use expression evaluation of &lt;tt&gt;x &amp;amp; 01&lt;/tt&gt; to control &lt;tt&gt;if&lt;/tt&gt; &lt;/tt&gt;&lt;/li&gt;&lt;li&gt;&lt;tt&gt; &lt;tt&gt;x &amp;amp; 01&lt;/tt&gt; &lt;em&gt;masks&lt;/em&gt; of 1st bit of &lt;tt&gt;x&lt;/tt&gt; if this is 1 then &lt;tt&gt; count++&lt;/tt&gt; &lt;/tt&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;&lt;a href="" name="SECTION001320000000000000000"&gt; Bit Fields&lt;/a&gt; &lt;/h1&gt;&lt;em&gt;Bit Fields&lt;/em&gt; allow the packing of data in a structure. This is especially useful when memory or data storage is at a premium. Typical examples: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; Packing several objects into a machine word. &lt;em&gt;e.g.&lt;/em&gt; 1 bit flags can be compacted -- Symbol tables in compilers. &lt;/li&gt;&lt;li&gt; Reading external file formats -- non-standard file formats could be read in. &lt;em&gt;E.g.&lt;/em&gt; 9 bit integers. &lt;/li&gt;&lt;/ul&gt;C lets us do this in a structure definition by putting  :&lt;em&gt;bit length&lt;/em&gt; after the variable. &lt;em&gt;i.e.&lt;/em&gt; &lt;br /&gt;&lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;struct packed_struct {&lt;br /&gt;   unsigned int f1:1;&lt;br /&gt;   unsigned int f2:1;&lt;br /&gt;   unsigned int f3:1;&lt;br /&gt;   unsigned int f4:1;&lt;br /&gt;   unsigned int type:4;&lt;br /&gt;   unsigned int funny_int:9;&lt;br /&gt;} pack;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;Here the &lt;tt&gt;packed_struct contains 6 members: Four 1 bit &lt;em&gt;flags&lt;/em&gt; &lt;tt&gt; f1..f3&lt;/tt&gt;, a 4 bit &lt;tt&gt;type&lt;/tt&gt; and a 9 bit &lt;tt&gt;funny_int&lt;/tt&gt;. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;C automatically packs the above bit fields as compactly as possible, provided that the maximum length of the field is less than or equal to the integer word length of the computer. If this is not the case then some compilers may allow memory overlap for the fields whilst other would store the next field in the next word (see comments on bit fiels portability below).  &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Access members as usual via: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;pack.type = 7;&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;b&gt;NOTE&lt;/b&gt;:  &lt;/tt&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;tt&gt; Only &lt;i&gt;n&lt;/i&gt; lower bits will be assigned to an &lt;i&gt;n&lt;/i&gt; bit number. So type cannot take values larger than 15 (4 bits long). &lt;/tt&gt;&lt;/li&gt;&lt;li&gt;&lt;tt&gt; Bit fields are &lt;u&gt;always&lt;/u&gt; converted to integer type for computation. &lt;/tt&gt;&lt;/li&gt;&lt;li&gt;&lt;tt&gt; You are allowed to mix ``normal'' types with bit fields. &lt;/tt&gt;&lt;/li&gt;&lt;li&gt;&lt;tt&gt; The &lt;tt&gt;unsigned&lt;/tt&gt; definition is important - ensures that no bits are used as a &lt;img align="middle" alt="$\pm$" border="0" height="28" src="http://www.cs.cf.ac.uk/Dave/C/img24.gif" width="17" /&gt; flag. &lt;/tt&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;&lt;a href="" name="SECTION001321000000000000000"&gt; Bit Fields: Practical Example&lt;/a&gt; &lt;/h2&gt;Frequently device controllers (&lt;em&gt;e.g.&lt;/em&gt; disk drives) and the operating system need to communicate at a low level. Device controllers contain several &lt;em&gt; registers&lt;/em&gt; which may be packed together in one integer (Figure&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node13.html#fig:drive"&gt;12.1&lt;/a&gt;). &lt;br /&gt;&lt;a href="" name="fig:drive"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/drive.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node13.html#fig:drive"&gt;12.1&lt;/a&gt; Example Disk Controller Register&lt;/b&gt; We could define this register easily with bit fields: &lt;br /&gt;&lt;pre&gt;struct DISK_REGISTER  {&lt;br /&gt;     unsigned ready:1;&lt;br /&gt;     unsigned error_occured:1;&lt;br /&gt;     unsigned disk_spinning:1;&lt;br /&gt;     unsigned write_protect:1;&lt;br /&gt;     unsigned head_loaded:1;&lt;br /&gt;     unsigned error_code:8;&lt;br /&gt;     unsigned track:9;&lt;br /&gt;     unsigned sector:5;&lt;br /&gt;     unsigned command:5;&lt;br /&gt;};&lt;br /&gt;&lt;/pre&gt;To access values stored at a particular memory address, &lt;tt&gt; DISK_REGISTER_MEMORY&lt;/tt&gt; we can assign a pointer of the above structure to access the memory via: &lt;br /&gt;&lt;pre&gt;struct DISK_REGISTER *disk_reg = (struct DISK_REGISTER *) DISK_REGISTER_MEMORY;&lt;br /&gt;&lt;/pre&gt;The disk driver code to access this is now relatively straightforward: &lt;br /&gt;&lt;pre&gt;/* Define sector and track to start read */&lt;br /&gt;&lt;br /&gt;disk_reg-&amp;gt;sector = new_sector;&lt;br /&gt;disk_reg-&amp;gt;track = new_track;&lt;br /&gt;disk_reg-&amp;gt;command = READ;&lt;br /&gt;&lt;br /&gt;/* wait until operation done, ready will be true */&lt;br /&gt;&lt;br /&gt;while ( ! disk_reg-&amp;gt;ready ) ;&lt;br /&gt;&lt;br /&gt;/* check for errors */&lt;br /&gt;&lt;br /&gt;if (disk_reg-&amp;gt;error_occured)&lt;br /&gt;  { /* interrogate disk_reg-&amp;gt;error_code for error type */&lt;br /&gt;    switch (disk_reg-&amp;gt;error_code)&lt;br /&gt;    ......&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;&lt;h2&gt;&lt;a href="" name="SECTION001322000000000000000"&gt; A note of caution: Portability&lt;/a&gt; &lt;/h2&gt;Bit fields are a convenient way to express many difficult operations. However, bit fields do suffer from a lack of portability between platforms: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; integers may be signed or unsigned &lt;/li&gt;&lt;li&gt; Many compilers limit the maximum number of bits in the bit field to the size of an &lt;tt&gt;integer&lt;/tt&gt; which may be either 16-bit or 32-bit varieties. &lt;/li&gt;&lt;li&gt; Some bit field members are stored left to right others are stored right to left in memory. &lt;/li&gt;&lt;li&gt; If bit fields too large, next bit field may be stored consecutively in memory (overlapping the boundary between memory locations) or in the next word of memory. &lt;/li&gt;&lt;/ul&gt;If portability of code is a premium you can use bit shifting and masking to achieve the same results but not as easy to express or read. For example: &lt;br /&gt;&lt;pre&gt;unsigned int  *disk_reg = (unsigned int *) DISK_REGISTER_MEMORY;&lt;br /&gt;&lt;br /&gt;/* see if disk error occured */&lt;br /&gt;&lt;br /&gt;disk_error_occured = (disk_reg &amp;amp; 0x40000000) &amp;gt;&amp;gt; 31;&lt;br /&gt;&lt;/pre&gt;&lt;h1&gt;&lt;a href="" name="SECTION001330000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12507&lt;/b&gt;&lt;br /&gt;&lt;a href="" name="ex:bin"&gt;&amp;nbsp;&lt;/a&gt;Write a function that prints out an 8-bit (unsigned char) number in binary format.&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Exercise 12514&lt;/b&gt;&lt;br /&gt;Write a function    setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n    bits of an unsigned char  variable y (leaving other bits unchanged). &lt;br /&gt;E.g. if &lt;i&gt;x&lt;/i&gt; =  10101010 (170 decimal) and &lt;i&gt;y&lt;/i&gt; = 10100111 (167 decimal)  and &lt;i&gt;n&lt;/i&gt; = 3 and &lt;i&gt;p&lt;/i&gt; =     6 say then you need to strip off 3 bits of y (111) and put them in x at position     10&lt;i&gt;xxx&lt;/i&gt;010 to get answer 10111010. &lt;br /&gt;Your answer should print out the result in binary form (see Exercise&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node13.html#ex:bin"&gt;12.1&lt;/a&gt; although input can be in decimal form. &lt;br /&gt;Your output should be like this: &lt;br /&gt;&lt;pre&gt;x = 10101010 (binary)&lt;br /&gt;   y = 10100111 (binary)&lt;br /&gt;   setbits n = 3, p = 6 gives x = 10111010 (binary)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Exercise 12515&lt;/b&gt;&lt;br /&gt;Write a function that inverts the bits of an unsigned char x and stores answer in y. &lt;br /&gt;Your answer should print out the result in binary form (see Exercise&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node13.html#ex:bin"&gt;12.1&lt;/a&gt; although input can be in decimal form. &lt;br /&gt;Your output should be like this: &lt;br /&gt;&lt;pre&gt;x = 10101010 (binary)&lt;br /&gt;   x inverted = 01010101 (binary)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Exercise 12516&lt;/b&gt;&lt;br /&gt;Write a function that rotates (&lt;b&gt;NOT shifts&lt;/b&gt;) to the right  by n bit positions the bits of an unsigned char x.ie no bits are lost in this process. &lt;br /&gt;Your answer should print out the result in binary form (see Exercise&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node13.html#ex:bin"&gt;12.1&lt;/a&gt; although input can be in decimal form. &lt;br /&gt;Your output should be like this: &lt;br /&gt;&lt;pre&gt;x = 10100111 (binary)&lt;br /&gt;   x rotated by 3 = 11110100 (binary)&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Note&lt;/b&gt;: All the functions developed should be as concise as    possible&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-2091930027073860855?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/2091930027073860855/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/low-level-operators-and-bit-fields.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2091930027073860855'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2091930027073860855'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/low-level-operators-and-bit-fields.html' title='Low Level Operators and Bit Fields'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3775073805077212560</id><published>2009-01-14T08:58:00.002-08:00</published><updated>2009-01-14T08:59:20.630-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Pointers'/><title type='text'>Advanced Pointer Topics</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION001200000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;We have introduced many  applications and techniques that use pointers. We have introduced some advanced pointer issues already. This chapter brings together some topics we have briefly mentioned and others to complete our study C pointers. &lt;br /&gt;In this chapter we will: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; Examine pointers to pointers in more detail. &lt;/li&gt;&lt;li&gt; See how pointers are used in command line input in C. &lt;/li&gt;&lt;li&gt; Study pointers to functions &lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;&lt;a href="" name="SECTION001210000000000000000"&gt; Pointers to Pointers&lt;/a&gt; &lt;/h1&gt;We introduced the concept of a pointer to a pointer previously. You can have a pointer to a pointer of any type. &lt;br /&gt;Consider the following: &lt;br /&gt;&lt;pre&gt;char ch;  /* a character */&lt;br /&gt;char *pch; /* a pointer to a character */&lt;br /&gt;char **ppch; /* a pointer to a pointer to a character */&lt;br /&gt;&lt;/pre&gt;We can visualise this in Figure&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node12.html#fig:ptrtoptr"&gt;11.1&lt;/a&gt;. Here we can see that &lt;tt&gt;**ppch&lt;/tt&gt; refers to memory address of &lt;tt&gt;*pch&lt;/tt&gt; which refers to the memory address of the variable &lt;tt&gt;ch&lt;/tt&gt;. But what does this mean in practice? &lt;br /&gt;&lt;a href="" name="fig:ptrtoptr"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/ptrtoptr.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node12.html#fig:ptrtoptr"&gt;11.1&lt;/a&gt; Pointers to pointers&lt;/b&gt; Recall that &lt;tt&gt;char *&lt;/tt&gt; refers to a (&lt;tt&gt;NULL&lt;/tt&gt; terminated string. So one common and convenient notion is to declare a pointer to a pointer to a string (Figure&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node12.html#fig:ptrtostr"&gt;11.2&lt;/a&gt;) &lt;br /&gt;&lt;a href="" name="fig:ptrtostr"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/ptrtostr.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node12.html#fig:ptrtostr"&gt;11.2&lt;/a&gt; Pointer to String&lt;/b&gt; Taking this one stage further we can have several strings being pointed to by the pointer (Figure&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node12.html#fig:argv"&gt;11.3&lt;/a&gt;) &lt;br /&gt;&lt;a href="" name="fig:argv"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/argv.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node12.html#fig:argv"&gt;11.3&lt;/a&gt; Pointer to Several Strings&lt;/b&gt; We can refer to individual strings by &lt;tt&gt;ppch[0], ppch[1], ....&lt;/tt&gt;. Thus this is identical to declaring &lt;tt&gt;char *ppch[]&lt;/tt&gt;. &lt;br /&gt;One common occurrence of this type is in C command line argument input which we now consider. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001220000000000000000"&gt; Command line input&lt;/a&gt; &lt;/h1&gt;C lets read arguments from the command line which can then be used in our programs. &lt;br /&gt;&lt;br /&gt;We can type arguments after the program name when we run the program. &lt;br /&gt;&lt;br /&gt;We have seen this with the compiler for example &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;c89 -o prog prog.c&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;&lt;tt&gt;c89&lt;/tt&gt; is the program, &lt;tt&gt;-o prog prog.c&lt;/tt&gt; the arguments.  &lt;br /&gt;&lt;br /&gt;In order to be able to use such arguments in our code we must define them as follows: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;main(int argc, char **argv)&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;So our &lt;tt&gt;main&lt;/tt&gt; function now has its own arguments. These are the only arguments main accepts. &lt;br /&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt; &lt;b&gt;argc&lt;/b&gt; is the number of arguments typed -- including the program name. &lt;/li&gt;&lt;li&gt; &lt;b&gt;argv&lt;/b&gt; is an array of strings holding each command line argument -- including the program name in the first array element. &lt;/li&gt;&lt;/ul&gt;A simple program example:  &lt;br /&gt;&lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;#include&lt;stdio.h _moz-userdefined=""&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;main (int argc, char **argv)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { /* program to print arguments&lt;br /&gt;     from command line */&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     int i;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     printf(``argc = %d&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;n&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;n'',argc);&lt;br /&gt;     for (i=0;i&lt;argc;++i) _moz-userdefined=""&gt;&lt;br /&gt;       printf(``argv[%d]: %s&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;n'',&lt;br /&gt;         i, argv[i]);&lt;br /&gt;   }&lt;br /&gt;&lt;/argc;++i)&gt;&lt;/stdio.h&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;Assume it is compiled to run it as args.  &lt;br /&gt;&lt;br /&gt;So if we type: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;args f1 ``f2'' f3 4 stop! &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;The output would be: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;argc = 6&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   argv[0] = args&lt;br /&gt;   argv[1] = f1&lt;br /&gt;   argv[2] = f2&lt;br /&gt;   argv[3] = f3&lt;br /&gt;   argv[4] = 4&lt;br /&gt;   argv[5] = stop!&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&lt;b&gt;NOTE:&lt;/b&gt; &lt;img align="bottom" alt="$\bullet$" border="0" height="14" src="http://www.cs.cf.ac.uk/Dave/C/img16.gif" width="12" /&gt; argv[0] is program name.&lt;br /&gt;   &lt;img align="bottom" alt="$\bullet$" border="0" height="14" src="http://www.cs.cf.ac.uk/Dave/C/img16.gif" width="12" /&gt; argc counts program name&lt;br /&gt;   &lt;img align="bottom" alt="$\bullet$" border="0" height="14" src="http://www.cs.cf.ac.uk/Dave/C/img16.gif" width="12" /&gt; Embedded `` '' are ignored.&lt;br /&gt;   Blank spaces delimit end of arguments.&lt;br /&gt;   Put blanks in `` '' if needed.&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;h1&gt;&lt;a href="" name="SECTION001230000000000000000"&gt; Pointers to a Function&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="ch:ptr2fn"&gt;&amp;nbsp;&lt;/a&gt;  Pointer to a function are perhaps on of the more confusing uses of pointers in C. Pointers to functions are not as common as other pointer uses. However, one common use is in a passing pointers to a function  as a parameter in a function call. (Yes this is getting confusing, hold on to your hats for a moment). &lt;br /&gt;This is especially useful when alternative functions maybe used to perform similar tasks on data. You can pass the data and the function to be used to some &lt;em&gt;control&lt;/em&gt; function for instance. As we will see shortly the C standard library provided some  basic sorting  (&lt;tt&gt; qsort&lt;/tt&gt;) and searching (&lt;tt&gt;bsearch&lt;/tt&gt;)  functions for free. You can easily embed your own functions. &lt;br /&gt;To declare a pointer to a function do: &lt;br /&gt;&lt;pre&gt;int (*pf) ();&lt;br /&gt;&lt;/pre&gt;This simply declares a pointer &lt;tt&gt;*pf&lt;/tt&gt; to function that returns and &lt;tt&gt;int&lt;/tt&gt;.  No actual function is &lt;em&gt;pointed&lt;/em&gt; to yet. &lt;br /&gt;If we have a function &lt;tt&gt;int f()&lt;/tt&gt; then we may simply (!!) write: &lt;br /&gt;&lt;pre&gt;pf = &amp;amp;f;&lt;br /&gt;&lt;/pre&gt;For compiler prototyping to fully work it is better to have full function prototypes for the function and the pointer to a function: &lt;br /&gt;&lt;pre&gt;int f(int);&lt;br /&gt;int (*pf) (int) = &amp;amp;f;&lt;br /&gt;&lt;/pre&gt;Now &lt;tt&gt;f()&lt;/tt&gt; returns an &lt;tt&gt;int&lt;/tt&gt; and takes one &lt;tt&gt;int&lt;/tt&gt; as a parameter. &lt;br /&gt;You can do things like: &lt;br /&gt;&lt;pre&gt;ans = f(5);&lt;br /&gt;ans = pf(5);&lt;br /&gt;&lt;/pre&gt;which are equivalent. &lt;br /&gt;The &lt;tt&gt;qsort&lt;/tt&gt; standard library function is very useful function that is designed to sort an array by a &lt;em&gt;key&lt;/em&gt; value of &lt;em&gt;any type&lt;/em&gt; into ascending order, as long as the elements of the array are of fixed type. &lt;br /&gt;qsort is prototyped in (&lt;tt&gt;stdlib.h&lt;/tt&gt;): &lt;br /&gt;&lt;pre&gt;void qsort(void *base, size_t num_elements, size_t element_size,&lt;br /&gt;   int (*compare)(void const *, void  const *));&lt;br /&gt;&lt;/pre&gt;The argument &lt;tt&gt;base&lt;/tt&gt; points to the array to be sorted, &lt;tt&gt; num_elements&lt;/tt&gt; indicates how long the array is, &lt;tt&gt;element_size&lt;/tt&gt; is the size in bytes of each array element and the final argument &lt;tt&gt; compare&lt;/tt&gt; is a pointer to a function. &lt;br /&gt;&lt;tt&gt;qsort&lt;/tt&gt; calls the &lt;tt&gt;compare&lt;/tt&gt; function which is user defined to compare the data when sorting. Note that &lt;tt&gt;qsort&lt;/tt&gt; maintains it's data type independence by giving the comparison responsibility to the user. The compare function must return certain (&lt;tt&gt;integer&lt;/tt&gt;) values according to the comparison result: &lt;br /&gt;&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;less than zero&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;: if first value is less than the second value &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;zero&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;: if first value is equal to the second value &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;greater than zero&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;: if first value is greater than the second value &lt;/dd&gt;&lt;/dl&gt;Some quite complicated data structures can be sorted in this manner. For example, to sort the following structure by &lt;tt&gt;integer&lt;/tt&gt; key: &lt;br /&gt;&lt;pre&gt;typedef struct {&lt;br /&gt;        int   key;&lt;br /&gt;        struct other_data;&lt;br /&gt;} Record;&lt;br /&gt;&lt;/pre&gt;We can write a compare function, &lt;tt&gt;record_compare&lt;/tt&gt;: &lt;br /&gt;&lt;pre&gt;int record\_compare(void const *a, void  const *a)&lt;br /&gt;  {  return ( ((Record *)a)-&amp;gt;key - ((Record *)b)-&amp;gt;key );&lt;br /&gt;  }&lt;br /&gt;&lt;/pre&gt;Assuming that we have an &lt;tt&gt;array&lt;/tt&gt; of &lt;tt&gt;array_length Record&lt;/tt&gt;s suitably filled with date we can call &lt;tt&gt;qsort&lt;/tt&gt; like this: &lt;br /&gt;&lt;pre&gt;qsort( array, arraylength, sizeof(Record), record_compare);&lt;br /&gt;&lt;/pre&gt;Further examples of standard library and system calls that use pointers to functions may be found in Chapters&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node16.html#ch:search"&gt;15.4&lt;/a&gt; and&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node20.html#ch:dir"&gt;19.1&lt;/a&gt;. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001240000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12476&lt;/b&gt;&lt;br /&gt;Write a program last that prints the last n lines of its text input. By default n should be 5, but your program should allow an optional argument so that &lt;br /&gt;&lt;pre&gt;last -n&lt;br /&gt;&lt;/pre&gt;prints out the last n lines, where n is any integer. Your program should make the best use of available storage. (Input of text  could be by reading a file specified from the command or reading a file from standard input) &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Exercise 12477&lt;/b&gt;&lt;br /&gt;Write a program that sorts a list of integers in ascending order. However if a -r flag is present on the command line your program should sort the list in descending order. (You may use any sorting routine you wish) &lt;br /&gt;&lt;b&gt;Exercise 12478&lt;/b&gt;&lt;br /&gt;Write a program that reads the following structure and sorts the data by keyword using &lt;tt&gt;qsort&lt;/tt&gt; &lt;br /&gt;&lt;pre&gt;typedef struct {&lt;br /&gt;        char   keyword[10];&lt;br /&gt;        int    other_data;&lt;br /&gt;} Record;&lt;br /&gt;&lt;/pre&gt;&lt;b&gt;Exercise 12479&lt;/b&gt;&lt;br /&gt;An &lt;em&gt;insertion sort&lt;/em&gt; is performed by adding values to an array one by one. The first value is simply stored at the beginning of the array. Each subsequent value is added by finding its ordered position in the array, moving data as needed to accommodate the value and inserting the value in this position. &lt;br /&gt;Write a function called &lt;tt&gt;insort&lt;/tt&gt; that performs this task and behaves in the same manner as &lt;tt&gt;qsort&lt;/tt&gt;, &lt;em&gt;i.e&lt;/em&gt; it can sort  an array by a &lt;em&gt;key&lt;/em&gt; value of &lt;em&gt;any type&lt;/em&gt; and it has similar prototyping.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3775073805077212560?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3775073805077212560/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/advanced-pointer-topics.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3775073805077212560'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3775073805077212560'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/advanced-pointer-topics.html' title='Advanced Pointer Topics'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-1214604571664437318</id><published>2009-01-14T08:58:00.001-08:00</published><updated>2009-01-14T08:58:36.631-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Dynamic Memory Allocation and Dynamic Structures'/><title type='text'>Dynamic Memory Allocation and Dynamic Structures</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION001100000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;Dynamic allocation is a pretty unique feature to C (amongst high level languages). It enables us to create data types and structures of any size and length to suit our programs need &lt;u&gt;within&lt;/u&gt; the program. &lt;br /&gt;&lt;br /&gt;We will look at two common applications of this: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; dynamic arrays &lt;/li&gt;&lt;li&gt; dynamic data structure &lt;em&gt;e.g.&lt;/em&gt; linked lists &lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;&lt;a href="" name="SECTION001110000000000000000"&gt; Malloc, Sizeof, and Free&lt;/a&gt; &lt;/h1&gt;The Function &lt;tt&gt;malloc&lt;/tt&gt; is most commonly used to attempt to ``grab'' a continuous portion of memory. It is defined by: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;void *malloc(size_t number_of_bytes)&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;That is to say it returns a pointer of type &lt;tt&gt;void *&lt;/tt&gt; that is the start in memory of the reserved portion of size &lt;tt&gt;number_of_bytes&lt;/tt&gt;. If memory cannot be allocated a &lt;tt&gt;NULL&lt;/tt&gt; pointer is returned. &lt;br /&gt;Since a &lt;tt&gt;void  *&lt;/tt&gt; is returned the C standard states that this pointer can be converted to any type.  The &lt;tt&gt;size_t&lt;/tt&gt; argument type is defined in &lt;tt&gt; stdlib.h&lt;/tt&gt; and is an &lt;em&gt;unsigned type&lt;/em&gt;. &lt;br /&gt;So: &lt;br /&gt;&lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; char *cp;&lt;br /&gt;   cp = malloc(100);&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;attempts to get 100 bytes and assigns the start address to &lt;tt&gt;cp. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Also it is usual to use the &lt;tt&gt;sizeof()&lt;/tt&gt; function to specify the number of bytes: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int *ip;&lt;br /&gt;   ip = (int *) malloc(100*sizeof(int));&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;Some C compilers may require to cast the type of conversion. The &lt;tt&gt;(int *) means coercion to an integer pointer. Coercion to the correct pointer type is very important to ensure pointer arithmetic is performed correctly. I personally use it as a means of ensuring that I am totally correct in my coding and use cast all the time. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;It is good practice to use &lt;tt&gt;sizeof()&lt;/tt&gt; even  if you know the actual size you want -- it makes for device independent (portable) code. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;sizeof&lt;/tt&gt; can be used to find the size of  any data type, variable or structure. Simply supply one of these as an argument to the function. &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;SO: &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int i;&lt;br /&gt;   struct COORD {float x,y,z};&lt;br /&gt;   typedef struct COORD PT;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   sizeof(int), sizeof(i),&lt;br /&gt;   sizeof(struct COORD) and&lt;br /&gt;   sizeof(PT) are all ACCEPTABLE&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;In the above we can use the link between pointers and arrays to treat the reserved memory like an array. &lt;em&gt;i.e&lt;/em&gt; we can do things like: &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;ip[0] = 100; &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;or &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;for(i=0;i&amp;lt;100;++i) scanf("%d",ip++);&lt;/tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;When you have finished using a portion of memory you should always &lt;tt&gt;free()&lt;/tt&gt; it. This allows the memory &lt;em&gt;freed&lt;/em&gt; to be aavailable again, possibly for further &lt;tt&gt;malloc()&lt;/tt&gt; calls &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;The function &lt;tt&gt;free()&lt;/tt&gt; takes a pointer as an argument and frees the memory to which the pointer refers. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001120000000000000000"&gt; Calloc and Realloc&lt;/a&gt; &lt;/h1&gt;There are two additional memory allocation functions, &lt;tt&gt;Calloc()&lt;/tt&gt; and &lt;tt&gt;Realloc()&lt;/tt&gt;. Their prototypes are given below: &lt;br /&gt;&lt;pre&gt;void *calloc(size_t num_elements, size_t element_size};&lt;br /&gt;&lt;br /&gt;void *realloc( void *ptr, size_t new_size);&lt;br /&gt;&lt;/pre&gt;&lt;tt&gt;Malloc&lt;/tt&gt; does not initialise memory (to &lt;em&gt;zero&lt;/em&gt;) in any way. If you wish to initialise memory then use &lt;tt&gt;calloc&lt;/tt&gt;. Calloc there is slightly more computationally expensive but, occasionally, more convenient than malloc. Also note the different syntax between &lt;tt&gt;calloc&lt;/tt&gt; and &lt;tt&gt;malloc&lt;/tt&gt; in that &lt;tt&gt; calloc&lt;/tt&gt; takes the number of desired elements, &lt;tt&gt;num_elements&lt;/tt&gt;, and element_size, &lt;tt&gt;element_size&lt;/tt&gt;, as two individual arguments. &lt;br /&gt;Thus to assign 100 integer elements that are all initially zero you would do: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int *ip;&lt;br /&gt;   ip = (int *) calloc(100, sizeof(int));&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;tt&gt;Realloc is a function which attempts to change the size of a previous allocated block of memory. The new size can be larger or smaller. If the block is made larger then the old contents remain unchanged and memory is added to the end of the block. If the size is made smaller then the remaining contents are unchanged. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;If the original block size cannot be resized then &lt;tt&gt;realloc&lt;/tt&gt; will attempt to assign a new block of memory and will copy the old block contents. Note a new pointer (of different value) will consequently be returned. You &lt;b&gt;must&lt;/b&gt; use this new value. If  new memory cannot be reallocated then &lt;tt&gt;realloc&lt;/tt&gt; returns &lt;tt&gt;NULL&lt;/tt&gt;. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Thus to change the size of memory allocated to the &lt;tt&gt;*ip&lt;/tt&gt; pointer above to an array block of  50 integers instead of 100, simply do: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;ip = (int *) calloc( ip, 50);&lt;/tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001130000000000000000"&gt; Linked Lists&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="sec:linklist"&gt;&amp;nbsp;&lt;/a&gt; Let us now return to our linked list example: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;typedef struct { &amp;nbsp;int value;&lt;br /&gt;        ELEMENT *next;&lt;br /&gt;     } ELEMENT; &lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;We can now try to grow the list dynamically: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;link = (ELEMENT *) malloc(sizeof(ELEMENT)); &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;This will allocate memory for a new &lt;tt&gt;link&lt;/tt&gt;. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;If we want to deassign memory from a pointer use the &lt;tt&gt;free()&lt;/tt&gt; function: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;free(link)&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;em&gt;See Example programs (&lt;tt&gt;queue.c&lt;/tt&gt;) below and try exercises for further practice.&lt;/em&gt; &lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001140000000000000000"&gt; Full Program: &lt;tt&gt;queue.c&lt;/tt&gt;&lt;/a&gt; &lt;/h1&gt;A queue is basically a special case of a linked list where one data element joins the list at the left end and leaves in a ordered fashion at the other end. &lt;br /&gt;The full listing for &lt;tt&gt;queue.c&lt;/tt&gt; is as follows: &lt;br /&gt;&lt;pre&gt;/*            */&lt;br /&gt;/* queue.c            */&lt;br /&gt;/* Demo of dynamic data structures in C                      */&lt;br /&gt;&lt;br /&gt;#include &lt;stdio.h _moz-userdefined=""&gt;&lt;br /&gt;&lt;br /&gt;#define FALSE 0&lt;br /&gt;#define NULL 0&lt;br /&gt;&lt;br /&gt;typedef struct {&lt;br /&gt;    int     dataitem;&lt;br /&gt;    struct listelement *link;&lt;br /&gt;}               listelement;&lt;br /&gt;&lt;br /&gt;void Menu (int *choice);&lt;br /&gt;listelement * AddItem (listelement * listpointer, int data);&lt;br /&gt;listelement * RemoveItem (listelement * listpointer);&lt;br /&gt;void PrintQueue (listelement * listpointer);&lt;br /&gt;void ClearQueue (listelement * listpointer);&lt;br /&gt;&lt;br /&gt;main () {&lt;br /&gt;    listelement listmember, *listpointer;&lt;br /&gt;    int     data,&lt;br /&gt;            choice;&lt;br /&gt;&lt;br /&gt;    listpointer = NULL;&lt;br /&gt;    do {&lt;br /&gt; Menu (&amp;amp;choice);&lt;br /&gt; switch (choice) {&lt;br /&gt;     case 1: &lt;br /&gt;  printf ("Enter data item value to add  ");&lt;br /&gt;  scanf ("%d", &amp;amp;data);&lt;br /&gt;  listpointer = AddItem (listpointer, data);&lt;br /&gt;  break;&lt;br /&gt;     case 2: &lt;br /&gt;  if (listpointer == NULL)&lt;br /&gt;      printf ("Queue empty!\n");&lt;br /&gt;  else&lt;br /&gt;      listpointer = RemoveItem (listpointer);&lt;br /&gt;  break;&lt;br /&gt;     case 3: &lt;br /&gt;  PrintQueue (listpointer);&lt;br /&gt;  break;&lt;br /&gt;&lt;br /&gt;     case 4: &lt;br /&gt;  break;&lt;br /&gt;&lt;br /&gt;     default: &lt;br /&gt;  printf ("Invalid menu choice - try again\n");&lt;br /&gt;  break;&lt;br /&gt; }&lt;br /&gt;    } while (choice != 4);&lt;br /&gt;    ClearQueue (listpointer);&lt;br /&gt;}    /* main */&lt;br /&gt;&lt;br /&gt;void Menu (int *choice) {&lt;br /&gt;&lt;br /&gt;    char    local;&lt;br /&gt;&lt;br /&gt;    printf ("\nEnter\t1 to add item,\n\t2 to remove item\n\&lt;br /&gt;\t3 to print queue\n\t4 to quit\n");&lt;br /&gt;    do {&lt;br /&gt; local = getchar ();&lt;br /&gt; if ((isdigit (local) == FALSE) &amp;amp;&amp;amp; (local != '\n')) {&lt;br /&gt;     printf ("\nyou must enter an integer.\n");&lt;br /&gt;     printf ("Enter 1 to add, 2 to remove, 3 to print, 4 to quit\n");&lt;br /&gt; }&lt;br /&gt;    } while (isdigit ((unsigned char) local) == FALSE);&lt;br /&gt;    *choice = (int) local - '0';&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;listelement * AddItem (listelement * listpointer, int data) {&lt;br /&gt;&lt;br /&gt;    listelement * lp = listpointer;&lt;br /&gt;&lt;br /&gt;    if (listpointer != NULL) {&lt;br /&gt; while (listpointer -&amp;gt; link != NULL)&lt;br /&gt;     listpointer = listpointer -&amp;gt; link;&lt;br /&gt; listpointer -&amp;gt; link = (struct listelement  *) malloc (sizeof (listelement));&lt;br /&gt; listpointer = listpointer -&amp;gt; link;&lt;br /&gt; listpointer -&amp;gt; link = NULL;&lt;br /&gt; listpointer -&amp;gt; dataitem = data;&lt;br /&gt; return lp;&lt;br /&gt;    }&lt;br /&gt;    else {&lt;br /&gt; listpointer = (struct listelement  *) malloc (sizeof (listelement));&lt;br /&gt; listpointer -&amp;gt; link = NULL;&lt;br /&gt; listpointer -&amp;gt; dataitem = data;&lt;br /&gt; return listpointer;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;listelement * RemoveItem (listelement * listpointer) {&lt;br /&gt;&lt;br /&gt;    listelement * tempp;&lt;br /&gt;    printf ("Element removed is %d\n", listpointer -&amp;gt; dataitem);&lt;br /&gt;    tempp = listpointer -&amp;gt; link;&lt;br /&gt;    free (listpointer);&lt;br /&gt;    return tempp;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void PrintQueue (listelement * listpointer) {&lt;br /&gt;&lt;br /&gt;    if (listpointer == NULL)&lt;br /&gt; printf ("queue is empty!\n");&lt;br /&gt;    else&lt;br /&gt; while (listpointer != NULL) {&lt;br /&gt;     printf ("%d\t", listpointer -&amp;gt; dataitem);&lt;br /&gt;     listpointer = listpointer -&amp;gt; link;&lt;br /&gt; }&lt;br /&gt;    printf ("\n");&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;void ClearQueue (listelement * listpointer) {&lt;br /&gt;&lt;br /&gt;    while (listpointer != NULL) {&lt;br /&gt; listpointer = RemoveItem (listpointer);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/stdio.h&gt;&lt;/pre&gt;&lt;h1&gt;&lt;a href="" name="SECTION001150000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12456&lt;/b&gt;&lt;br /&gt;Write a program that reads a number that says how many &lt;u&gt;integer&lt;/u&gt; numbers are to be stored in an array, creates an array to fit the &lt;u&gt;exact&lt;/u&gt; size of the data and then reads in that many numbers into the array.&lt;br /&gt;&lt;b&gt;Exercise 12457&lt;/b&gt;&lt;br /&gt;Write a program to implement the linked list as described in the notes above.&lt;br /&gt;&lt;b&gt;Exercise 12458&lt;/b&gt;&lt;br /&gt;Write a program to sort a sequence of numbers using a binary tree (Using Pointers). A binary tree is a tree structure with only two (possible) branches from each node (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node11.html#fig:bintree"&gt;10.1&lt;/a&gt;). Each branch then represents a false or true decision. To sort numbers simply assign the left branch to take numbers less than the node number and the right branch any other number (greater than or equal to). To obtain a sorted list simply search the tree in  a depth first fashion. &lt;br /&gt;&lt;a href="" name="fig:bintree"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/btree.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node11.html#fig:bintree"&gt;10.1&lt;/a&gt; Example of a binary tree sort&lt;/b&gt; Your program should:  Create a binary tree structure. Create routines for loading the tree appropriately.  Read in integer numbers terminated by a zero.  Sort numbers into numeric ascending order.  Print out the resulting ordered values, printing ten numbers per line  as far as possible. &lt;br /&gt;Typical output should be &lt;br /&gt;&lt;pre&gt;The sorted values are:&lt;br /&gt;     2  4  6  6  7  9 10 11 11 11&lt;br /&gt;    15 16 17 18 20 20 21 21 23 24&lt;br /&gt;    27 28 29 30&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-1214604571664437318?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/1214604571664437318/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/dynamic-memory-allocation-and-dynamic.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/1214604571664437318'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/1214604571664437318'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/dynamic-memory-allocation-and-dynamic.html' title='Dynamic Memory Allocation and Dynamic Structures'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-6233595133926433789</id><published>2009-01-14T08:57:00.001-08:00</published><updated>2009-01-14T08:57:52.165-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Pointers'/><title type='text'>Pointers</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION001000000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="ch:pointers"&gt;&amp;nbsp;&lt;/a&gt; Pointer are a fundamental part of C. If you cannot use pointers properly then you have basically lost all the power and flexibility that C allows. The secret to C is in its use of pointers.  C uses &lt;em&gt;pointers&lt;/em&gt; &lt;u&gt;a lot&lt;/u&gt;. &lt;b&gt;Why?&lt;/b&gt;: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; It is the only way to express some computations. &lt;/li&gt;&lt;li&gt; It produces compact and efficient code. &lt;/li&gt;&lt;li&gt; It provides a very powerful tool. &lt;/li&gt;&lt;/ul&gt;C uses pointers explicitly with: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; Arrays, &lt;/li&gt;&lt;li&gt; Structures, &lt;/li&gt;&lt;li&gt; Functions. &lt;/li&gt;&lt;/ul&gt;&lt;b&gt;NOTE:&lt;/b&gt; Pointers are perhaps the most difficult part of C to understand. C's implementation is slightly different &lt;u&gt;DIFFERENT&lt;/u&gt; from other languages. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001010000000000000000"&gt; What is a Pointer?&lt;/a&gt; &lt;/h1&gt;A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type. &lt;br /&gt;The &lt;em&gt;unary&lt;/em&gt; or &lt;em&gt;monadic&lt;/em&gt; operator &lt;b&gt;&amp;amp;&lt;/b&gt; gives the ``address of a variable''. &lt;br /&gt;The &lt;em&gt;indirection&lt;/em&gt; or dereference operator &lt;b&gt;*&lt;/b&gt; gives the ``contents of an object &lt;em&gt;pointed to&lt;/em&gt; by a pointer''. &lt;br /&gt;To declare a pointer to a variable do: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;int *pointer;&lt;/tt&gt; &lt;br /&gt;&lt;b&gt;NOTE:&lt;/b&gt; We must associate a pointer to a particular type: You can't assign the address of a &lt;b&gt;short int&lt;/b&gt; to a &lt;b&gt;long int&lt;/b&gt;, for instance. &lt;br /&gt;Consider the effect of the following code: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int x = 1, y = 2;&lt;br /&gt;   int *ip;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   ip = &amp;amp;x;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;y = *ip;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;x = ip;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;   *ip = 3;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;It is worth considering what is going on at the &lt;em&gt;machine level&lt;/em&gt; in memory to fully understand how pointer work. Consider Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:point"&gt;9.1&lt;/a&gt;. Assume for the sake of this discussion that variable &lt;tt&gt;x resides at memory location 100, &lt;tt&gt;y&lt;/tt&gt; at 200 and &lt;tt&gt;ip&lt;/tt&gt; at 1000. &lt;b&gt;Note&lt;/b&gt; A pointer is a variable and thus its values need to be stored somewhere. It is the nature of the pointers value that is &lt;em&gt;new&lt;/em&gt;. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;a href="" name="fig:point"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/point.gif" /&gt; &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:point"&gt;9.1&lt;/a&gt; Pointer, Variables and Memory&lt;/b&gt; Now the assignments &lt;tt&gt;x = 1&lt;/tt&gt; and &lt;tt&gt;y = 2&lt;/tt&gt; obviously load these values into the variables. &lt;tt&gt;ip&lt;/tt&gt; is declared to be a &lt;em&gt;pointer to an integer&lt;/em&gt; and is assigned to the address of &lt;tt&gt;x&lt;/tt&gt; (&lt;tt&gt;&amp;amp;x&lt;/tt&gt;). So ip gets loaded with the value 100. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Next &lt;tt&gt;y&lt;/tt&gt; gets assigned to the &lt;em&gt;contents of&lt;/em&gt; &lt;tt&gt;ip&lt;/tt&gt;. In this example &lt;tt&gt;ip&lt;/tt&gt; currently &lt;em&gt;points&lt;/em&gt; to memory location 100 -- the location of &lt;tt&gt; x&lt;/tt&gt;. So &lt;tt&gt;y&lt;/tt&gt; gets assigned to the values of &lt;tt&gt;x&lt;/tt&gt; -- which is 1. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;We have already seen that C is not too fussy about assigning values of different type. Thus it is perfectly &lt;b&gt;legal&lt;/b&gt; (although not all that common) to assign the current value of &lt;tt&gt;ip&lt;/tt&gt; to &lt;tt&gt;x&lt;/tt&gt;. The value of &lt;tt&gt;ip&lt;/tt&gt; at this instant is 100. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Finally we can assign a value to the contents of a pointer (&lt;tt&gt;*ip&lt;/tt&gt;). &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;b&gt;IMPORTANT&lt;/b&gt;: When a pointer is declared it does not point anywhere. You must set it to point somewhere before you use it. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;So ... &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int *ip;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   *ip = 100;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;will generate an error (program crash!!). &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;The correct use is: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int *ip;&lt;br /&gt;   int x;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   ip = &amp;amp;x;&lt;br /&gt;   *ip = 100;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;tt&gt;We can do integer arithmetic on a pointer: &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;float *flp, *flq;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   *flp = *flp + 10;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   ++*flp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   (*flp)++;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   flq = flp;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;b&gt;NOTE&lt;/b&gt;: A pointer to any variable type is an address in memory -- which is an integer address. A pointer is &lt;u&gt;definitely NOT&lt;/u&gt; an integer. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;The reason we associate a pointer to a data type is so that it knows how many bytes the data is stored in. When we increment a pointer we increase the pointer by one ``block'' memory. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;So for a character pointer  ++ch_ptr adds 1 byte to the address. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;For an integer or float ++ip or ++flp adds 4 bytes to the address. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;Consider a float variable (&lt;tt&gt;fl) and a pointer to a float (&lt;tt&gt;flp&lt;/tt&gt;) as shown in Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:float"&gt;9.2&lt;/a&gt;. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;a href="" name="fig:float"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/float.gif" /&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:float"&gt;9.2&lt;/a&gt; Pointer Arithmetic&lt;/b&gt; Assume that &lt;tt&gt;flp&lt;/tt&gt; points to &lt;tt&gt;fl&lt;/tt&gt; then if we increment the pointer (&lt;tt&gt; ++flp&lt;/tt&gt;) it moves to the position shown 4 bytes on. If on the other hand we added 2 to the pointer then it moves 2 &lt;b&gt;float positions&lt;/b&gt; &lt;em&gt;i.e&lt;/em&gt; &lt;u&gt;8 bytes&lt;/u&gt; as shown in the Figure. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001020000000000000000"&gt; Pointer and Functions&lt;/a&gt; &lt;/h1&gt;Let us now examine the close relationship between pointers and C's other major parts. We will start with functions. &lt;br /&gt;When C passes arguments to functions it passes them &lt;u&gt;by value&lt;/u&gt;. &lt;br /&gt;There are many cases when we may want to alter a passed argument in the function and receive the new value back once to function has finished. Other languages do this (&lt;em&gt;e.g.&lt;/em&gt; &lt;tt&gt;var&lt;/tt&gt; parameters in PASCAL). C uses pointers explicitly to do this. Other languages mask the fact that pointers also underpin the implementation of this. &lt;br /&gt;The best way to study this is to look at an example where we must be able to receive changed parameters. &lt;br /&gt;Let us try and write a function to swap variables around? &lt;br /&gt;The usual function &lt;em&gt;call&lt;/em&gt;: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;swap(a, b)&lt;/tt&gt;&amp;nbsp;&amp;nbsp; WON'T WORK. &lt;br /&gt;&lt;br /&gt;Pointers provide the solution: &lt;em&gt;Pass the address of the variables to the functions and access address of function.&lt;/em&gt; &lt;br /&gt;&lt;br /&gt;Thus our function call in our program would look like this: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;swap(&amp;amp;a, &amp;amp;b)&lt;/tt&gt;   &lt;br /&gt;&lt;br /&gt;The Code to swap is fairly straightforward: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; void swap(int *px, int *py)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   &amp;nbsp;&amp;nbsp;&amp;nbsp;{ int temp;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     temp = *px;&lt;br /&gt;     /* contents of pointer */ &lt;br /&gt;&amp;nbsp;&lt;br /&gt;     *px = *py;&lt;br /&gt;     *py = temp;&lt;br /&gt;   }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;We can return pointer from functions. A  common example is when passing back structures. &lt;em&gt;e.g.&lt;/em&gt;: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;typedef struct {float x,y,z;} COORD;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   main()&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  &amp;nbsp;&amp;nbsp;{ &amp;nbsp;COORD p1, *coord_fn();&lt;br /&gt;      /* declare fn to return ptr of&lt;br /&gt;       COORD type */ &lt;br /&gt;&amp;nbsp;&lt;br /&gt;       ....&lt;br /&gt;       p1 = *coord_fn(...);&lt;br /&gt;     /* assign contents of address returned */&lt;br /&gt;       ....&lt;br /&gt;     }&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;COORD *coord_fn(...)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  &amp;nbsp;&amp;nbsp;{ &amp;nbsp;COORD p;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;       .....&lt;br /&gt;       p = ....;&lt;br /&gt;       /* assign structure values */ &lt;br /&gt;&amp;nbsp;&lt;br /&gt;       return &amp;amp;p;&lt;br /&gt;       /* return address of p */&lt;br /&gt;     }&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;tt&gt;Here we return a pointer whose contents are immediately &lt;em&gt;unwrapped&lt;/em&gt; into a variable. We must do this straight away as the variable we pointed to was local to a function that has now finished. This means that the address space is free and can be overwritten. It will not have been overwritten straight after the function ha squit though so this is perfectly safe. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001030000000000000000"&gt; Pointers and Arrays&lt;/a&gt; &lt;/h1&gt;Pointers and arrays are very closely linked in C. &lt;br /&gt;&lt;br /&gt;Hint: think of array elements arranged in  consecutive memory locations. &lt;br /&gt;&lt;br /&gt;Consider the following: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int a[10], x;&lt;br /&gt;   int *pa;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   pa = &amp;amp;a[0];  /* pa pointer to address of a[0] */&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   x = *pa;&lt;br /&gt;   /* x = contents of pa (a[0] in this case) */&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;a href="" name="fig:arrays"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/arrays.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:arrays"&gt;9.3&lt;/a&gt; Arrays and Pointers&lt;/b&gt; &lt;br /&gt;To get somewhere in the array (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:arrays"&gt;9.3&lt;/a&gt;) using a pointer we could do: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;pa + i &lt;img align="bottom" alt="$\equiv$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img13.gif" width="17" /&gt; a[i] &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;b&gt;WARNING&lt;/b&gt;: There is no bound checking of arrays and pointers so you can easily go beyond array memory and overwrite other things. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;C however is much more subtle in its link between arrays and pointers. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;For example we can just type &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;pa = a;&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;instead of &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;pa = &amp;amp;a[0]&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;and &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;a[i]&lt;/tt&gt; can be written as &lt;tt&gt;*(a + i)&lt;/tt&gt;. &lt;br /&gt;&lt;em&gt;i.e.&lt;/em&gt; &lt;tt&gt;&amp;amp;a[i] &lt;img align="bottom" alt="$\equiv$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img13.gif" width="17" /&gt; a + i&lt;/tt&gt;. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;We also express pointer addressing like this: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;pa[i] &lt;img align="bottom" alt="$\equiv$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img13.gif" width="17" /&gt; *(pa + i)&lt;/tt&gt;.   &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;However pointers and arrays are different: &lt;/tt&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;tt&gt; A pointer is a variable. We can do &lt;br /&gt;&lt;tt&gt;pa = a&lt;/tt&gt; and &lt;tt&gt;pa++&lt;/tt&gt;. &lt;/tt&gt;&lt;/li&gt;&lt;li&gt;&lt;tt&gt; An Array &lt;u&gt;is not&lt;/u&gt; a variable. &lt;tt&gt;a = pa&lt;/tt&gt; and &lt;tt&gt;a++&lt;/tt&gt; ARE ILLEGAL. &lt;/tt&gt;&lt;/li&gt;&lt;/ul&gt;&lt;tt&gt;This stuff is very important. Make sure you understand it. We will see a lot more of this.   &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;We can now understand how arrays are passed to functions. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;When an array is passed to a function what is actually passed is its initial elements location in memory. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;So: &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;strlen(s) &lt;img align="bottom" alt="$\equiv$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img13.gif" width="17" /&gt; strlen(&amp;amp;s[0])&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;This is why we declare the function:  &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;int strlen(char s[]);&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;An equivalent declaration is :&amp;nbsp;&lt;tt&gt;int strlen(char *s);&lt;/tt&gt; &lt;br /&gt;since &lt;tt&gt;char s[] &lt;img align="bottom" alt="$\equiv$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img13.gif" width="17" /&gt; char *s&lt;/tt&gt;. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;strlen()&lt;/tt&gt; is a &lt;em&gt;standard library&lt;/em&gt; function (Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node19.html#ch:string"&gt;18&lt;/a&gt;) that returns the length of a string. Let's look at how we may write a function: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;int strlen(char *s)&lt;br /&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;char *p = s;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     while (*p != `&lt;img align="middle" alt="${\backslash}0$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img14.gif" width="21" /&gt;);&lt;br /&gt;    &amp;nbsp;&amp;nbsp;&amp;nbsp;p++;&lt;br /&gt;     return p-s;&lt;br /&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Now lets write a function to copy a string to another string. &lt;tt&gt;strcpy() is a standard library function that does this. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;void strcpy(char *s, char *t)&lt;br /&gt;  &amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp; while ( (*s++ = *t++) != `&lt;img align="middle" alt="${\backslash}0$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img14.gif" width="21" /&gt;);}&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;This uses pointers and assignment by value.  &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;Very Neat!! &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;b&gt;NOTE:&lt;/b&gt; Uses of Null statements with &lt;tt&gt;while. &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001040000000000000000"&gt; Arrays of Pointers&lt;/a&gt; &lt;/h1&gt;We can have arrays of pointers since pointers are variables. &lt;br /&gt;&lt;br /&gt;&lt;u&gt;Example use&lt;/u&gt;: &lt;br /&gt;&lt;br /&gt;&lt;em&gt;Sort lines of text of different length&lt;/em&gt;. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;NOTE:&lt;/b&gt; Text can't be moved or compared in a single operation. &lt;br /&gt;&lt;br /&gt;&lt;em&gt;Arrays of Pointers&lt;/em&gt; are a data representation that will cope efficiently and conveniently with variable length text lines.  &lt;br /&gt;&lt;br /&gt;How can we do this?: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; Store lines end-to-end in one big &lt;tt&gt;char&lt;/tt&gt; array (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:strings"&gt;9.4&lt;/a&gt;). &lt;tt&gt; &lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;n&lt;/tt&gt; will delimit lines. &lt;/li&gt;&lt;li&gt; Store pointers in a different array where each pointer points to 1st char of each new line. &lt;/li&gt;&lt;li&gt; Compare two lines using &lt;tt&gt;strcmp()&lt;/tt&gt; standard library function. &lt;/li&gt;&lt;li&gt; If 2 lines are out of order -- swap pointer in pointer array (&lt;u&gt;not text&lt;/u&gt;). &lt;/li&gt;&lt;/ul&gt;&lt;a href="" name="fig:strings"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/strings.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:strings"&gt;9.4&lt;/a&gt; Arrays of Pointers (String Sorting Example)&lt;/b&gt; &lt;br /&gt;This eliminates: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; complicated storage management. &lt;/li&gt;&lt;li&gt; high overheads of moving lines. &lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;&lt;a href="" name="SECTION001050000000000000000"&gt; Multidimensional arrays and pointers&lt;/a&gt; &lt;/h1&gt;We should think of multidimensional arrays in a different way in C: &lt;br /&gt;&lt;br /&gt;&lt;em&gt;A 2D array is really a 1D array, each of whose elements is itself an array&lt;/em&gt; &lt;br /&gt;&lt;br /&gt;Hence &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;a[n][m]&lt;/tt&gt; notation. &lt;br /&gt;&lt;br /&gt;Array elements are stored row by row. &lt;br /&gt;&lt;br /&gt;When we pass a 2D array to a function we must specify the number of columns -- the number of rows is irrelevant.  &lt;br /&gt;&lt;br /&gt;The reason for this is pointers again. C needs to know how many columns in order that it can jump from row to row in memory.   &lt;br /&gt;&lt;br /&gt;Consider&lt;tt&gt;int a[5][35]&lt;/tt&gt; to be passed in a function: &lt;br /&gt;&lt;br /&gt;We can do: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;f(int a[][35]) {.....}&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;or even: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;f(int (*a)[35]) {.....}&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;We need parenthesis (*a) since [] have a higher precedence than * &lt;br /&gt;&lt;br /&gt;So: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;int (*a)[35]&lt;/tt&gt;; declares a pointer to an array of 35 &lt;tt&gt;int&lt;/tt&gt;s. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;int *a[35];&lt;/tt&gt; declares an array of 35 pointers to &lt;tt&gt;int&lt;/tt&gt;s.  &lt;br /&gt;&lt;br /&gt;Now lets look at the (subtle) difference between pointers and arrays. Strings are a common application of this. &lt;br /&gt;&lt;br /&gt;Consider: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;char *name[10];&lt;/tt&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;char Aname[10][20];&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;We can legally do &lt;tt&gt;name[3][4]&lt;/tt&gt; and &lt;tt&gt;Aname[3][4]&lt;/tt&gt; in C. &lt;br /&gt;However &lt;br /&gt;&lt;ul&gt;&lt;li&gt; &lt;tt&gt;Aname&lt;/tt&gt; is a &lt;u&gt;true&lt;/u&gt; 200 element 2D char array. &lt;/li&gt;&lt;li&gt; access elements via &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;em&gt;20*row + col + base_address&lt;/em&gt; &lt;br /&gt;in memory. &lt;/li&gt;&lt;li&gt; &lt;tt&gt;name&lt;/tt&gt; has 10 pointer elements. &lt;/li&gt;&lt;/ul&gt;&lt;b&gt;NOTE:&lt;/b&gt; If each pointer in &lt;tt&gt;name&lt;/tt&gt; is set to point to a 20 element array then &lt;u&gt;and only then&lt;/u&gt; will 200 chars be set aside (+ 10 elements). &lt;br /&gt;&lt;br /&gt;The advantage of the latter is that each pointer can point to arrays be of different length.   &lt;br /&gt;&lt;br /&gt;Consider: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;char *name[] = { ``no month'', ``jan'',&lt;br /&gt;    ``feb'', ... };&lt;br /&gt;   char Aname[][15] = { ``no month'', ``jan'',&lt;br /&gt;    ``feb'', ... };&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;a href="" name="fig:arrptr"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/arrptr.gif" /&gt; &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:arrptr"&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/cross_ref_motif.gif" /&gt;&lt;/a&gt; 2D Arrays and Arrays of Pointers&lt;/b&gt; &lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001060000000000000000"&gt; Static Initialisation of Pointer Arrays&lt;/a&gt; &lt;/h1&gt;Initialisation of arrays of pointers is an ideal application for an internal static array. &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;some_fn()&lt;br /&gt;   &amp;nbsp;{ static char *months = { ``no month'',&lt;br /&gt;       ``jan'', ``feb'',&lt;br /&gt;       ...};&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;tt&gt;static reserves a private permanent bit of memory. &lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001070000000000000000"&gt; Pointers and Structures&lt;/a&gt; &lt;/h1&gt;These are fairly straight forward and are easily defined. Consider the following: &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;struct COORD {float x,y,z;} pt;&lt;br /&gt;   struct COORD *pt_ptr;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;pt_ptr = &amp;amp;pt; /* assigns pointer to pt */&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;the &lt;img align="middle" alt="$-\!\gt$" border="0" height="28" src="http://www.cs.cf.ac.uk/Dave/C/img15.gif" width="34" /&gt; operator lets us access a member of the structure pointed to by a pointer.&lt;em&gt;i.e.&lt;/em&gt;: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;pt_ptr&lt;img align="middle" alt="$-\!\gt$" border="0" height="28" src="http://www.cs.cf.ac.uk/Dave/C/img15.gif" width="34" /&gt;x = 1.0;&lt;/tt&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;pt_ptr&lt;img align="middle" alt="$-\!\gt$" border="0" height="28" src="http://www.cs.cf.ac.uk/Dave/C/img15.gif" width="34" /&gt;y = pt_ptr&lt;img align="middle" alt="$-\!\gt$" border="0" height="28" src="http://www.cs.cf.ac.uk/Dave/C/img15.gif" width="34" /&gt;y - 3.0;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Example: Linked Lists &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;typedef struct { &amp;nbsp;int value;&lt;br /&gt;       ELEMENT *next;&lt;br /&gt;     } ELEMENT; &lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;ELEMENT n1, n2;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;n1.next = &amp;amp;n2;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;a href="" name="fig:link"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/link.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#fig:link"&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/cross_ref_motif.gif" /&gt;&lt;/a&gt; Linking Two Nodes&lt;/b&gt; &lt;b&gt;NOTE:&lt;/b&gt; We can only declare &lt;tt&gt;next as a pointer to &lt;tt&gt;ELEMENT&lt;/tt&gt;. We cannot have a element of the variable type as this would set up a &lt;em&gt; recursive&lt;/em&gt; definition which is &lt;b&gt;NOT ALLOWED&lt;/b&gt;. We are allowed to set a pointer reference since 4 bytes are set aside for any pointer. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;The above code links a node &lt;tt&gt;n1&lt;/tt&gt; to &lt;tt&gt;n2&lt;/tt&gt; (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#figure:link"&gt;9.6&lt;/a&gt;) we will look at this matter further in the next Chapter. &lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001080000000000000000"&gt; Common Pointer Pitfalls&lt;/a&gt; &lt;/h1&gt;Here we will highlight two common mistakes made with pointers. &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION001081000000000000000"&gt; Not assigning a pointer to memory address before using it&lt;/a&gt; &lt;/h2&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int *x;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   *x = 100;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;we need a physical location say: int y;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   x = &amp;amp;y;&lt;br /&gt;   *x = 100;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;This may be hard to spot. &lt;b&gt;NO COMPILER ERROR&lt;/b&gt;. Also x could some random address at initialisation. &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION001082000000000000000"&gt; Illegal indirection&lt;/a&gt; &lt;/h2&gt;Suppose we have a function &lt;tt&gt;malloc()&lt;/tt&gt; which tries to allocate memory dynamically (at run time) and returns a pointer to block of memory requested if successful or a &lt;tt&gt;NULL&lt;/tt&gt; pointer &lt;br /&gt;otherwise. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;char *malloc()&lt;/tt&gt; -- a standard library function (see later). &lt;br /&gt;&lt;br /&gt;Let us have a pointer: &lt;tt&gt;char *p&lt;/tt&gt;; &lt;br /&gt;&lt;br /&gt;Consider: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;*p = (char *) malloc(100);&lt;/tt&gt; &amp;nbsp;&amp;nbsp; /* request 100 bytes of memory */ &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;*p = `y';&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;There is mistake above. What is it? &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;No * in &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;&lt;u&gt;*p&lt;/u&gt; = (char *) malloc(100);&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;Malloc returns a pointer. Also &lt;tt&gt;p&lt;/tt&gt; does not point to any address. &lt;br /&gt;&lt;br /&gt;The correct code should be: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;p = (char *) malloc(100);&lt;/tt&gt;  &lt;br /&gt;&lt;br /&gt;If code rectified one problem is if no memory is available and &lt;tt&gt;p&lt;/tt&gt; is &lt;tt&gt; NULL&lt;/tt&gt;. Therefore we can't do:  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;*p = `y';&lt;/tt&gt;.   &lt;br /&gt;&lt;br /&gt;A good C program would check for this: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;p = (char *) malloc(100);&lt;br /&gt;   if ( p == NULL)&lt;br /&gt;   &amp;nbsp;&amp;nbsp;{&amp;nbsp;printf(``Error: Out of Memory &lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;n'');&lt;br /&gt;       exit(1);&lt;br /&gt;     }&lt;br /&gt;   *p = `y';&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION001090000000000000000"&gt; Exercise&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12453&lt;/b&gt;&lt;br /&gt;Write a C program to read through an array of any type using pointers. Write a C program  to scan through this array to find a particular value. &lt;br /&gt;&lt;b&gt;Exercise 12454&lt;/b&gt;&lt;br /&gt;Write a program to find the number of times that a given word(i.e. a short string) occurs in a sentence (i.e. a long string!). &lt;br /&gt;Read data from standard input.  The first line is a single word,  which is followed by general text on the second line.  Read both up to a newline character, and insert a terminating null before processing. &lt;br /&gt;Typical output should be: &lt;br /&gt;&lt;pre&gt;The word is "the".&lt;br /&gt;        The sentence is "the cat sat on the mat".&lt;br /&gt;       The word occurs 2 times.&lt;br /&gt;&lt;/pre&gt;&lt;b&gt;Exercise 12455&lt;/b&gt;&lt;br /&gt;Write a program that takes three variable (a, b, b) in as separate parameters and rotates the values stored so that value a goes to be, b, to c and c to a.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-6233595133926433789?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/6233595133926433789/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/pointers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6233595133926433789'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6233595133926433789'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/pointers.html' title='Pointers'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-7934076441130730564</id><published>2009-01-14T08:56:00.001-08:00</published><updated>2009-01-14T08:56:37.204-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Further Data Types'/><title type='text'>Further Data Types</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION00900000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;This Chapter discusses how more advanced data types and structures can be created and used in a C program. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00910000000000000000"&gt; Structures&lt;/a&gt; &lt;/h1&gt;Structures in C are similar to records in Pascal. For example: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct gun&lt;br /&gt;    {&lt;br /&gt;    char name[50];&lt;br /&gt;    int magazinesize;&lt;br /&gt;    float calibre;&lt;br /&gt;    };&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  struct gun arnies;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;defines a new structure &lt;tt&gt;gun and makes &lt;tt&gt;arnies&lt;/tt&gt; an instance of it. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;b&gt;NOTE:&lt;/b&gt; that &lt;tt&gt;gun&lt;/tt&gt; is a &lt;em&gt;tag&lt;/em&gt; for the structure that serves as shorthand for future declarations. We now only need to say &lt;tt&gt;struct gun&lt;/tt&gt; and the body of the structure is implied as we do to make the &lt;tt&gt;arnies&lt;/tt&gt; variable. The tag is &lt;em&gt;optional&lt;/em&gt;. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Variables can also be declared between the &lt;tt&gt;}&lt;/tt&gt; and &lt;tt&gt;;&lt;/tt&gt; of a struct declaration, &lt;em&gt;i.e.&lt;/em&gt;: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;struct gun&lt;br /&gt;    {&lt;br /&gt;    char name[50];&lt;br /&gt;    int magazinesize;&lt;br /&gt;    float calibre;&lt;br /&gt;    } arnies;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;struct's can be pre-initialised at declaration: &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &amp;nbsp;&amp;nbsp;struct gun arnies={"Uzi",30,7}; &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;which gives &lt;tt&gt;arnie a 7mm. Uzi with 30 rounds of ammunition. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;To access a member (or field) of a &lt;tt&gt;struct&lt;/tt&gt;, C provides the &lt;tt&gt;.&lt;/tt&gt; operator. For example, to give &lt;tt&gt;arnie&lt;/tt&gt; more rounds of ammunition: &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;arnies.magazineSize=100;&lt;/tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00911000000000000000"&gt; Defining New Data Types&lt;/a&gt; &lt;/h2&gt;&lt;tt&gt;typedef&lt;/tt&gt; can also be used with structures. The following creates a new type &lt;tt&gt;agun&lt;/tt&gt; which is of type &lt;tt&gt;struct gun&lt;/tt&gt; and can be initialised as usual: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;typedef struct gun&lt;br /&gt;    {&lt;br /&gt;    char name[50];&lt;br /&gt;    int magazinesize;&lt;br /&gt;    float calibre;&lt;br /&gt;    } agun;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   agun arnies={"Uzi",30,7};&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;Here &lt;tt&gt;gun still acts as a &lt;em&gt;tag&lt;/em&gt; to the &lt;tt&gt;struct&lt;/tt&gt; and is optional. Indeed since we have defined a new data type it is not really of much use, &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;agun&lt;/tt&gt; is the new data type. &lt;tt&gt;arnies&lt;/tt&gt; is a variable of type &lt;tt&gt;agun&lt;/tt&gt; which is a structure. &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;C also allows arrays of structures: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;typedef struct gun&lt;br /&gt;    {&lt;br /&gt;    char name[50];&lt;br /&gt;    int magazinesize;&lt;br /&gt;    float calibre;&lt;br /&gt;    } agun;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  agun arniesguns[1000];&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;This gives &lt;tt&gt;arniesguns a 1000 guns. This may be used in the following way: &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;arniesguns[50].calibre=100;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;gives Arnie's gun number 50 a calibre of 100mm, and: &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;itscalibre=arniesguns[0].calibre;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;assigns the calibre of Arnie's first gun to &lt;tt&gt;itscalibre. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;h1&gt;&lt;a href="" name="SECTION00920000000000000000"&gt; Unions&lt;/a&gt; &lt;/h1&gt;A union is a variable which may hold (at different times) objects of different sizes and types. C uses the &lt;tt&gt;union&lt;/tt&gt; statement to create unions, for example: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;union number&lt;br /&gt;     {&lt;br /&gt;     short shortnumber;&lt;br /&gt;     long longnumber;&lt;br /&gt;     double floatnumber;&lt;br /&gt;     } anumber&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;defines a union called &lt;tt&gt;number and an instance of it called &lt;tt&gt; anumber&lt;/tt&gt;. &lt;tt&gt;number&lt;/tt&gt; is a union &lt;em&gt;tag&lt;/em&gt; and acts in the same way as a tag for a structure. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Members can be accessed in the following way: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;printf("%ld&lt;img align="middle" alt="$\setminus$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img10.gif" width="12" /&gt;n",anumber.longnumber);&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;This clearly displays the value of &lt;tt&gt;longnumber. &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;When the C compiler is allocating memory for unions it will  always reserve enough room for the largest member (in the above example this is 8 bytes for the &lt;tt&gt;double&lt;/tt&gt;). &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;In order that the program can keep track of the type of union variable being used at a given time it is common to have a structure (with union embedded in it) and a variable which flags the union type:   &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;An example is: &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;typedef struct&lt;br /&gt;     {&amp;nbsp;int maxpassengers;&lt;br /&gt;     } jet;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  typedef struct&lt;br /&gt;     {&amp;nbsp;int liftcapacity;&lt;br /&gt;     } helicopter;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  typedef struct&lt;br /&gt;     {&amp;nbsp;int maxpayload;&lt;br /&gt;     } cargoplane;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   typedef    union&lt;br /&gt;     {&amp;nbsp;jet jetu;&lt;br /&gt;       helicopter helicopteru;&lt;br /&gt;       cargoplane cargoplaneu;&lt;br /&gt;     } aircraft;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  typedef    struct&lt;br /&gt;     {&amp;nbsp;aircrafttype kind;&lt;br /&gt;       int speed;&lt;br /&gt;       aircraft description;&lt;br /&gt;     } an_aircraft;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;This example defines a base union &lt;tt&gt;aircraft which may either be &lt;tt&gt; jet&lt;/tt&gt;, &lt;tt&gt;helicopter&lt;/tt&gt;, or &lt;br /&gt;&lt;tt&gt;cargoplane&lt;/tt&gt;. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;In the &lt;tt&gt;an_aircraft&lt;/tt&gt; structure there is a &lt;tt&gt;kind&lt;/tt&gt; member which indicates which structure is being held at the time. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00930000000000000000"&gt; Coercion or Type-Casting&lt;/a&gt; &lt;/h1&gt;C is one of the few languages to allow &lt;em&gt;coercion&lt;/em&gt;, that is forcing one variable of one type to be another type. C allows this using the cast operator &lt;tt&gt;()&lt;/tt&gt;. So: &lt;br /&gt;&lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int integernumber;&lt;br /&gt;   float floatnumber=9.87;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     integernumber=(int)floatnumber;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;assigns 9 (the fractional part is thrown away) to &lt;tt&gt;integernumber. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;And: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int integernumber=10;&lt;br /&gt;   float floatnumber;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     floatnumber=(float)integernumber;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt; &lt;tt&gt;assigns 10.0  to &lt;tt&gt;floatnumber. &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;Coercion can be used with any of the simple data types including &lt;tt&gt;char&lt;/tt&gt;, so: &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;int integernumber;&lt;br /&gt;   char letter='A';&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     integernumber=(int)letter;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;assigns 65 (the ASCII code for `A') to &lt;tt&gt;integernumber. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;Some typecasting is done automatically -- this is mainly with integer compatibility. &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;A good rule to follow is: &lt;b&gt;If in doubt cast&lt;/b&gt;. &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;Another use is the make sure division behaves as requested: If we have two integers &lt;tt&gt;internumber&lt;/tt&gt; and &lt;tt&gt;anotherint&lt;/tt&gt; and we want the answer to be a float then : &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;e.g.&lt;br /&gt;&amp;nbsp;&lt;tt&gt;floatnumber =&lt;/tt&gt;&lt;br /&gt;  &amp;nbsp;&lt;tt&gt;(float) internumber / (float) anotherint;&lt;/tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt; &lt;tt&gt;&lt;tt&gt;&lt;tt&gt;ensures floating point division. &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00940000000000000000"&gt; Enumerated Types&lt;/a&gt; &lt;/h1&gt;Enumerated types contain a list of constants that can be addressed in integer values. &lt;br /&gt;&lt;br /&gt;We can declare types and variables as follows. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;enum days {mon, tues, ..., sun} week;&lt;/tt&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;enum days week1, week2;&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;&lt;b&gt;NOTE:&lt;/b&gt; As with arrays first enumerated name has index value 0. So &lt;tt&gt; mon&lt;/tt&gt; has value 0, &lt;tt&gt;tues&lt;/tt&gt; 1, and so on. &lt;br /&gt;&lt;br /&gt;&lt;tt&gt;week1&lt;/tt&gt; and &lt;tt&gt;week2&lt;/tt&gt; are variables. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;We can define other values: &lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;enum escapes&lt;/tt&gt; { &lt;tt&gt;bell = `&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;a',&lt;/tt&gt;&lt;br /&gt;   &lt;tt&gt;backspace = `&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;b',  tab = `&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;t',&lt;/tt&gt;&lt;br /&gt;   &lt;tt&gt;newline = `&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;n', vtab = `&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;v',&lt;/tt&gt;&lt;br /&gt;   &lt;tt&gt;return = `&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;r'};&lt;/tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;We can also override the 0 start value: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;enum months {jan = 1, feb, mar, ......, dec};&lt;/tt&gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Here it is implied that feb = 2 &lt;em&gt;etc.&lt;/em&gt; &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00950000000000000000"&gt; Static Variables&lt;/a&gt; &lt;/h1&gt;A &lt;tt&gt;&lt;b&gt;static&lt;/b&gt;&lt;/tt&gt; variable is &lt;u&gt;local&lt;/u&gt; to particular function. However, it is only initialised once (on the first call to function). &lt;br /&gt;Also the value of the variable on leaving the function remains &lt;b&gt;intact&lt;/b&gt;. On the next call to the function the the &lt;tt&gt;static&lt;/tt&gt; variable has the same value as on leaving. &lt;br /&gt;To define a &lt;tt&gt;static&lt;/tt&gt; variable simply prefix the variable declaration with the &lt;tt&gt;static&lt;/tt&gt; keyword. For example: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;void stat(); /* prototype fn */ &lt;br /&gt;&amp;nbsp;&lt;br /&gt;   main()&lt;br /&gt;  &amp;nbsp;&amp;nbsp;{ int i;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     for (i=0;i&amp;lt;5;++&lt;i&gt;i&lt;/i&gt;)&lt;br /&gt;       stat();&lt;br /&gt;  &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;stat()&lt;br /&gt;  &amp;nbsp;&amp;nbsp;{  int auto_var = 0;&lt;br /&gt;     static int static_var = 0;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     printf( ``auto = %d, static = %d &lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;n'',&lt;br /&gt;       auto_var, static_var);&lt;br /&gt;     ++auto_var;&lt;br /&gt;     ++static_var;&lt;br /&gt;  &amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;Output is: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;auto_var = 0, static_var= 0&lt;br /&gt;  auto_var = 0, static_var = 1&lt;br /&gt;  auto_var = 0, static_var = 2&lt;br /&gt;  auto_var = 0, static_var = 3&lt;br /&gt;  auto_var = 0, static_var = 4&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;br /&gt;&lt;/tt&gt; &lt;tt&gt;Clearly the &lt;tt&gt;auto_var variable is created each time. The &lt;tt&gt; static_var&lt;/tt&gt; is created once and remembers its value. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00960000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12386&lt;/b&gt;&lt;br /&gt;Write program using enumerated types which when given today's date will print out tomorrow's date in the for 31st January, for example.&lt;br /&gt;&lt;b&gt;Exercise 12387&lt;/b&gt;&lt;br /&gt;Write a simple database program that will store a persons details such as age, date of birth, address &lt;em&gt;etc.&lt;/em&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-7934076441130730564?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/7934076441130730564/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/further-data-types.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7934076441130730564'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7934076441130730564'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/further-data-types.html' title='Further Data Types'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-4589473804063817321</id><published>2009-01-14T08:54:00.000-08:00</published><updated>2009-01-14T08:55:44.023-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Functions'/><title type='text'>Functions</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION00800000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;C provides functions which are again similar most languages. One difference is that C regards &lt;tt&gt;main()&lt;/tt&gt; as function. Also unlike some languages, such as Pascal, C does not have &lt;em&gt;procedures&lt;/em&gt; -- it uses functions to service both requirements. &lt;br /&gt;Let us remind ourselves of the form of a function: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;i&gt;returntype&lt;/i&gt; &lt;tt&gt;fn_name&lt;/tt&gt;&lt;tt&gt;(&lt;/tt&gt;&lt;sub&gt;1&lt;/sub&gt;&lt;tt&gt;,&lt;/tt&gt; &lt;i&gt;parameterdef&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;tt&gt;,&lt;/tt&gt;&lt;img align="bottom" alt="$\cdots$" border="0" height="15" src="http://www.cs.cf.ac.uk/Dave/C/img11.gif" width="25" /&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;    &lt;br /&gt;&amp;nbsp;&lt;br /&gt;    {&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     &lt;i&gt;localvariables&lt;/i&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     &lt;i&gt;functioncode&lt;/i&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;Let us look at an example to find the average of two integers: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;float findaverage(float a, float b)&lt;br /&gt;    { float average;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&amp;gt;     average=(a+b)/2;&lt;br /&gt;       return(average);&lt;br /&gt;    }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;We would &lt;em&gt;call&lt;/em&gt; the function as follows:&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   main()&lt;br /&gt;    {    float a=5,b=15,result;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;       result=findaverage(a,b);&lt;br /&gt;       printf("average=%f&lt;img align="middle" alt="$\setminus$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img10.gif" width="12" /&gt;n",result);&lt;br /&gt;    }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;b&gt;Note:&lt;/b&gt; The &lt;tt&gt;return statement passes the result back to the main program. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00810000000000000000"&gt; &lt;tt&gt;void&lt;/tt&gt; functions&lt;/a&gt; &lt;/h1&gt;The &lt;tt&gt;void&lt;/tt&gt; function provide a way of emulating PASCAL type procedures. &lt;br /&gt;If you do not want to return a value you must use the return type &lt;tt&gt;void&lt;/tt&gt; and miss out the &lt;tt&gt;return&lt;/tt&gt; statement: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;void squares()&lt;br /&gt;    { int loop;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;       for (loop=1;loop&amp;lt;10;loop++);&lt;br /&gt;         printf("%d&lt;img align="middle" alt="$\setminus$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img10.gif" width="12" /&gt;n",loop*loop);&lt;br /&gt;    }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   main() &lt;br /&gt;&amp;nbsp;&lt;br /&gt;    {    squares();&lt;br /&gt;    }&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;b&gt;NOTE:&lt;/b&gt; We must have () even for no parameters unlike some languages. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00820000000000000000"&gt; Functions and Arrays&lt;/a&gt; &lt;/h1&gt;Single dimensional arrays can be passed to functions as follows:- &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;float findaverage(int size,float list[])&lt;br /&gt;&amp;nbsp;&lt;br /&gt;    { int i;&lt;br /&gt;       float sum=0.0;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;       for (i=0;i&lt;size;i++) _moz-userdefined=""&gt;&lt;br /&gt;         sum+=list[i];&lt;br /&gt;       return(sum/size);&lt;br /&gt;    }&lt;br /&gt;&lt;/size;i++)&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;Here the declaration &lt;tt&gt;&amp;nbsp;float list[] tells C that &lt;tt&gt;list&lt;/tt&gt; is an array of &lt;tt&gt;float&lt;/tt&gt;. &lt;b&gt;Note&lt;/b&gt; we do not specify the dimension of the array when it is a &lt;em&gt;parameter&lt;/em&gt; of a function. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Multi-dimensional arrays can be passed to &lt;br /&gt;functions as follows: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;void printtable(int xsize,int ysize,&lt;br /&gt;    &amp;nbsp;&amp;nbsp;&amp;nbsp;float table[][5])&lt;br /&gt;&amp;nbsp;&lt;br /&gt;    {&amp;nbsp;int x,y;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;       for (x=0;x&lt;xsize;x++) _moz-userdefined=""&gt;&lt;br /&gt;         {&amp;nbsp;for (y=0;y&lt;ysize;y++) _moz-userdefined=""&gt;&lt;br /&gt;           printf("&lt;img align="middle" alt="$\setminus$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img10.gif" width="12" /&gt;t%f",table[x][y]);&lt;br /&gt;         printf("&lt;img align="middle" alt="$\setminus$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img10.gif" width="12" /&gt;n");&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;&lt;/ysize;y++)&gt;&lt;/xsize;x++)&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;Here &lt;tt&gt;float table[][5] tells C that &lt;tt&gt;table&lt;/tt&gt; is an array of dimension N&lt;img align="middle" alt="$\times$" border="0" height="28" src="http://www.cs.cf.ac.uk/Dave/C/img12.gif" width="17" /&gt;5 of &lt;tt&gt;float&lt;/tt&gt;. &lt;b&gt;Note&lt;/b&gt; we must specify the second (and subsequent) dimension of the array &lt;u&gt;BUT&lt;/u&gt; not the first dimension. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00830000000000000000"&gt; Function Prototyping&lt;/a&gt; &lt;/h1&gt;Before you use a function C must have &lt;em&gt;knowledge&lt;/em&gt; about the type it returns and the parameter types the function expects. &lt;br /&gt;The ANSI standard of C introduced a new (better) way of doing this than previous versions of C. (Note: All new versions of C now adhere to the ANSI standard.) &lt;br /&gt;The importance of prototyping is twofold. &lt;br /&gt;&lt;ul&gt;&lt;li&gt; It makes for more structured and therefore easier to read code. &lt;/li&gt;&lt;li&gt; It allows the C compiler to check the &lt;em&gt;syntax&lt;/em&gt; of function calls. &lt;/li&gt;&lt;/ul&gt;How this is done depends on the scope of the function (See Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node35.html#ch:prog"&gt;34&lt;/a&gt;). Basically if a functions has been &lt;u&gt;defined&lt;/u&gt; before it is used (called) then you are ok to merely use the function. &lt;br /&gt;&lt;b&gt;If NOT&lt;/b&gt; then you must &lt;em&gt;declare&lt;/em&gt; the function. The declaration simply states the type the function returns and the type of parameters used by the function. &lt;br /&gt;It is usual (and therefore &lt;b&gt;good&lt;/b&gt;) practice to prototype all functions at the start of the program, although this is not strictly necessary. &lt;br /&gt;To &lt;em&gt;declare&lt;/em&gt; a function prototype simply state the type the function returns, the function name and in brackets list the type of parameters in the order they appear in the function definition. &lt;br /&gt;&lt;br /&gt;&lt;em&gt;e.g.&lt;/em&gt; &lt;br /&gt;&lt;pre&gt;int strlen(char []);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;This states that a function called &lt;tt&gt;strlen&lt;/tt&gt; returns an integer value and accepts a single string as a parameter. &lt;br /&gt;&lt;b&gt;NOTE:&lt;/b&gt; Functions can be prototyped and variables defined on the same line of code. This used to be more popular in pre-ANSI C days since functions are usually prototyped separately at the start of the program. This is still perfectly legal though: order they appear in the function definition. &lt;br /&gt;&lt;br /&gt;&lt;em&gt;e.g.&lt;/em&gt; &lt;br /&gt;&lt;pre&gt;int length, strlen(char []);&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Here &lt;tt&gt;length&lt;/tt&gt; is a variable, &lt;tt&gt;strlen&lt;/tt&gt; the function as before. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00840000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12346&lt;/b&gt;&lt;br /&gt;Write a function ``replace'' which takes a pointer to a string as a parameter, which replaces all spaces in that string by minus signs, and delivers the number of spaces it replaced. &lt;br /&gt;Thus &lt;br /&gt;&lt;pre&gt;char *cat = "The cat sat";&lt;br /&gt;        n = replace( cat );&lt;br /&gt;&lt;/pre&gt;should set &lt;pre&gt;cat to "The-cat-sat"&lt;br /&gt;&lt;/pre&gt;and &lt;pre&gt;n to 2.&lt;br /&gt;&lt;/pre&gt;&lt;b&gt;Exercise 12347&lt;/b&gt;&lt;br /&gt;Write a program which will read in the source of a C program from its standard input, and print out all the starred items in the following statistics for the program (all as integers). (Note the comment on tab characters at the end of this specification.) &lt;br /&gt;Print out the following values: &lt;br /&gt;&lt;pre&gt;Lines:&lt;br /&gt;  *  The total number of lines&lt;br /&gt;  *  The total number of blank lines&lt;br /&gt;        (Any lines consisting entirely of white space should be&lt;br /&gt;        considered as blank lines.)&lt;br /&gt;     The percentage of blank lines (100 * blank_lines / lines)&lt;br /&gt;&lt;br /&gt;  Characters:&lt;br /&gt;  *  The total number of characters after tab expansion&lt;br /&gt;  *  The total number of spaces after tab expansion&lt;br /&gt;  *  The total number of leading spaces after tab expansion&lt;br /&gt;      (These are the spaces at the start of a line, before any visible&lt;br /&gt;        character; ignore them if there are no visible characters.)&lt;br /&gt;    The average number of&lt;br /&gt;      characters per line&lt;br /&gt;      characters per line ignoring leading spaces&lt;br /&gt;      leading spaces per line&lt;br /&gt;      spaces per line ignoring leading spaces&lt;br /&gt;&lt;br /&gt;  Comments:&lt;br /&gt;  *  The total number of comments in the program&lt;br /&gt;  *  The total number of characters in the comments in the program&lt;br /&gt;       excluding the "/*" and "*/" thenselves&lt;br /&gt;    The percentage of number of comments to total lines&lt;br /&gt;    The percentage of characters in comments to characters&lt;br /&gt;&lt;br /&gt;  Identifiers:&lt;br /&gt;    We are concerned with all the occurrences of "identifiers" in the&lt;br /&gt;      program where each part of the text starting with a letter,&lt;br /&gt;      and continuing with letter, digits and underscores is considered&lt;br /&gt;      to be an identifier, provided that it is not&lt;br /&gt;          in a comment,&lt;br /&gt;          or in a string,&lt;br /&gt;          or within primes.&lt;br /&gt;        Note that&lt;br /&gt;            "abc\"def"&lt;br /&gt;        the internal escaped quote does not close the string.&lt;br /&gt;        Also, the representation of the escape character is&lt;br /&gt;            '\\'&lt;br /&gt; and of prime is&lt;br /&gt;            '\''&lt;br /&gt;      Do not attempt to exclude the fixed words of the language,&lt;br /&gt;      treat them as identifiers. Print&lt;br /&gt;  *  The total number of identifier occurrences.&lt;br /&gt;  *  The total number of characters in them.&lt;br /&gt;    The average identifier length.&lt;br /&gt;&lt;br /&gt;  Indenting:&lt;br /&gt;  *  The total number of times either of the following occurs:&lt;br /&gt;      a line containing a "}" is more indented than the preceding line&lt;br /&gt;      a line is preceded by a line containing a "{" and is less&lt;br /&gt;        indented than it.&lt;br /&gt;      The "{" and "}" must be ignored if in a comment or string or&lt;br /&gt;        primes, or if the other line involved is entirely comment.&lt;br /&gt;    A single count of the sum of both types of error is required.&lt;br /&gt;&lt;/pre&gt;NOTE: All tab characters ('') on input should be interpreted as multiple spaces using the rule: &lt;pre&gt;"move to the next modulo 8 column"&lt;br /&gt;  where the first column is numbered column 0.&lt;br /&gt; col before tab | col after tab&lt;br /&gt;        ---------------+--------------&lt;br /&gt;                0      |      8&lt;br /&gt;                1      |      8&lt;br /&gt;                7      |      8&lt;br /&gt;                8      |     16&lt;br /&gt;                9      |     16&lt;br /&gt;               15      |     16&lt;br /&gt;               16      |     24&lt;br /&gt;&lt;/pre&gt;To read input a character at a time the skeleton has code incorporated to read a line at a time for you using &lt;pre&gt;char ch;&lt;br /&gt;        ch = getchar();&lt;br /&gt;&lt;/pre&gt;Which will deliver each character exactly as read. The "getline" function then puts the line just read in the global array of characters "linec", null terminated, and delivers the length of the line, or a negative value if end of data has been encountered.  You can then look at the characters just read with (for example) &lt;br /&gt;&lt;pre&gt;switch( linec[0] ) {&lt;br /&gt;        case ' ': /* space ..... */&lt;br /&gt;                break;&lt;br /&gt;        case '\t': /* tab character .... */&lt;br /&gt;                break;&lt;br /&gt;        case '\n': /* newline ... */&lt;br /&gt;                break;&lt;br /&gt;        ....&lt;br /&gt;        } /* end switch */&lt;br /&gt;&lt;/pre&gt;End of data is indicated by scanf NOT delivering the value 1.  &lt;br /&gt;&lt;br /&gt;Your output should be in the following style: &lt;br /&gt;&lt;pre&gt;Total lines                     126&lt;br /&gt;        Total blank lines               3&lt;br /&gt;        Total characters                3897&lt;br /&gt;        Total spaces                    1844&lt;br /&gt;        Total leading spaces            1180&lt;br /&gt;        Total comments                  7&lt;br /&gt;        Total chars in comments         234&lt;br /&gt;        Total number of identifiers     132&lt;br /&gt;        Total length of identifiers     606&lt;br /&gt;        Total indenting errors          2&lt;br /&gt;&lt;/pre&gt;You may gather that the above program (together with the unstarred items) forms the basis of part of your marking system! Do the easy bits first, and leave it at that if some aspects worry you. Come back to me if you think my solution (or the specification) is wrong! That is quite possible!   &lt;b&gt;Exercise 12348&lt;/b&gt;&lt;br /&gt;It's rates of pay again! &lt;br /&gt;Loop performing the following operation in your program: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Read two integers, representing a rate of pay (pence per hour) and a number of hours. Print out the total pay, with hours up to 40 being paid at basic rate, from 40 to 60 at rate-and-a-half, above 60 at double-rate. Print the pay as pounds to two decimal places. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Terminate the loop when a zero rate is encountered. At the end of the loop, print out the total pay. &lt;br /&gt;The code for computing the pay from the rate and hours is to be written as a function. &lt;br /&gt;The recommended output format is something like: &lt;br /&gt;&lt;pre&gt;Pay at 200 pence/hr for 38 hours is 76.00 pounds&lt;br /&gt;        Pay at 220 pence/hr for 48 hours is 114.40 pounds&lt;br /&gt;        Pay at 240 pence/hr for 68 hours is 206.40 pounds&lt;br /&gt;        Pay at 260 pence/hr for 48 hours is 135.20 pounds&lt;br /&gt;        Pay at 280 pence/hr for 68 hours is 240.80 pounds&lt;br /&gt;        Pay at 300 pence/hr for 48 hours is 156.00 pounds&lt;br /&gt;        Total pay is 928.80 pounds&lt;br /&gt;&lt;/pre&gt;The ``program features'' checks that explicit values such as 40 and 60 appear only once, as a &lt;code&gt;#define&lt;/code&gt; or initialised variable value. This represents good programming practice.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-4589473804063817321?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/4589473804063817321/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/functions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4589473804063817321'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4589473804063817321'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/functions.html' title='Functions'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-8009594889936687626</id><published>2009-01-14T08:53:00.000-08:00</published><updated>2009-01-14T08:54:07.245-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Arrays and Strings'/><title type='text'>Arrays and Strings</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION00700000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="ch:array"&gt;&amp;nbsp;&lt;/a&gt; In principle arrays in C are similar to those found in other languages. As we shall shortly see arrays are defined slightly differently and there are many subtle differences due the close link between array and pointers. We will look more closely at the link between pointer and arrays later in Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node10.html#ch:pointers"&gt;9&lt;/a&gt;. &lt;h1&gt;&lt;a href="" name="SECTION00710000000000000000"&gt; Single and Multi-dimensional Arrays&lt;/a&gt; &lt;/h1&gt;Let us first look at how we define arrays in C: &lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int listofnumbers[50];&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;b&gt;BEWARE:&lt;/b&gt; In C Array subscripts start at &lt;b&gt;0&lt;/b&gt; and end one less than the array size. For example, in the above case valid subscripts range from 0 to 49. This is a &lt;b&gt;BIG&lt;/b&gt; difference between C and other languages and does require a bit of practice to get in &lt;em&gt;the right frame of mind&lt;/em&gt;. &lt;br /&gt;Elements can be accessed in the following ways:- &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;thirdnumber=listofnumbers[2];&lt;br /&gt;   listofnumbers[5]=100;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;Multi-dimensional arrays can be defined as follows: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int tableofnumbers[50][50];&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;tt&gt;for two dimensions. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;For further dimensions simply add more [&amp;nbsp;]: &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int bigD[50][50][40][30]......[50];&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;Elements can be accessed in the following ways: &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;anumber=tableofnumbers[2][3];&lt;br /&gt;   tableofnumbers[25][16]=100;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;h1&gt;&lt;a href="" name="SECTION00720000000000000000"&gt; Strings&lt;/a&gt; &lt;/h1&gt;In C Strings are defined as arrays of characters. For example, the following defines a string of 50 characters: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;char name[50];&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; C has no string handling facilities built in and so the following are all &lt;u&gt;illegal&lt;/u&gt;: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;char firstname[50],lastname[50],fullname[100];&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   firstname=&amp;nbsp;"Arnold"; /* Illegal */&lt;br /&gt;   lastname=&amp;nbsp;"Schwarznegger"; /* Illegal */&lt;br /&gt;   fullname=&amp;nbsp;"Mr"+firstname&lt;br /&gt;     +lastname; /* Illegal */ &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;However, there is a special library of string handling routines which we will come across later. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;To print a string we use printf with a special &lt;b&gt;%s&lt;/b&gt; control character: &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;printf(``%s'',name); &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;b&gt;NOTE:&lt;/b&gt; We just need to give the name of the string. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;In order to allow variable length strings the &lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;0 character is used to indicate the end of a string. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;So we if we have a string, &lt;tt&gt;char NAME[50];&lt;/tt&gt; and we store the ``DAVE'' in it its contents will look like: &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/string.gif" /&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00730000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12335&lt;/b&gt;&lt;br /&gt;Write a C program to read through an array of any type. Write a C program  to scan through this array to find a particular value. &lt;br /&gt;&lt;b&gt;Exercise 12336&lt;/b&gt;&lt;br /&gt;Read ordinary text a character at a time from the program's standard input, and print it with each line reversed from left to right. Read until you encounter end-of-data (see below). &lt;br /&gt;You may wish to test the program by typing &lt;br /&gt;&lt;pre&gt;prog5rev | prog5rev&lt;br /&gt;&lt;/pre&gt;to see if an exact copy of the original input is recreated.  To read characters to end of data, use a loop such as either &lt;br /&gt;&lt;pre&gt;char ch;&lt;br /&gt;        while( ch = getchar(), ch &amp;gt;= 0 ) /* ch &amp;lt; 0 indicates end-of-data */&lt;br /&gt;&lt;/pre&gt;or &lt;pre&gt;char ch;&lt;br /&gt;        while( scanf( "%c", &amp;amp;ch ) == 1 ) /* one character read */&lt;br /&gt;&lt;/pre&gt;&lt;b&gt;Exercise 12337&lt;/b&gt;&lt;br /&gt;Write a program to read English text to end-of-data (type control-D to indicate end of data at a terminal, see below for detecting it), and print a count of word lengths, i.e. the total number of words of length 1 which occurred, the number of length 2, and so on. &lt;br /&gt;Define a word to be a sequence of alphabetic characters.  You should allow for word lengths up to 25 letters. &lt;br /&gt;Typical output should be like this: &lt;br /&gt;&lt;pre&gt;length 1 : 10 occurrences&lt;br /&gt;      length 2 : 19 occurrences&lt;br /&gt;      length 3 : 127 occurrences&lt;br /&gt;      length 4 : 0 occurrences&lt;br /&gt;      length 5 : 18 occurrences&lt;br /&gt;      ....&lt;br /&gt;&lt;/pre&gt;To read characters to end of data see above question.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-8009594889936687626?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/8009594889936687626/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/arrays-and-strings.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/8009594889936687626'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/8009594889936687626'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/arrays-and-strings.html' title='Arrays and Strings'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-2017769708920392873</id><published>2009-01-14T08:52:00.002-08:00</published><updated>2009-01-14T08:53:23.386-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Looping and Iteration'/><title type='text'>Looping and Iteration</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION00600000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;This chapter will look at C's mechanisms for controlling looping and iteration. Even though some of these mechanisms may look familiar and indeed will operate in standard fashion most of the time. &lt;b&gt;NOTE:&lt;/b&gt; some non-standard features are available. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00610000000000000000"&gt; The &lt;tt&gt;for&lt;/tt&gt; statement&lt;/a&gt; &lt;/h1&gt;The C &lt;tt&gt;for&lt;/tt&gt; statement has the following form: &lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;for &lt;/tt&gt; &lt;tt&gt;(&lt;/tt&gt;&lt;i&gt;expression&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;; &lt;sub&gt;2&lt;/sub&gt;; &lt;i&gt;expression&lt;sub&gt;3&lt;/sub&gt;&lt;/i&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;br /&gt;     &lt;i&gt;statement&lt;/i&gt;;&lt;br /&gt;     or &lt;i&gt;{block of statements}&lt;/i&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;i&gt;expression&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; initialises; &lt;i&gt;expression&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt; is the terminate test; &lt;i&gt;expression&lt;sub&gt;3&lt;/sub&gt;&lt;/i&gt; is the modifier (which may be more than just simple increment); &lt;br /&gt;&lt;br /&gt;&lt;b&gt;NOTE&lt;/b&gt;: C basically treats &lt;tt&gt;for&lt;/tt&gt; statements as &lt;tt&gt;while&lt;/tt&gt; type loops   &lt;br /&gt;&lt;br /&gt;For example: &lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int x;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   main()&lt;br /&gt;     {&lt;br /&gt;    for (x=3;x&amp;gt;0;x-)&lt;br /&gt;       {&lt;br /&gt;       printf("x=%d&lt;img align="middle" alt="$\setminus$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img10.gif" width="12" /&gt;n",x);&lt;br /&gt;       }&lt;br /&gt;     }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;...outputs: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;x=3&lt;br /&gt;  x=2&lt;br /&gt;  x=1&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;...to the screen    &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;All the following are legal &lt;tt&gt;for statements in C. The practical application of such statements is not important here, we are just trying to illustrate peculiar features of C &lt;tt&gt;for&lt;/tt&gt; that may be useful:- &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;for (x=0;((x&amp;gt;3) &amp;amp;&amp;amp; (x&amp;lt;9)); x++)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   for (x=0,y=4;((x&amp;gt;3) &amp;amp;&amp;amp; (y&amp;lt;9)); x++,y+=2)&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   for (x=0,y=4,z=4000;z; z/=10)&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;The second example shows that multiple expressions can be separated a &lt;tt&gt;,.  &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;In the third example the loop will continue to iterate until &lt;tt&gt;z&lt;/tt&gt; becomes 0; &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00620000000000000000"&gt; The &lt;tt&gt;while&lt;/tt&gt; statement&lt;/a&gt; &lt;/h1&gt;The &lt;tt&gt;while&lt;/tt&gt; statement is similar to those used in other languages although more can be done with the &lt;tt&gt;expression&lt;/tt&gt; statement -- a standard feature of C. &lt;br /&gt;The &lt;tt&gt;while&lt;/tt&gt; has the form: &lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;while&lt;/tt&gt; &lt;tt&gt;(&lt;/tt&gt;&lt;i&gt;expression&lt;/i&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;br /&gt;     &lt;i&gt;statement&lt;/i&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;For example: &lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int x=3;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   main()&lt;br /&gt;     { while (x&amp;gt;0)&lt;br /&gt;       { printf("x=%d&lt;img align="middle" alt="$\setminus$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img10.gif" width="12" /&gt;n",x);&lt;br /&gt;         x-;&lt;br /&gt;       }&lt;br /&gt;     }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;...outputs: &lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;x=3&lt;br /&gt;  x=2&lt;br /&gt;  x=1&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;...to the screen. &lt;/tt&gt;&lt;tt&gt; &lt;br /&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;Because the &lt;tt&gt;while loop can accept expressions, not just conditions, the following are all legal:- &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;while (x-);&lt;br /&gt;  while (x=x+1);&lt;br /&gt;  while (x+=5);&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;Using this type of expression, only when the result of &lt;tt&gt;x-, &lt;tt&gt; x=x+1&lt;/tt&gt;, or &lt;tt&gt;x+=5&lt;/tt&gt;, evaluates to 0 will the &lt;tt&gt;while&lt;/tt&gt; condition fail and the loop be exited. &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;We can go further still and perform complete operations within the &lt;tt&gt;while&lt;/tt&gt; &lt;em&gt;expression&lt;/em&gt;: &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;while (i++ &amp;lt; 10);&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  while ( (ch = getchar()) != `q')&lt;br /&gt;  &amp;nbsp;&amp;nbsp;putchar(ch);&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;The first example counts &lt;tt&gt;i up to 10. &lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;&lt;tt&gt;The second example uses C standard library functions (See Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node19.html#ch:string"&gt;18&lt;/a&gt;) &lt;tt&gt;getchar()&lt;/tt&gt; - reads a character from the keyboard - and &lt;tt&gt;putchar()&lt;/tt&gt; - writes a given char to screen. The &lt;tt&gt; while&lt;/tt&gt; loop will proceed to read from the keyboard and echo characters to the screen until a 'q' character is read. &lt;b&gt;NOTE:&lt;/b&gt; This type of operation is used a lot in C and not just with character reading!! (See Exercises). &lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00630000000000000000"&gt; The &lt;tt&gt;do-while&lt;/tt&gt; statement&lt;/a&gt; &lt;/h1&gt;C's &lt;tt&gt;do-while&lt;/tt&gt; statement has the form: &lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;do &lt;/tt&gt;&lt;br /&gt;     &lt;i&gt;statement&lt;/i&gt;;&lt;br /&gt;     &lt;tt&gt;while&lt;/tt&gt; &lt;tt&gt;(&lt;/tt&gt;&lt;i&gt;expression&lt;/i&gt;&lt;tt&gt;);&lt;/tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;It is similar to PASCAL's &lt;tt&gt;repeat&lt;/tt&gt; ... &lt;tt&gt;until&lt;/tt&gt; &lt;u&gt;except&lt;/u&gt; &lt;tt&gt;do while&lt;/tt&gt; &lt;em&gt;expression&lt;/em&gt; is true. &lt;br /&gt;&lt;br /&gt;For example: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int x=3;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   main()&lt;br /&gt;     { do {&lt;br /&gt;       printf("x=%d&lt;img align="middle" alt="$\setminus$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img10.gif" width="12" /&gt;n",x-);&lt;br /&gt;       }&lt;br /&gt;     while (x&amp;gt;0);&lt;br /&gt;     }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;..outputs:- &lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;x=3&lt;br /&gt;   x=2&lt;br /&gt;   x=1&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;tt&gt;&lt;b&gt;NOTE:&lt;/b&gt; The postfix &lt;tt&gt;x- operator which uses the current value of &lt;tt&gt;x&lt;/tt&gt;  while printing and &lt;em&gt;then&lt;/em&gt; decrements &lt;tt&gt;x&lt;/tt&gt;. &lt;/tt&gt;&lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00640000000000000000"&gt; &lt;tt&gt;break&lt;/tt&gt; and &lt;tt&gt;continue&lt;/tt&gt;&lt;/a&gt; &lt;/h1&gt;C provides two commands to control how we loop: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; &lt;tt&gt;break&lt;/tt&gt; -- exit form loop or switch. &lt;/li&gt;&lt;li&gt; &lt;tt&gt;continue&lt;/tt&gt; -- skip 1 iteration of loop. &lt;/li&gt;&lt;/ul&gt;Consider the following example where we read in integer values and process them according to the following conditions. If the value we have read is negative, we wish to print an error message and abandon the loop.  If the value read is great than 100, we  wish  to ignore  it  and continue to the next value in the data.  If the value is zero, we wish to terminate the loop. &lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; while (scanf( ``%d'', &amp;amp;value ) == 1 &amp;amp;&amp;amp; value != 0) { &lt;br /&gt;&amp;nbsp;&lt;br /&gt;     if (value &amp;lt; 0) {&lt;br /&gt;       printf(``Illegal value&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;n'');&lt;br /&gt;       break;&lt;br /&gt;       /* Abandon the loop */&lt;br /&gt;     }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     if (value &amp;gt; 100) {&lt;br /&gt;       printf(``Invalid value&lt;img align="middle" alt="$\backslash$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img3.gif" width="12" /&gt;n'');&lt;br /&gt;       continue;&lt;br /&gt;       /* Skip to start loop again */&lt;br /&gt;     }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     /* Process the value read */&lt;br /&gt;       /* guaranteed between 1 and 100 */&lt;br /&gt;           ....;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     ....;&lt;br /&gt;   } /* end while value != 0 */&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;h1&gt;&lt;a href="" name="SECTION00650000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12327&lt;/b&gt;&lt;br /&gt;Write a program to read in 10 numbers and compute the average, maximum and minimum values.&lt;br /&gt;&lt;b&gt;Exercise 12328&lt;/b&gt;&lt;br /&gt;Write a program to read in numbers until the number -999 is encountered. The sum of all number read until this point should be printed out.&lt;br /&gt;&lt;b&gt;Exercise 12329&lt;/b&gt;&lt;br /&gt;Write a program which will read an integer value for a base, then read a positive integer written to that base and print its value. &lt;br /&gt;Read the second integer a character at a time; skip over any leading non-valid (i.e. not a digit between zero and ``base-1'') characters, then read valid characters until an invalid one is encountered. &lt;br /&gt;&lt;pre&gt;Input       Output&lt;br /&gt;        ==========     ======&lt;br /&gt;        10    1234      1234&lt;br /&gt;         8      77        63   (the value of 77 in base 8, octal)&lt;br /&gt;         2    1111        15   (the value of 1111 in base 2, binary)&lt;br /&gt;&lt;/pre&gt;The base will be less than or equal to 10.   &lt;b&gt;Exercise 12330&lt;/b&gt;&lt;br /&gt;Read in three values representing respectively &lt;br /&gt;a capital sum (integer number of pence), &lt;br /&gt;a rate of interest in percent (float), &lt;br /&gt;and a number of years (integer). &lt;br /&gt;Compute the values of the capital sum with compound interest added over the given period of years. Each year's interest is calculated as &lt;br /&gt;interest = capital * interest_rate / 100; &lt;br /&gt;and is added to the capital sum by &lt;br /&gt;capital += interest; &lt;br /&gt;Print out money values as pounds (pence / 100.0) accurate to two decimal places. &lt;br /&gt;Print out a floating value for the value with compound interest for each year up to the end of the period. &lt;br /&gt;Print output year by year in a form such as: &lt;br /&gt;&lt;pre&gt;Original sum 30000.00 at  12.5 percent for 20 years&lt;br /&gt;&lt;br /&gt;Year Interest  Sum&lt;br /&gt;----+-------+--------&lt;br /&gt;  1  3750.00 33750.00&lt;br /&gt;  2  4218.75 37968.75&lt;br /&gt;  3  4746.09 42714.84&lt;br /&gt;  4  5339.35 48054.19&lt;br /&gt;  5  6006.77 54060.96&lt;br /&gt;  6  6757.62 60818.58&lt;br /&gt;  7  7602.32 68420.90&lt;br /&gt;  8  8552.61 76973.51&lt;br /&gt;  9  9621.68 86595.19&lt;br /&gt; 10 10824.39 97419.58&lt;br /&gt;&lt;/pre&gt;&lt;b&gt;Exercise 12331&lt;/b&gt;&lt;br /&gt;Read a positive integer value, and compute the following sequence: If the number is even, halve it; if it's odd, multiply by 3 and add 1. Repeat this process until the value is 1, printing out each value. Finally print out how many of these operations you performed. &lt;br /&gt;Typical output might be: &lt;br /&gt;&lt;pre&gt;Inital value is 9&lt;br /&gt; Next value is  28&lt;br /&gt; Next value is  14&lt;br /&gt; Next value is   7&lt;br /&gt; Next value is  22&lt;br /&gt; Next value is  11&lt;br /&gt; Next value is  34&lt;br /&gt; Next value is  17&lt;br /&gt; Next value is  52&lt;br /&gt; Next value is  26&lt;br /&gt; Next value is  13&lt;br /&gt; Next value is  40&lt;br /&gt; Next value is  20&lt;br /&gt; Next value is  10&lt;br /&gt; Next value is   5&lt;br /&gt; Next value is  16&lt;br /&gt; Next value is   8&lt;br /&gt; Next value is   4&lt;br /&gt; Next value is   2&lt;br /&gt; Final value 1, number of steps 19&lt;br /&gt;&lt;/pre&gt;If the input value is less than 1, print a message containing the word  &lt;pre&gt;Error&lt;br /&gt;&lt;/pre&gt;and perform an &lt;pre&gt;exit( 0 );&lt;br /&gt;&lt;/pre&gt;&lt;b&gt;Exercise 12332&lt;/b&gt;&lt;br /&gt;Write a program to count the vowels and letters in free text given as standard input. Read text a character at a time until you encounter end-of-data. &lt;br /&gt;Then print out the number of occurrences of each of the vowels a, e, i, o and u in the text, the total number of letters, and each of the vowels as an integer percentage of the letter total. &lt;br /&gt;Suggested output format is: &lt;br /&gt;&lt;pre&gt;Numbers of characters:&lt;br /&gt;        a   3 ; e   2 ; i   0 ; o   1 ; u   0 ; rest  17&lt;br /&gt;        Percentages of total:&lt;br /&gt;        a  13%; e   8%; i   0%; o   4%; u   0%; rest  73%&lt;br /&gt;&lt;/pre&gt;Read characters to end of data using a construct such as &lt;pre&gt;char ch;&lt;br /&gt;        while(&lt;br /&gt;            ( ch = getchar() ) &amp;gt;= 0&lt;br /&gt;        ) {&lt;br /&gt;            /* ch is the next character */    ....&lt;br /&gt;        }&lt;br /&gt;&lt;/pre&gt;to read characters one at a time using &lt;tt&gt;getchar()&lt;/tt&gt; until a negative value is returned.   &lt;b&gt;Exercise 12333&lt;/b&gt;&lt;br /&gt;Read a file of English text, and print it out one word per line, all punctuation and non-alpha characters being omitted. &lt;br /&gt;For end-of-data, the program loop should read until "getchar" delivers a value &amp;lt;= 0. When typing input, end the data by typing the end-of-file character, usually control-D. When reading from a file, "getchar" will deliver a negative value when it encounters the end of the file. &lt;br /&gt;Typical output might be &lt;br /&gt;&lt;pre&gt;Read&lt;br /&gt;a&lt;br /&gt;file&lt;br /&gt;of&lt;br /&gt;English&lt;br /&gt;text&lt;br /&gt;and&lt;br /&gt;print&lt;br /&gt;it&lt;br /&gt;out&lt;br /&gt;one&lt;br /&gt;&lt;/pre&gt;etc.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-2017769708920392873?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/2017769708920392873/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/looping-and-iteration.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2017769708920392873'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2017769708920392873'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/looping-and-iteration.html' title='Looping and Iteration'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-5152241955050040886</id><published>2009-01-14T08:52:00.001-08:00</published><updated>2009-01-14T08:52:37.694-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Conditionals'/><title type='text'>Conditionals</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION00500000000000000000"&gt;&lt;/a&gt; &lt;/h1&gt;This Chapter deals with the various methods that C can control the &lt;em&gt;flow&lt;/em&gt; of logic in a program. Apart from slight syntactic variation they are similar to other languages. &lt;br /&gt;As we have seen following logical operations exist in C: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;==, !=, &lt;img align="middle" alt="$\parallel$" border="0" height="32" src="http://www.cs.cf.ac.uk/Dave/C/img8.gif" width="12" /&gt;, &amp;amp;&amp;amp;. &lt;br /&gt;&lt;br /&gt;One other operator is the unitary - it takes only one argument - &lt;em&gt;not&lt;/em&gt; &lt;b&gt; !&lt;/b&gt;. &lt;br /&gt;&lt;br /&gt;These operators are used in conjunction with the following statements. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00510000000000000000"&gt; The &lt;tt&gt;if&lt;/tt&gt; statement&lt;/a&gt; &lt;/h1&gt;The &lt;tt&gt;if&lt;/tt&gt; statement has the same function as other languages. It has three basic forms: &lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;if &lt;/tt&gt; &lt;tt&gt;(&lt;/tt&gt;&lt;i&gt;expression&lt;/i&gt;)&lt;br /&gt;     &lt;i&gt;statement&lt;/i&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;...or: &lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;if &lt;/tt&gt; &lt;tt&gt;(&lt;/tt&gt;&lt;i&gt;expression&lt;/i&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;br /&gt;     &lt;i&gt;statement&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;   &lt;tt&gt;else&lt;/tt&gt;&lt;br /&gt;     &lt;i&gt;statement&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;...or: &lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;if &lt;/tt&gt; &lt;tt&gt;(&lt;/tt&gt;&lt;i&gt;expression&lt;/i&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;br /&gt;     &lt;i&gt;statement&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;   &lt;tt&gt;else if (&lt;/tt&gt;&lt;i&gt;expression&lt;/i&gt;&lt;tt&gt;)&lt;/tt&gt;&lt;br /&gt;     &lt;i&gt;statement&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;   &lt;tt&gt;else&lt;/tt&gt;&lt;br /&gt;     &lt;i&gt;statement&lt;sub&gt;3&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;For example:- &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;int x,y,w;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;   main()&lt;br /&gt;     {&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     if (x&amp;gt;0)&lt;br /&gt;      {&lt;br /&gt;      z=w;&lt;br /&gt;      ........&lt;br /&gt;      }&lt;br /&gt;    else&lt;br /&gt;      {&lt;br /&gt;      z=y;&lt;br /&gt;      ........&lt;br /&gt;      }&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00520000000000000000"&gt; The &lt;tt&gt;?&lt;/tt&gt; operator&lt;/a&gt; &lt;/h1&gt;The &lt;tt&gt;?&lt;/tt&gt; (&lt;em&gt;ternary condition&lt;/em&gt;) operator is a more efficient form for expressing simple &lt;tt&gt;if&lt;/tt&gt; statements. It has the following form: &lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;i&gt;expression&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; &lt;tt&gt;?&lt;/tt&gt; &lt;i&gt;expression&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;tt&gt;:&lt;/tt&gt;  &lt;i&gt;expression&lt;sub&gt;3&lt;/sub&gt;&lt;/i&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;It simply states: &lt;br /&gt;&lt;br /&gt;&lt;tt&gt;if&lt;/tt&gt; &lt;i&gt;expression&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt; &lt;tt&gt;then&lt;/tt&gt;  &lt;i&gt;expression&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt; &lt;tt&gt;else&lt;/tt&gt;  &lt;i&gt;expression&lt;sub&gt;3&lt;/sub&gt;&lt;/i&gt; &lt;br /&gt;&lt;br /&gt;For example to assign the maximum of &lt;tt&gt;a&lt;/tt&gt; and &lt;tt&gt;b&lt;/tt&gt; to &lt;tt&gt;z&lt;/tt&gt;: &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;z = (a&amp;gt;b) ? a : b;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;which is the same as: &lt;br /&gt;&lt;tt&gt; &lt;/tt&gt;&lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if (a&amp;gt;b)&lt;br /&gt;     z = a;&lt;br /&gt;   else&lt;br /&gt;     z=b;&lt;br /&gt;&lt;/tt&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;h1&gt;&lt;a href="" name="SECTION00530000000000000000"&gt; The &lt;tt&gt;switch&lt;/tt&gt; statement&lt;/a&gt; &lt;/h1&gt;The C &lt;tt&gt;switch&lt;/tt&gt; is similar to Pascal's &lt;tt&gt;case&lt;/tt&gt; statement and it allows multiple choice of a selection of items at one level of a conditional where it is a far neater way of writing multiple &lt;tt&gt;if&lt;/tt&gt; statements: &lt;br /&gt;&lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;tt&gt;switch&lt;/tt&gt; &lt;tt&gt;(&lt;/tt&gt;&lt;i&gt;expression&lt;/i&gt;&lt;tt&gt;)&lt;/tt&gt;&amp;nbsp;&lt;tt&gt;{&lt;/tt&gt;&lt;br /&gt;     &lt;tt&gt;case&lt;/tt&gt; &lt;i&gt;item&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;&lt;tt&gt;:&lt;/tt&gt;&lt;br /&gt;       &lt;i&gt;statement&lt;sub&gt;1&lt;/sub&gt;&lt;/i&gt;&lt;tt&gt;;&lt;/tt&gt;&lt;br /&gt;       &lt;tt&gt;break;&lt;/tt&gt;&lt;br /&gt;     &lt;tt&gt;case&lt;/tt&gt; &lt;i&gt;item&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;tt&gt;:&lt;/tt&gt;&lt;br /&gt;       &lt;i&gt;statement&lt;sub&gt;2&lt;/sub&gt;&lt;/i&gt;&lt;tt&gt;;&lt;/tt&gt;&lt;br /&gt;       &lt;tt&gt;break;&lt;/tt&gt;&lt;br /&gt;       &lt;img align="bottom" alt="$\vdots$" border="0" height="27" src="http://www.cs.cf.ac.uk/Dave/C/img9.gif" width="8" /&gt;       &lt;img align="bottom" alt="$\vdots$" border="0" height="27" src="http://www.cs.cf.ac.uk/Dave/C/img9.gif" width="8" /&gt;     &lt;tt&gt;case&lt;/tt&gt; &lt;i&gt;item&lt;sub&gt;&lt;i&gt;n&lt;/i&gt;&lt;/sub&gt;&lt;/i&gt;&lt;tt&gt;:&lt;/tt&gt;&lt;br /&gt;       &lt;i&gt;statement&lt;sub&gt;&lt;i&gt;n&lt;/i&gt;&lt;/sub&gt;&lt;/i&gt;&lt;tt&gt;;&lt;/tt&gt;&lt;br /&gt;       &lt;tt&gt;break;&lt;/tt&gt;&lt;br /&gt;     &lt;tt&gt;default:&lt;/tt&gt;&lt;br /&gt;       &lt;i&gt;statement&lt;/i&gt;&lt;tt&gt;;&lt;/tt&gt;&lt;br /&gt;       &lt;tt&gt;break;&lt;/tt&gt;&lt;br /&gt;    &lt;tt&gt;}&lt;/tt&gt;&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;In each case the value of &lt;i&gt;item&lt;sub&gt;&lt;i&gt;i&lt;/i&gt;&lt;/sub&gt;&lt;/i&gt; must be a constant, variables are &lt;u&gt;not&lt;/u&gt; allowed. &lt;br /&gt;The &lt;tt&gt;break&lt;/tt&gt; is needed if you want to terminate the &lt;tt&gt;switch&lt;/tt&gt; after execution of one choice. Otherwise the next case would get evaluated. &lt;b&gt;Note:&lt;/b&gt; This is unlike most other languages. &lt;br /&gt;We can also have &lt;b&gt;null&lt;/b&gt; statements by just including a ; or let the switch statement &lt;em&gt;fall through&lt;/em&gt; by omitting any statements (see &lt;em&gt;e.g.&lt;/em&gt; below). &lt;br /&gt;The &lt;tt&gt;default&lt;/tt&gt; case is optional and catches any other cases.   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;For example:- &lt;br /&gt;&lt;tt&gt; &lt;pre&gt;&lt;tt&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;switch (letter)&lt;br /&gt;    {&lt;br /&gt;     case `A':&lt;br /&gt;     case `E':&lt;br /&gt;     case `I':&lt;br /&gt;     case `O':&lt;br /&gt;     case `U':&lt;br /&gt;       numberofvowels++;&lt;br /&gt;       break;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     case ` ':&lt;br /&gt;       numberofspaces++;&lt;br /&gt;       break;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;     default:&lt;br /&gt;       numberofconstants++;&lt;br /&gt;       break;&lt;br /&gt;    }&lt;br /&gt;&lt;/tt&gt;&lt;/pre&gt;&lt;/tt&gt; &lt;br /&gt;In the above example if the value of &lt;tt&gt;letter is `A', `E', `I', `O' or `U' then &lt;tt&gt;numberofvowels&lt;/tt&gt; is incremented. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;If the value of &lt;tt&gt;letter&lt;/tt&gt; is `&amp;nbsp;' then &lt;tt&gt;numberofspaces&lt;/tt&gt; is incremented. &lt;/tt&gt;&lt;br /&gt;&lt;tt&gt;If none of these is true then the &lt;tt&gt; default&lt;/tt&gt; condition is executed, that is &lt;tt&gt;numberofconstants&lt;/tt&gt; is incremented. &lt;/tt&gt;&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00540000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12304&lt;/b&gt;&lt;br /&gt;Write a program to read two characters, and print their value when interpreted as a 2-digit hexadecimal number. Accept upper case letters for values from 10 to 15.  &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Exercise 12305&lt;/b&gt;&lt;br /&gt;Read an integer value. Assume it is the number of a month of the year; print out the name of that month. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Exercise 12306&lt;/b&gt;&lt;br /&gt;Given as input three integers representing a date as day, month, year, print out the number day, month and year for the following day's date. &lt;br /&gt;Typical input:         28 2 1992  Typical output:         Date following 28:02:1992 is 29:02:1992 &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Exercise 12307&lt;/b&gt;&lt;br /&gt;Write a program which reads two integer values. If the first is less than the second, print the message up. If the second is less than the first, print the message down  If the numbers are equal, print the message equal  If there is an error reading the data, print a message containing the word         Error and perform exit( 0 );&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-5152241955050040886?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/5152241955050040886/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/conditionals.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5152241955050040886'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5152241955050040886'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/conditionals.html' title='Conditionals'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-4278223596221717304</id><published>2009-01-14T08:41:00.000-08:00</published><updated>2009-01-14T08:42:14.383-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='C++'/><title type='text'>C/C++ Program Compilation</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION00300000000000000000"&gt; C/C++ Program Compilation&lt;/a&gt; &lt;/h1&gt;In this chapter we begin by outlining the basic processes you need to go through in order to compile your C (or C++) programs. We then proceed to formally describe the C compilation model and also how C supports additional libraries. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00310000000000000000"&gt; Creating, Compiling and Running Your Program&lt;/a&gt; &lt;/h1&gt;The stages of developing your C program are as follows. (See Appendix&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node3.html#app:compiler"&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/cross_ref_motif.gif" /&gt;&lt;/a&gt; and exercises for more info.) &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00311000000000000000"&gt; Creating the program&lt;/a&gt; &lt;/h2&gt;Create a file  containing  the  complete  program,  such  as  the  above example.  You can use any ordinary editor with which you are familiar to create the file. One such editor is  &lt;em&gt;textedit&lt;/em&gt;  available on most UNIX systems. &lt;br /&gt;The filename must by convention end ``.c'' (full stop, lower case c), &lt;em&gt;e.g. myprog.c&lt;/em&gt;  or &lt;em&gt;progtest.c&lt;/em&gt;.  The contents must obey C syntax. For example, they might be as in the above example, starting with the line &lt;tt&gt;/* Sample ....&lt;/tt&gt; (or a blank line preceding it) and ending with the line &lt;tt&gt;} /* end of program */&lt;/tt&gt; (or a blank line following it). &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00312000000000000000"&gt; Compilation&lt;/a&gt; &lt;/h2&gt;There are many C compilers around. The &lt;tt&gt;cc&lt;/tt&gt; being the default Sun compiler. The GNU C compiler &lt;tt&gt;gcc&lt;/tt&gt; is popular and available for many platforms. PC users may also be familiar with the Borland &lt;tt&gt;bcc&lt;/tt&gt; compiler. &lt;br /&gt;There are also equivalent C++ compilers which are usually denoted by &lt;tt&gt;CC&lt;/tt&gt; (&lt;em&gt;note&lt;/em&gt; upper case CC. For example Sun provides CC and GNU &lt;tt&gt;GCC&lt;/tt&gt;. The GNU compiler is also denoted by &lt;tt&gt;g++&lt;/tt&gt; &lt;br /&gt;Other (less common) C/C++ compilers exist. All the above compilers operate in essentially the same manner and share many common command line options. Below and in Appendix&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node3.html#app:compiler"&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/cross_ref_motif.gif" /&gt;&lt;/a&gt; we list and give example uses many of the common compiler options. However, the &lt;b&gt;best&lt;/b&gt; source of each compiler is through the online manual pages of your system: &lt;em&gt;e.g.&lt;/em&gt; &lt;tt&gt;man cc&lt;/tt&gt;. &lt;br /&gt;For the sake of compactness in the basic discussions of compiler operation we will simply refer to the &lt;tt&gt;cc&lt;/tt&gt; compiler -- other compilers can simply be substituted in place of &lt;tt&gt;cc&lt;/tt&gt; unless otherwise stated. &lt;br /&gt;To Compile your program simply invoke the command &lt;tt&gt; cc&lt;/tt&gt;. The command must be followed by the name of the  (C) program you wish to compile. A number of compiler options can be specified also. We will not concern ourselves with many of these options yet, some useful and often essential options are introduced below -- See Appendix&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node3.html#app:compiler"&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/cross_ref_motif.gif" /&gt;&lt;/a&gt; or online manual help for further details. &lt;br /&gt;Thus, the basic compilation command is: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;cc program.c&lt;/tt&gt; &lt;br /&gt;where &lt;em&gt;program.c&lt;/em&gt; is the name of the file. &lt;br /&gt;If there are obvious errors in your program (such as mistypings, misspelling one of the key  words  or  omitting  a  semi-colon),  the compiler  will  detect  and report them. &lt;br /&gt;There may, of course, still be logical errors that the  compiler  cannot detect.  You may be telling the computer to do the wrong operations. &lt;br /&gt;When the compiler has successfully digested your program,  the  compiled version,  or  executable,  is  left  in a file called &lt;em&gt;a.out&lt;/em&gt; or if the compiler option &lt;em&gt;-o&lt;/em&gt; is used : the file listed after the &lt;em&gt;-o&lt;/em&gt;. &lt;br /&gt;It is &lt;u&gt;more&lt;/u&gt; convenient to use a &lt;em&gt;-o&lt;/em&gt; and filename in the  compilation as in &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;cc -o program program.c&lt;/tt&gt; &lt;br /&gt;which puts the compiled program into the file program (or any  file  you name  following  the  "-o"  argument)  &lt;b&gt;instead&lt;/b&gt; of putting it in the file a.out . &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00313000000000000000"&gt; Running the program&lt;/a&gt; &lt;/h2&gt;The next stage is to actually run your executable program.   To  run  an executable  in UNIX, you simply type the name of the file containing it, in this case &lt;em&gt;program&lt;/em&gt; (or &lt;em&gt;a.out&lt;/em&gt;) &lt;br /&gt;This executes your program, printing any results to the screen.  At this stage  there may be run-time errors, such as division by zero, or it may become evident that the program has produced incorrect output. &lt;br /&gt;If  so, you  must  return to edit your program source, and recompile it, and run it again. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00320000000000000000"&gt; The C Compilation Model&lt;/a&gt; &lt;/h1&gt;We will briefly highlight key features of the C Compilation model (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node3.html#fig:comp"&gt;2.1&lt;/a&gt;) here. &lt;br /&gt;&lt;a href="" name="fig:comp"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/cmodel.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node3.html#fig:comp"&gt;2.1&lt;/a&gt; The C Compilation Model&lt;/b&gt; &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00321000000000000000"&gt; The Preprocessor&lt;/a&gt; &lt;/h2&gt;We will study this part of the compilation process in greater detail later (Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node14.html#ch:preprocess"&gt;13&lt;/a&gt;. However we need some basic information for some C programs. &lt;br /&gt;The Preprocessor accepts source code as input and is responsible for &lt;br /&gt;&lt;ul&gt;&lt;li&gt; removing comments &lt;/li&gt;&lt;li&gt; interpreting special &lt;em&gt;preprocessor directives&lt;/em&gt; denoted by &lt;tt&gt;#&lt;/tt&gt;. &lt;/li&gt;&lt;/ul&gt;For example &lt;br /&gt;&lt;ul&gt;&lt;li&gt; &lt;tt&gt;#include&lt;/tt&gt;  -- includes contents of a named file. Files usually called &lt;em&gt;header&lt;/em&gt; files. &lt;em&gt;e.g&lt;/em&gt; &lt;ul&gt;&lt;li&gt; &lt;tt&gt;#include &lt;math.h _moz-userdefined=""&gt;&lt;/math.h&gt;&lt;/tt&gt; -- standard library maths file. &lt;/li&gt;&lt;li&gt; &lt;tt&gt;#include &lt;stdio.h _moz-userdefined=""&gt;&lt;/stdio.h&gt;&lt;/tt&gt; -- standard library I/O file &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt; &lt;tt&gt;#define&lt;/tt&gt;  -- defines a symbolic name or constant. Macro substitution. &lt;ul&gt;&lt;li&gt; &lt;tt&gt;#define MAX_ARRAY_SIZE 100&lt;/tt&gt; &lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;&lt;a href="" name="SECTION00322000000000000000"&gt; C Compiler&lt;/a&gt; &lt;/h2&gt;The C compiler translates source to assembly code. The source code is received from the preprocessor. &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00323000000000000000"&gt; Assembler&lt;/a&gt; &lt;/h2&gt;The assembler creates object code. On a UNIX system you may see files with a &lt;tt&gt;.o&lt;/tt&gt; suffix (&lt;tt&gt;.OBJ&lt;/tt&gt; on MSDOS) to indicate object code files. &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00324000000000000000"&gt; Link Editor&lt;/a&gt; &lt;/h2&gt;If a source file references library functions or functions defined in other source files the &lt;em&gt;link editor&lt;/em&gt; combines these functions (with &lt;tt&gt;main()&lt;/tt&gt;) to create an executable file. External Variable references resolved here also. &lt;em&gt;More on this later&lt;/em&gt; (Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node35.html#ch:prog"&gt;34&lt;/a&gt;). &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00325000000000000000"&gt; Some Useful Compiler Options&lt;/a&gt; &lt;/h2&gt;Now that we have a basic understanding of the compilation model we can now introduce some useful and sometimes essential common compiler options. Again see the online &lt;tt&gt;man&lt;/tt&gt; pages and Appendix&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node3.html#app:compiler"&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/cross_ref_motif.gif" /&gt;&lt;/a&gt; for further information and additional options. &lt;br /&gt;&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;&lt;tt&gt;-c&lt;/tt&gt;&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;Suppress the linking process and produce a &lt;tt&gt;.o&lt;/tt&gt;  file for each source file listed.  Several can be subsequently linked by the &lt;tt&gt;cc&lt;/tt&gt; command, for example: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;cc  file1.o file2.o ...... -o executable&lt;/tt&gt; &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;&lt;tt&gt;-llibrary&lt;/tt&gt;&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;Link with  object libraries. This option must follow the source file arguments. The object libraries are archived and can be standard, third party or user created libraries (We discuss this topic briefly below and also in detail later (Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node35.html#ch:prog"&gt;34&lt;/a&gt;). Probably the most commonly used library is the math library (&lt;tt&gt; math.h&lt;/tt&gt;). You must link in this library explicitly if you wish to use the maths functions (&lt;b&gt;note&lt;/b&gt; do note forget to &lt;tt&gt;#include &lt;math.h _moz-userdefined=""&gt;&lt;/math.h&gt;&lt;/tt&gt; header file), for example: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;cc  calc.c -o calc -lm&lt;/tt&gt; Many other libraries are linked in this fashion (see below) &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;&lt;tt&gt;-Ldirectory&lt;/tt&gt;&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;Add directory to the list of directories  containing  object-library  routines. The linker always looks for standard and other system libraries in &lt;tt&gt;/lib&lt;/tt&gt; and &lt;tt&gt;/usr/lib&lt;/tt&gt;. If you want to link in libraries that you have created or installed yourself (unless you have certain privileges and get the libraries installed in &lt;tt&gt;/usr/lib&lt;/tt&gt;) you &lt;b&gt;will&lt;/b&gt; have to specify where you files are stored, for example: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;cc  prog.c -L/home/myname/mylibs  mylib.a&lt;/tt&gt; &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;&lt;tt&gt;-Ipathname&lt;/tt&gt;&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;Add pathname to the list of directories  in  which                to   search   for  #include  files  with  relative                filenames  (not  beginning  with  slash  /).  BY default, The preprocessor  first searches for #include files in                the  directory  containing  source file,  then   in                directories  named  with  -I options (if any), and                finally, in /usr/include. So to include header files stored in &lt;tt&gt; /home/myname/myheaders&lt;/tt&gt; you would do: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;cc  prog.c -I/home/myname/myheaders&lt;/tt&gt; &lt;b&gt;Note:&lt;/b&gt; System library header files are stored in a special place (&lt;tt&gt;/usr/include&lt;/tt&gt;) and are not affected by the &lt;tt&gt;-I option&lt;/tt&gt;. System header files and user header files are included in a slightly different manner (see Chapters&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node14.html#ch:preprocess"&gt;13&lt;/a&gt; and&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node35.html#ch:prog"&gt;34&lt;/a&gt;) &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;&lt;tt&gt;-g&lt;/tt&gt;&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;invoke debugging option. This instructs the compiler to produce additional symbol table information that is used by a variety of debugging utilities. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;&lt;tt&gt;-D&lt;/tt&gt;&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;define symbols either as identifiers (&lt;tt&gt;-D&lt;/tt&gt;&lt;em&gt;identifer&lt;/em&gt;) or as values (&lt;tt&gt;-D&lt;/tt&gt;&lt;em&gt;symbol=value&lt;/em&gt;) in a similar fashion as the &lt;tt&gt;#define&lt;/tt&gt; preprocessor command. For more details on the use of this argument see Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node14.html#ch:preprocess"&gt;13&lt;/a&gt;. &lt;/dd&gt;&lt;/dl&gt;For further information on general compiler options and the GNU compiler refer to Appendix&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node3.html#app:compiler"&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/cross_ref_motif.gif" /&gt;&lt;/a&gt;. &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00326000000000000000"&gt; Using Libraries&lt;/a&gt; &lt;/h2&gt;C is an extremely small language. Many of the functions of other languages are not included in C. &lt;em&gt;e.g.&lt;/em&gt; No built in I/O, string handling or  maths functions. &lt;br /&gt;&lt;em&gt;What use is C then?&lt;/em&gt; &lt;br /&gt;C provides functionality through a rich set function libraries. &lt;br /&gt;As a result most C implementations include &lt;em&gt;standard&lt;/em&gt; libraries of functions for many facilities ( I/O &lt;em&gt;etc.&lt;/em&gt;). For many practical purposes these may be regarded as being part of C. But they may vary from machine to machine. (&lt;em&gt;cf&lt;/em&gt; Borland C for a PC to UNIX C). &lt;br /&gt;A programmer can also develop his or her own function libraries and also include special purpose third party libraries (&lt;em&gt;e.g.&lt;/em&gt; NAG, PHIGS). &lt;br /&gt;All libraries (except standard I/O) need to be explicitly linked in with the &lt;tt&gt;-l&lt;/tt&gt; and, possibly, &lt;tt&gt;-L&lt;/tt&gt; compiler options described above. &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00327000000000000000"&gt; UNIX Library Functions&lt;/a&gt; &lt;/h2&gt;The UNIX system provides a large number of C functions as libraries.  Some of these implement frequently used operations, while others are very specialised in their application. &lt;br /&gt;&lt;b&gt;Do Not Reinvent Wheels&lt;/b&gt;: It is wise for  programmers to check whether a library function is available to perform a task before writing their own version. This will reduce program development time. The library functions have been tested, so they are more likely to be correct than any function which the programmer might write. This will save time when debugging the program. &lt;br /&gt;Later chapters deal with all important standard library issues and other common system libraries. &lt;br /&gt;&lt;h2&gt;&lt;a href="" name="SECTION00328000000000000000"&gt; Finding Information about Library Functions&lt;/a&gt; &lt;/h2&gt;The UNIX manual has an entry for all available functions.  Function documentation is stored in &lt;em&gt;section 3&lt;/em&gt; of the manual, and there are many other useful system calls in &lt;em&gt;section 2&lt;/em&gt;. If you already know the name of the function you want, you can read the page by typing (to find about &lt;tt&gt;sqrt&lt;/tt&gt;): &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;man 3 sqrt&lt;/tt&gt; &lt;br /&gt;If you don't know the name of the function, a full list is included in the introductory page for section 3 of the manual. To read this, type &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;man 3 intro&lt;/tt&gt; &lt;br /&gt;There are approximately 700 functions described here.  This number tends to increase with each upgrade of the system. &lt;br /&gt;On any manual page, the SYNOPSIS section will include information on the use of the function.  For example: &lt;br /&gt;&lt;pre&gt;#include &lt;time.h _moz-userdefined=""&gt;&lt;br /&gt;&lt;br /&gt;  char *ctime(time_t *clock)&lt;br /&gt;&lt;/time.h&gt;&lt;/pre&gt;This means that you must have &lt;br /&gt;&lt;pre&gt;#include &lt;time.h _moz-userdefined=""&gt;&lt;br /&gt;&lt;/time.h&gt;&lt;/pre&gt;in your file before you call &lt;tt&gt;ctime&lt;/tt&gt;. And that function ctime takes a pointer to type &lt;tt&gt;time_t&lt;/tt&gt; as an argument, and returns a &lt;tt&gt;string (char *)&lt;/tt&gt;. &lt;tt&gt;time_t&lt;/tt&gt; will probably be defined in the same manual page. &lt;br /&gt;The DESCRIPTION section will then give a short description of what the function does.  For example: &lt;br /&gt;&lt;pre&gt;ctime() converts a long integer, pointed to by clock,  to  a&lt;br /&gt;  26-character  string  of the form produced by asctime().&lt;br /&gt;&lt;/pre&gt;&lt;h1&gt;&lt;a href="" name="SECTION00330000000000000000"&gt; Lint  -- A C program verifier&lt;/a&gt; &lt;/h1&gt;You will soon discover (if you have not already) that the C compiler is pretty vague in many aspects of checking program correctness, particularly in type checking. Careful use of prototyping of functions can assist modern C compilers in this task. However, There is still no guarantee that once you have successfully compiled your program that it will run correctly. &lt;br /&gt;The UNIX utility &lt;tt&gt;lint&lt;/tt&gt; can assist in checking for a multitude of programming errors. Check out the online manual pages  (&lt;tt&gt;man lint&lt;/tt&gt;) for complete details of lint. It is well worth the effort as it can help save many hours debugging your C code. &lt;br /&gt;To run lint simply enter the command: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;tt&gt;lint myprog.c&lt;/tt&gt;. &lt;br /&gt;Lint is particularly good at checking type checking of variable and function assignments, efficiency, unused variables and function identifiers, unreachable code and possibly memory leaks. There are many useful options to help control lint (see &lt;tt&gt;man lint&lt;/tt&gt;). &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00340000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12171&lt;/b&gt;&lt;br /&gt;Enter, compile and run the following program: &lt;br /&gt;&lt;pre&gt;main()&lt;br /&gt;&lt;br /&gt; { int i;&lt;br /&gt;&lt;br /&gt;    printf("\t Number \t\t Square of Number\n\n");&lt;br /&gt;&lt;br /&gt;    for (i=0; i&amp;lt;=25;++i)&lt;br /&gt;            printf("\t %d \t\t\t %d \n",i,i*i);&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;b&gt;Exercise 12172&lt;/b&gt;&lt;br /&gt;The following program uses the math library. Enter compile and run it correctly. &lt;br /&gt;&lt;pre&gt;#include &lt;math.h _moz-userdefined=""&gt;&lt;br /&gt;&lt;br /&gt; main()&lt;br /&gt;&lt;br /&gt; { int i;&lt;br /&gt;&lt;br /&gt;    printf("\t Number \t\t Square Root of Number\n\n");&lt;br /&gt;&lt;br /&gt;    for (i=0; i&amp;lt;=360; ++i)&lt;br /&gt;            printf("\t %d \t\t\t %d \n",i, sqrt((double) i));&lt;br /&gt;&lt;br /&gt; }&lt;br /&gt;&lt;/math.h&gt;&lt;/pre&gt;&lt;b&gt;Exercise 12173&lt;/b&gt;&lt;br /&gt;Look in &lt;tt&gt;/lib&lt;/tt&gt; and &lt;tt&gt;/usr/lib&lt;/tt&gt; and see what libraries are available. &lt;br /&gt;&lt;ul&gt;&lt;li&gt; Use the &lt;tt&gt;man&lt;/tt&gt; command to get details of library functions &lt;/li&gt;&lt;li&gt; Explore the libraries to see what each contains by running the command &lt;tt&gt;ar t libfile&lt;/tt&gt;. &lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;b&gt;Exercise 12174&lt;/b&gt;&lt;br /&gt;Look in  &lt;tt&gt;/usr/include&lt;/tt&gt; and see what header files are available. &lt;br /&gt;&lt;ul&gt;&lt;li&gt; Use the &lt;tt&gt;more&lt;/tt&gt; or &lt;tt&gt;cat&lt;/tt&gt; commands to view these text files &lt;/li&gt;&lt;li&gt; Explore the header files to see what each contains, note the include, define, type definitions and function prototypes declared in them &lt;/li&gt;&lt;/ul&gt;&lt;b&gt;Exercise 12175&lt;/b&gt;&lt;br /&gt;Suppose you have a C program whose main function is in &lt;tt&gt;main.c&lt;/tt&gt; and has other functions in the files &lt;tt&gt;input.c&lt;/tt&gt; and &lt;tt&gt;output.c&lt;/tt&gt;: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; What command(s) would you use on your system to compile and link this program? &lt;/li&gt;&lt;li&gt; How would you modify the above commands to link a library called &lt;tt&gt;process1&lt;/tt&gt; stored in the standard system library directory? &lt;/li&gt;&lt;li&gt; How would you modify the above commands to link a library called &lt;tt&gt;process2&lt;/tt&gt; stored in your home directory? &lt;/li&gt;&lt;li&gt; Some header files need to be read and have been found to located in a &lt;tt&gt;header&lt;/tt&gt; subdirectory of your home directory and also in the current working directory. How would you modify the compiler commands to account for this? &lt;/li&gt;&lt;/ul&gt;&lt;b&gt;Exercise 12176&lt;/b&gt;&lt;br /&gt;Suppose you have a C program composed of several separate files, and they include one another as shown below: &lt;br /&gt;&lt;br /&gt;&lt;div align="center"&gt; &lt;a href="" name="140"&gt;&lt;/a&gt; &lt;table border="1" cellpadding="3"&gt;&lt;caption&gt;&lt;strong&gt;Figure 1.5:&lt;/strong&gt; Sample Icon from Xterm Application&lt;/caption&gt; &lt;tbody&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;File&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;Include Files&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;main.c&lt;/tt&gt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;stdio.h, process1.h&lt;/tt&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;input.c&lt;/tt&gt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;stdio.h, list.h&lt;/tt&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;output.c&lt;/tt&gt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;stdio.h&lt;/tt&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;process1.c&lt;/tt&gt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;stdio.h, process1.h&lt;/tt&gt;&lt;/td&gt; &lt;/tr&gt;&lt;tr valign="top"&gt;&lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;process2.c&lt;/tt&gt;&lt;/td&gt; &lt;td align="center" nowrap="nowrap"&gt;&lt;tt&gt;stdio.h, list.h&lt;/tt&gt;&lt;/td&gt; &lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt; Which files have to recompiled after you make changes to &lt;tt&gt;process1.c&lt;/tt&gt;? &lt;/li&gt;&lt;li&gt; Which files have to recompiled after you make changes to &lt;tt&gt;process1.h&lt;/tt&gt;? &lt;/li&gt;&lt;li&gt; Which files have to recompiled after you make changes to &lt;tt&gt;list.h&lt;/tt&gt;? &lt;/li&gt;&lt;/ul&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-4278223596221717304?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/4278223596221717304/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/cc-program-compilation.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4278223596221717304'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4278223596221717304'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/cc-program-compilation.html' title='C/C++ Program Compilation'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-2338126567208585520</id><published>2009-01-14T08:37:00.000-08:00</published><updated>2009-01-14T08:39:07.040-08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='IDE'/><title type='text'>The Common Desktop Environment</title><content type='html'>&lt;h1&gt;&lt;a href="" name="SECTION00200000000000000000"&gt; The Common Desktop Environment&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="sec:CDE_use"&gt;&amp;nbsp;&lt;/a&gt;&lt;a href="" name="29"&gt;&amp;nbsp;&lt;/a&gt;  In order to use Solaris and most other Unix Systems you will need to be familiar with the Common Desktop Environment (CDE). Before embarking on learning C with briefly introduce the main features of the CDE. &lt;br /&gt;Most major Unix vendors now provide the CDE as standard. Consequently, most users of the X Window system will now be exposed to the CDE. Indeed, continuing trends in the development of Motif and CDE will probably lead to a convergence of these technologies in the near future. This section highlights the key features of the CDE from a Users perspective. &lt;br /&gt;Upon login, the user is presented with the CDE Desktop (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:CDE_desktop"&gt;1.1&lt;/a&gt;).  The desktop includes a front panel (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:CDE_panel"&gt;1.2&lt;/a&gt;)&lt;a href="" name="32"&gt;&amp;nbsp;&lt;/a&gt;, multiple virtual workspaces, and window management.  CDE supports the running of applications  from a file manager, from an application manager and from the front panel.  Each of the subcomponents of the desktop are described below. &lt;br /&gt;&lt;a href="" name="fig:CDE_desktop"&gt;&amp;nbsp;&lt;/a&gt;&lt;img src="http://www.cs.cf.ac.uk/Dave/C/CDE.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:CDE_desktop"&gt;1.1&lt;/a&gt; Sample CDE Desktop&lt;/b&gt; &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00210000000000000000"&gt; The front panel&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="36"&gt;&amp;nbsp;&lt;/a&gt;  The front panel (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:CDE_panel"&gt;1.2&lt;/a&gt;) contains a set of icons and popup menus (more like roll-up menus) that appear at the bottom of the screen, by default (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:CDE_desktop"&gt;1.1&lt;/a&gt;).  The front panel contains the most regularly used applications and tools for managing the workspace.  Users can drag-and-drop application icons from the file manager or application manager to the popups for addition of the application(s) to the associated menu.  The user can also manipulate the default actions and icons for the popups.  The front panel can be locked so that users can't change it.  A user can configure several virtual workspaces -- each with different backgrounds and colors if desired.  Each workspace can have any number of applications running in it.  An application can be set to appear in one, more than one, or all workspaces simultaneously. &lt;br /&gt;&lt;a href="" name="fig:CDE_panel"&gt;&amp;nbsp;&lt;/a&gt; &lt;img src="http://www.cs.cf.ac.uk/Dave/C/CDE_panel.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:CDE_panel"&gt;1.2&lt;/a&gt; Clients, Servers and Xlib&lt;/b&gt; &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00220000000000000000"&gt; The file manager&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="42"&gt;&amp;nbsp;&lt;/a&gt;  CDE includes a standard file manager. The functionality is similar to that of the Microsoft Windows, Macintosh, or Sun Open Look file manager. Users can directly manipulate icons associated with UNIX files, drag-and-drop them, and launch associated applications. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00230000000000000000"&gt; The application manager&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="44"&gt;&amp;nbsp;&lt;/a&gt;  The user interaction with the application manager is similar to the file manager except that is is intended to be a list of executable modules available to a particular user. The user launches the application manager from an icon in the front panel. Users are notified when a new application is available on a server by additions (or deletions) to the list of icons in the application manager window. Programs and icons can be installed and pushed out to other workstations as an integral part of the installation process. The list of workstations that new software is installed on is configurable. The application manager comes preconfigured to include several utilities and programs. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00240000000000000000"&gt; The session manager&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="46"&gt;&amp;nbsp;&lt;/a&gt;  The session manager is responsible for the start up and shut down of a user session.   In the CDE, applications that are made &lt;em&gt;CDE aware&lt;/em&gt; are warned via an X Event when the X session is closing down. The application responds by returning a string that can be used by the session manager at the user's next login to restart the application. CDE  can remember  two sessions per user. One is the &lt;em&gt;current&lt;/em&gt; session, where a snapshot of the currently running applications is saved.  These applications can be automatically restarted at the user's next login. The other is the default login, which is analogous to starting an X session in the Motif window manager. The user can choose  which of the two sessions to use at the next login. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00250000000000000000"&gt; Other CDE desktop tools&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="50"&gt;&amp;nbsp;&lt;/a&gt;  CDE 1.0 includes a set of applications that enable users to become productive immediately. Many of these are available directly from the front panel, others from the desktop or personal application managers. Common and productive desktop tools include: &lt;br /&gt;&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;Mail Tool&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;--  Used to compose, view, and manage electronic mail through a GUI. Allows the inclusion of attachments and communications with other      applications through the messaging system. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Calendar Manager&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;--  Used to manage, schedule, and view      appointments, create calendars, and interact with the Mail Tool. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Editor&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- A text editor with common functionality including data transfer      with other applications via the clipboard, drag and drop, and primary      and quick transfer. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Terminal Emulator&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;--  An &lt;em&gt;xterm&lt;/em&gt; terminal emulator. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Calculator&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- A standard calculator with scientific, financial, and      logical modes. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Print Manager&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- A graphical print job manager for the scheduling and      management of print jobs on any available printer. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Help System&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- A context-sensitive graphical help system based on      Standard Generalized Markup Language (SGML). &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Style Manager&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- A graphical interface that allows a user to      interactively set their preferences, such as colors, backdrops, and      fonts, for a session. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Icon Editor&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- This application is a fairly full featured graphical icon (pixmap) editor.  &lt;/dd&gt;&lt;/dl&gt;&lt;h1&gt;&lt;a href="" name="SECTION00260000000000000000"&gt; Application development tools&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="55"&gt;&amp;nbsp;&lt;/a&gt;  CDE includes two components for application development. The first is a shell command language interpreter that has built-in commands for most X Window system and CDE functions. The interpreter is based on ksh93 (The Korn Shell), and should provide anyone familiar with shell scripts the ability to develop X, Motif, and CDE applications. &lt;br /&gt;To support interactive user interface development, developers can use the Motif Application Builder. This is a GUI front end for building Motif applications that generates C source code. The source code is then compiled and linked with the X  and Motif libraries to produce the executable binary. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00270000000000000000"&gt; Application integration&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="57"&gt;&amp;nbsp;&lt;/a&gt;  CDE provides a number of tools to ease integration. The overall model of the CDE session is intended to allow a straightforward integration for virtually all types of applications. Motif and other X toolkit applications usually require little integration. &lt;br /&gt;The task of integrating in-house and third party applications into a desktop, often the most difficult aspect of a desktop installation, is simplified by CDE. The power and advantage of CDE functionality can be realized in most cases without recompiling applications. &lt;br /&gt;For example, Open Look&lt;a href="" name="58"&gt;&amp;nbsp;&lt;/a&gt; applications can be integrated through the use of scripts that perform front-end execution of the application and scripts that perform pre- and post-session processing. &lt;br /&gt;After the initial task of integrating applications so that they fit within session management, further integration can be done to increase their overall common &lt;em&gt;look-and-feel&lt;/em&gt;&lt;a href="" name="60"&gt;&amp;nbsp;&lt;/a&gt; with the rest of the desktop and to take advantage of the full range of CDE functionality. Tools that ease this aspect of integration include an &lt;em&gt;Icon Editor&lt;/em&gt; used to create colour and monochrome icons. Images can be copied from the desktop into an icon, or they can be drawn freehand. &lt;br /&gt;The &lt;em&gt;Action Creation Utility&lt;/em&gt; is used to create action entries in the action database. Actions allow applications to be launched using desktop icons, and they ease administration by removing an application's specific details from the user interface. &lt;br /&gt;The &lt;em&gt;Application Gather&lt;/em&gt; and &lt;em&gt;Application Integrate&lt;/em&gt; routines are used to control and format the application manager. They simplify installations so that applications can be accessible from virtually anywhere on the network. &lt;br /&gt;&lt;h1&gt;&lt;a href="" name="SECTION00280000000000000000"&gt; Windows and the Window Manager&lt;/a&gt; &lt;/h1&gt;&lt;a href="" name="sec:window_menu"&gt;&amp;nbsp;&lt;/a&gt;&lt;a href="" name="67"&gt;&amp;nbsp;&lt;/a&gt;  From a user's perspective, one of the first distinguishing features of Motif's &lt;em&gt;look and feel&lt;/em&gt;&lt;a href="" name="69"&gt;&amp;nbsp;&lt;/a&gt; is the &lt;em&gt;window frame&lt;/em&gt;&lt;a href="" name="71"&gt;&amp;nbsp;&lt;/a&gt; (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:winframe"&gt;1.3&lt;/a&gt;). Every application window is contained inside such a frame. The following items appear in the window frame: &lt;br /&gt;&lt;a href="" name="fig:winframe"&gt;&amp;nbsp;&lt;/a&gt; &lt;img src="http://www.cs.cf.ac.uk/Dave/C/win_frame.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:winframe"&gt;1.3&lt;/a&gt; The Motif Window Frame&lt;/b&gt; &lt;br /&gt;&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;Title Bar&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;&lt;a href="" name="76"&gt;&amp;nbsp;&lt;/a&gt; -- This identifies the window by a text string. The string is usually the name of the application program. However, an application's resource controls the label (Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#ch:resources"&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/cross_ref_motif.gif" /&gt;&lt;/a&gt;). &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Window Menu&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;&lt;a href="" name="78"&gt;&amp;nbsp;&lt;/a&gt; -- Every window under the control of &lt;em&gt;mwm&lt;/em&gt; has a window menu. The application has a certain amount of control over items that can be placed in the menu. The &lt;em&gt;Motif Style Guide&lt;/em&gt;&lt;a href="" name="192"&gt;&amp;nbsp;&lt;/a&gt; insists that certain commands are always available in this menu and that they can be accessed from either mouse or keyboard selection. Keyboard selections are called &lt;em&gt;mnemonics&lt;/em&gt; and allow routine actions (that may involve several mouse actions) to be called from the keyboard. The action from the keyboard usually involves pressing two keys at the same time: the &lt;em&gt;&lt;tt&gt;Meta&lt;/tt&gt;&lt;/em&gt; key&lt;a href="" name="84"&gt;&amp;nbsp;&lt;/a&gt;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/footnode.html#193" name="tex2html22"&gt;&lt;sup&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/foot_motif.gif" /&gt;&lt;/sup&gt;&lt;/a&gt; and another key. The default window menu items and &lt;em&gt;mnemonics&lt;/em&gt; are listed below and illustrated in Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:win_menu"&gt;1.4&lt;/a&gt;: &lt;a href="" name="fig:win_menu"&gt;&amp;nbsp;&lt;/a&gt; &lt;img src="http://www.cs.cf.ac.uk/Dave/C/win_menu.gif" /&gt; &lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:win_menu"&gt;1.4&lt;/a&gt; The Window Menu&lt;/b&gt; &lt;ul&gt;&lt;li&gt; &lt;b&gt;Restore (&lt;tt&gt;Meta+F5&lt;/tt&gt;)&lt;/b&gt;&lt;a href="" name="98"&gt;&amp;nbsp;&lt;/a&gt; -- Restore window to previous size after iconification (&lt;em&gt;see&lt;/em&gt; below). &lt;/li&gt;&lt;li&gt; &lt;b&gt;Move (&lt;tt&gt;Meta+F7&lt;/tt&gt;)&lt;/b&gt; -- Allows the window to be repositioned with a drag of the mouse. &lt;/li&gt;&lt;li&gt; &lt;b&gt;Size (&lt;tt&gt;Meta+F8&lt;/tt&gt;)&lt;/b&gt; -- Allows the size of the window to be changed by dragging on the corners of the window. &lt;/li&gt;&lt;li&gt; &lt;b&gt;Minimize (&lt;tt&gt;Meta+F9&lt;/tt&gt;)&lt;/b&gt;&lt;a href="" name="103"&gt;&amp;nbsp;&lt;/a&gt; -- Iconify  the window. &lt;/li&gt;&lt;li&gt; &lt;b&gt;Maximize (&lt;tt&gt;Meta+F10&lt;/tt&gt;)&lt;/b&gt;&lt;a href="" name="105"&gt;&amp;nbsp;&lt;/a&gt; -- Make the window the size of the root window, usually the whole of the display size. &lt;/li&gt;&lt;li&gt; &lt;b&gt;Lower (&lt;tt&gt;Meta+F3&lt;/tt&gt;)&lt;/b&gt;&lt;a href="" name="107"&gt;&amp;nbsp;&lt;/a&gt; -- Move the window to the bottom of the window stack. Windows may be &lt;em&gt;tiled&lt;/em&gt; on top of each other (&lt;em&gt;see&lt;/em&gt; below). The front window being the top of the stack. &lt;/li&gt;&lt;li&gt; &lt;b&gt;Close (&lt;tt&gt;Meta+F4&lt;/tt&gt;)&lt;/b&gt;&lt;a href="" name="111"&gt;&amp;nbsp;&lt;/a&gt; -- Quit the program. Some simple applications (Chapter&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#ch:1stprog"&gt;&lt;img align="bottom" alt="[*]" border="1" src="http://www.cs.cf.ac.uk/opt/LaTeX2html/icons.gif/cross_ref_motif.gif" /&gt;&lt;/a&gt;) provide no &lt;em&gt;internal&lt;/em&gt; means of termination. The &lt;tt&gt;Close&lt;/tt&gt; option being the only means to achieve this. &lt;/li&gt;&lt;/ul&gt;&lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Minimize Button&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- another way to iconify a window&lt;a href="" name="116"&gt;&amp;nbsp;&lt;/a&gt;. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Maximize Button&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- another way to make a window the size of the root window&lt;a href="" name="117"&gt;&amp;nbsp;&lt;/a&gt;. &lt;/dd&gt;&lt;/dl&gt;The window manager must also be able to manage multiple windows from multiple client applications. There are a few important issues that need to be resolved. When running several  applications together, several windows may be displayed on the screen. As a result, the display may appear cluttered and hard to navigate. The window manager provides two mechanisms to help deal with such problems: &lt;br /&gt;&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;Active Window&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- Only one window can receive input at any time. If you are selecting a graphical object with a mouse, then it is relatively easy for the window manager to detect this and schedule appropriate actions related to the chosen object. It is not so easy when you enter data or make selections directly from the keyboard. To resolve this only one window at a time is allowed &lt;em&gt;keyboard focus&lt;/em&gt;. This window is called the &lt;em&gt;active window&lt;/em&gt;.  The selection of the active window will depend on the system configuration which the user typically has control over. There are two common methods for selecting the active window:&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;Focus follows pointer&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- The active window is the window is the window underneath mouse pointer. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Click-to-type&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- The active window is selected, by clicking on an area of the window, and remains active until another window is selected no matter where the mouse points. &lt;/dd&gt;&lt;/dl&gt;When a window is made active its  appearance will change slightly: &lt;ul&gt;&lt;li&gt; Its outline frame will become shaded. &lt;/li&gt;&lt;li&gt; The cursor will change appearance when placed in the window. &lt;/li&gt;&lt;li&gt; The window may jump, or be &lt;em&gt;raised&lt;/em&gt; to the top of the window stack. &lt;/li&gt;&lt;/ul&gt;The exact appearance of the above may vary from system to system and may be controlled by the user by setting environment settings in the window manager. &lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;strong&gt;Window  tiling&lt;/strong&gt; &lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;-- Windows may be stacked on top of each other. The window manager tries to maintain a three-dimensional &lt;em&gt;look and feel&lt;/em&gt;&lt;a href="" name="128"&gt;&amp;nbsp;&lt;/a&gt;. Apart from the fact that buttons, dialog boxes appear to be elevated from the screen, windows are shaded and framed in a three-dimensional fashion. The top window (or currently active window) will have slightly different appearance for instance.  The window menu has a few options for controlling the tiling of a window. Also a window can be brought to the top of the stack, or &lt;em&gt;raised&lt;/em&gt; by clicking a part of its frame. &lt;/dd&gt;&lt;br /&gt;&lt;dt&gt;&lt;strong&gt;Iconification&lt;/strong&gt; &lt;/dt&gt;&lt;br /&gt;&lt;dd&gt;&lt;a href="" name="130"&gt;&amp;nbsp;&lt;/a&gt;&lt;a href="" name="131"&gt;&amp;nbsp;&lt;/a&gt; -- If a window is currently active and not required for input or displaying output then it may be &lt;em&gt;iconified&lt;/em&gt; or &lt;em&gt;minimised&lt;/em&gt; thus reducing the screen clutter. An icon (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:icon"&gt;1.5&lt;/a&gt;) is a small graphical symbol that represents the window (or application). It occupies a significantly less amount of screen area. Icons are usually arranged around the perimeter (typically bottom or left side) of the screen. The application will still be running and occupying computer memory. The window related to the icon may be reverted to by either double clicking on the icon, or selecting &lt;em&gt;Restore&lt;/em&gt; or &lt;em&gt; Maximise&lt;/em&gt;&lt;a href="" name="137"&gt;&amp;nbsp;&lt;/a&gt; from the icon's window menu.  &lt;br /&gt;&lt;br /&gt;&lt;div align="center"&gt;&lt;a href="" name="fig:icon"&gt;&amp;nbsp;&lt;/a&gt;&lt;a href="" name="140"&gt;&amp;nbsp;&lt;/a&gt; &lt;table&gt;&lt;caption&gt;&lt;strong&gt;Figure 1.5:&lt;/strong&gt; Sample Icon from Xterm Application&lt;/caption&gt; &lt;tbody&gt;&lt;tr&gt;&lt;td&gt;&lt;img alt="\begin{figure}\centerline{\psfig {figure=icon.ps,height=1.056in,width=1.083in}}\end{figure}" height="122" src="http://www.cs.cf.ac.uk/Dave/C/img2.gif" width="125" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;br /&gt;&lt;/dd&gt;&lt;/dl&gt;&lt;h1&gt;&lt;a href="" name="SECTION00290000000000000000"&gt; The Root Menu&lt;/a&gt; &lt;/h1&gt;The &lt;em&gt;Root Menu&lt;/em&gt;&lt;a href="" name="146"&gt;&amp;nbsp;&lt;/a&gt;&lt;a href="" name="147"&gt;&amp;nbsp;&lt;/a&gt; is the main menu of the window manager. The root menu typically is used to control the whole display, for example starting up new windows and quitting the desktop. To display the Root menu: &lt;br /&gt;&lt;ul&gt;&lt;li&gt; Move the mouse pointer to the Root Window. &lt;/li&gt;&lt;li&gt; Hold down the left mouse button. &lt;/li&gt;&lt;/ul&gt;The default Root Menu  has the following The root menu can be customised to start up common applications for example. The root menu for the &lt;em&gt;mwm&lt;/em&gt; (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:mwm_root_menu"&gt;1.6&lt;/a&gt;)&lt;a href="" name="152"&gt;&amp;nbsp;&lt;/a&gt; and &lt;em&gt; dtwm&lt;/em&gt; (Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:dtwm_root_menu"&gt;1.7&lt;/a&gt;) &lt;a href="" name="155"&gt;&amp;nbsp;&lt;/a&gt; have slightly different appearance but have broadly similar actions, which are summarised below: &lt;br /&gt;&lt;a href="" name="fig:mwm_root_menu"&gt;&amp;nbsp;&lt;/a&gt; &lt;img src="http://www.cs.cf.ac.uk/Dave/C/mwm_root.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:mwm_root_menu"&gt;1.6&lt;/a&gt; The &lt;em&gt;mwm&lt;/em&gt; Root Menu&lt;/b&gt; &lt;a href="" name="fig:dtwm_root_menu"&gt;&amp;nbsp;&lt;/a&gt; &lt;img src="http://www.cs.cf.ac.uk/Dave/C/rootmenu.gif" /&gt; &lt;br /&gt;&lt;b&gt;Fig.&amp;nbsp;&lt;a href="http://www.cs.cf.ac.uk/Dave/C/node2.html#fig:dtwm_root_menu"&gt;1.7&lt;/a&gt; The CDE &lt;em&gt;dtwm&lt;/em&gt; Root Menu&lt;/b&gt; &lt;br /&gt;&lt;dl&gt;&lt;dt&gt;&lt;strong&gt;Program&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;(&lt;em&gt;dtwm&lt;/em&gt;) -- A sub-menu is displayed that allows a variety of programs to be called from the desktop, for example to create a new window. The list of available programs can be customised from the desktop. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;New Window&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;(&lt;em&gt;mwm&lt;/em&gt;) -- Create a new window which is usually an &lt;em&gt;Xterm&lt;/em&gt; window. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Shuffle Up&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- Move the bottom of the window stack to the top. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Shuffle Down&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- Move the top of the window stack to the bottom. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Refresh&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- Refresh the current screen display. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Restart&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;-- Restart the Workspace. &lt;/dd&gt;&lt;dt&gt;&lt;strong&gt;Logout&lt;/strong&gt; &lt;/dt&gt;&lt;dd&gt;&lt;a href="" name="166"&gt;&amp;nbsp;&lt;/a&gt; (&lt;em&gt;dtwm&lt;/em&gt;) --  Quit the Window Manager. &lt;/dd&gt;&lt;/dl&gt;&lt;h1&gt;&lt;a href="" name="SECTION002100000000000000000"&gt; Exercises&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;Exercise 12158&lt;/b&gt;&lt;br /&gt;&lt;a href="" name="ex:cde1"&gt;&amp;nbsp;&lt;/a&gt; &lt;b&gt;Exercise~\ref{ex.cde1}&lt;/b&gt; &lt;br /&gt;&lt;br /&gt;Add an application to the application manager&lt;br /&gt;&lt;b&gt;Exercise 12159&lt;/b&gt;&lt;br /&gt;Practice opening, closing and moving windows around the screen and to/from the background/foreground. Get used to using the mouse and its buttons for such tasks.&lt;br /&gt;&lt;b&gt;Exercise 12160&lt;/b&gt;&lt;br /&gt;Figure out the function of each of the three mouse buttons. Pay particular attention to the different functions the buttons in different windows (applications) and also when the mouse is pointing to the background. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Exercise 12161&lt;/b&gt;&lt;br /&gt;Find out how to resize windows etc. and practice  this. &lt;br /&gt;&lt;b&gt;Exercise 12162&lt;/b&gt;&lt;br /&gt;Fire up the texteditor of your choice  (You may use &lt;tt&gt;dtpad&lt;/tt&gt; (basic but functional), &lt;tt&gt;textedit&lt;/tt&gt; application (SOLARIS basic editor), emacs/Xemacs, or vi) and practice editing text files. Create any files you wish for now. Figure out basic options like cut and paste of text around the file, saving and loading files, searching for strings in the text and replacing strings. &lt;br /&gt;Particularly pay attention in getting used to using the Key Strokes and / or mouse to perform the above tasks.&lt;br /&gt;&lt;b&gt;Exercise 12163&lt;/b&gt;&lt;br /&gt;Use Unix Commands  to  &lt;br /&gt;&lt;dl compact="compact"&gt;&lt;dt&gt;1. &lt;/dt&gt;&lt;dd&gt;Copy a file (created by text editor or other means) to another file called spare. &lt;/dd&gt;&lt;dt&gt;2. &lt;/dt&gt;&lt;dd&gt;Rename your original file to b called new.  &lt;/dd&gt;&lt;dt&gt;3. &lt;/dt&gt;&lt;dd&gt;Delete the file spare.  &lt;/dd&gt;&lt;dt&gt;4. &lt;/dt&gt;&lt;dd&gt;Display your original file on the terminal.  &lt;/dd&gt;&lt;dt&gt;5. &lt;/dt&gt;&lt;dd&gt;Print your file out. &lt;/dd&gt;&lt;/dl&gt;&lt;br /&gt;&lt;b&gt;Exercise 12164&lt;/b&gt;&lt;br /&gt;Familiarise yourself with other  UNIX functions by creating various  files of text etc. and trying out the various functions listed in handouts.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-2338126567208585520?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/2338126567208585520/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/common-desktop-environment.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2338126567208585520'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2338126567208585520'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2009/01/common-desktop-environment.html' title='The Common Desktop Environment'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-8233183625829805627</id><published>2008-12-31T18:44:00.000-08:00</published><updated>2008-12-31T18:47:58.592-08:00</updated><title type='text'>How to use the keyboard as input for games - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;Using the following code to let the player move in a game works. However, it doesn't really work the way we want. The problem is that when the player, for example, holds down the left arrow key and then fires, then the left movement will stop because another key than left is sent to the FormKeyDown() procedure. This will result in a nearly unplayable game in most cases. &lt;br /&gt;&lt;br /&gt;procedure TForm1.FormKeyDown(Sender: TObject; &lt;br /&gt;&amp;nbsp;&amp;nbsp;var Key: Word; Shift: TShiftState); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;case Key of &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_left&amp;nbsp;&amp;nbsp;: Dec(x); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_right : Inc(x);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_up&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;: Dec(y); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_down&amp;nbsp;&amp;nbsp;: Inc(x); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_space : BulletFired;&amp;nbsp;&amp;nbsp;// call to another procedure &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;What we need is to detect key presses without interrupting already present keys which are down. The solution for this is to keep a state of each key. This is done with the following global variables. &lt;br /&gt;&lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;movingUp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;: boolean; &lt;br /&gt;&amp;nbsp;&amp;nbsp;movingDown&amp;nbsp;&amp;nbsp;: boolean; &lt;br /&gt;&amp;nbsp;&amp;nbsp;movingRight : boolean; &lt;br /&gt;&amp;nbsp;&amp;nbsp;movingLeft&amp;nbsp;&amp;nbsp;: boolean; &lt;br /&gt;&lt;br /&gt;Now, instead of triggering the result directly (i.e. increasing/decreasing x/y position) we use the FormKeyDown() and FormKeyUp() procedures to toggle the state of each key. In this way, multiple keys may be down at once (for example the user may move up and right (diagonally) the same time as he presses fire). &lt;br /&gt;&lt;br /&gt;FormKeyDown sets respectively state to True. Because someone can't move both up AND down at the same time the "opposite" key has to be set to False. &lt;br /&gt;&lt;br /&gt;procedure TForm1.FormKeyDown(Sender: TObject; &lt;br /&gt;&amp;nbsp;&amp;nbsp;var Key: Word; Shift: TShiftState); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;case Key of &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_left: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movingLeft&amp;nbsp;&amp;nbsp;:= True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movingRight := False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_right: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movingLeft&amp;nbsp;&amp;nbsp;:= False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movingRight := True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_up: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movingUp&amp;nbsp;&amp;nbsp; := True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movingDown := False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_down: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movingUp&amp;nbsp;&amp;nbsp; := False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;movingDown := True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_space: BulletFired; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;When a key is released the state is set to False in the FormKeyUp procedure. &lt;br /&gt;&lt;br /&gt;procedure TForm1.FormKeyUp(Sender: TObject; &lt;br /&gt;&amp;nbsp;&amp;nbsp;var Key: Word; Shift: TShiftState); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;case Key of &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_left&amp;nbsp;&amp;nbsp;: movingLeft&amp;nbsp;&amp;nbsp;:= False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_right : movingRight := False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_up&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;: movingUp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;:= False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;vk_down&amp;nbsp;&amp;nbsp;: movingDown&amp;nbsp;&amp;nbsp;:= False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;The actual moving of the player is then performed from the game loop (Application.OnIdle for example) depending on each state. This is also the place to check to see if the player doesn't move out of bounds or collide into anything else. &lt;br /&gt;&lt;br /&gt;procedure TForm1.MovePlayer; &lt;br /&gt;&amp;nbsp;&amp;nbsp;if movingUp&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;then Dec(y); &lt;br /&gt;&amp;nbsp;&amp;nbsp;if movingDown&amp;nbsp;&amp;nbsp;then Inc(y); &lt;br /&gt;&amp;nbsp;&amp;nbsp;if movingLeft&amp;nbsp;&amp;nbsp;then Dec(x); &lt;br /&gt;&amp;nbsp;&amp;nbsp;if movingRight then Inc(x); &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;This article was an example of how it may be done. I've tried to make it as general as possible so it shouldn't be a problem to apply this to a project, no matter if it's &lt;a href="http://amazon.de/gp/product/3937858350?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=3937858350&amp;amp;adid=2f58ece5-495b-4127-8f60-cd84230ef830" id="amzn_cl_link_0" name="3937858350" style="border-bottom: 1px dashed; color: #990000; text-decoration: none;" target="_blank"&gt;a jump'n run&lt;/a&gt;, racing or a shoot'em up game. &lt;br /&gt;&lt;br /&gt;(more than one year later...) &lt;br /&gt;&lt;br /&gt;As commented below, there is also another approach of doing this by using GetKeyState. The reason for the above, quite lenghty, solution is basically that I didn't know of GetKeyState when I wrote it more than one year ago. Therefore I do this little rewrite and add the GetKeyState solution. &lt;br /&gt;&lt;br /&gt;Simply get rid of all the code above, and just replace the MovePlayer procedure with this code: &lt;br /&gt;&lt;br /&gt;procedure TMainForm.MovePlayer; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;if (GetKeyState(VK_UP) and $80) = $80 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dec(y); &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if (GetKeyState(VK_DOWN) and $80) = $80 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inc(y); &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if (GetKeyState(VK_LEFT) and $80) = $80 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dec(x); &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if (GetKeyState(VK_RIGHT) and $80) = $80 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inc(x); &lt;br /&gt;end; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-8233183625829805627?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/8233183625829805627/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-use-keyboard-as-input-for-games.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/8233183625829805627'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/8233183625829805627'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-use-keyboard-as-input-for-games.html' title='How to use the keyboard as input for games - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-6411652508628260050</id><published>2008-12-31T18:41:00.000-08:00</published><updated>2008-12-31T18:43:29.745-08:00</updated><title type='text'>How to send keystrokes to another application (a better one)  -Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;Found another unit which has exactly the same functionality as the SendKeys method of VB. The Code was written by Ken Henderson, email:khen@compuserve.com. &lt;br /&gt;&lt;span style="color: #3333ff;"&gt; &lt;br /&gt;Begin of listing &lt;br /&gt;====================================================================== &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(* &lt;br /&gt;SendKeys routine for 32-bit Delphi. &lt;br /&gt;&lt;br /&gt;Written by Ken Henderson &lt;br /&gt;&lt;br /&gt;Copyright (c) 1995 Ken Henderson email:khen@compuserve.com &lt;br /&gt;&lt;br /&gt;This unit includes two routines that simulate popular &lt;a href="http://amazon.de/gp/product/3836211165?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=3836211165&amp;amp;adid=19a8f147-ba1b-4c67-a348-e8a2c4736b65" id="amzn_cl_link_3" name="3836211165" style="border-bottom: 1px dashed; color: #990000; text-decoration: none;" target="_blank"&gt;Visual Basic&lt;/a&gt; &lt;br /&gt;routines: Sendkeys and AppActivate. SendKeys takes a PChar &lt;br /&gt;as its first parameter and a boolean as its second, like so: &lt;br /&gt;&lt;br /&gt;SendKeys('KeyString', Wait); &lt;br /&gt;&lt;br /&gt;where KeyString is a string of key names and modifiers that you want &lt;br /&gt;to send to the current input focus and Wait is a boolean variable or value &lt;br /&gt;that indicates whether SendKeys should wait for each key message to be &lt;br /&gt;processed before proceeding. See the table below for more information. &lt;br /&gt;&lt;br /&gt;AppActivate also takes a PChar as its only parameter, like so: &lt;br /&gt;&lt;br /&gt;AppActivate('WindowName'); &lt;br /&gt;&lt;br /&gt;where WindowName is the name of the window that you want to make the &lt;br /&gt;current input focus. &lt;br /&gt;&lt;br /&gt;SendKeys supports the Visual Basic SendKeys syntax, as documented below. &lt;br /&gt;&lt;br /&gt;Supported modifiers: &lt;br /&gt;&lt;br /&gt;+ = Shift &lt;br /&gt;^ = Control &lt;br /&gt;% = Alt &lt;br /&gt;&lt;br /&gt;Surround sequences of characters or key names with parentheses in order to &lt;br /&gt;modify them as a group. For example, '+abc' shifts only 'a', while '+(abc)' shifts &lt;br /&gt;all three characters. &lt;br /&gt;&lt;br /&gt;Supported special characters &lt;br /&gt;&lt;br /&gt;~ = Enter &lt;br /&gt;( = Begin modifier group (see above) &lt;br /&gt;) = End modifier group (see above) &lt;br /&gt;{ = Begin key name text (see below) &lt;br /&gt;} = End key name text (see below) &lt;br /&gt;&lt;br /&gt;Supported characters: &lt;br /&gt;&lt;br /&gt;Any character that can be typed is supported. Surround the modifier keys &lt;br /&gt;listed above with braces in order to send as normal text. &lt;br /&gt;&lt;br /&gt;Supported key names (surround these with braces): &lt;br /&gt;&lt;br /&gt;BKSP, BS, BACKSPACE &lt;br /&gt;BREAK &lt;br /&gt;CAPSLOCK &lt;br /&gt;CLEAR &lt;br /&gt;DEL &lt;br /&gt;DELETE &lt;br /&gt;DOWN &lt;br /&gt;END &lt;br /&gt;ENTER &lt;br /&gt;ESC &lt;br /&gt;ESCAPE &lt;br /&gt;F1 &lt;br /&gt;F2 &lt;br /&gt;F3 &lt;br /&gt;F4 &lt;br /&gt;F5 &lt;br /&gt;F6 &lt;br /&gt;F7 &lt;br /&gt;F8 &lt;br /&gt;F9 &lt;br /&gt;F10 &lt;br /&gt;F11 &lt;br /&gt;F12 &lt;br /&gt;F13 &lt;br /&gt;F14 &lt;br /&gt;F15 &lt;br /&gt;F16 &lt;br /&gt;HELP &lt;br /&gt;HOME &lt;br /&gt;INS &lt;br /&gt;LEFT &lt;br /&gt;NUMLOCK &lt;br /&gt;PGDN &lt;br /&gt;PGUP &lt;br /&gt;PRTSC &lt;br /&gt;RIGHT &lt;br /&gt;SCROLLLOCK &lt;br /&gt;TAB &lt;br /&gt;UP &lt;br /&gt;&lt;br /&gt;Follow the keyname with a space and a number to send the specified key a &lt;br /&gt;given number of times (e.g., {left 6}). &lt;br /&gt;*) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;unit sndkey32; &lt;br /&gt;&lt;br /&gt;interface &lt;br /&gt;&lt;br /&gt;Uses SysUtils, Windows, Messages; &lt;br /&gt;&lt;br /&gt;Function SendKeys(SendKeysString : PChar; Wait : Boolean) : Boolean; &lt;br /&gt;function AppActivate(WindowName : PChar) : boolean; &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #3333ff;"&gt; &lt;br /&gt;{Buffer for working with PChar's} &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;const &lt;br /&gt;&amp;nbsp;&amp;nbsp;WorkBufLen = 40; &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;WorkBuf : array[0..WorkBufLen] of Char; &lt;br /&gt;&lt;br /&gt;implementation &lt;br /&gt;type &lt;br /&gt;&amp;nbsp;&amp;nbsp;THKeys = array[0..pred(MaxLongInt)] of byte; &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;AllocationSize : integer; &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #3333ff;"&gt; &lt;br /&gt;(* &lt;br /&gt;Converts a string of characters and key names to keyboard events and &lt;br /&gt;passes them to Windows. &lt;br /&gt;&lt;br /&gt;Example syntax: &lt;br /&gt;&lt;br /&gt;SendKeys('abc123{left}{left}{left}def{end}456{left 6}ghi{end}789', True); &lt;br /&gt;&lt;br /&gt;*) &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;Function SendKeys(SendKeysString : PChar; Wait : Boolean) : Boolean; &lt;br /&gt;type &lt;br /&gt;&amp;nbsp;&amp;nbsp;WBytes = array[0..pred(SizeOf(Word))] of Byte; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;TSendKey = record &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Name : ShortString; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;VKey : Byte; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;const &lt;br /&gt;&lt;span style="color: #3333ff;"&gt; &lt;br /&gt;{Array of keys that SendKeys recognizes.  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;If you add to this list, you must be sure to keep it sorted alphabetically &lt;br /&gt;&amp;nbsp;&amp;nbsp;by Name because a binary search routine is used to scan it.} &lt;br /&gt;&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;MaxSendKeyRecs = 41; &lt;br /&gt;&amp;nbsp;&amp;nbsp;SendKeyRecs : array[1..MaxSendKeyRecs] of TSendKey = &lt;br /&gt;&amp;nbsp;&amp;nbsp;( &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'BKSP'; VKey:VK_BACK), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'BS'; VKey:VK_BACK), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'BACKSPACE'; VKey:VK_BACK), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'BREAK'; VKey:VK_CANCEL), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'CAPSLOCK'; VKey:VK_CAPITAL), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'CLEAR'; VKey:VK_CLEAR), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'DEL'; VKey:VK_DELETE), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'DELETE'; VKey:VK_DELETE), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'DOWN'; VKey:VK_DOWN), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'END'; VKey:VK_END), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'ENTER'; VKey:VK_RETURN), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'ESC'; VKey:VK_ESCAPE), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'ESCAPE'; VKey:VK_ESCAPE), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F1'; VKey:VK_F1), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F10'; VKey:VK_F10), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F11'; VKey:VK_F11), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F12'; VKey:VK_F12), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F13'; VKey:VK_F13), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F14'; VKey:VK_F14), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F15'; VKey:VK_F15), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F16'; VKey:VK_F16), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F2'; VKey:VK_F2), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F3'; VKey:VK_F3), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F4'; VKey:VK_F4), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F5'; VKey:VK_F5), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F6'; VKey:VK_F6), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F7'; VKey:VK_F7), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F8'; VKey:VK_F8), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'F9'; VKey:VK_F9), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'HELP'; VKey:VK_HELP), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'HOME'; VKey:VK_HOME), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'INS'; VKey:VK_INSERT), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'LEFT'; VKey:VK_LEFT), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'NUMLOCK'; VKey:VK_NUMLOCK), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'PGDN'; VKey:VK_NEXT), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'PGUP'; VKey:VK_PRIOR), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'PRTSC'; VKey:VK_PRINT), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'RIGHT'; VKey:VK_RIGHT), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'SCROLLLOCK'; VKey:VK_SCROLL), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'TAB'; VKey:VK_TAB), &lt;br /&gt;&amp;nbsp;&amp;nbsp; (Name:'UP'; VKey:VK_UP) &lt;br /&gt;&amp;nbsp;&amp;nbsp;); &lt;br /&gt;&lt;span style="color: #3333ff;"&gt;{Extra VK constants missing from Delphi's Windows API interface}&lt;/span&gt;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_NULL=0; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_SemiColon=186; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_Equal=187; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_Comma=188; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_Minus=189; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_Period=190; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_Slash=191; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_BackQuote=192; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_LeftBracket=219; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_BackSlash=220; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_RightBracket=221; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_Quote=222; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VK_Last=VK_Quote; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;ExtendedVKeys : set of byte = &lt;br /&gt;&amp;nbsp;&amp;nbsp;[VK_Up, &lt;br /&gt;&amp;nbsp;&amp;nbsp; VK_Down, &lt;br /&gt;&amp;nbsp;&amp;nbsp; VK_Left, &lt;br /&gt;&amp;nbsp;&amp;nbsp; VK_Right, &lt;br /&gt;&amp;nbsp;&amp;nbsp; VK_Home, &lt;br /&gt;&amp;nbsp;&amp;nbsp; VK_End, &lt;br /&gt;&amp;nbsp;&amp;nbsp; VK_Prior, &lt;span style="color: #3333ff;"&gt;{PgUp}&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp; VK_Next, &lt;span style="color: #3333ff;"&gt;{PgDn}&lt;/span&gt; &lt;br /&gt;&amp;nbsp;&amp;nbsp; VK_Insert, &lt;br /&gt;&amp;nbsp;&amp;nbsp; VK_Delete]; &lt;br /&gt;&lt;br /&gt;const &lt;br /&gt;&amp;nbsp;&amp;nbsp;INVALIDKEY = $FFFF; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VKKEYSCANSHIFTON = $01; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VKKEYSCANCTRLON = $02; &lt;br /&gt;&amp;nbsp;&amp;nbsp;VKKEYSCANALTON = $04; &lt;br /&gt;&amp;nbsp;&amp;nbsp;UNITNAME = 'SendKeys'; &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;UsingParens, ShiftDown, ControlDown, AltDown, FoundClose : Boolean; &lt;br /&gt;&amp;nbsp;&amp;nbsp;PosSpace : Byte; &lt;br /&gt;&amp;nbsp;&amp;nbsp;I, L : Integer; &lt;br /&gt;&amp;nbsp;&amp;nbsp;NumTimes, MKey : Word; &lt;br /&gt;&amp;nbsp;&amp;nbsp;KeyString : String[20]; &lt;br /&gt;&lt;br /&gt;procedure DisplayMessage(Message : PChar); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;MessageBox(0,Message,UNITNAME,0); &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;function BitSet(BitTable, BitMask : Byte) : Boolean; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result:=ByteBool(BitTable and BitMask); &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;procedure SetBit(var BitTable : Byte; BitMask : Byte); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;BitTable:=BitTable or Bitmask; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;Procedure KeyboardEvent(VKey, ScanCode : Byte; Flags : Longint); &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;KeyboardMsg : TMsg; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;keybd_event(VKey, ScanCode, Flags,0); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (Wait) then While (PeekMessage(KeyboardMsg,0,WM_KEYFIRST, WM_KEYLAST, PM_REMOVE)) do begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TranslateMessage(KeyboardMsg); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DispatchMessage(KeyboardMsg); &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;Procedure SendKeyDown(VKey: Byte; NumTimes : Word; GenUpMsg : Boolean); &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;Cnt : Word; &lt;br /&gt;&amp;nbsp;&amp;nbsp;ScanCode : Byte; &lt;br /&gt;&amp;nbsp;&amp;nbsp;NumState : Boolean; &lt;br /&gt;&amp;nbsp;&amp;nbsp;KeyBoardState : TKeyboardState; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (VKey=VK_NUMLOCK) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NumState:=ByteBool(GetKeyState(VK_NUMLOCK) and 1); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;GetKeyBoardState(KeyBoardState); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If NumState then KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] and not 1) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else KeyBoardState[VK_NUMLOCK]:=(KeyBoardState[VK_NUMLOCK] or 1); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetKeyBoardState(KeyBoardState); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;exit; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;ScanCode:=Lo(MapVirtualKey(VKey,0)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;For Cnt:=1 to NumTimes do &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (VKey in ExtendedVKeys)then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (GenUpMsg) then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY or KEYEVENTF_KEYUP) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end else begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;KeyboardEvent(VKey, ScanCode, 0); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (GenUpMsg) then KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;Procedure SendKeyUp(VKey: Byte); &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;ScanCode : Byte; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;ScanCode:=Lo(MapVirtualKey(VKey,0)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (VKey in ExtendedVKeys)then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;KeyboardEvent(VKey, ScanCode, KEYEVENTF_EXTENDEDKEY and KEYEVENTF_KEYUP) &lt;br /&gt;&amp;nbsp;&amp;nbsp;else KeyboardEvent(VKey, ScanCode, KEYEVENTF_KEYUP); &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;Procedure SendKey(MKey: Word; NumTimes : Word; GenDownMsg : Boolean); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (BitSet(Hi(MKey),VKKEYSCANSHIFTON)) then SendKeyDown(VK_SHIFT,1,False); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (BitSet(Hi(MKey),VKKEYSCANCTRLON)) then SendKeyDown(VK_CONTROL,1,False); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (BitSet(Hi(MKey),VKKEYSCANALTON)) then SendKeyDown(VK_MENU,1,False); &lt;br /&gt;&amp;nbsp;&amp;nbsp;SendKeyDown(Lo(MKey), NumTimes, GenDownMsg); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (BitSet(Hi(MKey),VKKEYSCANSHIFTON)) then SendKeyUp(VK_SHIFT); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (BitSet(Hi(MKey),VKKEYSCANCTRLON)) then SendKeyUp(VK_CONTROL); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (BitSet(Hi(MKey),VKKEYSCANALTON)) then SendKeyUp(VK_MENU); &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #3333ff;"&gt;{Implements a simple binary search to locate special key name strings}&lt;/span&gt; &lt;br /&gt;Function StringToVKey(KeyString : ShortString) : Word; &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;Found, Collided : Boolean; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Bottom, Top, Middle : Byte; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result:=INVALIDKEY; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Bottom:=1; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Top:=MaxSendKeyRecs; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Found:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Middle:=(Bottom+Top) div 2; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Repeat &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Collided:=((Bottom=Middle) or (Top=Middle)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (KeyString=SendKeyRecs[Middle].Name) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Found:=True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Result:=SendKeyRecs[Middle].VKey; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end else begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If (KeyString&amp;gt;SendKeyRecs[Middle].Name) then Bottom:=Middle &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else Top:=Middle; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Middle:=(Succ(Bottom+Top)) div 2; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Until (Found or Collided); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (Result=INVALIDKEY) then DisplayMessage('Invalid Key Name'); &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;procedure PopUpShiftKeys; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (not UsingParens) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If ShiftDown then SendKeyUp(VK_SHIFT); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If ControlDown then SendKeyUp(VK_CONTROL); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If AltDown then SendKeyUp(VK_MENU); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShiftDown:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ControlDown:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AltDown:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;AllocationSize:=MaxInt; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;UsingParens:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;ShiftDown:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;ControlDown:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;AltDown:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;I:=0; &lt;br /&gt;&amp;nbsp;&amp;nbsp;L:=StrLen(SendKeysString); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (L&amp;gt;AllocationSize) then L:=AllocationSize; &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (L=0) then Exit; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;While (I &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;case SendKeysString[I] of &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'(' : begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;UsingParens:=True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;')' : begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;UsingParens:=False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PopUpShiftKeys; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'%' : begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AltDown:=True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SendKeyDown(VK_MENU,1,False); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'+' : begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ShiftDown:=True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SendKeyDown(VK_SHIFT,1,False); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'^' : begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ControlDown:=True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SendKeyDown(VK_CONTROL,1,False); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'{' : begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;NumTimes:=1; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (SendKeysString[Succ(I)]='{') then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MKey:=VK_LEFTBRACKET; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetBit(Wbytes(MKey)[1],VKKEYSCANSHIFTON); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SendKey(MKey,1,True); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PopUpShiftKeys; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inc(I,3); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Continue; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;KeyString:=''; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FoundClose:=False; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;While (I&amp;lt;=L) do begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (SendKeysString[I]='}') then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FoundClose:=True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Break; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;KeyString:=KeyString+Upcase(SendKeysString[I]); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (Not FoundClose) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DisplayMessage('No Close'); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Exit; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (SendKeysString[I]='}') then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MKey:=VK_RIGHTBRACKET; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetBit(Wbytes(MKey)[1],VKKEYSCANSHIFTON); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SendKey(MKey,1,True); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PopUpShiftKeys; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Continue; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PosSpace:=Pos(' ',KeyString); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (PosSpace&amp;lt;&amp;gt;0) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; NumTimes:=StrToInt(Copy(KeyString,Succ(PosSpace),Length(KeyString)-PosSpace)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; KeyString:=Copy(KeyString,1,Pred(PosSpace)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (Length(KeyString)=1) then MKey:=vkKeyScan(KeyString[1]) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else MKey:=StringToVKey(KeyString); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (MKey&amp;lt;&amp;gt;INVALIDKEY) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SendKey(MKey,NumTimes,True); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PopUpShiftKeys; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Continue; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'~' : begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SendKeyDown(VK_RETURN,1,True); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PopUpShiftKeys; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MKey:=vkKeyScan(SendKeysString[I]); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; If (MKey&amp;lt;&amp;gt;INVALIDKEY) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SendKey(MKey,1,True); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PopUpShiftKeys; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end else DisplayMessage('Invalid KeyName'); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Inc(I); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result:=true; &lt;br /&gt;&amp;nbsp;&amp;nbsp;PopUpShiftKeys; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #3333ff;"&gt;{AppActivate &lt;br /&gt;&lt;br /&gt;This is used to set the current input focus to a given window using its &lt;br /&gt;name. This is especially useful for ensuring a window is active before &lt;br /&gt;sending it input messages using the SendKeys function. You can specify &lt;br /&gt;a window's name in its entirety, or only portion of it, beginning from &lt;br /&gt;the left. &lt;br /&gt;&lt;br /&gt;}&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;WindowHandle : HWND; &lt;br /&gt;&lt;br /&gt;function EnumWindowsProc(WHandle: HWND; lParam: LPARAM): BOOL; export; stdcall; &lt;br /&gt;const &lt;br /&gt;&amp;nbsp;&amp;nbsp;MAX_WINDOW_NAME_LEN = 80; &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;WindowName : array[0..MAX_WINDOW_NAME_LEN] of char; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;span style="color: #3333ff;"&gt;{Can't test GetWindowText's return value since some windows don't have a title}&lt;/span&gt;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;GetWindowText(WHandle,WindowName,MAX_WINDOW_NAME_LEN); &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result := (StrLIComp(WindowName,PChar(lParam), StrLen(PChar(lParam))) &amp;lt;&amp;gt; 0); &lt;br /&gt;&amp;nbsp;&amp;nbsp;If (not Result) then WindowHandle:=WHandle; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;function AppActivate(WindowName : PChar) : boolean; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;try &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result:=true; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WindowHandle:=FindWindow(nil,WindowName); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (WindowHandle=0) then EnumWindows(@EnumWindowsProc,Intege (PChar(WindowName))); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;If (WindowHandle&amp;lt;&amp;gt;0) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SendMessage(WindowHandle, WM_SYSCOMMAND, SC_HOTKEY, WindowHandle); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SendMessage(WindowHandle, WM_SYSCOMMAND, SC_RESTORE, WindowHandle); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end else Result:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;except &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;on Exception do Result:=false; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;end. &lt;br /&gt;&lt;br /&gt;&lt;span style="color: #3333ff;"&gt; &lt;br /&gt;======================================================================&lt;br /&gt;End of listing&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;Try creating a new unit and paste the code above in it. The comments in the unit explain how to use it. In Holland we would say: "Beter goed gejat dan slecht verzonnen" &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-6411652508628260050?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/6411652508628260050/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-send-keystrokes-to-another.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6411652508628260050'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6411652508628260050'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-send-keystrokes-to-another.html' title='How to send keystrokes to another application (a better one)  -Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3265114005653891701</id><published>2008-12-31T18:40:00.000-08:00</published><updated>2008-12-31T18:41:09.875-08:00</updated><title type='text'>Adding Application or Ports to the Windows Firewall - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;//Don't forget to add ActiveX and ComObj in your uses statement. &lt;br /&gt;&lt;br /&gt;Const &lt;br /&gt;&amp;nbsp;&amp;nbsp;NET_FW_PROFILE_DOMAIN = 0; &lt;br /&gt;&amp;nbsp;&amp;nbsp;NET_FW_PROFILE_STANDARD = 1; &lt;br /&gt;&amp;nbsp;&amp;nbsp;NET_FW_IP_VERSION_ANY = 2; &lt;br /&gt;&amp;nbsp;&amp;nbsp;NET_FW_IP_PROTOCOL_UDP = 17; &lt;br /&gt;&amp;nbsp;&amp;nbsp;NET_FW_IP_PROTOCOL_TCP = 6; &lt;br /&gt;&amp;nbsp;&amp;nbsp;NET_FW_SCOPE_ALL = 0; &lt;br /&gt;&amp;nbsp;&amp;nbsp;NET_FW_SCOPE_LOCAL_SUBNET = 1; &lt;br /&gt;&lt;br /&gt;procedure addPortToFirewall(EntryName:string;PortNumber:Cardinal); &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;fwMgr,port:OleVariant; &lt;br /&gt;&amp;nbsp;&amp;nbsp;profile:OleVariant; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;fwMgr := CreateOLEObject('HNetCfg.FwMgr'); &lt;br /&gt;&amp;nbsp;&amp;nbsp;profile := fwMgr.LocalPolicy.CurrentProfile; &lt;br /&gt;&amp;nbsp;&amp;nbsp;port := CreateOLEObject('HNetCfg.FWOpenPort'); &lt;br /&gt;&amp;nbsp;&amp;nbsp;port.Name := EntryName; &lt;br /&gt;&amp;nbsp;&amp;nbsp;port.Protocol := NET_FW_IP_PROTOCOL_TCP; &lt;br /&gt;&amp;nbsp;&amp;nbsp;port.Port := PortNumber; &lt;br /&gt;&amp;nbsp;&amp;nbsp;port.Scope := NET_FW_SCOPE_ALL; &lt;br /&gt;&amp;nbsp;&amp;nbsp;port.Enabled := true; &lt;br /&gt;&amp;nbsp;&amp;nbsp;profile.GloballyOpenPorts.Add(port); &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;procedure addApplicationToFirewall(EntryName:string;ApplicationPathAndExe:string); &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;fwMgr,app,port:OleVariant; &lt;br /&gt;&amp;nbsp;&amp;nbsp;profile:OleVariant; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;fwMgr := CreateOLEObject('HNetCfg.FwMgr'); &lt;br /&gt;&amp;nbsp;&amp;nbsp;profile := fwMgr.LocalPolicy.CurrentProfile; &lt;br /&gt;&amp;nbsp;&amp;nbsp;app := CreateOLEObject('HNetCfg.FwAuthorizedApplication'); &lt;br /&gt;&amp;nbsp;&amp;nbsp;app.ProcessImageFileName := ApplicationPathAndExe; &lt;br /&gt;&amp;nbsp;&amp;nbsp;app.Name := EntryName; &lt;br /&gt;&amp;nbsp;&amp;nbsp;app.Scope := NET_FW_SCOPE_ALL; &lt;br /&gt;&amp;nbsp;&amp;nbsp;app.IpVersion := NET_FW_IP_VERSION_ANY; &lt;br /&gt;&amp;nbsp;&amp;nbsp;app.Enabled :=true; &lt;br /&gt;&amp;nbsp;&amp;nbsp;profile.AuthorizedApplications.Add(app); &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//more things you can see at this Address: http://msdn2.microsoft.com/en-us/library/aa366415.aspx  &lt;br /&gt;&lt;br /&gt;NOTE: If you application doesn't initialize any activex objects you will also need to call CoInitialize(nil); before using the firewall functions and CoUninitialize; at the end.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3265114005653891701?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3265114005653891701/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/adding-application-or-ports-to-windows.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3265114005653891701'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3265114005653891701'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/adding-application-or-ports-to-windows.html' title='Adding Application or Ports to the Windows Firewall - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-6383740317724995030</id><published>2008-12-31T18:39:00.000-08:00</published><updated>2008-12-31T18:40:27.177-08:00</updated><title type='text'>How to detect a program that Not Responding? - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;function IsHung(theWindow: Hwnd; timeOut: LongInt): Boolean; &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;dwResult: DWord; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result:= SendMessageTimeOut(theWindow, WM_NULL, 0, 0 , &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SMTO_ABORTIFHUNG or SMTO_BLOCK, timeOut, dwResult) &amp;lt;&amp;gt; 0; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;you can use FindWindow() function to get Handle of Window. &lt;br /&gt;timOut is in Miliseconds.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-6383740317724995030?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/6383740317724995030/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-detect-program-that-not.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6383740317724995030'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6383740317724995030'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-detect-program-that-not.html' title='How to detect a program that Not Responding? - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3532873948472120072</id><published>2008-12-31T18:31:00.000-08:00</published><updated>2008-12-31T18:32:57.710-08:00</updated><title type='text'>Execute screensaver, then force execute your application at the same time</title><content type='html'>&lt;span id="ArticleContent"&gt;&lt;br /&gt;function KillScreenSaverWndProc(hwndx: HWND; lParamx: LPARAM): boolean; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp; PostMessage(hwndx, WM_CLOSE, 0, 0); &lt;br /&gt;&amp;nbsp;&amp;nbsp; Result := true; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;: &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp; osversioninfo: TOSVersionInfo; &lt;br /&gt;&amp;nbsp;&amp;nbsp; hssWnd: HWND; &lt;br /&gt;&amp;nbsp;&amp;nbsp; hDeskWnd: HDESK; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp; osversioninfo.dwOSVersionInfoSize := sizeof(osversioninfo); &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; GetVersionEx(osversioninfo); &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; case osversioninfo.dwPlatformId of &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; VER_PLATFORM_WIN32_WINDOWS: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hssWnd := FindWindow('WindowsScreenSaverClass', nil); &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if hwwWnd &amp;lt;&amp;gt; 0 then &lt;br /&gt;PostMessage(hsswnd, WM_CLOSE, 0, 0); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; VER_PLATFORM_WIN32_NT: &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; hDeskWnd := OpenDesktop( &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'Screen-saver', &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; false, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DESKTOP_READOBJECTS or DESKTOP_WRITEOBJECTS &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ); &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if hDeskWnd &amp;lt;&amp;gt; 0 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; EnumDesktopWindows(hDeskWnd, TFNWndEnumProc(@KillScreenSaverWndProc), 0); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; CloseDesktop(hDeskWnd); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end; &lt;br /&gt;&amp;nbsp;&amp;nbsp; end; &lt;br /&gt;end; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3532873948472120072?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3532873948472120072/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/execute-screensaver-then-force-execute.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3532873948472120072'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3532873948472120072'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/execute-screensaver-then-force-execute.html' title='Execute screensaver, then force execute your application at the same time'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-5412520741955025864</id><published>2008-12-31T18:25:00.000-08:00</published><updated>2008-12-31T18:28:48.256-08:00</updated><title type='text'>Send characters to another control (in any application)</title><content type='html'>&lt;span id="ArticleContent"&gt;There are several methods to send keystrokes or characters to a WinControl. The SetKeyboardState requires the control to have the focus but can send more (esp. special) keys. &lt;br /&gt;The use of WM_CHAR message enables you to send characters even to a hidden control unless you have found out the handle of it (there are a couple of ways to find out a controls handle). &lt;br /&gt;Once you've got it, you can &lt;a href="http://amazon.de/gp/product/0671872389?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=0671872389&amp;amp;adid=fba32fc3-9df3-4f14-8d8c-9e984c61ec02" id="amzn_cl_link_0" name="0671872389" style="border-bottom: 1px dashed; color: #990000; text-decoration: none;" target="_blank"&gt;send messages to it&lt;/a&gt;. &lt;br /&gt;&lt;br /&gt;I'm using this method for a little tool to "type" certain frequently used phrases while posting to newsgroups. &lt;br /&gt;&lt;br /&gt;I hardcoded the handle of the edit control of my favorite newsreader and now I have a small and handy tool to type messages faster than ever. &lt;br /&gt;&lt;br /&gt;// === code starts here === &lt;br /&gt;&lt;br /&gt;procedure SendMsg(const h: HWND; const s: string); &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;i: integer; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;if h = 0 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Exit; &lt;br /&gt;&amp;nbsp;&amp;nbsp;if Length(s) = 0 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Exit; &lt;br /&gt;&amp;nbsp;&amp;nbsp;for i:= 1 to Length(s) do &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if Ord(s[i]) in [9, 13, 32..254] then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SendMessage(h, WM_CHAR, Ord(s[i]), 0); &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;// === code ends here === &lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-5412520741955025864?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/5412520741955025864/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/send-characters-to-another-control-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5412520741955025864'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5412520741955025864'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/send-characters-to-another-control-in.html' title='Send characters to another control (in any application)'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-1122766580166568969</id><published>2008-12-31T18:21:00.000-08:00</published><updated>2008-12-31T18:24:19.789-08:00</updated><title type='text'>How to find out if CAPS LOCK is on? b - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;function IsCapsLockOn : Boolean; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result := 0 &amp;lt;&amp;gt; (GetKeyState(VK_CAPITAL) and $01); &lt;br /&gt;end; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-1122766580166568969?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/1122766580166568969/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-find-out-if-caps-lock-is-on-b.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/1122766580166568969'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/1122766580166568969'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-find-out-if-caps-lock-is-on-b.html' title='How to find out if CAPS LOCK is on? b - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-1066779488626806010</id><published>2008-12-31T18:17:00.000-08:00</published><updated>2008-12-31T18:19:10.570-08:00</updated><title type='text'>Changing the screen resolution - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;To change the screen resolution you can use the following function &lt;br /&gt;which is a wrapper for the Windows API ChangeDisplaySettings. The &lt;br /&gt;function takes the desired width and height as parameters and returns &lt;br /&gt;the return value of ChangeDisplaySettings (see the documentation for &lt;br /&gt;more datails). &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;function SetScreenResolution(Width, Height: integer): Longint; &lt;br /&gt;&amp;nbsp;&amp;nbsp;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DeviceMode: TDeviceMode; &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;with DeviceMode do begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dmSize := SizeOf(TDeviceMode); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dmPelsWidth := Width; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dmPelsHeight := Height; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;dmFields := DM_PELSWIDTH or DM_PELSHEIGHT; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result := ChangeDisplaySettings(DeviceMode, CDS_UPDATEREGISTRY); &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;You can use ChangeDisplaySettings to change other properties of the &lt;br /&gt;display like the color depth and the display frequency. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Sample call &lt;br /&gt;----------- &lt;br /&gt;&lt;br /&gt;In the following example first we get the current screen resolution &lt;br /&gt;before setting it to 800x600, and then we restore it calling &lt;br /&gt;SetScreenResolution again. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OldWidth, OldHeight: integer; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;procedure TForm1.Button1Click(Sender: TObject); &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OldWidth := GetSystemMetrics(SM_CXSCREEN); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OldHeight := GetSystemMetrics(SM_CYSCREEN); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetScreenResolution(800, 600); &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;procedure TForm1.Button2Click(Sender: TObject); &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetScreenResolution(OldWidth, OldHeight); &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-1066779488626806010?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/1066779488626806010/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/changing-screen-resolution-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/1066779488626806010'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/1066779488626806010'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/changing-screen-resolution-delphi.html' title='Changing the screen resolution - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-4891819688575859090</id><published>2008-12-31T18:16:00.000-08:00</published><updated>2008-12-31T18:17:14.096-08:00</updated><title type='text'>Show Windows properties window - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;This function uses Shell API &lt;br /&gt;&lt;br /&gt;The function: &lt;br /&gt;&lt;br /&gt;function VisaEgenskaper(hWndOwner: HWND; const FileName: string): Boolean; &lt;br /&gt;var Info: TShellExecuteInfo; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;{ Fill in the SHELLEXECUTEINFO structure } &lt;br /&gt;&amp;nbsp;&amp;nbsp;with Info do &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cbSize := SizeOf(Info); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;fMask := SEE_MASK_NOCLOSEPROCESS or &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SEE_MASK_INVOKEIDLIST or &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SEE_MASK_FLAG_NO_UI; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;wnd&amp;nbsp;&amp;nbsp;:= hWndOwner; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lpVerb := 'properties'; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lpFile := pChar(FileName); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lpParameters := nil; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lpDirectory := nil; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nShow := 0; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hInstApp := 0; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;lpIDList := nil; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{ Call Windows to display the properties dialog. } &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result := ShellExecuteEx(@Info); &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Call the function with this line: &lt;br /&gt;&lt;br /&gt;VisaEgenskaper(Application.Handle, 'C:\Thomas.txt');&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-4891819688575859090?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/4891819688575859090/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/show-windows-properties-window-delphi_31.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4891819688575859090'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4891819688575859090'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/show-windows-properties-window-delphi_31.html' title='Show Windows properties window - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-8657031576203314905</id><published>2008-12-31T18:15:00.001-08:00</published><updated>2008-12-31T18:15:55.842-08:00</updated><title type='text'>Accessing the Windows Registry - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;What is the Registry? &lt;br /&gt;--------------------- &lt;br /&gt;&lt;br /&gt;It is where Windows stores many of its configuration options and also allows applications to access this data as well as save their own data. If you want to take a look at the registry, just execute the REGEDIT.EXE application located in the Windows directory. Be careful not to change anything or you could end up ruining your installation! Now, the data in the registry is stored in a tree structure. There are many roots (many trees): &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;HKEY_CLASSES_ROOT &lt;br /&gt;&amp;nbsp;&amp;nbsp;HKEY_CURRENT_USER &lt;br /&gt;&amp;nbsp;&amp;nbsp;HKEY_LOCAL_MACHINE &lt;br /&gt;&amp;nbsp;&amp;nbsp;HKEY_USERS &lt;br /&gt;&amp;nbsp;&amp;nbsp;HKEY_PERFORMANCE_DATA &lt;br /&gt;&amp;nbsp;&amp;nbsp;HKEY_CURRENT_CONFIG &lt;br /&gt;&amp;nbsp;&amp;nbsp;HKEY_DYN_DATA &lt;br /&gt;&lt;br /&gt;Each root can have values and keys. The values are data stored under item names (right panel of RegEdit). Keys can have values and other keys, forming a tree structure (left panel of RegEdit). &lt;br /&gt;&lt;br /&gt;For example, the tree HKEY_CURRENT_USER has many keys, like AppEvents, Control Panel, Identities, Network, Software, etc. Each key may have sub-keys. For example, Control Panel has some sub-keys, like Accessibility, Appearance, Colors, Cursors, Desktop, International, etc. All keys have at least one value (the first value in the right panel of RegEdit), which is the default value (the name of the value is the empty string), not necessarily set. A key may have more values. For example, let's see the key Control Panel\Colors under HKEY_CURRENT_USER. Appart from the default value, it has values like ActiveBorder, ActiveTitle, AppWorkspace, Background, etc. In turn, each "value" has a "data" (the actual value, sort to speak). For example, the data of the value ActiveTitle would be "0 0 128" (may be different if you are not using the standard Windows colors). &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;TRegistry &lt;br /&gt;--------- &lt;br /&gt;&lt;br /&gt;Before getting into GetRegistryData and SetRegistryData, let's see how we would accomplish the same tasks the hard way, using TRegistry. &lt;br /&gt;&lt;br /&gt;The TRegistry class is declared in the Registry unit, so you will have to add this unit to the Uses clause of the unit or program where you want to use it. To access a value in the registry, first you should create an object of this class, assign the root to its RootKey property (the values are defined in the Windows unit) and then try to open a key with the OpenKey function method, which will return True if successful. Then you can read (with the ReadXxxx functions) or write (with the WriteXxxx procedures) the values of the open key and, after that, you should close the key with CloseKey. When you are done with the registry, you should free the registry object you created. Let's see an example of how to obtain the name of the processor in our computer: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;procedure TForm1.Button1Click(Sender: TObject); &lt;br /&gt;&amp;nbsp;&amp;nbsp;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg: TRegistry; &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg := TRegistry.Create; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.RootKey := HKEY_LOCAL_MACHINE; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if Reg.OpenKey('\Hardware\Description\System' &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;+ '\CentralProcessor\0', False) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowMessage(Reg.ReadString('Identifier')); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.CloseKey; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; // if &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.Free; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;You can see another example in the article Determining the associated application. Of course, there are many more things you can do with the registry, like creating and deleting keys and values... &lt;br /&gt;&lt;br /&gt;The TRegistryIniFile class makes it simpler for applications to write and read their configuration information to and from the registry, while TRegistry operates at a lower level. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;GetRegistryData &lt;br /&gt;--------------- &lt;br /&gt;&lt;br /&gt;To simplify reading a data value from the registry you can use the following function that can read any data type from the registry and returns it as a variant (string or integer). The function performs exception handling. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;uses Registry; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;function GetRegistryData(RootKey: HKEY; Key, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Value: string): variant; &lt;br /&gt;&amp;nbsp;&amp;nbsp;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg: TRegistry; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;RegDataType: TRegDataType; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DataSize, Len: integer; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s: string; &lt;br /&gt;&amp;nbsp;&amp;nbsp;label cantread; &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg := nil; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg := TRegistry.Create(KEY_QUERY_VALUE); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.RootKey := RootKey; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if Reg.OpenKeyReadOnly(Key) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;RegDataType := Reg.GetDataType(Value); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (RegDataType = rdString) or &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (RegDataType = rdExpandString) then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result := Reg.ReadString(Value) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if RegDataType = rdInteger then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result := Reg.ReadInteger(Value) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if RegDataType = rdBinary then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DataSize := Reg.GetDataSize(Value); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if DataSize = -1 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise Exception.Create(SysErrorMessage(ERROR_CANTREAD)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetLength(s, DataSize); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Len := Reg.ReadBinaryData(Value, PChar(s)^, DataSize); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if Len &amp;lt;&amp;gt; DataSize then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise Exception.Create(SysErrorMessage(ERROR_CANTREAD)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result := s; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end else &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise Exception.Create(SysErrorMessage(ERROR_CANTREAD)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;except &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s := ''; // Deallocates memory if allocated &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.CloseKey; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.CloseKey; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end else &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise Exception.Create(SysErrorMessage(GetLastError)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;except &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.Free; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.Free; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;Sample Call: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;ShowMessage(GetRegistryData(HKEY_LOCAL_MACHINE, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'\Hardware\Description\System\CentralProcessor\0', 'Identifier')); &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SetRegistryData &lt;br /&gt;--------------- &lt;br /&gt;&lt;br /&gt;To simplify writing a data value to the registry you can use the following procedure that can write any data type to the registry. The procedure performs exception handling. &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;uses Registry; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;procedure SetRegistryData(RootKey: HKEY; Key, Value: string; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;RegDataType: TRegDataType; Data: variant); &lt;br /&gt;&amp;nbsp;&amp;nbsp;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg: TRegistry; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s: string; &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg := nil; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg := TRegistry.Create(KEY_WRITE); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.RootKey := RootKey; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if Reg.OpenKey(Key, True) then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if RegDataType = rdUnknown then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;RegDataType := Reg.GetDataType(Value); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if RegDataType = rdString then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.WriteString(Value, Data) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if RegDataType = rdExpandString then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.WriteExpandString(Value, Data) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if RegDataType = rdInteger then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.WriteInteger(Value, Data) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else if RegDataType = rdBinary then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;s := Data; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.WriteBinaryData(Value, PChar(s)^, Length(s)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end else &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise Exception.Create(SysErrorMessage(ERROR_CANTWRITE)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;except &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.CloseKey; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.CloseKey; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end else &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise Exception.Create(SysErrorMessage(GetLastError)); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;except &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.Free; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;raise; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.Free; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;Sample Call: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;SetRegistryData(HKEY_LOCAL_MACHINE, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'\Software\Microsoft\Windows\CurrentVersion', &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'RegisteredOrganization', rdString, 'Latium Software'); &lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-8657031576203314905?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/8657031576203314905/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/accessing-windows-registry-delphi.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/8657031576203314905'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/8657031576203314905'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/accessing-windows-registry-delphi.html' title='Accessing the Windows Registry - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-5224069411453942769</id><published>2008-12-31T18:14:00.000-08:00</published><updated>2008-12-31T18:15:02.996-08:00</updated><title type='text'>logged in as administator - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;const  &lt;br /&gt;&amp;nbsp;&amp;nbsp;SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority =  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(Value: (0, 0, 0, 0, 0, 5));  &lt;br /&gt;&amp;nbsp;&amp;nbsp;SECURITY_BUILTIN_DOMAIN_RID = $00000020;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;DOMAIN_ALIAS_RID_ADMINS&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = $00000220;  &lt;br /&gt;&lt;br /&gt;function IsAdmin: Boolean;  &lt;br /&gt;var  &lt;br /&gt;&amp;nbsp;&amp;nbsp;hAccessToken: THandle;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;ptgGroups: PTokenGroups;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;dwInfoBufferSize: DWORD;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;psidAdministrators: PSID;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;x: Integer;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;bSuccess: BOOL;  &lt;br /&gt;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result := False;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;bSuccess := OpenThreadToken(GetCurrentThread, TOKEN_QUERY, True,  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hAccessToken);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;if not bSuccess then  &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if GetLastError = ERROR_NO_TOKEN then  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bSuccess := OpenProcessToken(GetCurrentProcess, TOKEN_QUERY,  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hAccessToken);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;if bSuccess then  &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;GetMem(ptgGroups, 1024);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;bSuccess := GetTokenInformation(hAccessToken, TokenGroups,  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ptgGroups, 1024, dwInfoBufferSize);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CloseHandle(hAccessToken);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if bSuccess then  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;AllocateAndInitializeSid(SECURITY_NT_AUTHORITY, 2,  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0, 0, 0, 0, 0, 0, psidAdministrators);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{$R-}  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for x := 0 to ptgGroups.GroupCount - 1 do  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if EqualSid(psidAdministrators, ptgGroups.Groups[x].Sid) then  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result := True;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Break;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{$R+}  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FreeSid(psidAdministrators);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FreeMem(ptgGroups);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;end;  &lt;br /&gt;&lt;br /&gt;procedure TForm1.Button1Click(Sender: TObject);  &lt;br /&gt;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;if isAdmin then  &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowMessage('Logged in as Administrator');  &lt;br /&gt;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;end;  &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-5224069411453942769?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/5224069411453942769/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/logged-in-as-administator-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5224069411453942769'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5224069411453942769'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/logged-in-as-administator-delphi.html' title='logged in as administator - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-7043423958004559573</id><published>2008-12-31T18:13:00.000-08:00</published><updated>2008-12-31T18:14:23.183-08:00</updated><title type='text'>How to set the current screensaver - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;The following code sets up your desired screensave then shows it. For actual use this code needs to be changed to suite your needs i.e. remove ShowMessage dialogs, change name of screensaver, change timeout value and remove actual testing section. &lt;br /&gt;&lt;br /&gt;Tested under Windows XP but should work with earlier versions of Windows &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;program SetupDefaultScrnSaver; &lt;br /&gt;&lt;br /&gt;uses &lt;br /&gt;&amp;nbsp;&amp;nbsp;Windows, SysUtils, Forms, Dialogs, Registry; &lt;br /&gt;&lt;br /&gt;const &lt;br /&gt;&amp;nbsp;&amp;nbsp;{ from Windows.pas } &lt;br /&gt;&amp;nbsp;&amp;nbsp;SPI_SETSCREENSAVEACTIVE = 17; &lt;br /&gt;&amp;nbsp;&amp;nbsp;SPIF_SENDWININICHANGE = 2; &lt;br /&gt;&amp;nbsp;&amp;nbsp;SPI_SETSCREENSAVETIMEOUT = 15; &lt;br /&gt;&amp;nbsp;&amp;nbsp;{ from Messages.pas } &lt;br /&gt;&amp;nbsp;&amp;nbsp;WM_SYSCOMMAND&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; = $0112; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{ From Project JEDI JCL library &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;http://www.delphi-jedi.org/Jedi:CODELIBJCL:22798 } &lt;br /&gt;&amp;nbsp;&amp;nbsp;function GetWindowsSysFolder: string; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;procedure StrResetLength(var S: AnsiString); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetLength(S, StrLen(PChar(S))); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Required: Cardinal; &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result := ''; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Required := GetSystemDirectory(nil, 0); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if Required &amp;lt;&amp;gt; 0 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetLength(Result, Required); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;GetSystemDirectory(PChar(Result), Required); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StrResetLength(Result) ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;function SetScreenSave(const Name: string; Const TimeOut: Integer = 30):Boolean; &lt;br /&gt;&amp;nbsp;&amp;nbsp;const &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SixtySeconds = 60 ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg:TRegistry ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if not FileExists(Name) then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowMessage('ScreenSaver "' + Name + '" not located') ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;exit ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end ; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg := TRegistry.Create ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.RootKey := HKEY_CURRENT_USER ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;try &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;with Reg do &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if OpenKey('Control Panel\Desktop',False) then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WriteString('SCRNSAVE.EXE', Name) ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WriteString('ScreenSaverIsSecure','1') ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CloseKey ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end else &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result := False ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;exit ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;finally &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.Free ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end ; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SystemParametersInfo(SPI_SETSCREENSAVEACTIVE,1,nil,SPIF_SENDWININICHANGE); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,SixtySeconds * TimeOut, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;nil,SPIF_SENDWININICHANGE); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Result := True ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;{ -=CHANGE THE SCREENSAVER NAME TO ONE YOU WANT THE USER TO HAVE=- } &lt;br /&gt;&amp;nbsp;&amp;nbsp;if SetScreenSave(GetWindowsSysFolder + '\ss3dfo.scr') then&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowMessage('Screensaver set, press OK to test (remember grace period)') ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;PostMessage(GetDesktopWindow, WM_SYSCOMMAND, SC_SCREENSAVE, 0); &lt;br /&gt;&amp;nbsp;&amp;nbsp;end ; &lt;br /&gt;end. &lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-7043423958004559573?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/7043423958004559573/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-set-current-screensaver-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7043423958004559573'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7043423958004559573'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-set-current-screensaver-delphi.html' title='How to set the current screensaver - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-1518040769232741799</id><published>2008-12-31T18:12:00.000-08:00</published><updated>2008-12-31T18:13:13.181-08:00</updated><title type='text'>Making an application run automatically when Windows starts</title><content type='html'>&lt;span id="ArticleContent"&gt;One way is placing a direct access to the application in the Startup folder of Windows Start Menu. Alternatively, you can add a value under the appropriate key in the Windows Registry, as shown below: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;procedure TForm1.Button1Click(Sender: TObject); &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SetRegistryData(HKEY_LOCAL_MACHINE, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;'Software\Microsoft\Windows\CurrentVersion\Run', &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Application.Title, rdString, Application.ExeName); &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;NOTE: SetRegistryData has been featured in the article "Accessing the&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Windows Registry": http://www.delphi3000.com/articles/article_1575.asp &lt;br /&gt;&lt;br /&gt;Instead of Application.Title you can write a string with a unique name for the application, and instead of Application.ExeName you can write the full path name of the application (as well as its command-line parameters if they are needed)&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-1518040769232741799?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/1518040769232741799/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/making-application-run-automatically.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/1518040769232741799'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/1518040769232741799'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/making-application-run-automatically.html' title='Making an application run automatically when Windows starts'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3359234217704513390</id><published>2008-12-31T18:05:00.002-08:00</published><updated>2008-12-31T18:11:52.380-08:00</updated><title type='text'>Hiding caption bars - Delphi</title><content type='html'>&lt;div style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 9pt; margin: 10px 20px 20px;"&gt;//--------------------------------------------------------------------------- &lt;br /&gt;// Author&amp;nbsp;&amp;nbsp; : Digital Survivor [Esteban Rodríguez Nieto | José Plano] &lt;br /&gt;// Email&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;: plmad666@gmail.com | jose.plano@gmail.com &lt;br /&gt;// Web site : www.ds-studioscom.ar &lt;br /&gt;//--------------------------------------------------------------------------- &lt;br /&gt;&lt;br /&gt;We can use this example in the onCreate event: &lt;br /&gt;&lt;br /&gt;Procedure TForm1.FormCreate (Sender : TObject); &lt;br /&gt;&lt;br /&gt;Begin &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetWindowLong (Handle, GWL_STYLE, GetWindowLong (Handle, GWL_STYLE) AND NOT WS_CAPTION); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ClientHeight := Height; &lt;br /&gt;&lt;br /&gt;End; &lt;br /&gt;&lt;br /&gt;//--------------------------------------------------------------------------- &lt;br /&gt;&lt;/div&gt;&lt;div style="margin-top: 5px; text-align: center;"&gt;&lt;script type="text/javascript"&gt;&lt;!--google_ad_client = "pub-0924303565538336";google_alternate_color = "eeeecc";google_ad_width = 468;google_ad_height = 15;google_ad_format = "468x15_0ads_al";google_ad_channel = "8056644127";google_color_border = "eeeecc";google_color_bg = "eeeecc";google_color_link = "990000";google_color_text = "000000";google_color_url = "990000";//--&gt;&lt;/script&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3359234217704513390?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3359234217704513390/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/hiding-caption-bars-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3359234217704513390'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3359234217704513390'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/hiding-caption-bars-delphi.html' title='Hiding caption bars - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-997223401245429228</id><published>2008-12-31T18:05:00.001-08:00</published><updated>2008-12-31T18:05:38.696-08:00</updated><title type='text'>Detecting CPU type - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;//--------------------------------------------------------------------------- &lt;br /&gt;// Author&amp;nbsp;&amp;nbsp; : Digital Survivor [Esteban Rodríguez Nieto | José Plano] &lt;br /&gt;// Email&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;: plmad666@gmail.com | jose.plano@gmail.com &lt;br /&gt;// Web site : www.ds-studios.com.ar &lt;br /&gt;//--------------------------------------------------------------------------- &lt;br /&gt;&lt;br /&gt;{ Note: This code seems not to work in all machines. It's seems also not to work with all kinds of CPUs, but try it. It works for me... } &lt;br /&gt;&lt;br /&gt;Unit CPUid; &lt;br /&gt;&lt;br /&gt;Interface &lt;br /&gt;&lt;br /&gt;Type &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TCpuType = (cpu8086, cpu286, cpu386, cpu486, cpuPentium); &lt;br /&gt;&lt;br /&gt;Function CpuType : TCpuType; &lt;br /&gt;Function CpuTypeString : String; &lt;br /&gt;&lt;br /&gt;Implementation &lt;br /&gt;&lt;br /&gt;Uses &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;SysUtils; &lt;br /&gt;&lt;br /&gt;Function CpuType : TCpuType; ASSEMBLER; &lt;br /&gt;&lt;br /&gt;Asm &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; // 8086 CPU check &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; push ds &lt;br /&gt;&amp;nbsp;&amp;nbsp; pushf &lt;br /&gt;&amp;nbsp;&amp;nbsp; pop bx &lt;br /&gt;&amp;nbsp;&amp;nbsp; mov ax, 0fffh &lt;br /&gt;&amp;nbsp;&amp;nbsp; and ax, bx &lt;br /&gt;&amp;nbsp;&amp;nbsp; push ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; popf &lt;br /&gt;&amp;nbsp;&amp;nbsp; pushf &lt;br /&gt;&amp;nbsp;&amp;nbsp; pop ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; and ax, 0f000h &lt;br /&gt;&amp;nbsp;&amp;nbsp; cmp ax, 0f000h &lt;br /&gt;&amp;nbsp;&amp;nbsp; mov ax, cpu8086 &lt;br /&gt;&amp;nbsp;&amp;nbsp; je @@End_CpuType &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; // 80286 CPU check &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; or bx, 0f000h &lt;br /&gt;&amp;nbsp;&amp;nbsp; push bx &lt;br /&gt;&amp;nbsp;&amp;nbsp; popf &lt;br /&gt;&amp;nbsp;&amp;nbsp; pushf &lt;br /&gt;&amp;nbsp;&amp;nbsp; pop ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; and ax, 0f000h &lt;br /&gt;&amp;nbsp;&amp;nbsp; mov ax, cpu286 &lt;br /&gt;&amp;nbsp;&amp;nbsp; jz @@End_CpuType  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; // 386 CPU check &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; pushf &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; pop ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; mov cx, ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; xor ax, 0h &lt;br /&gt;&amp;nbsp;&amp;nbsp; dw 0004h &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; push ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h  &lt;br /&gt;&amp;nbsp;&amp;nbsp; popf &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; pushf &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; pop ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; xor ax, cx &lt;br /&gt;&amp;nbsp;&amp;nbsp; mov ax, cpu386 &lt;br /&gt;&amp;nbsp;&amp;nbsp; je @@End_CpuType &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; // 486 CPU check &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; pushf &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; pop ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; mov cx, ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h  &lt;br /&gt;&amp;nbsp;&amp;nbsp; xor ax, 0h &lt;br /&gt;&amp;nbsp;&amp;nbsp; dw 0020h &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; push ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; popf &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; pushf &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; pop ax &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; xor ax, cx &lt;br /&gt;&amp;nbsp;&amp;nbsp; mov ax, cpu486 &lt;br /&gt;&amp;nbsp;&amp;nbsp; je @@End_CpuType &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; // Pentium CPU check &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; mov ax, 1 &lt;br /&gt;&amp;nbsp;&amp;nbsp; dw 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 0Fh &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 0a2h &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; and ax, 0F00H &lt;br /&gt;&amp;nbsp;&amp;nbsp; dw 0 &lt;br /&gt;&amp;nbsp;&amp;nbsp; db 66h &lt;br /&gt;&amp;nbsp;&amp;nbsp; shr ax, 8 &lt;br /&gt;&amp;nbsp;&amp;nbsp; sub ax, 1 &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; @@End_CpuType: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp; pop ds &lt;br /&gt;&lt;br /&gt;End; &lt;br /&gt;&lt;br /&gt;Function CpuTypeString : String; &lt;br /&gt;&lt;br /&gt;Var &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;Kind : TCpuType; &lt;br /&gt;&lt;br /&gt;Begin &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Kind := CpuType; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Case Kind Of &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cpu8086 : Result := '8086'; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cpu286 : Result := '286'; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cpu386 : Result := '386'; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cpu486 : Result := '486'; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;cpuPentium : Result := 'Pentium'; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Else Result := Format ('P%d', [Ord (kind)]); &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; End; &lt;br /&gt;&lt;br /&gt;End; &lt;br /&gt;&lt;br /&gt;End.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-997223401245429228?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/997223401245429228/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/detecting-cpu-type-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/997223401245429228'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/997223401245429228'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/detecting-cpu-type-delphi.html' title='Detecting CPU type - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-6182266581952562115</id><published>2008-12-31T18:04:00.001-08:00</published><updated>2008-12-31T18:04:53.041-08:00</updated><title type='text'>Changing hard drive's icon under Windows XP - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;&amp;nbsp;This article shows that how can changing a driver icon in MyComputer under&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;WinXP. This modification is so different than Win9x and not easy. &lt;br /&gt;&amp;nbsp;&amp;nbsp;As you know that you have so many articles that just change some settings  &lt;br /&gt;&amp;nbsp;&amp;nbsp;in the registry in D3K database, but the following registry codes are&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;undocumanted and you don't see it normally in XP Registry.  &lt;br /&gt;&amp;nbsp;&amp;nbsp;if you can want to change your hard driver icon then insert the following&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;code into Button1's OnClick event. I hope this be usefull for someone.  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;add Registry in USES clause  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;procedure TForm1.Button1Click(Sender: TObject);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;var Reg: TRegistry;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Reg := TRegistry.Create;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;with Reg do  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;RootKey := HKEY_LOCAL_MACHINE;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OpenKey&amp;nbsp;&amp;nbsp;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;('Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\', True);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;{ True, because if a subkey named DriveIcons and/or DefaultIcon  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;does not exist, windows automatically creates it }  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OpenKey('C\DefaultIcon\', True); // as default first drive C  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// we are choosing empty field as default value  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WriteString('', 'C:\YourIcons\Sample1.ico');  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ and icon file path&amp;nbsp;&amp;nbsp;or if dll file then&amp;nbsp;&amp;nbsp;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;e.g&amp;nbsp;&amp;nbsp;'c:\YourIcons\Icons.dll, 2' }  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CloseKey;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finally  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Reg.Free;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // if 2nd hard drive is present..  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Reg := TRegistry.Create;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;with Reg do  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;RootKey := HKEY_LOCAL_MACHINE;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OpenKey  &lt;br /&gt;&amp;nbsp;&amp;nbsp;('Software\Microsoft\Windows\CurrentVersion\Explorer\DriveIcons\', True);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;OpenKey('D\DefaultIcon\', True);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WriteString('', 'C:\YourIcons\Sample2.ico');  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CloseKey;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; finally  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Reg.Free;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;in the same way, you can write these techniques for another drivers.  &lt;br /&gt;&amp;nbsp;&amp;nbsp;Now, hard drive's icon have been changed. Press F5 on the keyboard  &lt;br /&gt;&amp;nbsp;&amp;nbsp;or reboot for the changes to take effect.  &lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-6182266581952562115?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/6182266581952562115/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/changing-hard-drives-icon-under-windows.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6182266581952562115'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6182266581952562115'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/changing-hard-drives-icon-under-windows.html' title='Changing hard drive&apos;s icon under Windows XP - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-2394141246948872969</id><published>2008-12-31T18:03:00.000-08:00</published><updated>2008-12-31T18:04:15.471-08:00</updated><title type='text'>Changing attributes of files - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;To read a file's attributes, pass the filename to the FileGetAttr function, which returns the file attributes of a file.For example, add a TButton&amp;nbsp;&amp;nbsp;and TLabel components to the form and write the following code to the OnClick event of button. &lt;br /&gt;&lt;br /&gt;var &lt;br /&gt;attr:Integer; &lt;br /&gt;s:string; &lt;br /&gt;begin &lt;br /&gt;attr:=FileGetAttr('c:\Autoexec.bat'); &lt;br /&gt;if(attr and faHidden)&amp;lt;&amp;gt;0 then s:='Hidden'; &lt;br /&gt;if(attr and faReadOnly)&amp;lt;&amp;gt;0 then s:=s+'Read-Only'; &lt;br /&gt;if(attr and faSysFile)&amp;lt;&amp;gt;0 then s:=s+'System'; &lt;br /&gt;if(attr and faArchive)&amp;lt;&amp;gt;0 then s:=s+'Archive'; &lt;br /&gt;Label1.Caption:=s; &lt;br /&gt;--------------------------- &lt;br /&gt;To set a file's attributes, pass the name of the file and the attributes you want to the FileSetAttr function.Each attribute has a mnemonic name defined in the SysUtils unit. For example, to set a file's system attribute, you would do the following: &lt;br /&gt;&lt;br /&gt;Attributes := Attributes or faSystem; &lt;br /&gt;&lt;br /&gt;You can also set or clear several attributes at once. For example, the clear both the read-only file and hidden attributes: &lt;br /&gt;&lt;br /&gt;Attributes := Attributes and not (faReadOnly or faHidden); &lt;br /&gt;--------------------------- &lt;br /&gt;Furthermore,To change file's attribute, you can use the following return values.The routine can be simplified.  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +----------------------------------+ &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; | Return Value | Attribute of File |&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +----------------------------------+ &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; 128&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|&amp;nbsp;&amp;nbsp; Normal&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; 1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|&amp;nbsp;&amp;nbsp; Read Only&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; 2&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|&amp;nbsp;&amp;nbsp; Hidden&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|&amp;nbsp;&amp;nbsp; System&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;| &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; 32&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&amp;nbsp; Archive&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; +--------------+-------------------+ &lt;br /&gt;Sample call: We use a code like the following:  &lt;br /&gt;&lt;br /&gt;FileSetAttr('C:\Autoexec.bat',2);{Hidden}&amp;nbsp;&amp;nbsp; &lt;br /&gt;FileSetAttr('C:\Autoexec.bat',3);{Hidden and Read-Only.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FileGetAttr returns 3 value}  &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-2394141246948872969?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/2394141246948872969/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/changing-attributes-of-files-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2394141246948872969'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2394141246948872969'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/changing-attributes-of-files-delphi.html' title='Changing attributes of files - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-7996438783585845444</id><published>2008-12-31T18:02:00.000-08:00</published><updated>2008-12-31T18:03:01.023-08:00</updated><title type='text'>Get the next day of the month</title><content type='html'>&lt;span id="ArticleContent"&gt;NOTE: You need do add the unit "DateUtils" to the uses clause (where DayOfTheWeek is declared). &lt;br /&gt;&lt;br /&gt;//---------------------------------------------------------------- &lt;br /&gt;type &lt;br /&gt;&amp;nbsp;&amp;nbsp;TWeeks&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;= (wFirst, wSecond, wThird, wFourth, wLast); &lt;br /&gt;&amp;nbsp;&amp;nbsp;TWeekDays = (wdMonday, wdTuesday, wdWednesday, wdThursday, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; wdFriday, wdSaturday, wdSunday); &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//Examples: &lt;br /&gt;//ShowMessage(DateTimeToStr(GetDayOfMonth(Now, wLast, wdTuesday))); &lt;br /&gt;//ShowMessage(DateTimeToStr(GetDayOfMonth(Now, wFirst, wdSunday))); &lt;br /&gt;&lt;br /&gt;function DaysPerMonth(AYear, AMonth: Integer): Integer; &lt;br /&gt;const &lt;br /&gt;&amp;nbsp;&amp;nbsp;DaysInMonth: array[1..12] of Integer = &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;Result := DaysInMonth[AMonth]; &lt;br /&gt;&amp;nbsp;&amp;nbsp;if (AMonth = 2) and IsLeapYear(AYear) then Inc(Result); { leap-year Feb is special } &lt;br /&gt;end; &lt;br /&gt;&lt;br /&gt;function GetDayOfMonth(DT: TDateTime; Weeks: TWeeks; WeekDays: TWeekDays): TDateTime; &lt;br /&gt;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;aDateTime: TDateTime; &lt;br /&gt;&amp;nbsp;&amp;nbsp;aDayOfWeek, bDayOfWeek: Word; &lt;br /&gt;&amp;nbsp;&amp;nbsp;AYear, AMonth, ADay: Word; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;DecodeDate(DT, AYear, AMonth, ADay); &lt;br /&gt;&amp;nbsp;&amp;nbsp;aDateTime := EncodeDate(AYear, AMonth, 1); //Returns the 1st day of the month&amp;nbsp;&amp;nbsp; &lt;br /&gt;&amp;nbsp;&amp;nbsp;//-DayOfTheWeek- &lt;br /&gt;&amp;nbsp;&amp;nbsp;//1 - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, 7 - Sunday &lt;br /&gt;&amp;nbsp;&amp;nbsp;aDayOfWeek := DayOfTheWeek(aDateTime); &lt;br /&gt;&amp;nbsp;&amp;nbsp;//-WeekDays- &lt;br /&gt;&amp;nbsp;&amp;nbsp;//0 - Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, 6 - Sunday &lt;br /&gt;&amp;nbsp;&amp;nbsp;bDayOfWeek := Ord(WeekDays) +1; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if aDayOfWeek &amp;lt;= bDayOfWeek //The day we want is still yet to come &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;then ADay := (bDayOfWeek - aDayOfWeek) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;else ADay := 7 - (aDayOfWeek - bDayOfWeek); &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;//-Weeks- &lt;br /&gt;&amp;nbsp;&amp;nbsp;//0 - first, 1 - second, 2 - third, 3 - fourth, 4 - last &lt;br /&gt;&amp;nbsp;&amp;nbsp;ADay := ADay + Ord(Weeks) * 7; &lt;br /&gt;&amp;nbsp;&amp;nbsp; if Ord(Weeks) = 4 then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (ADay+1 &amp;gt; DaysPerMonth(AYear, AMonth)) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;or ( (ADay+1 &amp;lt;= DaysPerMonth(AYear, AMonth)) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;and (bDayOfWeek &amp;lt;&amp;gt; DayOfTheWeek(EncodeDate(AYear, AMonth, ADay+1))) ) &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;then ADay := ADay - 7; &lt;br /&gt;&amp;nbsp;&amp;nbsp; Result := aDateTime + ADay; &lt;br /&gt;end; &lt;br /&gt;//---------------------------------------------------------------- &lt;br /&gt;&lt;br /&gt;I'm sure there are better ways to deal with this problem so please let me know if you happen to know any.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-7996438783585845444?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/7996438783585845444/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/get-next-day-of-month.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7996438783585845444'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7996438783585845444'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/get-next-day-of-month.html' title='Get the next day of the month'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-4740082933259183069</id><published>2008-12-31T18:00:00.001-08:00</published><updated>2008-12-31T18:00:34.120-08:00</updated><title type='text'>Make A System-Wide Shorcut Key !! - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;Try This Code........... &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;type  &lt;br /&gt;&amp;nbsp;&amp;nbsp;TForm1 = class(TForm)  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;procedure FormCreate(Sender: TObject);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;procedure FormDestroy(Sender: TObject);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;protected  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;procedure hotykey(var msg:TMessage); message WM_HOTKEY;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;&lt;br /&gt;var  &lt;br /&gt;&amp;nbsp;&amp;nbsp;Form1: TForm1;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;id,id2:Integer;  &lt;br /&gt;&lt;br /&gt;implementation  &lt;br /&gt;&lt;br /&gt;{$R *.DFM}  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;procedure TForm1.hotykey(var msg:TMessage);  &lt;br /&gt;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;if (msg.LParamLo=MOD_CONTROL) and (msg.LParamHi=81) then  &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowMessage('Ctrl + Q wurde gedrückt !');  &lt;br /&gt;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;if (msg.LParamLo=MOD_CONTROL) and (msg.LParamHi=82) then  &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowMessage('Ctrl + R wurde gedrückt !');  &lt;br /&gt;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;end;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;procedure TForm1.FormCreate(Sender: TObject);  &lt;br /&gt;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;id:=GlobalAddAtom('hotkey');  &lt;br /&gt;&amp;nbsp;&amp;nbsp;RegisterHotKey(handle,id,mod_control,81);  &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;id2:=GlobalAddAtom('hotkey2');  &lt;br /&gt;&amp;nbsp;&amp;nbsp;RegisterHotKey(handle,id2,mod_control,82);  &lt;br /&gt;end;  &lt;br /&gt;&lt;br /&gt;procedure TForm1.FormDestroy(Sender: TObject);  &lt;br /&gt;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;UnRegisterHotKey(handle,id);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;UnRegisterHotKey(handle,id2);  &lt;br /&gt;end;  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-4740082933259183069?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/4740082933259183069/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/make-system-wide-shorcut-key-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4740082933259183069'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4740082933259183069'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/make-system-wide-shorcut-key-delphi.html' title='Make A System-Wide Shorcut Key !! - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-5204253332859009052</id><published>2008-12-31T17:56:00.002-08:00</published><updated>2008-12-31T17:59:24.994-08:00</updated><title type='text'>Preventing the user from closing a form - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;To prevent the user from closing a form you need to disable the close &lt;br /&gt;button in the title bar of a form and at the same time disable the &lt;br /&gt;"Close" menu item in the form's system menu. This is done by calling &lt;br /&gt;the EnableMenuItem API function (see the example below). Nonetheless, &lt;br /&gt;the user can still close the form using the Alt+F4 key combination, &lt;br /&gt;so we have to set the KeyPreview form property to True and write an &lt;br /&gt;event handler for the OnKeyDown event to cancel out this hot key: &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;procedure TForm1.FormCreate(Sender: TObject); &lt;br /&gt;&amp;nbsp;&amp;nbsp;var &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hSysMenu: HMENU; &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;hSysMenu := GetSystemMenu(Self.Handle, False); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if hSysMenu &amp;lt;&amp;gt; 0 then begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;EnableMenuItem(hSysMenu, SC_CLOSE, &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MF_BYCOMMAND Or MF_GRAYED); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DrawMenuBar(Self.Handle); &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;KeyPreview := True; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;procedure TForm1.FormKeyDown(Sender: TObject; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;var Key: Word; Shift: TShiftState); &lt;br /&gt;&amp;nbsp;&amp;nbsp;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (Key = VK_F4) and (ssAlt in Shift) then &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Key := 0; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-5204253332859009052?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/5204253332859009052/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/preventing-user-from-closing-form.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5204253332859009052'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5204253332859009052'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/preventing-user-from-closing-form.html' title='Preventing the user from closing a form - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-6219452967368321990</id><published>2008-12-31T17:56:00.001-08:00</published><updated>2008-12-31T17:56:54.260-08:00</updated><title type='text'>Display forms full screen</title><content type='html'>&lt;span id="ArticleContent"&gt;Covering the entire screen with a form is relatively easy to accomplish as shown below. &lt;br /&gt;&lt;/span&gt;&lt;span id="ArticleContent"&gt;&lt;b&gt;Component Download: &lt;/b&gt;&lt;a href="http://www.delphi3000.com/redirect.asp?Link=../article/2863/KG_FullScreen.zip" target="_blank"&gt;../article/2863/KG_FullScreen.zip&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;&lt;span id="ArticleContent"&gt;procedure TfrmMainForm.FormCreate(Sender: TObject); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;{ Position form } &lt;br /&gt;&amp;nbsp;&amp;nbsp;Top := 0 ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Left := 0 ; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{ Go full screen } &lt;br /&gt;&amp;nbsp;&amp;nbsp;WindowState&amp;nbsp;&amp;nbsp;:= wsmaximized; &lt;br /&gt;&amp;nbsp;&amp;nbsp;ClientWidth&amp;nbsp;&amp;nbsp;:= Screen.Width ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;ClientHeight := Screen.Height; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Refresh; &lt;br /&gt;end;  &lt;br /&gt;&lt;br /&gt;If this is a typical form it will have borders which you might consider removing by setting BorderStyle property to bsNone as shown below. &lt;br /&gt;&lt;br /&gt;procedure TfrmMainForm.FormCreate(Sender: TObject); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;{ Position form } &lt;br /&gt;&amp;nbsp;&amp;nbsp;Top := 0 ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Left := 0 ; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{ Go full screen } &lt;br /&gt;&amp;nbsp;&amp;nbsp;BorderStyle := bsNone ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;WindowState&amp;nbsp;&amp;nbsp;:= wsmaximized; &lt;br /&gt;&amp;nbsp;&amp;nbsp;ClientWidth&amp;nbsp;&amp;nbsp;:= Screen.Width ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;ClientHeight := Screen.Height; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Refresh; &lt;br /&gt;end;  &lt;br /&gt;&lt;br /&gt;Sometimes the code shown above will go full screen but still display the Windows TaskBar, if this happens we can force the form on top using either SetForeGroundWindow or SetActiveWindow. From my testing it is best to use both if the problem persist. &lt;br /&gt;&lt;br /&gt;procedure TfrmMainForm.FormCreate(Sender: TObject); &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;{ Position form } &lt;br /&gt;&amp;nbsp;&amp;nbsp;Top := 0 ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Left := 0 ; &lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;{ Go full screen } &lt;br /&gt;&amp;nbsp;&amp;nbsp;BorderStyle := bsNone ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;WindowState&amp;nbsp;&amp;nbsp;:= wsmaximized; &lt;br /&gt;&amp;nbsp;&amp;nbsp;ClientWidth&amp;nbsp;&amp;nbsp;:= Screen.Width ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;ClientHeight := Screen.Height; &lt;br /&gt;&amp;nbsp;&amp;nbsp;Refresh; &lt;br /&gt;&amp;nbsp;&amp;nbsp;SetForegroundWindow(Handle) ; &lt;br /&gt;&amp;nbsp;&amp;nbsp;SetActiveWindow(Application.Handle) ; &lt;br /&gt;end;  &lt;br /&gt;&lt;br /&gt;Other&amp;nbsp;&amp;nbsp;considerations (see attachment for code to address these items) &lt;br /&gt;If the form is already in maximized window state the above code will not work. &lt;br /&gt;Controlling the system menu commands as per above needs to be considered &lt;br /&gt;Ghost items in the TaskBar after terminating your application.&amp;nbsp;&amp;nbsp; &lt;br /&gt;&lt;br /&gt;Delphi makes it simple to cover the display screen but what if you need to duplicate the functionality in another programming language such as Microsoft Visual Basic? Well in this case you might want to learn the API methods (again see attachment). &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-6219452967368321990?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/6219452967368321990/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/display-forms-full-screen.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6219452967368321990'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6219452967368321990'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/display-forms-full-screen.html' title='Display forms full screen'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-6104295511669342453</id><published>2008-12-31T17:54:00.002-08:00</published><updated>2008-12-31T17:55:23.502-08:00</updated><title type='text'>Rotate an image by 90º</title><content type='html'>&lt;span id="ArticleContent"&gt;You have to create two images first. I'm sure anyone could convert this code to use just on image by using a TBitmap. &lt;br /&gt;&lt;br /&gt;Procedure TurnImage(Src, Dst: TImage); &lt;br /&gt;var x,y: integer; &lt;br /&gt;begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;Dst.Width:= Src.Height; Dst.Height:= Src.Width; &lt;br /&gt;&amp;nbsp;&amp;nbsp;For x:= 0 to Src.Width-1 do begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;For y:= 0 to Src.Height-1 do begin &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Dst.Canvas.Pixels[(Src.Height-1)-y,x]:= Src.Canvas.Pixels[x,y]; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;end;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-6104295511669342453?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/6104295511669342453/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/rotate-image-by-90.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6104295511669342453'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6104295511669342453'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/rotate-image-by-90.html' title='Rotate an image by 90º'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3102195952941060625</id><published>2008-12-31T17:54:00.001-08:00</published><updated>2008-12-31T17:54:42.169-08:00</updated><title type='text'>Inserting a .bmp file into your .exe</title><content type='html'>&lt;div style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 9pt; margin: 10px 20px 20px;"&gt;This is helpful if you do not want to have a bitmap file in the directory of the program you distribute.  &lt;br /&gt;&lt;br /&gt;One problem with inserting a bitmap file into your .exe is that it makes your programs .exe larger, so it is a good idea if your &lt;br /&gt;program uses small bitmap files.  &lt;br /&gt;The Delphi example below will work on any version of Delphi above Delphi 2.  &lt;br /&gt;&lt;br /&gt;First open an editor like Notepad.exe, for this example we will use a bitmap file called Picture.bmp  &lt;br /&gt;and this is the bitmap file we will insert into our programs .exe  &lt;br /&gt;(Make sure that there is a picture in your folder/directory named Picture.bmp.  &lt;br /&gt;If you do not have a picture already called Picture.bmp you can rename a different bitmap file so that it is called Picture.bmp) &lt;br /&gt;&lt;br /&gt;Type these words into the text file:  &lt;br /&gt;&lt;br /&gt;ThePicture Bitmap "Picture.bmp"  &lt;br /&gt;&lt;br /&gt;Then save the text file as:  &lt;br /&gt;&lt;br /&gt;mybmp.rc  &lt;br /&gt;&lt;br /&gt;We will use BRCC32.exe that comes with Delphi and it should be in Delphi's Bin directory, to compile the file.  &lt;br /&gt;In order to use BRCC32.exe you have to use a DOS window, using DOS is not hard at all.  &lt;br /&gt;To run a DOS window, go to your TaskBar, click Start|Programs|MS-DOS Prompt  &lt;br /&gt;Whenever you use DOS all you have to do to get back to Windows (Your desktop) is type the word Exit, then press the Enter &lt;br /&gt;key.  &lt;br /&gt;&lt;br /&gt;You should have your bitmap files and your .rc file in the same folder/directory.  &lt;br /&gt;&lt;br /&gt;So we will type this sentence in the DOS window:  &lt;br /&gt;C:\Delphi 3\Bin\Brcc32.exe C:\Delphi 3\bin\bmps\MyBmp.rc  &lt;br /&gt;The above paths may have to be changed to the place where you have Delphi installed and to the folder/directory where you &lt;br /&gt;have saved your files for this example.  &lt;br /&gt;&lt;br /&gt;If this does not work or you get an error, you may not have Delphi in your computers path.  &lt;br /&gt;A way around this is for you to copy the BRCC32.exe and RW32CORE.DLL into your folder/directory that you are using, &lt;br /&gt;then try again.  &lt;br /&gt;(The above files, BRCC32.exe and RW32CORE.DLL, may be different for different versions of&amp;nbsp;&amp;nbsp;Delphi)  &lt;br /&gt;&lt;br /&gt;Use Explorer or another File Manager and have a look in your directory, you should find that you have an extra file called &lt;br /&gt;mybmp.RES.  &lt;br /&gt;&lt;br /&gt;Start Delphi, then start a new project (Application).  &lt;br /&gt;&lt;br /&gt;Look for {$R *.DFM} in Unit1 and add {$R MYBMP.RES} next to it.  &lt;br /&gt;&lt;br /&gt;Add a TImage component to your form and make its AutoSize property True.  &lt;br /&gt;Next drop a Button onto your Form, double-click on the Button and add this code:  &lt;br /&gt;&lt;br /&gt;Image1.Picture.Bitmap.LoadFromResourceName(hInstance, 'ThePicture');  &lt;br /&gt;&lt;br /&gt;Note we use 'ThePicture' to call the bitmap file.  &lt;br /&gt;Run the program and then click on the Button, you should see the image that you put into the resource file.  &lt;br /&gt;If you have to save the project before you can run it, save in in the same folder/directory where you have the resource (*.RES) &lt;br /&gt;file.  &lt;br /&gt;&lt;br /&gt;Your complete unit should look like this:  &lt;br /&gt;&lt;br /&gt;unit Unit1;  &lt;br /&gt;&lt;br /&gt;interface  &lt;br /&gt;&lt;br /&gt;uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  &lt;br /&gt;&amp;nbsp;&amp;nbsp;StdCtrls, ExtCtrls;  &lt;br /&gt;&lt;br /&gt;type  &lt;br /&gt;&amp;nbsp;&amp;nbsp;TForm1 = class(TForm)  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Button1: TButton;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Image1: TImage;  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;procedure Button1Click(Sender: TObject);  &lt;br /&gt;&amp;nbsp;&amp;nbsp;private  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ Private declarations }  &lt;br /&gt;&amp;nbsp;&amp;nbsp;public  &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{ Public declarations }  &lt;br /&gt;&amp;nbsp;&amp;nbsp;end;  &lt;br /&gt;&lt;br /&gt;var  &lt;br /&gt;&amp;nbsp;&amp;nbsp;Form1: TForm1;  &lt;br /&gt;&lt;br /&gt;implementation  &lt;br /&gt;&lt;br /&gt;{$R *.DFM} {$R MYBMP.RES}  &lt;br /&gt;&lt;br /&gt;procedure TForm1.Button1Click(Sender: TObject);  &lt;br /&gt;begin  &lt;br /&gt;&amp;nbsp;&amp;nbsp;Image1.Picture.Bitmap.LoadFromResourceName(hInstance, 'THEPICTURE');  &lt;br /&gt;end;  &lt;br /&gt;&lt;br /&gt;end. &lt;/div&gt;&lt;div style="margin-top: 5px; text-align: center;"&gt;&lt;script type="text/javascript"&gt;&lt;!--google_ad_client = "pub-0924303565538336";google_alternate_color = "eeeecc";google_ad_width = 468;google_ad_height = 15;google_ad_format = "468x15_0ads_al";google_ad_channel = "8056644127";google_color_border = "eeeecc";google_color_bg = "eeeecc";google_color_link = "990000";google_color_text = "000000";google_color_url = "990000";//--&gt;&lt;/script&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3102195952941060625?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3102195952941060625/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/inserting-bmp-file-into-your-exe.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3102195952941060625'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3102195952941060625'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/inserting-bmp-file-into-your-exe.html' title='Inserting a .bmp file into your .exe'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3814212173951612037</id><published>2008-12-31T17:52:00.000-08:00</published><updated>2008-12-31T17:53:43.496-08:00</updated><title type='text'>Screencapture with animated gif support!</title><content type='html'>&lt;span id="ArticleContent"&gt;In may last year i promised to create a component which could &lt;br /&gt;take screenshots and save them in a gif image, and here it  &lt;br /&gt;FINALLY is. :) &lt;br /&gt;&lt;br /&gt;This component can be used in ANY kind of application, royalty  &lt;br /&gt;free with only 1 demand: credits! &lt;br /&gt;&lt;br /&gt;============================================================== &lt;br /&gt;&lt;br /&gt;Description: &lt;br /&gt;&lt;br /&gt;This component can create screenshots of the screen (or  &lt;br /&gt;portions of the screen). &lt;br /&gt;&lt;br /&gt;You can set a region of what you want to "photograph" by  &lt;br /&gt;calling the function  &lt;br /&gt;&lt;br /&gt;=&amp;gt; TtspScreenCapture.SetRegion(ARegion: TCaptureRegion) &lt;br /&gt;&lt;br /&gt;After setting the region you must specify a capture mode. &lt;br /&gt;there are 4 capturemodes: &lt;br /&gt;&lt;br /&gt;- cmStillBitmap &lt;br /&gt;- cmStillJPEG &lt;br /&gt;- cmStillGIF &lt;br /&gt;- cmMotionGIF &lt;br /&gt;&lt;br /&gt;Now you can take a still image by calling the function &lt;br /&gt;&lt;br /&gt;=&amp;gt; TtspScreenCapture.GetStillImage: TCaptureResult; &lt;br /&gt;&lt;br /&gt;According to the chosen capture mode you can access the  &lt;br /&gt;screenshot in the proper TGraphic object: &lt;br /&gt;&lt;br /&gt;=&amp;gt;&amp;nbsp;&amp;nbsp; TCaptureResult = record &lt;br /&gt;VBitmap: TBitmap; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; VJPEG: TJPEGImage; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; VGIFImage: TGIFImage; &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end; &lt;br /&gt;&lt;br /&gt;With these objects you can save the images to wherever you want &lt;br /&gt;them. &lt;br /&gt;&lt;br /&gt;Motion gifs require a little different approach. &lt;br /&gt;&lt;br /&gt;First you must set the capture mode to cmMotionGIF. &lt;br /&gt;Accordingly you must set the interval in which a screenshot &lt;br /&gt;should be taken by setting the property  &lt;br /&gt;&lt;br /&gt;=&amp;gt; TtspScreenCapture.MotionGifInterval: integer &lt;br /&gt;&lt;br /&gt;This property is in milliseconds. (1 second = 1000 millisecond) &lt;br /&gt;&lt;br /&gt;Everything is now ready for capturing. &lt;br /&gt;&lt;br /&gt;To activate the capturing set the property &lt;br /&gt;&lt;br /&gt;=&amp;gt; TtspScreenCapture.Active: boolean; &lt;br /&gt;&lt;br /&gt;to true. &lt;br /&gt;&lt;br /&gt;while capturing you can read the current GIFImage by calling the  &lt;br /&gt;function  &lt;br /&gt;&lt;br /&gt;=&amp;gt; TtspScreenCapture.GetMotionGif: TGIFImage; &lt;br /&gt;&lt;br /&gt;To stop capturing set the Active property back to false. &lt;br /&gt;&lt;br /&gt;The final Gif image is again accesible by calling the function  &lt;br /&gt;GetMotionGIF. &lt;br /&gt;&lt;br /&gt;====================================================================== &lt;br /&gt;&lt;br /&gt;Benchmark: &lt;br /&gt;&lt;br /&gt;The component also contains a function benchmark. this is a very simple &lt;br /&gt;function which calculates how much frames can be captured in 10 seconds. &lt;br /&gt;&lt;br /&gt;the result value is in captured frames per second (rounded). &lt;br /&gt;&lt;br /&gt;======================================================================= &lt;br /&gt;&lt;br /&gt;well, this wraps it up for now. &lt;br /&gt;&lt;br /&gt;Good luck with using this component in your application!  &lt;br /&gt;&lt;br /&gt;Nevertheless i have 1 more thing to tell you: &lt;br /&gt;&lt;br /&gt;1. DON'T forget to credit me in your application. You got this for free &lt;br /&gt;&amp;nbsp;&amp;nbsp; so it would be quite fair to do this simple thing. &lt;br /&gt;&lt;br /&gt;======================================================================== &lt;br /&gt;&lt;br /&gt;Have fun, &lt;br /&gt;&lt;br /&gt;Christiaan ten Klooster &lt;br /&gt;webmaster@tsp2000.tmfweb.nl &lt;br /&gt;www.tsp2000.tmfweb.nl &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3814212173951612037?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3814212173951612037/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/screencapture-with-animated-gif-support.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3814212173951612037'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3814212173951612037'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/screencapture-with-animated-gif-support.html' title='Screencapture with animated gif support!'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-4182052688022851764</id><published>2008-12-31T17:51:00.000-08:00</published><updated>2008-12-31T17:52:12.844-08:00</updated><title type='text'>check, if a directory is empty  - Delphi</title><content type='html'>&lt;span class="sourcecode"&gt;&lt;span style="color: navy; font-family: Courier New; font-size: x-small;"&gt;&lt;i&gt;//&amp;nbsp;Diese&amp;nbsp;Funktion&amp;nbsp;gibt&amp;nbsp;true&amp;nbsp;zurück,&amp;nbsp;falls&amp;nbsp;Directory&amp;nbsp;ein&amp;nbsp;leeres&amp;nbsp;Verzeichnis&amp;nbsp;ist.&lt;br /&gt;//&amp;nbsp;This&amp;nbsp;function&amp;nbsp;returns&amp;nbsp;true&amp;nbsp;if&amp;nbsp;Directory&amp;nbsp;is&amp;nbsp;an&amp;nbsp;empty&amp;nbsp;directory.&lt;br /&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;b&gt;function&amp;nbsp;&lt;/b&gt;DirectoryIsEmpty(Directory:&amp;nbsp;&lt;b&gt;string&lt;/b&gt;):&amp;nbsp;Boolean;&lt;br /&gt;&lt;b&gt;var&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;SR:&amp;nbsp;TSearchRec;&lt;br /&gt;&amp;nbsp;&amp;nbsp;i:&amp;nbsp;Integer;&lt;br /&gt;&lt;b&gt;begin&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;/b&gt;Result&amp;nbsp;:=&amp;nbsp;False;&lt;br /&gt;&amp;nbsp;&amp;nbsp;FindFirst(IncludeTrailingPathDelimiter(Directory)&amp;nbsp;+&amp;nbsp;'*',&amp;nbsp;faAnyFile,&amp;nbsp;SR);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;b&gt;for&amp;nbsp;&lt;/b&gt;i&amp;nbsp;:=&amp;nbsp;1&amp;nbsp;&lt;b&gt;to&amp;nbsp;&lt;/b&gt;2&amp;nbsp;&lt;b&gt;do&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if&amp;nbsp;&lt;/b&gt;(SR.Name&amp;nbsp;=&amp;nbsp;'.')&amp;nbsp;&lt;b&gt;or&amp;nbsp;&lt;/b&gt;(SR.Name&amp;nbsp;=&amp;nbsp;'..')&amp;nbsp;&lt;b&gt;then&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/b&gt;Result&amp;nbsp;:=&amp;nbsp;FindNext(SR)&amp;nbsp;&amp;lt;&amp;gt;&amp;nbsp;0;&lt;br /&gt;&amp;nbsp;&amp;nbsp;FindClose(SR);&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: navy;"&gt;&lt;i&gt;//&amp;nbsp;Beispiel:&lt;br /&gt;//&amp;nbsp;Example:&lt;br /&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;b&gt;procedure&amp;nbsp;&lt;/b&gt;TForm1.Button1Click(Sender:&amp;nbsp;TObject);&lt;br /&gt;&lt;b&gt;begin&lt;br /&gt;&amp;nbsp;&amp;nbsp;if&amp;nbsp;&lt;/b&gt;DirectoryIsEmpty('C:\test')&amp;nbsp;&lt;b&gt;then&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/b&gt;Label1.Caption&amp;nbsp;:=&amp;nbsp;'empty'&lt;br /&gt;&amp;nbsp;&amp;nbsp;&lt;b&gt;else&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/b&gt;Label1.Caption&amp;nbsp;:=&amp;nbsp;'not&amp;nbsp;empty';&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-4182052688022851764?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/4182052688022851764/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/check-if-directory-is-empty-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4182052688022851764'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4182052688022851764'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/check-if-directory-is-empty-delphi.html' title='check, if a directory is empty  - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3368980850394993815</id><published>2008-12-31T05:18:00.001-08:00</published><updated>2008-12-31T05:18:50.823-08:00</updated><title type='text'>Show Windows properties window - Delphi</title><content type='html'>This function uses Shell API&lt;br /&gt;&lt;br /&gt;The function:&lt;br /&gt;&lt;br /&gt;function VisaEgenskaper(hWndOwner: HWND; const FileName: string): Boolean;&lt;br /&gt;var Info: TShellExecuteInfo;&lt;br /&gt;begin&lt;br /&gt; { Fill in the SHELLEXECUTEINFO structure }&lt;br /&gt; with Info do&lt;br /&gt; begin&lt;br /&gt;   cbSize := SizeOf(Info);&lt;br /&gt;   fMask := SEE_MASK_NOCLOSEPROCESS or&lt;br /&gt;            SEE_MASK_INVOKEIDLIST or&lt;br /&gt;            SEE_MASK_FLAG_NO_UI;&lt;br /&gt;   wnd  := hWndOwner;&lt;br /&gt;   lpVerb := 'properties';&lt;br /&gt;   lpFile := pChar(FileName);&lt;br /&gt;   lpParameters := nil;&lt;br /&gt;   lpDirectory := nil;&lt;br /&gt;   nShow := 0;&lt;br /&gt;   hInstApp := 0;&lt;br /&gt;   lpIDList := nil;&lt;br /&gt; end;&lt;br /&gt;&lt;br /&gt; { Call Windows to display the properties dialog. }&lt;br /&gt; Result := ShellExecuteEx(@Info);&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Call the function with this line:&lt;br /&gt;&lt;br /&gt;VisaEgenskaper(Application.Handle, 'C:\Thomas.txt');&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3368980850394993815?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3368980850394993815/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/show-windows-properties-window-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3368980850394993815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3368980850394993815'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/show-windows-properties-window-delphi.html' title='Show Windows properties window - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3444081440984069697</id><published>2008-12-31T05:17:00.001-08:00</published><updated>2008-12-31T05:17:54.015-08:00</updated><title type='text'>Send a file to windows recycle bin - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;Don't forget to add ShellAPI in your uses-group.&lt;br /&gt;&lt;br /&gt;This is the function that does all the work:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;function RecycleFile(sFileName: string): Boolean;&lt;br /&gt;var&lt;br /&gt;FOS: TSHFileOpStruct;&lt;br /&gt;begin&lt;br /&gt;   FillChar(FOS, SizeOf(FOS), 0);&lt;br /&gt;   with FOS do&lt;br /&gt;      begin&lt;br /&gt;         wFunc := FO_DELETE; { Functions as FO_COPY also works.&lt;br /&gt;         pFrom := PChar(sFileName + #0);&lt;br /&gt;         pTo := { Only for FO_COPY }&lt;br /&gt;         fFlags := FOF_ALLOWUNDO; { Since we wan't to send the file the file to recycle bin }&lt;br /&gt;      end;&lt;br /&gt;   // Send the file&lt;br /&gt;   Result := (SHFileOperation(FOS) = 0);&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;Call the function by using this line:&lt;br /&gt;RecycleFile('E:\Test.exe');&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3444081440984069697?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3444081440984069697/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/send-file-to-windows-recycle-bin-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3444081440984069697'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3444081440984069697'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/send-file-to-windows-recycle-bin-delphi.html' title='Send a file to windows recycle bin - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-5753104113111700949</id><published>2008-12-31T05:14:00.000-08:00</published><updated>2008-12-31T05:15:16.024-08:00</updated><title type='text'>Searching for a directory</title><content type='html'>&lt;span id="ArticleContent"&gt;//---------------------------------------------------------------------------&lt;br /&gt;// Author   : Digital Survivor [Esteban Rodríguez Nieto | José Plano]&lt;br /&gt;// Email    : plmad666@gmail.com | jose.plano@gmail.com&lt;br /&gt;// Web site : www.ds-studios.com.ar&lt;br /&gt;//---------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;The most simple way to view a complete list of drives and directories in the system is calling the function SelectDirectory included in the FileCtrl unit. This function shows Windows' standard "Search directory" dialog box. With it, we can search and select any &lt;a name="0321228480" id="amzn_cl_link_0" style="border-bottom: 1px dashed; color: rgb(153, 0, 0); text-decoration: none;" target="_blank" href="http://amazon.de/gp/product/0321228480?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=0321228480&amp;amp;adid=a5829b22-21f0-42a7-8dab-a6ab537674df"&gt;directory of the system&lt;/a&gt;. The only problem I can find, is that we can't set the where this dialog box appears. Generaly appears in the right-bottom corner of the screen. If anyone know how to change this, please let me know. Good luck...&lt;br /&gt;&lt;br /&gt;//---------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;Uses&lt;br /&gt;&lt;br /&gt;    FileCtrl;&lt;br /&gt;&lt;br /&gt;Procedure BrowseClick;&lt;br /&gt;&lt;br /&gt;Var&lt;br /&gt;&lt;br /&gt;   S : string;&lt;br /&gt;&lt;br /&gt;Begin&lt;br /&gt;&lt;br /&gt;     S := '';&lt;br /&gt;&lt;br /&gt;     If SelectDirectory ('Select Directory', '', S) Then SetPath (S);&lt;br /&gt;&lt;br /&gt;End; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-5753104113111700949?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/5753104113111700949/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/searching-for-directory.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5753104113111700949'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5753104113111700949'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/searching-for-directory.html' title='Searching for a directory'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-2601805594129031820</id><published>2008-12-31T05:13:00.000-08:00</published><updated>2008-12-31T05:14:34.518-08:00</updated><title type='text'>Getting the icon of an application, library or document</title><content type='html'>&lt;div style="margin: 10px 20px 20px; font-family: &amp;quot;Courier New&amp;quot;; font-size: 9pt;"&gt;ExtractAssociatedIcon&lt;br /&gt;---------------------&lt;br /&gt;&lt;br /&gt;To get the icon of an application or document we can use this API&lt;br /&gt;function (declared in the ShellAPI unit):&lt;br /&gt;&lt;br /&gt;  function ExtractAssociatedIcon(hInst: HINST; lpIconPath: PChar;&lt;br /&gt;                                 var lpiIcon: Word): HICON; stdcall;&lt;br /&gt;&lt;br /&gt;* hInst: The application handle. This value is contained in the&lt;br /&gt;  predefined variable HInstance.&lt;br /&gt;&lt;br /&gt;* lpIconPath: A pointer to a character buffer that should contain a&lt;br /&gt;  null terminated string with the full path name of the application,&lt;br /&gt;  library (DLL) or document. If it is a document, the function will&lt;br /&gt;  place there the full pathname of the associated application from&lt;br /&gt;  where the icon was extracted, so we should allocate a buffer large&lt;br /&gt;  enough.&lt;br /&gt;&lt;br /&gt;* lpiIcon: The icon &lt;a name="3927372285" id="amzn_cl_link_0" style="border-bottom: 1px dashed; color: rgb(153, 0, 0); text-decoration: none;" target="_blank" href="http://amazon.de/gp/product/3927372285?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=3927372285&amp;amp;adid=c213744f-ebbe-448e-a3cf-ccfc04b7e9cb"&gt;index&lt;/a&gt; (the first icon in the file has an index of&lt;br /&gt;  0). If lpIconPath specifies a document, then lpiIcon is set by the&lt;br /&gt;  function (that's why it is passed by reference) to the &lt;a name="3927372285" id="amzn_cl_link_1" style="border-bottom: 1px dashed; color: rgb(153, 0, 0); text-decoration: none;" target="_blank" href="http://amazon.de/gp/product/3927372285?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=3927372285&amp;amp;adid=561e6279-fa81-49f5-b442-313e71c52389"&gt;index&lt;/a&gt;&lt;br /&gt;  position of the actual icon taken from the associated executable&lt;br /&gt;  (defined in the file association).&lt;br /&gt;&lt;br /&gt;Return value:&lt;br /&gt;If the function fails, it returns 0. If it succeeds, it returns an&lt;br /&gt;icon handle, which is an integer value &lt;a name="3827370191" id="amzn_cl_link_2" style="border-bottom: 1px dashed; color: rgb(153, 0, 0); text-decoration: none;" target="_blank" href="http://amazon.de/gp/product/3827370191?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=3827370191&amp;amp;adid=6b210ee1-3e42-4d74-9c07-2df7af075daf"&gt;Windows&lt;/a&gt; uses to identify the&lt;br /&gt;allocated resource. It is not necessary to call the API DestroyIcon&lt;br /&gt;to release the icon since it'll be deallocated automatically when the&lt;br /&gt;application finishes, although you can do it if you want.&lt;br /&gt;&lt;br /&gt;Sample call&lt;br /&gt;-----------&lt;br /&gt;Now, what do we do with the icon handle? Normally what we want is an&lt;br /&gt;icon, namely and instance of the TIcon class. All we have to do is&lt;br /&gt;create a TIcon object and assign this handle to its Handle property.&lt;br /&gt;If later we assign the Handle property to another value, the previous&lt;br /&gt;icon will be automatically be released. The same happens if the TIcon&lt;br /&gt;object is freed. Here is an example that changes the icon of the form:&lt;br /&gt;&lt;br /&gt;  procedure TForm1.Button1Click(Sender: TObject);&lt;br /&gt;  var&lt;br /&gt;    IconIndex: word;&lt;br /&gt;    Buffer: array[0..2048] of char;&lt;br /&gt;    IconHandle: HIcon;&lt;br /&gt;  begin&lt;br /&gt;    StrCopy(@Buffer, 'C:\Windows\Help\Windows.hlp');&lt;br /&gt;    IconIndex := 0;&lt;br /&gt;    IconHandle := ExtractAssociatedIcon(HInstance, Buffer, IconIndex);&lt;br /&gt;    if IconHandle &lt;&gt; 0 then&lt;br /&gt;      Icon.Handle := IconHandle;&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;GetAssociatedIcon&lt;br /&gt;-----------------&lt;br /&gt;&lt;br /&gt;Unfortunately, ExtractAssociatedIcon fails if the file does not exists&lt;br /&gt;on disk, so we defined a procedure that gets the icon of a file&lt;br /&gt;whether it exists or not, and can also get the small icon (ideal for&lt;br /&gt;a TListView that can be shown in vsIcon or vsReport view styles). The&lt;br /&gt;procedure receives three parameters: the filename and two pointers to&lt;br /&gt;HICON (integer) variables: one for the large icon (32x32) and another&lt;br /&gt;one for the small icon (16x16). Any of them can be nil if you don't&lt;br /&gt;need one of these icons. The icons "returned" by the procedure must be&lt;br /&gt;freed with the DestroyIcon API. This will be done automatically if you&lt;br /&gt;assign the icon handle (HICON) to the Handle property of a TIcon object&lt;br /&gt;(the icon will be released when this object gets freed or a new value&lt;br /&gt;is assigned to it).&lt;br /&gt;&lt;br /&gt;  uses&lt;br /&gt;    Registry, ShellAPI;&lt;br /&gt;&lt;br /&gt;  type&lt;br /&gt;    PHICON = ^HICON;&lt;br /&gt;&lt;br /&gt;  procedure GetAssociatedIcon(FileName: TFilename;&lt;br /&gt;      PLargeIcon, PSmallIcon: PHICON);&lt;br /&gt;  // Gets the icons of a given file&lt;br /&gt;  var&lt;br /&gt;    IconIndex: word;  // Position of the icon in the file&lt;br /&gt;    FileExt, FileType: string;&lt;br /&gt;    Reg: TRegistry;&lt;br /&gt;    p: integer;&lt;br /&gt;    p1, p2: pchar;&lt;br /&gt;  label&lt;br /&gt;    noassoc;&lt;br /&gt;  begin&lt;br /&gt;    IconIndex := 0;&lt;br /&gt;    // Get the extension of the file&lt;br /&gt;    FileExt := UpperCase(ExtractFileExt(FileName));&lt;br /&gt;    if ((FileExt &lt;&gt; '.EXE') and (FileExt &lt;&gt; '.ICO')) or&lt;br /&gt;        not FileExists(FileName) then begin&lt;br /&gt;      // If the file is an EXE or ICO and it exists, then&lt;br /&gt;      // we will extract the icon from this file. Otherwise&lt;br /&gt;      // here we will try to find the associated icon in the&lt;br /&gt;      // Windows Registry...&lt;br /&gt;      Reg := nil;&lt;br /&gt;      try&lt;br /&gt;        Reg := TRegistry.Create(KEY_QUERY_VALUE);&lt;br /&gt;        Reg.RootKey := HKEY_CLASSES_ROOT;&lt;br /&gt;        if FileExt = '.EXE' then FileExt := '.COM';&lt;br /&gt;        if Reg.OpenKeyReadOnly(FileExt) then&lt;br /&gt;          try&lt;br /&gt;            FileType := Reg.ReadString('');&lt;br /&gt;          finally&lt;br /&gt;            Reg.CloseKey;&lt;br /&gt;          end;&lt;br /&gt;        if (FileType &lt;&gt; '') and Reg.OpenKeyReadOnly(&lt;br /&gt;            FileType + '\DefaultIcon') then&lt;br /&gt;          try&lt;br /&gt;            FileName := Reg.ReadString('');&lt;br /&gt;          finally&lt;br /&gt;            Reg.CloseKey;&lt;br /&gt;          end;&lt;br /&gt;      finally&lt;br /&gt;        Reg.Free;&lt;br /&gt;      end;&lt;br /&gt;&lt;br /&gt;      // If we couldn't find the association, we will&lt;br /&gt;      // try to get the default icons&lt;br /&gt;      if FileName = '' then goto noassoc;&lt;br /&gt;&lt;br /&gt;      // Get the filename and icon &lt;a name="3927372285" id="amzn_cl_link_3" style="border-bottom: 1px dashed; color: rgb(153, 0, 0); text-decoration: none;" target="_blank" href="http://amazon.de/gp/product/3927372285?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=3927372285&amp;amp;adid=a8ea4e67-9038-4d19-9391-c66b9e849104"&gt;index&lt;/a&gt; from the&lt;br /&gt;      // association (of form '"filaname",index')&lt;br /&gt;      p1 := PChar(FileName);&lt;br /&gt;      p2 := StrRScan(p1, ',');&lt;br /&gt;      if p2 &lt;&gt; nil then begin&lt;br /&gt;        p := p2 - p1 + 1; // Position of the comma&lt;br /&gt;        IconIndex := StrToInt(Copy(FileName, p + 1,&lt;br /&gt;          Length(FileName) - p));&lt;br /&gt;        SetLength(FileName, p - 1);&lt;br /&gt;      end;&lt;br /&gt;    end;&lt;br /&gt;    // Attempt to get the icon&lt;br /&gt;    if ExtractIconEx(pchar(FileName), IconIndex,&lt;br /&gt;        PLargeIcon^, PSmallIcon^, 1) &lt;&gt; 1 then&lt;br /&gt;    begin&lt;br /&gt;  noassoc:&lt;br /&gt;      // The operation failed or the file had no associated&lt;br /&gt;      // icon. Try to get the default icons from SHELL32.DLL&lt;br /&gt;&lt;br /&gt;      try // to get the location of SHELL32.DLL&lt;br /&gt;        FileName := IncludeTrailingBackslash(GetSystemDir)&lt;br /&gt;          + 'SHELL32.DLL';&lt;br /&gt;      except&lt;br /&gt;        FileName := 'C:\WINDOWS\SYSTEM\SHELL32.DLL';&lt;br /&gt;      end;&lt;br /&gt;      // Determine the default icon for the file extension&lt;br /&gt;      if      (FileExt = '.DOC') then IconIndex := 1&lt;br /&gt;      else if (FileExt = '.EXE')&lt;br /&gt;           or (FileExt = '.COM') then IconIndex := 2&lt;br /&gt;      else if (FileExt = '.HLP') then IconIndex := 23&lt;br /&gt;      else if (FileExt = '.INI')&lt;br /&gt;           or (FileExt = '.INF') then IconIndex := 63&lt;br /&gt;      else if (FileExt = '.TXT') then IconIndex := 64&lt;br /&gt;      else if (FileExt = '.BAT') then IconIndex := 65&lt;br /&gt;      else if (FileExt = '.DLL')&lt;br /&gt;           or (FileExt = '.SYS')&lt;br /&gt;           or (FileExt = '.VBX')&lt;br /&gt;           or (FileExt = '.OCX')&lt;br /&gt;           or (FileExt = '.VXD') then IconIndex := 66&lt;br /&gt;      else if (FileExt = '.FON') then IconIndex := 67&lt;br /&gt;      else if (FileExt = '.TTF') then IconIndex := 68&lt;br /&gt;      else if (FileExt = '.FOT') then IconIndex := 69&lt;br /&gt;      else IconIndex := 0;&lt;br /&gt;      // Attempt to get the icon.&lt;br /&gt;      if ExtractIconEx(pchar(FileName), IconIndex,&lt;br /&gt;          PLargeIcon^, PSmallIcon^, 1) &lt;&gt; 1 then&lt;br /&gt;      begin&lt;br /&gt;        // Failed to get the icon. Just "return" zeroes.&lt;br /&gt;        if PLargeIcon &lt;&gt; nil then PLargeIcon^ := 0;&lt;br /&gt;        if PSmallIcon &lt;&gt; nil then PSmallIcon^ := 0;&lt;br /&gt;      end;&lt;br /&gt;    end;&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;Sample call&lt;br /&gt;-----------&lt;br /&gt;This example will change the icon of your form:&lt;br /&gt;&lt;br /&gt;  procedure TForm1.Button1Click(Sender: TObject);&lt;br /&gt;  var&lt;br /&gt;    SmallIcon: HICON;&lt;br /&gt;  begin&lt;br /&gt;    GetAssociatedIcon('file.doc', nil, @SmallIcon);&lt;br /&gt;    if SmallIcon &lt;&gt; 0 then&lt;br /&gt;      Icon.Handle := SmallIcon;&lt;br /&gt;  end;&lt;br /&gt;&lt;/div&gt;&lt;div style="margin-top: 5px; text-align: center;"&gt;&lt;script type="text/javascript"&gt;&lt;!-- google_ad_client = "pub-0924303565538336"; google_alternate_color = "eeeecc"; google_ad_width = 468; google_ad_height = 15; google_ad_format = "468x15_0ads_al"; google_ad_channel = "8056644127"; google_color_border = "eeeecc"; google_color_bg = "eeeecc"; google_color_link = "990000"; google_color_text = "000000"; google_color_url = "990000"; //--&gt; &lt;/script&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-2601805594129031820?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/2601805594129031820/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/getting-icon-of-application-library-or.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2601805594129031820'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/2601805594129031820'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/getting-icon-of-application-library-or.html' title='Getting the icon of an application, library or document'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-6710981595514566253</id><published>2008-12-31T05:10:00.002-08:00</published><updated>2008-12-31T05:16:48.061-08:00</updated><title type='text'>Minimize All Windows on Desktop - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;&lt;p&gt;To minimize all the windows on the Desktop manually, you need to right-click the taskbar and select "Minimize All Windows" (Win 98) ; another solution requires a click to the "Show Desktop" icon in the "Quick Launch" area of the taskbar (Windows 2000 or Windows XP).&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Finally you can press the Windows logo key and the M key simultaneously. Unfortunately, you can't programmatically send this key combination to the Windows shell or any other application.&lt;/p&gt;&lt;br /&gt;&lt;p&gt;You can use the Shell API Application object, take a look on the code:&lt;/p&gt;&lt;br /&gt;&lt;strong&gt;uses&lt;/strong&gt; ShlObj;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;procedure&lt;/strong&gt; Shell_MinimizeAll;&lt;br /&gt;&lt;strong&gt;var&lt;/strong&gt;&lt;br /&gt;   Shell : OleVariant;&lt;br /&gt;&lt;strong&gt;begin&lt;/strong&gt;&lt;br /&gt;   Shell := CreateOleObject(&lt;span style="color:#007f00;"&gt;'Shell.Application'&lt;/span&gt;);&lt;br /&gt;   Shell.MinimizeAll;&lt;br /&gt;&lt;strong&gt;end&lt;/strong&gt;;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-6710981595514566253?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/6710981595514566253/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/minimize-all-windows-on-desktop-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6710981595514566253'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6710981595514566253'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/minimize-all-windows-on-desktop-delphi.html' title='Minimize All Windows on Desktop - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-7618517933452978451</id><published>2008-12-31T05:10:00.001-08:00</published><updated>2008-12-31T05:10:26.304-08:00</updated><title type='text'>Determining if there is a disk/diskette/CD in a removable-disk drive - Delphi</title><content type='html'>The trick is done by calling the API GetDiskFreeSpace and returning&lt;br /&gt;its return value as a boolean. The following function takes the drive&lt;br /&gt;letter as a parameter (for example 'A', 'D', etc.) and returns True&lt;br /&gt;if there is a disk in the drive, or False if not.&lt;br /&gt;&lt;br /&gt; var&lt;br /&gt;   DrivePath: array [0..3] of char = 'A:\';&lt;br /&gt;&lt;br /&gt; function IsDiskIn(drive: char): boolean;&lt;br /&gt; var&lt;br /&gt;   d1, d2, d3, d4: longword;&lt;br /&gt; begin&lt;br /&gt;   DrivePath[0] := drive;&lt;br /&gt;   Result := GetDiskFreeSpace(DrivePath, d1, d2, d3, d4);&lt;br /&gt; end;&lt;br /&gt;&lt;br /&gt;In the implementation we use an initialized null-terminated string&lt;br /&gt;(DrivePath) that contains the root directory of drive A: and we&lt;br /&gt;substitute the drive letter with the one passed as parameter before&lt;br /&gt;calling GetDiskFreeSpace.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Sample call&lt;br /&gt;-----------&lt;br /&gt;&lt;br /&gt; procedure TForm1.Button1Click(Sender: TObject);&lt;br /&gt; begin&lt;br /&gt;   if not IsDiskIn('A') then&lt;br /&gt;     ShowMessage('Drive A: Not Ready');&lt;br /&gt; end;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-7618517933452978451?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/7618517933452978451/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/determining-if-there-is-diskdiskettecd.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7618517933452978451'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7618517933452978451'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/determining-if-there-is-diskdiskettecd.html' title='Determining if there is a disk/diskette/CD in a removable-disk drive - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-4564822982836700364</id><published>2008-12-31T05:09:00.002-08:00</published><updated>2008-12-31T05:13:30.506-08:00</updated><title type='text'>Starting an application as an icon in the system tray - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;Placing an icon the system tray&lt;br /&gt;-------------------------------&lt;br /&gt;&lt;br /&gt;  NOTE: You can download the full source code of this article from&lt;br /&gt;        http://www.latiumsoftware.com/download/d00035.zip&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The system tray is the area at the right of the task bar where "background" applications can place their icons. To place an icon in the tray you have to call the Shell_NotifyIcon API declared in the ShellAPI unit, passing it the constant NIM_ADD (to tell it to add an icon) and the address of a TNotifyIconData record with the icon information that includes the handle of the icon to show, the text to show as tool tip when the mouse is over the icon, the handle of the window that will receive the messages of the icon and the message type the icon will send to this window.&lt;br /&gt;&lt;br /&gt;  uses&lt;br /&gt;     ..., ShellAPI;&lt;br /&gt;&lt;br /&gt;  const&lt;br /&gt;    WM_ICONTRAY = WM_USER + 1;   // User-defined message&lt;br /&gt;&lt;br /&gt;  type&lt;br /&gt;    ...&lt;br /&gt;&lt;br /&gt;  var&lt;br /&gt;    ...&lt;br /&gt;    NotifyIconData : TNotifyIconData;&lt;br /&gt;&lt;br /&gt;  implementation&lt;br /&gt;&lt;br /&gt;  procedure TForm1.FormCreate(Sender: TObject);&lt;br /&gt;  begin&lt;br /&gt;    with NotifyIconData do begin&lt;br /&gt;      hIcon := Icon.Handle;&lt;br /&gt;      StrPCopy(szTip, Application.Title);&lt;br /&gt;      Wnd := Handle;&lt;br /&gt;      uCallbackMessage := WM_ICONTRAY;&lt;br /&gt;      uID := 1;&lt;br /&gt;      uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;&lt;br /&gt;      cbSize := sizeof(TNotifyIconData);&lt;br /&gt;    end;&lt;br /&gt;    Shell_NotifyIcon(NIM_ADD, @NotifyIconData);&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Handling icon messages&lt;br /&gt;----------------------&lt;br /&gt;&lt;br /&gt;Form1 will receive WM_ICONTRAY messages from the icon. To capture and handle these messages we have to declare a new method in our form. For example:&lt;br /&gt;&lt;br /&gt;  type&lt;br /&gt;    TForm1 = class(TForm)&lt;br /&gt;      ...&lt;br /&gt;    private&lt;br /&gt;      { Private declarations }&lt;br /&gt;      ...&lt;br /&gt;      procedure Icontray(var Msg: TMessage); message WM_ICONTRAY;&lt;br /&gt;    public&lt;br /&gt;      { Public declarations }&lt;br /&gt;      ...&lt;br /&gt;    end;&lt;br /&gt;&lt;br /&gt;In the implementation of this method we can for example display a popup menu when the user right-clicks on the icon. Assuming you have created a popup menu named PopupMenu1, the following code will do the trick:&lt;br /&gt;&lt;br /&gt;  procedure TForm1.Icontray(var Msg: TMessage);&lt;br /&gt;  var&lt;br /&gt;    CursorPos : TPoint;&lt;br /&gt;  begin&lt;br /&gt;    if Msg.lParam = WM_RBUTTONDOWN then begin&lt;br /&gt;      GetCursorPos(CursorPos);&lt;br /&gt;      SetForegroundWindow(Handle);        // suggested by Berend Radstaat&lt;br /&gt;      PopupMenu1.Popup(CursorPos.x, CursorPos.y);&lt;br /&gt;      PostMessage(Handle, WM_NULL, 0, 0); // suggested by Berend Radstaat&lt;br /&gt;    end else&lt;br /&gt;      inherited;&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;Thanks to Berend Radstaat for the additions to this article.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Showing and hiding the form&lt;br /&gt;---------------------------&lt;br /&gt;&lt;br /&gt;Normally you will include a menu item in the popup menu to show the form, so all you have to do is write an event handler to call the Show method of the form and optionally remove the icon from the system tray:&lt;br /&gt;&lt;br /&gt;  procedure TForm1.mnuShowClick(Sender: TObject);&lt;br /&gt;  begin&lt;br /&gt;    Show;&lt;br /&gt;    // Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;When the user closes the form, you can for example hide it instead of closing it and optionally show the icon again in the system tray if you have removed it when you made the form visible. For example:&lt;br /&gt;&lt;br /&gt;  procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);&lt;br /&gt;  begin&lt;br /&gt;    Action := caNone;&lt;br /&gt;    Hide;&lt;br /&gt;    // Shell_NotifyIcon(NIM_ADD, @NotifyIconData);&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Finishing the application&lt;br /&gt;-------------------------&lt;br /&gt;&lt;br /&gt;You can have a menu item in the popup menu to exit the application. In this case you have to write an event handler for its click event to remove the icon and finish the application:&lt;br /&gt;&lt;br /&gt;  procedure TForm1.mnuExitClick(Sender: TObject);&lt;br /&gt;  begin&lt;br /&gt;    Shell_NotifyIcon(NIM_DELETE, @NotifyIconData);&lt;br /&gt;    Application.ProcessMessages;&lt;br /&gt;    Application.Terminate;&lt;br /&gt;  end; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-4564822982836700364?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/4564822982836700364/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/starting-application-as-icon-in-system.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4564822982836700364'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4564822982836700364'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/starting-application-as-icon-in-system.html' title='Starting an application as an icon in the system tray - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-3317712396440575430</id><published>2008-12-31T05:09:00.001-08:00</published><updated>2008-12-31T05:09:23.321-08:00</updated><title type='text'>How to start the "Control Panel"</title><content type='html'>In fact it is realy simple&lt;br /&gt;see the following...&lt;br /&gt;&lt;br /&gt;procedure TForm1.Buton1Click(Sender : TObject);&lt;br /&gt;begin&lt;br /&gt; ShellExecute(Handle,'Open','control',nil,nil,SW_SHOWNORMAL);&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;there are a lot of things you can do with this code&lt;br /&gt;just replace the string 'control' with a&lt;br /&gt;normal command line (c:\)&lt;br /&gt;&lt;br /&gt;NOTE you must add "ShellApi" to the uses clause!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-3317712396440575430?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/3317712396440575430/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-start-control-panel.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3317712396440575430'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/3317712396440575430'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-start-control-panel.html' title='How to start the &quot;Control Panel&quot;'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-6914125602035158497</id><published>2008-12-31T05:07:00.001-08:00</published><updated>2008-12-31T05:07:40.817-08:00</updated><title type='text'>Windows Search Engine - Delphi</title><content type='html'>&lt;span id="ArticleContent"&gt;The Shell supports several search utilities that allow users to locate namespace objects such as files or printers. You can create a custom search engine and make it available to users by implementing and registering a search handler.&lt;br /&gt;&lt;br /&gt;Users have two ways to select a search engine. The first way is from the Start menu. With systems earlier than Microsoft &lt;a name="3768812707" id="amzn_cl_link_0" style="border-bottom: 1px dashed; color: rgb(153, 0, 0); text-decoration: none;" target="_blank" href="http://amazon.de/gp/product/3768812707?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=3768812707&amp;amp;adid=04dbc182-65aa-4960-b9d2-2c9c971ecd08"&gt;Windows 2000&lt;/a&gt;, selecting the Find command on the Start menu displays a submenu of the available search engines. With Windows 2000 and later, the Start menu's Find command is renamed Search.&lt;br /&gt;Users can also launch a search from Windows Explorer. On systems earlier than Windows 2000, they click the Find command on the Tools menu to display essentially the same menu as the one associated with the Start menu. However, Windows Explorer for Windows 2000 handles search engines in a very different way. Instead of handling search engines as a submenu of the Tools menu, there is now a Search button on the toolbar. Clicking this button opens the Explorer bar's Search pane&lt;br /&gt;&lt;br /&gt;The following example shows how to implement Shell Search Handler using &lt;a name="3826686292" id="amzn_cl_link_1" style="border-bottom: 1px dashed; color: rgb(153, 0, 0); text-decoration: none;" target="_blank" href="http://amazon.de/gp/product/3826686292?ie=UTF8&amp;amp;tag=delphi300comc-21&amp;amp;link_code=em1&amp;amp;camp=2510&amp;amp;creative=11150&amp;amp;creativeASIN=3826686292&amp;amp;adid=4d84fd89-0521-4dd4-b2b4-73469dd90de8"&gt;Borland Delphi&lt;/a&gt;. As any Shell Extension it must be implemented as in-process Component Object Model (COM) object. It must be assigned a globally unique identifier (GUID) and registered using regsvr32.exe&lt;br /&gt;&lt;br /&gt;library SearchHandler;&lt;br /&gt;&lt;br /&gt;uses&lt;br /&gt;  ComServ,&lt;br /&gt;  HandlerM in 'HandlerM.pas';&lt;br /&gt;&lt;br /&gt;{$R *.RES}&lt;br /&gt;&lt;br /&gt;exports&lt;br /&gt;  DllGetClassObject,&lt;br /&gt;  DllCanUnloadNow,&lt;br /&gt;  DllRegisterServer,&lt;br /&gt;  DllUnregisterServer;&lt;br /&gt;&lt;br /&gt;begin&lt;br /&gt;end.&lt;br /&gt;&lt;br /&gt;{*****************************************************************************&lt;br /&gt;  Name           : TSearchEngine&lt;br /&gt;  Author         : Perevoznyk Serhiy&lt;br /&gt;  Description    : Shell Search Handler&lt;br /&gt;*****************************************************************************}&lt;br /&gt;&lt;br /&gt;unit HandlerM;&lt;br /&gt;&lt;br /&gt;interface&lt;br /&gt;&lt;br /&gt;uses&lt;br /&gt;  Windows, ActiveX, ComObj, ShlObj, Dialogs;&lt;br /&gt;&lt;br /&gt;type&lt;br /&gt;  TSearchEngine = class(TComObject, IShellExtInit, IContextMenu)&lt;br /&gt;  protected&lt;br /&gt;    { IShellExtInit }&lt;br /&gt;    function IShellExtInit.Initialize = SEIInitialize; // Avoid compiler warning&lt;br /&gt;    function SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;&lt;br /&gt;      hKeyProgID: HKEY): HResult; stdcall;&lt;br /&gt;    { IContextMenu }&lt;br /&gt;    function QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst, idCmdLast,&lt;br /&gt;      uFlags: UINT): HResult; stdcall;&lt;br /&gt;    function InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult; stdcall;&lt;br /&gt;    function GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;&lt;br /&gt;      pszName: LPSTR; cchMax: UINT): HResult; stdcall;&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;const&lt;br /&gt;  Class_SearchEngine: TGUID = '{B8091A44-1F5E-4EFE-8F26-194ACBDE4465}';&lt;br /&gt;&lt;br /&gt;implementation&lt;br /&gt;&lt;br /&gt;uses ComServ, SysUtils, ShellApi, Registry;&lt;br /&gt;&lt;br /&gt;function TSearchEngine.SEIInitialize(pidlFolder: PItemIDList; lpdobj: IDataObject;&lt;br /&gt;  hKeyProgID: HKEY): HResult;&lt;br /&gt;begin&lt;br /&gt;  Result := NOERROR;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;function TSearchEngine.QueryContextMenu(Menu: HMENU; indexMenu, idCmdFirst,&lt;br /&gt;          idCmdLast, uFlags: UINT): HResult;&lt;br /&gt;begin&lt;br /&gt;  Result := 0; &lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;function TSearchEngine.InvokeCommand(var lpici: TCMInvokeCommandInfo): HResult;&lt;br /&gt;begin&lt;br /&gt;  //enter your code here&lt;br /&gt;  ShowMessage('Executed');&lt;br /&gt;  Result := NOERROR;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;function TSearchEngine.GetCommandString(idCmd, uType: UINT; pwReserved: PUINT;&lt;br /&gt;  pszName: LPSTR; cchMax: UINT): HRESULT;&lt;br /&gt;begin&lt;br /&gt;  if (idCmd = 0) then&lt;br /&gt;  begin&lt;br /&gt;    if (uType = GCS_HELPTEXT) then&lt;br /&gt;      // return help string for menu item&lt;br /&gt;      StrCopy(pszName, 'Find document');&lt;br /&gt;    Result := NOERROR;&lt;br /&gt;  end&lt;br /&gt;  else&lt;br /&gt;    Result := E_INVALIDARG;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;type&lt;br /&gt;  TSearchEngineFactory = class(TComObjectFactory)&lt;br /&gt;  public&lt;br /&gt;    procedure UpdateRegistry(Register: Boolean); override;&lt;br /&gt;  end;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;procedure CreateKey(const Key, ValueName, Value: string);&lt;br /&gt;var&lt;br /&gt;  Handle: HKey;&lt;br /&gt;  Status, Disposition: Integer;&lt;br /&gt;begin&lt;br /&gt;  Status := RegCreateKeyEx(HKEY_LOCAL_MACHINE, PChar(Key), 0, '',&lt;br /&gt;    REG_OPTION_NON_VOLATILE, KEY_READ or KEY_WRITE, nil, Handle,&lt;br /&gt;    @Disposition);&lt;br /&gt;  if Status = 0 then&lt;br /&gt;  begin&lt;br /&gt;     RegSetValueEx(Handle, PChar(ValueName), 0, REG_SZ,&lt;br /&gt;      PChar(Value), Length(Value) + 1);&lt;br /&gt;    RegCloseKey(Handle);&lt;br /&gt;  end;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;procedure DeleteKey(const Key: string);&lt;br /&gt;begin&lt;br /&gt;  RegDeleteKey(HKEY_LOCAL_MACHINE, PChar(Key));&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;procedure TSearchEngineFactory.UpdateRegistry(Register: Boolean);&lt;br /&gt;var&lt;br /&gt;  ClassID: string;&lt;br /&gt;  FileName: array [0..MAX_PATH] of Char;&lt;br /&gt;begin&lt;br /&gt;  if Register then&lt;br /&gt;  begin&lt;br /&gt;    inherited UpdateRegistry(Register);&lt;br /&gt;    ClassID := GUIDToString(Class_SearchEngine);&lt;br /&gt;    GetModuleFileName(HInstance, FileName, SizeOf(FileName));&lt;br /&gt;    CreateKey('Software\Microsoft\Windows\CurrentVersion\Explorer\FindExtensions\Static\SearchHandler', '', ClassID);&lt;br /&gt;    CreateKey('Software\Microsoft\Windows\CurrentVersion\Explorer\FindExtensions\Static\SearchHandler\0', '', 'Using Delphi...');&lt;br /&gt;    CreateKey('Software\Microsoft\Windows\CurrentVersion\Explorer\FindExtensions\Static\SearchHandler\0\DefaultIcon', '', FileName + ',0');&lt;br /&gt;  end&lt;br /&gt;  else&lt;br /&gt;    begin&lt;br /&gt;      DeleteKey('Software\Microsoft\Windows\CurrentVersion\Explorer\FindExtensions\Static\SearchHandler\0\DefaultIcon');&lt;br /&gt;      DeleteKey('Software\Microsoft\Windows\CurrentVersion\Explorer\FindExtensions\Static\SearchHandler\0');&lt;br /&gt;      DeleteKey('Software\Microsoft\Windows\CurrentVersion\Explorer\FindExtensions\Static\SearchHandler');&lt;br /&gt;      inherited UpdateRegistry(Register);&lt;br /&gt;  end;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;initialization&lt;br /&gt;  TSearchEngineFactory.Create(ComServer, TSearchEngine, Class_SearchEngine,&lt;br /&gt;    '', 'Delphi Search Engine Example', ciMultiInstance,&lt;br /&gt;    tmApartment);&lt;br /&gt;end. &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-6914125602035158497?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/6914125602035158497/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/windows-search-engine-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6914125602035158497'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/6914125602035158497'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/windows-search-engine-delphi.html' title='Windows Search Engine - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-7681045154033701197</id><published>2008-12-31T05:04:00.000-08:00</published><updated>2008-12-31T05:05:38.401-08:00</updated><title type='text'>Multi-files properties dialog? - Delphi</title><content type='html'>&lt;span class="sourcecode"&gt;&lt;span style="font-family:Courier New;font-size:85%;color:#000080;"&gt;&lt;i&gt;{ Copyright Kingron 2005 }&lt;br /&gt;{ For Windows 2000 or Higher, Shell32.dll version 5.0 or later }&lt;br /&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;b&gt;uses &lt;/b&gt;ActiveX, ShlObj, ComObj;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;function &lt;/b&gt;SHMultiFileProperties(pDataObj: IDataObject; Flag: DWORD): HRESULT;&lt;br /&gt;  &lt;b&gt;stdcall&lt;/b&gt;; &lt;b&gt;external &lt;/b&gt;'shell32.dll';&lt;br /&gt;&lt;br /&gt;&lt;b&gt;function &lt;/b&gt;GetFileListDataObject(Files: TStrings): IDataObject;&lt;br /&gt;&lt;b&gt;type&lt;br /&gt;  &lt;/b&gt;PArrayOfPItemIDList = ^TArrayOfPItemIDList;&lt;br /&gt;  TArrayOfPItemIDList = &lt;b&gt;array&lt;/b&gt;[0..0] &lt;b&gt;of &lt;/b&gt;PItemIDList;&lt;br /&gt;&lt;b&gt;var&lt;br /&gt;  &lt;/b&gt;Malloc: IMalloc;&lt;br /&gt;  Root: IShellFolder;&lt;br /&gt;  p: PArrayOfPItemIDList;&lt;br /&gt;  chEaten, dwAttributes: ULONG;&lt;br /&gt;  i, FileCount: Integer;&lt;br /&gt;&lt;b&gt;begin&lt;br /&gt;  &lt;/b&gt;Result := &lt;b&gt;nil&lt;/b&gt;;&lt;br /&gt;  FileCount := Files.Count;&lt;br /&gt;  &lt;b&gt;if &lt;/b&gt;FileCount = 0 &lt;b&gt;then &lt;/b&gt;Exit;&lt;br /&gt;&lt;br /&gt;  OleCheck(SHGetMalloc(Malloc));&lt;br /&gt;  OleCheck(SHGetDesktopFolder(Root));&lt;br /&gt;  p := AllocMem(SizeOf(PItemIDList) * FileCount);&lt;br /&gt;  &lt;b&gt;try&lt;br /&gt;    for &lt;/b&gt;i := 0 &lt;b&gt;to &lt;/b&gt;FileCount - 1 &lt;b&gt;do&lt;br /&gt;      try&lt;br /&gt;        if not &lt;/b&gt;(DirectoryExists(Files[i]) &lt;b&gt;or &lt;/b&gt;FileExists(Files[i])) &lt;b&gt;then &lt;/b&gt;Continue;&lt;br /&gt;        OleCheck(Root.ParseDisplayName(GetActiveWindow,&lt;br /&gt;          &lt;b&gt;nil&lt;/b&gt;,&lt;br /&gt;          PWideChar(WideString(Files[i])),&lt;br /&gt;          chEaten,&lt;br /&gt;          p^[i],&lt;br /&gt;          dwAttributes));&lt;br /&gt;      &lt;b&gt;except&lt;br /&gt;      end&lt;/b&gt;;&lt;br /&gt;    OleCheck(Root.GetUIObjectOf(GetActiveWindow,&lt;br /&gt;      FileCount,&lt;br /&gt;      p^[0],&lt;br /&gt;      IDataObject,&lt;br /&gt;      &lt;b&gt;nil&lt;/b&gt;,&lt;br /&gt;      Pointer(Result)));&lt;br /&gt;  &lt;b&gt;finally&lt;br /&gt;    for &lt;/b&gt;i := 0 &lt;b&gt;to &lt;/b&gt;FileCount - 1 &lt;b&gt;do&lt;br /&gt;    begin&lt;br /&gt;      if &lt;/b&gt;p^[i] &lt;&gt; &lt;b&gt;nil then &lt;/b&gt;Malloc.Free(p^[i]);&lt;br /&gt;    &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;    FreeMem(p);&lt;br /&gt;  &lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;procedure &lt;/b&gt;ShowFileProperties(Files: TStrings; aWnd: HWND);&lt;br /&gt;&lt;b&gt;type&lt;br /&gt;  &lt;/b&gt;PArrayOfPItemIDList = ^TArrayOfPItemIDList;&lt;br /&gt;  TArrayOfPItemIDList = &lt;b&gt;array&lt;/b&gt;[0..0] &lt;b&gt;of &lt;/b&gt;PItemIDList;&lt;br /&gt;&lt;b&gt;var&lt;br /&gt;  &lt;/b&gt;Data: IDataObject;&lt;br /&gt;&lt;b&gt;begin&lt;br /&gt;  if &lt;/b&gt;Files.Count = 0 &lt;b&gt;then &lt;/b&gt;Exit;&lt;br /&gt;  Data := GetFileListDataObject(Files);&lt;br /&gt;  SHMultiFileProperties(Data, 0);&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#000080;"&gt;&lt;i&gt;// Example:&lt;br /&gt;// Beispiel:&lt;br /&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;b&gt;procedure &lt;/b&gt;TForm1.Button1Click(Sender: TObject);&lt;br /&gt;&lt;b&gt;begin&lt;br /&gt;  if &lt;/b&gt;OpenDialog1.Execute &lt;b&gt;then&lt;br /&gt;    &lt;/b&gt;Memo1.Lines.AddStrings(OpenDialog1.Files);&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;procedure &lt;/b&gt;TForm1.Button2Click(Sender: TObject);&lt;br /&gt;&lt;b&gt;begin&lt;br /&gt;  &lt;/b&gt;ShowFileProperties(Memo1.Lines, 0);&lt;br /&gt;&lt;b&gt;end&lt;/b&gt;;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-7681045154033701197?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/7681045154033701197/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/multi-files-properties-dialog-delphi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7681045154033701197'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7681045154033701197'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/multi-files-properties-dialog-delphi.html' title='Multi-files properties dialog? - Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-5593599229711740968</id><published>2008-12-31T05:00:00.000-08:00</published><updated>2008-12-31T05:02:54.114-08:00</updated><title type='text'>Transparent Desktop Icon -  Delphi</title><content type='html'>Below is code to a quick unit I threw together to allow my Desktop icons to be transparent:&lt;br /&gt;&lt;br /&gt;unit Desktop;&lt;br /&gt;&lt;br /&gt;interface&lt;br /&gt;&lt;br /&gt;uses Windows, CommCtrl, Graphics;&lt;br /&gt;&lt;br /&gt;function FindDesktopWindow: HWND;&lt;br /&gt;function IsDesktopTransparent: Boolean;&lt;br /&gt;procedure SetDesktopIconColor(Foreground, Background: TColor; Trans: Boolean);&lt;br /&gt;procedure SetDefaultIconColors;&lt;br /&gt;&lt;br /&gt;implementation&lt;br /&gt;&lt;br /&gt;function FindDesktopWindow: HWND;&lt;br /&gt;var&lt;br /&gt;Window: HWND;&lt;br /&gt;begin&lt;br /&gt;Window := FindWindow('Progman','Program Manager');&lt;br /&gt;Window := FindWindowEx(Window,0,'SHELLDLL_DefView','');&lt;br /&gt;Window := FindWindowEx(Window,0,'SysListView32','');&lt;br /&gt;Result := Window;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;function IsDesktopTransparent: Boolean;&lt;br /&gt;var&lt;br /&gt;BkColor: COLORREF;&lt;br /&gt;begin&lt;br /&gt;BkColor := ListView_GetTextBkColor(FindDesktopWindow);&lt;br /&gt;if BkColor &lt;&gt; $FFFFFFFF then&lt;br /&gt; Result := False&lt;br /&gt;else&lt;br /&gt; Result := True;&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;procedure SetDesktopIconColor(Foreground, Background: TColor; Trans: Boolean);&lt;br /&gt;var&lt;br /&gt;Window: HWND;&lt;br /&gt;begin&lt;br /&gt;Window := FindDesktopWindow;&lt;br /&gt;if Trans = True then&lt;br /&gt; ListView_SetTextBkColor(Window,$FFFFFFFF)&lt;br /&gt;else&lt;br /&gt; ListView_SetTextBkColor(Window,Background);&lt;br /&gt;ListView_SetTextColor(Window, Foreground);&lt;br /&gt;ListView_RedrawItems(Window,0,ListView_GetItemCount(Window) - 1);&lt;br /&gt;UpdateWindow(Window);&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;procedure SetDefaultIconColors;&lt;br /&gt;var&lt;br /&gt;Kind: Integer;&lt;br /&gt;Color: TColor;&lt;br /&gt;begin&lt;br /&gt;Kind := COLOR_DESKTOP;&lt;br /&gt;Color := GetSysColor(COLOR_DESKTOP);&lt;br /&gt;SetSysColors(0,Kind,Color);&lt;br /&gt;end;&lt;br /&gt;&lt;br /&gt;end.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-5593599229711740968?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/5593599229711740968/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/transparent-desktop-icon.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5593599229711740968'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/5593599229711740968'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/transparent-desktop-icon.html' title='Transparent Desktop Icon -  Delphi'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-7017517302849863165</id><published>2008-12-31T04:57:00.000-08:00</published><updated>2008-12-31T05:04:30.408-08:00</updated><title type='text'>Theme application- Make a different style!</title><content type='html'>&lt;p&gt;&lt;img src="http://www.codeproject.com/KB/miscctrl/XP_theme/1.png" alt="Sample Image - 1.png" height="152" width="417" /&gt;&lt;/p&gt;  &lt;h2&gt;Introduction&lt;/h2&gt;  &lt;p&gt;&lt;em&gt;Dear.&lt;br /&gt;Don't worry about your application style.&lt;br /&gt;I'll tell you how to make your application deference. You can design them to the XP themes. How? Follow me.&lt;/em&gt; &lt;/p&gt;  &lt;p&gt;1) Create a new windows application project with C#.NET or VB.NET&lt;br /&gt;2) In your project constructor write this code:&lt;br /&gt;&lt;br /&gt;/* C# Application */&lt;br /&gt;public WindowsApplication1()&lt;br /&gt;{&lt;br /&gt;InitializeComponent(); &lt;/p&gt;  &lt;p&gt;////////////////////////////////////////////&lt;br /&gt;///////////////// Add It ///////////////// &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;System.Windows.Forms.Application.EnableVisualStyles();&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;////////////////////////////////////////////&lt;br /&gt;//////////////////////////////////////////// &lt;/p&gt;  &lt;p&gt;} &lt;/p&gt;  &lt;p&gt;static void Main()&lt;br /&gt;{&lt;br /&gt;&lt;strong&gt;&lt;em&gt;Application.EnableVisualStyles();&lt;br /&gt;Application.DoEvents();&lt;/em&gt;&lt;/strong&gt;Application.Run(new MainForm());&lt;br /&gt;}&lt;/p&gt;  &lt;p&gt;/* VB Application */&lt;br /&gt;Public Sub New()&lt;br /&gt;MyBase.New()&lt;br /&gt;InitializeComponent() &lt;/p&gt;  &lt;p&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;'''''''''''''''''''''' Add It ''''''''''''''' &lt;/p&gt;  &lt;p&gt;&lt;strong&gt;System.Windows.Forms.Application.EnableVisualStyles()&lt;/strong&gt; &lt;/p&gt;  &lt;p&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;''''''''''''''''''''''''''''''''''''''''''''''''''''&lt;br /&gt;End Sub &lt;/p&gt;  &lt;p&gt;3) Now, design your from with some of standard components.&lt;br /&gt;For example put a TextBox, a Button and a CheckBox. &lt;/p&gt;  &lt;p&gt;4) For your text box, do nothing. But for your button, set the FlatStyle property to the System.You must do that for the Checkbox and the radio button but do nothing for Combo Box, Progress and etc. &lt;/p&gt;  &lt;p&gt;&lt;img src="http://www.codeproject.com/KB/miscctrl/XP_theme/2.png" alt="Sample screenshot" height="267" width="543" /&gt; &lt;/p&gt;  &lt;p&gt;5) Run your project. Wow. See a deference style. &lt;/p&gt;  &lt;p&gt;6) Good luck. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-7017517302849863165?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/7017517302849863165/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/heme-application-make-different-style.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7017517302849863165'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/7017517302849863165'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/heme-application-make-different-style.html' title='Theme application- Make a different style!'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-2219773753775246759.post-4378317951114660010</id><published>2008-12-31T04:52:00.000-08:00</published><updated>2008-12-31T04:54:39.313-08:00</updated><title type='text'>How to make XP Style Buttons in your VB.Net</title><content type='html'>&lt;span id="_ctl0_ContentPlaceHolder1_Label1"&gt;&lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span id="_ctl0_ContentPlaceHolder1_Label1"&gt;&lt;span style=";font-family:arial;font-size:85%;"  &gt;&lt;b&gt;Author: &lt;/b&gt;&lt;a href="http://www.devasp.net/net/Forum/pop_profile.asp?mode=display&amp;amp;id=1700"&gt;DevASP&lt;/a&gt;&lt;br /&gt;Download Source Code : &lt;a href="http://www.devasp.net/SourceCode/364_XP_STYLE_BUTTONS.zip"&gt;364_XP_STYLE_BUTTONS.zip&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:arial;font-size:85%;"  &gt;&lt;span style=";font-family:Arial;font-size:85%;"  &gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:arial;font-size:85%;"  &gt;&lt;span style=";font-family:Arial;font-size:85%;"  &gt;Create a New Project in VB.Net. Drag ten Button controls from tool box . Now write code on Form Load Event and Buttons Click events &lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:arial;font-size:85%;"  &gt; &lt;/span&gt;&lt;/p&gt;&lt;span style=";font-family:arial;font-size:85%;"  &gt;&lt;span style="background-color: rgb(238, 238, 238);font-family:Times New Roman;font-size:100%;"  &gt; &lt;fieldset&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;color:blue;"   &gt;Private&lt;/span&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Form1_Load(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; &lt;span style="color:blue;"&gt;MyBase&lt;/span&gt;.Load&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button1.Text = "Click Me"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button2.Text = "Click Me"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button3.Text = "Click Me"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button4.Text = "Click Me"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button5.Text = "Click Me"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button6.Text = "Click Me"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button7.Text = "Click Me"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button8.Text = "Click Me"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button9.Text = "Click Me"&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button1_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button1.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button1.FlatStyle = FlatStyle.System&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button3_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button3.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button3.FlatStyle = FlatStyle.System&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button9_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button9.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button9.FlatStyle = FlatStyle.System&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;color:blue;"   &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button8_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button8.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button8.FlatStyle = FlatStyle.System&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;color:blue;"   &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button6_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button6.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button6.FlatStyle = FlatStyle.System&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;color:blue;"   &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button2_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button2.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button2.FlatStyle = FlatStyle.System&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;color:blue;"   &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button4_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button4.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button4.FlatStyle = FlatStyle.System&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;color:blue;"   &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button7_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button7.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button7.FlatStyle = FlatStyle.System&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;color:blue;"   &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button5_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button5.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button5.FlatStyle = FlatStyle.System&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;color:blue;"   &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;&lt;/span&gt;&lt;/span&gt; &lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;Private&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt; Button10_Click(&lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; sender &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.Object, &lt;span style="color:blue;"&gt;ByVal&lt;/span&gt; e &lt;span style="color:blue;"&gt;As&lt;/span&gt; System.EventArgs) &lt;span style="color:blue;"&gt;Handles&lt;/span&gt; Button10.Click&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;o:p&gt; &lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button1.FlatStyle = FlatStyle.Standard&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button2.FlatStyle = FlatStyle.Standard&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button3.FlatStyle = FlatStyle.Standard&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button4.FlatStyle = FlatStyle.Standard&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button5.FlatStyle = FlatStyle.Standard&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button6.FlatStyle = FlatStyle.Standard&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button7.FlatStyle = FlatStyle.Standard&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button8.FlatStyle = FlatStyle.Standard&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;        &lt;/span&gt;Button9.FlatStyle = FlatStyle.Standard&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style=""&gt;      &lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/p&gt; &lt;p class="MsoNormal" style="margin: 0in 0in 0pt;"&gt;&lt;span style=";font-family:'Courier New';font-size:10;"  &gt;&lt;span style="color:blue;"&gt;End&lt;/span&gt; &lt;span style="color:blue;"&gt;Sub&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/fieldset&gt;&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/2219773753775246759-4378317951114660010?l=sourcecode-koreng.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sourcecode-koreng.blogspot.com/feeds/4378317951114660010/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-make-xp-style-buttons-in-your.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4378317951114660010'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/2219773753775246759/posts/default/4378317951114660010'/><link rel='alternate' type='text/html' href='http://sourcecode-koreng.blogspot.com/2008/12/how-to-make-xp-style-buttons-in-your.html' title='How to make XP Style Buttons in your VB.Net'/><author><name>putra's koreng</name><uri>http://www.blogger.com/profile/08949725517241673291</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='28' src='http://2.bp.blogspot.com/_Hvw30jZUcrk/SUuFXTpHLOI/AAAAAAAAAA0/xZ8SRQhiGE4/s1600-R/1_351126251m.jpg'/></author><thr:total>0</thr:total></entry></feed>
