﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-kevin-随笔分类-c\c++</title><link>http://www.blogjava.net/kevinfriend/category/14072.html</link><description /><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 06:56:22 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 06:56:22 GMT</pubDate><ttl>60</ttl><item><title>window programming --keybord.txt</title><link>http://www.blogjava.net/kevinfriend/archive/2006/09/19/70599.html</link><dc:creator>康文</dc:creator><author>康文</author><pubDate>Tue, 19 Sep 2006 10:25:00 GMT</pubDate><guid>http://www.blogjava.net/kevinfriend/archive/2006/09/19/70599.html</guid><wfw:comment>http://www.blogjava.net/kevinfriend/comments/70599.html</wfw:comment><comments>http://www.blogjava.net/kevinfriend/archive/2006/09/19/70599.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kevinfriend/comments/commentRss/70599.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kevinfriend/services/trackbacks/70599.html</trackback:ping><description><![CDATA[
		<p>1 Keyboardd Basics<br /> 1) Ignoring the Keyboard<br /> Your programm does not need to act on every keyboard message it received,Window handle many keyboard message function itself.<br /> 2)Who's Got thefocus<br />  Though the keyboard is shared by all the window in the application ,The DispatchMessage send the message to the window procedure associated the window which message is intended.<br />  The window that receives a particular keyboard event is the window has the input foucs.<br />  Sometime no window has input foucs,this is the case if all your programs have been minmized,window continue send keyboard to the active window ,but it is send in the deffert form from sending to the input foucs.<br />  A window procedure can be determine when its window has the input focus by trapping WM_SETFOCUS and WM_KILLFOCUS.<br /> 3) Queues and Synchronization<br />  As the user presses and releases key on keyborad,Windows and keyboard device driver translate the hardware scan code into formatted message.Howerver the messages are not palced on the application queue right away,Instean Windows store these message in a system message queue .The System message queue is a single message maintained by windows specifically for the prelimary storage of user input from keyboard and the mouse. Window will take the next message from the system message queue and place it on the application message queue only when a window application has finished proecssing the previous user input message. <br />4) Keystorkes and Character<br /> The message that an application receives from windows about keyboard events distingush between keystrokes and characters.<br /> for instance The keysokes has only one key labbed 'A' ,and the character may be 'a' or 'ctr-a' etc.<br />2 Keystroke Message<br /> 1)Wirtual key Codes<br />  The virtual key code is stored in the wParam parameter of WM_KEYDOWN,WM_KEYUP,the code identifies the key being pressed or release.<br />  2)lParam Information<br />  the wParam message parameter contain virtual key code and the lparam message parameter contains other information in understanding the keystoke.<br />  3) Shift States<br />  iState = GetKeyState (VK_SHIFT) ;<br />  iState variable will be negative if the Shift key is donw <br />  iState = GetKeyState (VK_CAPITAL) ;<br />  4)Using Keystroke Messages<br />    <br />  5)<br />   case WM_KEYDOWN:<br />     switch (wParam)<br />     {<br />     case VK_HOME:<br />          SendMessage (hwnd, WM_VSCROLL, SB_TOP, 0) ;<br />          break ;</p>
		<p>     case VK_END:<br />          SendMessage (hwnd, WM_VSCROLL, SB_BOTTOM, 0) ;<br />          break ;</p>
		<p>     case VK_PRIOR:<br />          SendMessage (hwnd, WM_VSCROLL, SB_PAGEUP, 0) ;<br />          break ;<br />3 Character Messages<br />Message Key or Code <br />WM_KEYDOWN Virtual key code for `A' (0x41) <br />WM_CHAR Character code for `a' (0x61) <br />WM_KEYUP Virtual key code for `A' (0x41) </p>
		<p>Message Key or Code <br />WM_KEYDOWN Virtual key code VK_SHIFT (0x10) <br />WM_KEYDOWN Virtual key code for `A' (0x41) <br />WM_CHAR Character code for `A' (0x41) <br />WM_KEYUP Virtual key code for `A' (0x41) <br />WM_KEYUP Virtual key code VK_SHIFT (0x10) </p>
		<p>Key Character Code Duplicated by ANSI C  Escape <br />Backspace     0x08 Ctrl-H                \b <br />Tab           0x09 Ctrl-I                \t <br />Ctrl-Enter    0x0A Ctrl-J                \n <br />Enter         0x0D Ctrl-M                 \r <br />Esc 0x1B Ctrl-[ </p>
		<p>  case WM_CHAR:<br />     [other program lines]<br />     switch (wParam)<br />     {<br />     case `\b':          // backspace<br />          [other program line<br />          break ;<br />     case `\t':          // tab<br />          [other program lines]<br />          break ;</p>
		<p>     case `\n':          // linefeed<br />          [other program lines]<br />          break ;</p>
		<p>     case `\r':          // carriage return<br />          [other program lines]<br />          break ;</p>
		<p>     default:            // character codes<br />          [other program lines]<br />          break ;<br />     }<br />     return 0 ;<br /></p>
<img src ="http://www.blogjava.net/kevinfriend/aggbug/70599.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kevinfriend/" target="_blank">康文</a> 2006-09-19 18:25 <a href="http://www.blogjava.net/kevinfriend/archive/2006/09/19/70599.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>windows programming --window and message</title><link>http://www.blogjava.net/kevinfriend/archive/2006/09/18/70208.html</link><dc:creator>康文</dc:creator><author>康文</author><pubDate>Mon, 18 Sep 2006 01:17:00 GMT</pubDate><guid>http://www.blogjava.net/kevinfriend/archive/2006/09/18/70208.html</guid><wfw:comment>http://www.blogjava.net/kevinfriend/comments/70208.html</wfw:comment><comments>http://www.blogjava.net/kevinfriend/archive/2006/09/18/70208.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kevinfriend/comments/commentRss/70208.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kevinfriend/services/trackbacks/70208.html</trackback:ping><description><![CDATA[
		<p>1 An Architectural Overview<br />  1)Getting a good feel for messages is an import part of learning how to write programs for windows.<br />  Windows send a message to your porgram means that Windows calls a function in your program .The parameter of this function describe the message that is being send.The function in your program is know as Window Procedure.<br />  Windows send a message to window by calling window procedure ,The window procedure do some processing based on the message and return control to Windows.<br />  More precisely , a Window  is always createdd based on a "window class".The window class identifies the window procedure that precesses messages to the windows.The use of window class allow mutiple window to be based the same window class and hence use the same window procedure.<br />  2) Requsteriing the Window Class<br /> typedef struct<br /> {<br />     UINT        style ;<br />     WNDPROC     lpfnWndProc ;<br />     int         cbClsExtra ;<br />     int         cbWndExtra ;<br />     HINSTANCE   hInstance ;<br />     HICON       hIcon ;<br />     HCURSOR     hCursor ;<br />     HBRUSH      hbrBackground ;<br />     LPCTSTR     lpszMenuName ;<br />     LPCTSTR     lpszClassName ;<br /> }<br /> if (!RegisterClass (&amp;wndclass))<br /> {<br />     MessageBox (NULL, TEXT ("This program requires Windows NT!"), <br />                 szAppName, MB_ICONERROR) ;<br />     return 0 ;<br /> }<br /> 2)Creating the Window<br />  Window class define the general Characteristics of the a window.If you want to create window based on the same window class,you can use CreateWindwo which allow you to specify more detail imformation about the window. <br />  hwnd = CreateWindow (szAppName,                  // window class name<br />                     TEXT ("The Hello Program"), // window caption<br />                     WS_OVERLAPPEDWINDOW,        // window style<br />                     CW_USEDEFAULT,              // initial x position<br />                     CW_USEDEFAULT,              // initial y position<br />                     CW_USEDEFAULT,              // initial x size<br />                     CW_USEDEFAULT,              // initial y size<br />                     NULL,                       // parent window handle<br />                     NULL,                       // window menu handle<br />                     hInstance,                  // program instance handle<br />                     NULL) ;                     // creation parameters</p>
		<p> 3)Displaying the window<br />   When thee CreateWindow function return ,windows create a window internal.What it means is that windows allocate a block memory to store the imformation about the window.If you want to show that ,you should call<br />ShowWindow (hwnd, iCmdShow) ;<br />UpdateWindow (hwnd) ;  <br /> 4) The Message Loop<br /> while (GetMessage (&amp;msg, NULL, 0, 0))<br /> {<br />     TranslateMessage (&amp;msg) ;<br />     DispatchMessage (&amp;msg) ;<br /> }</p>
		<p> typedef struct tagMSG <br />{<br />     HWND   hwnd ;<br />     UINT   message ;// message identifier<br />     WPARAM wParam ;<br />     LPARAM lParam ;<br />     DWORD  time ; // the time the message is placed on the message queen<br />     POINT  pt ; // the mouse coordiante<br /> }<br /> typedef struct tagPOINT<br /> {<br />     LONG  x ;<br />     LONG  y ;<br /> }<br />POINT, * PPOINT;<br />the message filed of message retrived from the message queen is anyting except WM_QUITm ,GetMessage return a nonzero value<br />wM_QUIT cause GetMessage reuturn 0.</p>
		<p>TranslateMessage (&amp;msg) ;<br />passing the msg struct back to window for some keysboard translation</p>
		<p>DispatchMessage (&amp;msg) ;<br />pass the msessageback to window .Windwo then send the message to the appropriate window procedure for processing.After window<br />procedure processing the message ,it return control to the windows,which is still in serving the Dispatchmessage <br /> 5) The Window Procedure<br />  a The window procedure determined the how the window display on the client area and how the window respose to user input<br />  b LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam),the four parameter is idential to the first four filed of the message struct.<br /> 6) Processing the Message<br /> switch (iMsg)<br />{<br />case WM_CREATE :<br />     [process WM_CREATE message]<br />     return 0 ;<br />          <br />case WM_PAINT :<br />     [process WM_PAINT message]<br />     return 0 ;<br />          <br />case WM_DESTROY :<br />     [process WM_DESTROY message]<br />     return 0 ;<br />}<br />return DefWindowProc (hwnd, iMsg, wParam, lParam) ;</p>
		<p>when a window procedure process the message ,it should return 0;<br /> 7) The WM_PAINT Message<br />  WU_PAINT message is extremely import in window.it inform a program when part or all of the window 's client area are invalid and must be redraw or repaint.<br /> WM_PAINT processing almost always begins with a call to BeginPaint: <br />  hdc = BeginPaint (hwnd, &amp;ps) ; //return a handle to device context<br />and ends with a call to EndPaint: <br /> EndPaint (hwnd, &amp;ps) ;<br /> 8)The WM_DESTROY Message<br />  PostQuitMessage (0) ;<br />   <br /> WNDCLASS, * PWNDCLASS ;<br /> #include &lt;windows.h&gt;<br />/* the same as long _stdcall WndPro(long,unsign int,unsign int ,long)*/<br />LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;</p>
		<p>/*int _stdcall WinMain(long hInstance,long hPrevInstance,char * szComdLine,int iCmdShow)*/<br />int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,    <br />                    PSTR szCmdLine, int iCmdShow) <br />{<br />     static TCHAR szAppName[] = TEXT ("HelloWin") ;<br />     HWND         hwnd ;<br />     MSG          msg ;<br />     WNDCLASS     wndclass ;<br />/*<br />  all window createe based on this window class will completely repaint whenever  horizational window sizd and vertial   <br />  window size change.<br />*/<br />     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;                  <br />     wndclass.lpfnWndProc   = WndProc ; // set the window procedure for the window class<br />     wndclass.cbClsExtra    = 0 ;<br />     wndclass.cbWndExtra    = 0 ;<br />     wndclass.hInstance     = hInstance ; // the handle of this program<br />     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;<br />     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;<br />     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;<br />     wndclass.lpszMenuName  = NULL ;<br />     wndclass.lpszClassName = szAppName ;</p>
		<p>     if (!RegisterClass (&amp;wndclass))<br />     {<br />          MessageBox (NULL, TEXT ("This program requires Windows NT!"), <br />                      szAppName, MB_ICONERROR) ;<br />          return 0 ;<br />     }<br />     hwnd = CreateWindow (szAppName,                  // window class name<br />                          TEXT ("The Hello Program"), // window caption<br />                          WS_OVERLAPPEDWINDOW,        // window style<br />                          CW_USEDEFAULT,              // initial x position<br />                          CW_USEDEFAULT,              // initial y position<br />                          CW_USEDEFAULT,              // initial x size<br />                          CW_USEDEFAULT,              // initial y size<br />                          NULL,                       // parent window handle<br />                          NULL,                       // window menu handle<br />                          hInstance,                  // program instance handle<br />                          NULL) ;                     // creation parameters<br />     <br />     ShowWindow (hwnd, iCmdShow) ;// iCmdShow determine how the window is to be initially displayed on the screen.<br />     UpdateWindow (hwnd) ; // cause the client area to paint<br />     <br />     while (GetMessage (&amp;msg, NULL, 0, 0))<br />     {<br />          TranslateMessage (&amp;msg) ;<br />          DispatchMessage (&amp;msg) ;<br />     }<br />     return msg.wParam ;<br />}</p>
		<p>LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)<br />{<br />     HDC         hdc ;<br />     PAINTSTRUCT ps ;<br />     RECT        rect ;<br />     <br />     switch (message)<br />     {<br />     case WM_CREATE:<br />          PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;<br />          return 0 ;</p>
		<p>     case WM_PAINT:<br />          hdc = BeginPaint (hwnd, &amp;ps) ;//return a handle to device context<br />          <br />          GetClientRect (hwnd, &amp;rect) ;// set the struct rect with the dimensions of the client area<br />          <br />          DrawText (hdc, TEXT ("Hello, Windows 98!"), -1, &amp;rect,<br />                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;<br />          EndPaint (hwnd, &amp;ps) ;<br />          return 0 ;<br />          <br />     case WM_DESTROY:<br />          PostQuitMessage (0) ;// insert a WM_QIUT in the message queue.<br />          return 0 ;<br />     }<br />     return DefWindowProc (hwnd, message, wParam, lParam) ;<br />}</p>
		<p>2 The Window Programming Hurdles<br /> 1)Queue and noqueue message<br />  queue message are post the message queue and the noqueue message send to the window procdure directly.<br />     </p>
<img src ="http://www.blogjava.net/kevinfriend/aggbug/70208.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kevinfriend/" target="_blank">康文</a> 2006-09-18 09:17 <a href="http://www.blogjava.net/kevinfriend/archive/2006/09/18/70208.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>window programming --Unicode </title><link>http://www.blogjava.net/kevinfriend/archive/2006/09/14/69650.html</link><dc:creator>康文</dc:creator><author>康文</author><pubDate>Thu, 14 Sep 2006 07:12:00 GMT</pubDate><guid>http://www.blogjava.net/kevinfriend/archive/2006/09/14/69650.html</guid><wfw:comment>http://www.blogjava.net/kevinfriend/comments/69650.html</wfw:comment><comments>http://www.blogjava.net/kevinfriend/archive/2006/09/14/69650.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kevinfriend/comments/commentRss/69650.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kevinfriend/services/trackbacks/69650.html</trackback:ping><description><![CDATA[
		<p>一 Unicode 简介<br /> 1 Unicode 是ASCII 扩展，从传统的7位，扩展位16 位，可以显示世界上所有语言<br />ASCII 码<br />       0-     1-     2-     3-     4-     5-     6-     7-<br />-0     NUL    DLE    SP     0      @      P      `      p<br />-1     SOH    DC1    !      1      A      Q      a      q<br />-2     STX    DC2    "      2      B      R      b      r<br />-3     ETX    DC3    #      3      C      S      c      s<br />-4     EOT    DC4    $      4      D      T      d      t<br />-5     ENQ    NAK    %      5      E      U      e      u<br />-6     ACK    SYN    &amp;      6      F      V      f      v<br />-7     BEL    ETB    '      7      G      W      g      w<br />-8     BS     CAN    (      8      H      X      h      x<br />-9     HT     EM     )      9      I      Y      I      y<br />-A     LF     SUB    *      :      J      Z      j      z<br />-B     VT     ESC    +      ;      K      [      k      {<br />-C     FF     FS     ,      &lt;      L      \      l      |<br />-D     CR     GS     -      =      M      ]      m      }<br />-E     SO     RS     .      &gt;      N      ^      n      ~<br />-F     SI     US     /      ?      O      _      o      DEL</p>
		<p> 2 双位字符集<br />DBCS：double-byte character set，最初的128个代码是ASCII，较高的128个代码中的某些总是跟随著第</p>
		<p>二个位元组。这两个位元组一起（称作首位元组和跟随位元组）定义一个字元。</p>
		<p> 3 Unicode 解决方案<br />  Unicode是统一的16位元系统，也DBCS 这样的同时含有一位和两位的字符集不同，Unicode 可以表示</p>
		<p>65536 个字符。<br />  Unicode 的缺点是，Unicode 使用的空间是ASCII 的两倍<br />二 宽字符和c<br /> 1 char <br />  char c='A';<br />  变量c 用一个字节来存储，用16 进制表示位0x41<br />  char * p; <br />  32 位系统，一次指针变量需要用4个字节表示<br />  char * p="Hello!";<br />  字符串占用7个字节 其中 6个用于保存字符串，1个用于保存中止符号0<br />  char [10]<br />  占用10个字节<br />  char a[]="Hello!"; <br />  占用 7个字节<br /> 2 宽字节<br />  typedef unsigned short whcar_t   which is define in the window.h  <br />  与unsign short 一样 为16 字节，两个字节<br />  wchar_t c='A'   0x0041<br />  wchar_t *p=L"Hello!"  指针占用 4个字节 而 字符串占用 14 个字节<br /> 3 宽字元程序库函数<br />  char * pc = "Hello!" ;<br />  iLength = strlen (pc) ;<br />  wchar_t * pw = L"Hello!" ;<br />  iLength = wcslen (pw) ;<br /> 4 维护单一原始码<br /> Microsoft Visual C++包含的TCHAR.H<br /> 如果定义了名为_UNICODE的识别字，并且程式中包含了TCHAR.H表头档案，那么_tcslen就定义为wcslen<br /> #define _tcslen wcslen<br /> 如果没有定义UNICODE，则_tcslen定义为strlen：<br /> #define _tcslen strlen<br /> 如果定义了 _UNICODE识别字，那么TCHAR就是wchar_t：<br /> typedef wchar_t TCHAR ;<br /> 否则，TCHAR就是char：<br /> typedef char TCHAR ;</p>
		<p> 如果没有定义_UNICODE识别字<br /> #define __T(x) x<br /> #define _T（x） __T（x）<br /> #define _TEXT（x） __T（x）</p>
		<p>三 宽字节和windows<br />  1 window 头文件中的类型<br />  typedef char CHAR ;<br />  typedef wchar_t WCHAR ；<br />  typedef CHAR * PCHAR, * LPCH, * PCH, * NPSTR, * LPSTR, * PSTR ;<br />  typedef CONST CHAR * LPCCH, * PCCH, * LPCSTR, * PCSTR ;<br />  typedef WCHAR * PWCHAR, * LPWCH, * PWCH, * NWPSTR, * LPWSTR, * PWSTR ;<br />  typedef CONST WCHAR * LPCWCH, * PCWCH, * LPCWSTR, * PCWSTR ;</p>
		<p>  #ifdef  UNICODE                   <br />  typedef WCHAR TCHAR, * PTCHAR ;<br />  typedef LPWSTR LPTCH, PTCH, PTSTR, LPTSTR ;<br />  typedef LPCWSTR LPCTSTR ;<br />  #else <br />  typedef char TCHAR, * PTCHAR ;<br />  typedef LPSTR LPTCH, PTCH, PTSTR, LPTSTR ;<br />  typedef LPCSTR LPCTSTR ;<br />  #endif<br />  2 Windows 函数调用<br />  #ifdef UNICODE<br />  #define MessageBox  MessageBoxW<br />  #else<br />  #define MessageBox  MessageBoxA<br />  #endif<br />  3 Windows 的字符函数<br />  ILength = lstrlen (pString) ;<br />  pString = lstrcpy (pString1, pString2) ;<br />  pString = lstrcpyn (pString1, pString2, iCount) ;<br />  pString = lstrcat (pString1, pString2) ;<br />  iComp = lstrcmp (pString1, pString2) ;<br />  iComp = lstrcmpi (pString1, pString2) ;<br />  4 在windows 使用printf <br />   windows 并不支持printf 但是可以使用sprintf <br />   int printf (const char * szFormat, ...) ;<br />   printf ("The sum of %i and %i is %i", 5, 3, 5+3) ;<br />   int sprintf (char * szBuffer, const char * szFormat, ...) ;<br />   char szBuffer [100] ;<br />   sprintf (szBuffer, "The sum of %i and %i is %i", 5, 3, 5+3) ;<br />   puts (szBuffer) ;</p>
<img src ="http://www.blogjava.net/kevinfriend/aggbug/69650.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kevinfriend/" target="_blank">康文</a> 2006-09-14 15:12 <a href="http://www.blogjava.net/kevinfriend/archive/2006/09/14/69650.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>windows programming -jump from start.txt</title><link>http://www.blogjava.net/kevinfriend/archive/2006/09/13/69398.html</link><dc:creator>康文</dc:creator><author>康文</author><pubDate>Wed, 13 Sep 2006 08:37:00 GMT</pubDate><guid>http://www.blogjava.net/kevinfriend/archive/2006/09/13/69398.html</guid><wfw:comment>http://www.blogjava.net/kevinfriend/comments/69398.html</wfw:comment><comments>http://www.blogjava.net/kevinfriend/archive/2006/09/13/69398.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kevinfriend/comments/commentRss/69398.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kevinfriend/services/trackbacks/69398.html</trackback:ping><description><![CDATA[
		<p>1 动态连接<br />Windows 运作机制的核心是一个称作动态连接的概念</p>
		<p>
				<br />#include &lt;windows.h&gt;<br />int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,<br />   PSTR szCmdLine, int iCmdShow)<br />{<br />MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0);<br />return 0 ;<br />}</p>
		<p> 1) #include &lt;windows.h&gt;包含其它的头文件<br /> 2) 程序入口<br />   int WINAPI WinMain （ HINSTANCE hInstance,HINSTANCE hPrevInstance,<br />             PSTR szCmdLine,int iCmdShow）</p>
		<p>   a #define WINAPI __stdcall  指定一个呼叫约定，包括如何生产机器码，参数如何入栈<br />   b HINSTANCE hInstance 执行体代号，唯一标识该程序<br />   c HINSTANCE hPrevInstance 已经不采用<br />   d PSTR szCmdLine 参数列表<br />   e int iCmdShow 显示方式<br />  3) MessageBox 函数<br />   MessageBox (NULL, TEXT ("Hello, Windows 98!"), TEXT ("HelloMsg"), 0);<br />   参数1 窗体代号<br />   参数2 主题显示文字<br />   参数3 标题显示文字<br />   参数4 按钮，0 为确认 使用C语言的OR（|）操作符号将上面显示的一个常数与代表内定按钮的常数组合：</p>
		<p>  <br />#define  MB_OK                           0x00000000L<br />#define  MB_OKCANCEL                     0x00000001L<br />#define  MB_ABORTRETRYIGNORE             0x00000002L<br />#define  MB_YESNOCANCEL                  0x00000003L<br />#define  MB_YESNO                        0x00000004L<br />#define  MB_RETRYCANCEL  </p>
		<p>#define  MB_DEFBUTTON1                   0x00000000L<br />#define  MB_DEFBUTTON2                   0x00000100L<br />#define  MB_DEFBUTTON3                   0x00000200L<br />#define  MB_DEFBUTTON4</p>
		<p>图示的外观<br />#define  MB_ICONHAND                        0x00000010L<br />#define  MB_ICONQUESTION                       0x00000020L<br />#define  MB_ICONEXCLAMATION                    0x00000030L<br />#define  MB_ICONASTERISK </p>
		<p>#define  MB_ICONWARNING               MB_ICONEXCLAMATION<br />#define  MB_ICONERROR                 MB_ICONHAND<br />#define  MB_ICONINFORMATION           MB_ICONASTERISK<br />#define  MB_ICONSTOP </p>
		<p>2 c 语言编译过程 c--compile --   .obj   ---linking----  .exe</p>
<img src ="http://www.blogjava.net/kevinfriend/aggbug/69398.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kevinfriend/" target="_blank">康文</a> 2006-09-13 16:37 <a href="http://www.blogjava.net/kevinfriend/archive/2006/09/13/69398.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++ builder 文件的操作</title><link>http://www.blogjava.net/kevinfriend/archive/2006/09/09/68769.html</link><dc:creator>康文</dc:creator><author>康文</author><pubDate>Sat, 09 Sep 2006 15:25:00 GMT</pubDate><guid>http://www.blogjava.net/kevinfriend/archive/2006/09/09/68769.html</guid><wfw:comment>http://www.blogjava.net/kevinfriend/comments/68769.html</wfw:comment><comments>http://www.blogjava.net/kevinfriend/archive/2006/09/09/68769.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kevinfriend/comments/commentRss/68769.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kevinfriend/services/trackbacks/68769.html</trackback:ping><description><![CDATA[
		<p>一 文件<br />   1 c 标准文件驱动器，可以支持两种文件类型，二进制文件，和文本文件。c 标准文件是在头文件</p>
		<p>stdio.h 中声明。<br />标准文件类型通过指针来进行存储 FILE * fp;<br />   2 c++ 流式文件类 fstream，ifstream 和ofstream，分别对应读写，读和写，并支持文本和二进制文</p>
		<p>件。<br />   3 非缓冲文件<br />二 文件对话框组件<br />   1 OpenDialog 两种.TXT and .PAS 两种类型的过滤器。<br />     1) Filter OpenDaalog1-&gt;Filter="Text files{*.txt}|*.TXT|Pascal files{*.pas}|*.PAS";<br />  同一个过滤器中，还可以有多种文件后缀<br />      OpenDialog1-&gt;Filter="Pascal files|*.PAS;*.DPK;*.DPR";<br />     2) FilterIndex 设置对话框一打开时选中的文件过滤。数值从1开始计算。<br />     3) InitialDir 设置对话框打开时定位的目录。<br />     4) Options <br />       OpenPictureDialog1-&gt;Options.Clear();<br />       OeenPictureDialog1-&gt;Options&lt;&lt;ofFileMustExist&lt;&lt;ofHideReadOnly&lt;&lt;ofNoChangeDir;<br />     5) Title 设置对话框标题中显示的内容。<br />   2 SaveDialog 组建可以选择并保存文件<br />   3 OpenPictureDialog 可以选择并打开图形文件。<br />   4 SavePictureDialog 可以选择并保存图形文件。<br />三 Win3。1 相关组件<br />   FileListBox，DirectoryListBox，DriveCombox,FilterComboBox<br />四 常用文件管理函数<br />  1 文件函数常用函数<br />  将一个文件从记录盘上删除，如果不存在或无法删除。则返回False。<br />  extern PACKAGE bool __fastcall DeleteFile(const AnsiString FileName);<br />  void __fastcall TFORM1::ButtonClick(TObject *Sender)<br />  {<br />      char buffer[256];<br />      GetWindowsDirectory(buffer,sizeof(buffer));//获取Windows 系统目录<br />     AnsiString asFileName=FileSearch(Edit1-&gt;Text,GetCurrentDir()+AnsiString(";")</p>
		<p>+AnsiString(buffer));//在当前目录下和windows系统}//目录下查询文件。  <br />  if(asFileName.IsEmty()) ShowMessage(AnsiString("Couldn't Found")+Edit1-&gt;Text1);<br />  2 FileSeek <br />  extern PACKAGE int __fastcall FileSeek(int Handle, int Offset, int Origin);<br />  extern PACKAGE __int64 __fastcall FileSeek(int Handle, const __int64 Offset, int Origin);</p>
		<p>  Description</p>
		<p>  Use FileSeek to reposition the read/write point in a file that was opened with FileOpen or </p>
		<p>FileCreate. Handle is the file handle that was returned by FileOpen or FileCreate.</p>
		<p>  Offset specifies the number of bytes from Origin where the file pointer should be </p>
		<p>positioned. Origin is a code with three possible values, denoting the beginning of the file, <br />  the end of the file, and the current position of the file pointer.</p>
		<p>  Origin Action</p>
		<p>  0 The file pointer is positioned Offset bytes from the beginning of the file. <br />  1 The file pointer is positioned Offset bytes from its current position. <br />  2 The file pointer is positioned Offset bytes from the end of the file.</p>
		<p>  If FileSeek is successful, it returns the new position of the file pointer; otherwise, it </p>
		<p>returns -1.<br />   void __fastcall TForm1::Button1Click(TObject *Sender)</p>
		<p>{<br />  int iFileHandle;<br />  int iFileLength;<br />  int iBytesRead;<br />  char *pszBuffer;<br />  if (OpenDialog1-&gt;Execute())<br />  {<br />    try<br />    {<br />      iFileHandle = FileOpen(OpenDialog1-&gt;FileName, fmOpenRead);<br />      iFileLength = FileSeek(iFileHandle,0,2);<br />      FileSeek(iFileHandle,0,0);<br />      pszBuffer = newchar[iFileLength+1];<br />      iBytesRead = FileRead(iFileHandle, pszBuffer, iFileLength);<br />      FileClose(iFileHandle);</p>
		<p>      for (int i=0;i&lt;iBytesRead;i++)<br />      {<br />        StringGrid1-&gt;RowCount += 1;<br />        StringGrid1-&gt;Cells[1][i+1] = pszBuffer[i];<br />        StringGrid1-&gt;Cells[2][i+1] = IntToStr((int)pszBuffer[i]);<br />      }<br />      delete [] pszBuffer;<br />    }<br />    catch(...)<br />    {<br />      Application-&gt;MessageBox("Can't perform one of the following file operations: Open, </p>
		<p>Seek, Read, Close.", "File Error", IDOK);<br />    }<br />  }<br />}<br /> 3FileExists<br />  if(FileExist(SaveDialog1-&gt;FileName))<br /> {<br />   RenameFile(SaveDialog1-&gt;File,SaveDialog1-&gt;FileName+".bak");<br /> }<br /> iFileHandle=fileCreate(SaveSialog1-&gt;FileName);<br /> for(int i=0;i&lt;Memo2-&gt;Lines-&gt;String[i].length())<br /> {<br />   FileWrite(iFileHandle,Memo2-&gt;Lines-&gt;String[i].c_str(),length);<br /> }<br /> FileClose(iFileHandle);<br />  4 FileGetAttrs<br />  FileGetAttr returns the attributes of the file as a string of bits. This value is the same </p>
		<p>as the Attr field of a TSearchRec struct. Check for individual attributes with code such as </p>
		<p>the following:<br />  int Attrs = FileGetAttr("MyFile.sys");<br />  if x(Attrs &amp; faHidden)<br />  FileSetAttr("MyFile.sys", Attrs &amp; !faHidden);<br />  A return value of -1 indicates that an error occurred.<br />  5 FileSetAttrs<br />  FileSetAttr sets the file attributes of the file given by FileName to the value given by </p>
		<p>Attr. The value of Attr is formed by combining the appropriate file attribute constants, as </p>
		<p>in the following: <br />  FileSetAttr("MyFile.sys", faReadOnly | faSysFile);<br />  FileSetAttr returns zero if the function was successful. Otherwise the return value is an </p>
		<p>error code.<br />三 目录操作常用函数<br />  1 CreateDir<br />     #include &lt;Filectrl.hpp&gt;<br />   void __fastcall TForm1::Button1Click(TObject *Sender)</p>
		<p>   {<br />      if (!DirectoryExists("c:\\temp"))<br />      {<br />            if (!CreateDir("C:\\temp"))<br />            throw Exception("Cannot create c:\\temp directory.");<br />      }<br />   }<br />   2 ForceDirectories<br />   ForceDirectories creates a new directory as specified in Dir, which must be a fully-</p>
		<p>qualified path name. If the directories given in the path do not yet exist, ForceDirectories </p>
		<p>attempts to create them.<br />   ForceDirectories returns true if it successfully creates all necessary directories, false </p>
		<p>if it could not create a needed directory.<br />   Important<br />   Do not call ForceDirectories with an empty string. Doing so causes ForceDirectories to </p>
		<p>throw an exception.  <br />   void __fastcall TForm1::Button1Click(TObject *Sender)<br />   {<br />         AnsiString Dir = "C:\Apps\Sales\Local";<br />         if (ForceDirectories(Dir))<br />         Label1-&gt;Caption = Dir + " was created";<br />   }<br />   3 GetCurrentDir<br />   获取当前的目录完整的路径名<br />   4 RemoveDir<br />   删除一个存在的目录，目录必须为空<br />   5 SetCurrentDir设置系统的当前目录 <br />   6 SelectDirectory<br />   extern PACKAGE bool __fastcall SelectDirectory(constAnsiString Caption, const WideString </p>
		<p>Root, AnsiString &amp;Directory);<br />   Call SelectDirectory to let the user enter a directory name.   <br />   Use the first syntax to display the Windows directory browser. The Caption parameter </p>
		<p>specifies a caption for the dialog. The Root parameter specifies the root directory from </p>
		<p>which to browse. The selected directory is returned as the Directory parameter. When using </p>
		<p>this syntax, <br />   SelectDirectory does not change the value of the current directory.<br />   <br />   extern PACKAGE bool __fastcall SelectDirectory(AnsiString &amp;Directory, TSelectDirOpts </p>
		<p>Options, int HelpCtx);<br />   enum TSelectDirOpt { sdAllowCreate, sdPerformCreate, sdPrompt };<br />   typedef Set&lt;TSelectDirOpt, sdAllowCreate, sdPrompt&gt;  TSelectDirOpts;<br />   <br />   sdAllowCreate An edit box allows the user to type in the name of a directory that <br />                        does not exist. This option does not create a directory: the </p>
		<p>application <br />                        must read the name of the selected directory and create it i<br />                      f desired.<br />   sdPerformCreate Used only in combination with sdAllowCreate. If the user enters a </p>
		<p>directory <br />                        name that does not exist, the directory selection dialog creates it.<br />   sdPrompt        Used only in combination with sdAllowCreate. Displays a message box <br />                       that informs the user when the entered directory does not exist <br />                       and asks if the directory should be created. <br />                       If the user chooses OK, the directory is created <br />                       if the option set includes sdPerformCreate. <br />                       If the option set does not include sdPerformCreate, <br />                       the directory is not created: <br />                the application must read the directory name and create<br />    #include &lt;FileCtrl.hpp&gt;<br />    void __fastcall TForm1::Button1Click(TObject *Sender)<br />   {<br />      AnsiString Dir = "C:\\Program Files\\MyApp";<br />      if (SelectDirectory(Dir, TSelectDirOpts() &lt;&lt; sdAllowCreate &lt;&lt; sdPerformCreate &lt;&lt; </p>
		<p>sdPrompt,1000))<br />      Label1-&gt;Caption = Dir;</p>
		<p>   }<br />三 驱动器常用函数<br />  1 DiskFree 指定驱动器中剩余空间的字节数<br />  2 DiskSize 驱动器容量<br />四文件名常用函数<br />  1 ChangeFileExt<br />  2 ExtractFileDir<br />  3 ExtractFileDriver<br />  4 ExtractFileExt<br />  5 ExtractFileName<br />  6 ExtractFilePath<br />  7 ExtractRelativePath<br />实例<br />1<br />   1<br />L a b e l 1 目录列表( &amp; D ) : FocusControl: DirectoryListBox1<br />D i r e c t o r y L i s t B o x 1 D i r L a b e l : L a b e l 6 ;<br />                                                    FileList: FileListBox1<br />L a b e l 2 文件列表( &amp; S ) : FocusControl: FileListBox1<br />F i l e L i s t B o x 1 FileEdit: Edit1<br />L a b e l 3 驱动器( &amp; R ) : FocusControl: DriveComboBox1<br />D r i v e C o m b o B o x 1 DirList: DirectoryListBox1<br />L a b e l 4 文件类型( &amp; F ) : FocusControl: FilterComboBox1<br />F i l t e r C o m b o B o x 1 FileList: FileListBox1<br />      Filter: 所有文件 ( * . * ) | * . * |文本文件( * . t x t ) | * . t x t<br />L a b e l 5 路径名：<br />L a b e l 6 C : \ S a m p l e s \ S 0 6 B<br />L a b e l 7 文件名( &amp; N ) : FocusControl: Edit1<br />E d i t 1<br />B u t t o n 1 文件长度( &amp; L ) . . . Te x t : * . *<br />#include&lt;stdio.h&gt;<br />void __fastcall TForm1::Button1Click(TObject *Sender)<br />{<br />FILE* fp;<br />AnsiString FileFullName;<br />long size;<br />AnsiString PropertyMes;<br />FileFullName=Label2-&gt;Caption+"<a href="file://\\&quot;+Edit1-&gt;Text">\\"+Edit1-&gt;Text</a>;<br />if(FileExists(FileFullName))<br />{<br />  fp=fopen(FileFullName.c_str(),"rt");<br />  if(fp!=NULL)<br />  {<br />    fseek(fp,0L,SEEK_END);<br />    size=ftell(fp);//get the length of file<br />    PropertyMes="file is total"+IntToStr(size)+"bytes.";<br />    MessageDlg(PropertyMes,mtInformation,TMsgDlgButtons() &lt;&lt; mbOK, 0);</p>
		<p>  }else<br />  {<br />      MessageDlg(PropertyMes,mtWarning,TMsgDlgButtons() &lt;&lt; mbOK, 0);<br />  }<br />  fclose(fp);<br /> }<br />}</p>
		<p> 2 获取驱动器类型信息<br /> UINT GetDriveType(<br />  LPCTSTR lpRootPathName //获取根目录的路径名称<br /> )<br /> <br />表6-5   函数G e t D r i v e Ty p e的返回值及其含义<br />数 值                             含 义<br />0                            无法检测驱动器的类型<br />1                             根目录不存在<br />D R I V E _ R E M O VA B L E 可移动驱动器<br />D R I V E _ F I X E D 不可移动驱动器<br />D R I V E _ R E M O T E 网络驱动器<br />D R I V E _ C D R O M C D - R O M驱动器<br />D R I V E _ R A M D I S K 虚拟驱动器</p>
		<p>Result=GetDriveType(Edit2-&gt;Text.c_str());<br />3 操作ini 文件</p>
<img src ="http://www.blogjava.net/kevinfriend/aggbug/68769.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kevinfriend/" target="_blank">康文</a> 2006-09-09 23:25 <a href="http://www.blogjava.net/kevinfriend/archive/2006/09/09/68769.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>动态链接库</title><link>http://www.blogjava.net/kevinfriend/archive/2006/09/06/68088.html</link><dc:creator>康文</dc:creator><author>康文</author><pubDate>Wed, 06 Sep 2006 10:13:00 GMT</pubDate><guid>http://www.blogjava.net/kevinfriend/archive/2006/09/06/68088.html</guid><wfw:comment>http://www.blogjava.net/kevinfriend/comments/68088.html</wfw:comment><comments>http://www.blogjava.net/kevinfriend/archive/2006/09/06/68088.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kevinfriend/comments/commentRss/68088.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kevinfriend/services/trackbacks/68088.html</trackback:ping><description><![CDATA[
		<p>动态连接库（Dynamic link library）,是一些编译过的可以执行的代码模块，后缀为.dll<br />1 DLL的基本理论<br />  在使用普通函数库时，可以在程序连接时将库中的代码拷贝到执行文件中，这时静态链接，在多个同样程序执行时，体统保留了许多<br />代码副本，造成了内存资源的浪费。在使用dll时，不必将dll链接到程序中，而是在应用程序运行时动态的装载dll，装载dll被映射<br />到进程的地址空间中。同时，使用dll 并不时将库代码拷贝，只是在程序中记录了函数的入口点和接口。<br />2 DLL 的优点<br /> 1） 节省系统资源<br /> 2） 不仅可以包括可执行代码，还可以包括数据和各种资源<br /> 3）支持多语言<br /> 4）升级可以仅仅覆盖dll 文件<br /> 5）dll 独立编程语言，c++builder 中的dll vc 也可以使用<br />3导入导出匹配<br />    DLL函数一般有两种函数，内部函数（internal）和导出函数(export).在实际情况下，许多DLL 调用了其他DLL里面的函数，因此<br />DLL可以同时有导入和导出函数。<br />     DLL 包含有一个导出函数表，可以通过函数的符号化的名字和称为序号的正书来识别这些函数，函数表中包含了函数在dll内部的<br />地址。在动态链接进程好建立一张表，把客户的调用与dll里的函数的地址连接起来。<br />    double dbValue(value);//内部函数<br />    double dbValue(value);//内部函数<br />    extern"c" _declspec(dllexpoert) double changeValue(double,bool);//外部函数<br />   double dblValue(double value)<br />   {<br />       return value*vlaue;<br />   }<br />   double changeValue(double value,bool whichOp)<br />  {<br />     return whichOp?doublValue(value):halfValue(value);<br />   }<br />  如果我们希望dll可以用于其他语言或不同版本的c++ 需要在声明导出函数的时候加上extern "C."<br /> 4 隐式的链接和显示的链接<br />   隐式链接（前面所讲）在创建dll 的时候，链接器产生一个lib 文件把拿获了dll中的导出符号和序号<br />   显示链接调用Win32 的LoadLibrary 函数，使用完后，调用 FreeLibrary.<br /> 5 查找dll <br />   依次顺序为包含exe 的目录，进程的当前目录，Windows 目录，path 环境变量里列出的目录。<br /> 6 创建动态链接库<br />   （如何查看dll 文件的定义）<br /> 7 导出函数：extern "C" __declspec(dllexport) ExportType FunctionName(Parameter) <br />    extern "C" __declspec(dllimport) __stdcall void CreateFromFunct(); <br />    extern "C" __declspec(dllexport) __stdcall void CreateFromFunct();//导出函数 <br />    导出类：class __declspec(dllexport) ExportType ClassName{...}<br />    class __declspec(dllexport) __stdcall MyDllClass { //导出类 <br />     public: <br />    MyDllClass(); <br />    void CreateAForm(); <br />    TDllFrm* DllMyForm; <br />    }; </p>
		<p>     __declspec(dllimport) class __stdcall MyDllClass { <br />       public: <br />       MyDllClass(); <br />       void CreateAForm(); <br />       TDllFrm* DllMyForm; <br />    }; <br />    静态调用，build 生成dll 文件和lib 文件，并把lib 文件导入到工程中<br />     void __fastcall TForm1::Button1Click(TObject *Sender) <br />    { // 导出类实现，导出类只能使用静态方式调用 <br />       DllClass = new MyDllClass(); <br />       DllClass-&gt;CreateAForm();   <br />    } <br />    void __fastcall TForm1::Button2Click(TObject *Sender) <br />    { // 导出函数实现 <br />       CreateFromFunct(); <br />     } <br />     void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &amp;Action) <br />    { <br />         delete DllClass; <br />     } <br />   动态调用<br />   class TForm1 : public TForm <br />  { <br />         ... <br />      private: // User declarations <br />      void (__stdcall *CreateFromFunct)(); <br />           ... <br />   } <br />  HINSTANCE DLLInst = NULL;<br />  void __fastcall TForm1::Button2Click(TObject *Sender) <br />  {   <br />     if( NULL == DLLInst ) DLLInst = LoadLibrary("DLL.dll"); //上面的 Dll <br />     if (DLLInst) { <br />             CreateFromFunct = (void (__stdcall*)()) GetProcAddress(DLLInst, <br />                          "CreateFromFunct"); <br />      if (CreateFromFunct) CreateFromFunct(); <br />      else ShowMessage("Could not obtain function pointer"); <br />   } <br />      else ShowMessage("Could not load DLL.dll"); <br />  } <br />   void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &amp;Action) <br />  { <br />     if ( DLLInst ) FreeLibrary (DLLInst); <br />  } <br />8  bcb 调用vc 编写的DLL<br />  1） 名字分解<br />   1. 名字分解： <br />  没有名字分解的函数 <br />    TestFunction1 // __cdecl calling convention <br />    @TestFunction2 // __fastcall calling convention <br />    TESTFUNCTION3 // __pascal calling convention <br />    TestFunction4 // __stdcall calling convention <br />  有名字分解的函数 <br />    @TestFunction1$QV // __cdecl calling convention <br />    @TestFunction2$qv // __fastcall calling convention <br />    TESTFUNCTION3$qqrv // __apscal calling convention <br />    @TestFunction4$qqrv // __stdcall calling convention <br />  使用 extern "C" 不会分解函数名 <br />   2）<br />      __cdecl 缺省 <br />   是 Borland C++ 的缺省的 C 格式命名约定，它在标识符前加一下划线，以保留 <br />  它原来所有的全程标识符。参数按最右边参数优先的原则传递给栈，然后清栈。 <br />    extaern "C" bool __cdecl TestFunction(); <br />   在 def 文件中显示为 <br />    TestFunction @1 <br />   注释： @1 表示函数的顺序数，将在“使用别名”时使用。 </p>
		<p>  __pascal Pascal格式 <br />   这时函数名全部变成大写，第一个参数先压栈，然后清栈。 <br />    TESTFUNCTION @1 //def file </p>
		<p>  __stdcall 标准调用 <br />   最后一个参数先压栈，然后清栈。 <br />    TestFunction @1 //def file </p>
		<p>  __fastcall 把参数传递给寄存器 <br />   第一个参数先压栈，然后清栈。 <br />    @TestFunction @1 //def file <br />  3）<br />   3. 解决调用约定： <br />   Microsoft 与 Borland 的 __stdcall 之间的区别是命名方式。 Borland 采用 <br />  __stdcall 的方式去掉了名字起前的下划线。 Microsoft 则是在前加上下划线，在 <br />  后加上 @ ，再后跟为栈保留的字节数。字节数取决于参数在栈所占的空间。每一个 <br />  参数都舍入为 4 的倍数加起来。这种 Miocrosoft 的 DLL 与系统的 DLL 不一样。 <br /> 4 查看dll 的调用接口tdump -ee MyDll.dll &gt;1.txt (查看 1.txt 文件即可) <br />5 编辑c++ builder build 为一个可以直接调用的 .exe 。 点击project option  中linker 标签 去掉user dynamic RTL 选项 和package 中 去掉builder with runtime package 选项.<br />6 调用代参数的vl 编写的dll 调用的实例。<br />int (__cdecl *fun)(char*,char*,char*);<br /><br />HINSTANCE DLLInst = NULL;<br />void __fastcall TForm1::Button1Click(TObject *Sender)<br />{<br />if( NULL == DLLInst )<br />{<br />DLLInst = LoadLibrary("HollyIVRCard.dll");<br />}<br />if(DLLInst)<br />{<br />  fun=(int (__cdecl*)(char*,char*,char*))GetProcAddress(DLLInst,"hTrade");<br />  if(fun)<br />  {<br />    cardid=Edit1-&gt;Text.c_str();<br />    num=Edit2-&gt;Text.c_str();<br />    ShowMessage(fun(cardid,num,res));<br />    //cout&lt;&lt;cardid&lt;&lt;endl;<br />    //cout&lt;&lt;num&lt;&lt;endl;<br />  }<br />} else{<br />   ShowMessage("load dll fail");<br />}<br />   ShowMessage(AnsiString(res));<br />}</p>
<img src ="http://www.blogjava.net/kevinfriend/aggbug/68088.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kevinfriend/" target="_blank">康文</a> 2006-09-06 18:13 <a href="http://www.blogjava.net/kevinfriend/archive/2006/09/06/68088.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++ 速成</title><link>http://www.blogjava.net/kevinfriend/archive/2006/08/25/65808.html</link><dc:creator>康文</dc:creator><author>康文</author><pubDate>Fri, 25 Aug 2006 07:28:00 GMT</pubDate><guid>http://www.blogjava.net/kevinfriend/archive/2006/08/25/65808.html</guid><wfw:comment>http://www.blogjava.net/kevinfriend/comments/65808.html</wfw:comment><comments>http://www.blogjava.net/kevinfriend/archive/2006/08/25/65808.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kevinfriend/comments/commentRss/65808.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kevinfriend/services/trackbacks/65808.html</trackback:ping><description><![CDATA[
		<p>    用了几年java 了，突然想学习c++ ,昨天用了一天的时间疯狂的学习了一天c++ 基础知识，发现感觉还不错，不过精验告诉我，学编程语言一定要实践，在这里指记录了一些学习中的点滴。<br /><br />  1 const 与volatile 的用法<br />  1 const<br />   #include&lt;conio.h&gt;<br />   #include&lt;iostream.h&gt;<br />   //行参数指向const 类型变量的指针<br />   void display_c(cons int * pi)<br />   {<br />     cout&lt;&lt;"display_c:"&lt;&lt;*pi&lt;&lt;endl;<br />   }<br />   //行参为普通类型变量的指针<br />   void display(int *pi)<br />   {<br />     cout&lt;&lt;"display_c:"&lt;&lt;*pi&lt;&lt;endl;<br />   }<br />   //const 类型变量<br />   const int i1=1;<br />   //error  i1=3;<br />   //const 类型变量的指针<br />   int i2=2;<br />   const int * pi2=&amp;i2;<br />   //error *pi2=3<br />   // const 指针<br />   int * const pi3=&amp;i3;<br />   *pi3=5;<br />   // error *pi3=&amp;i4  可以赋予不同的值，但是不可一指向不同的变量<br />   //指向const 内容的const 指针<br />   const int * cosnt pi5=&amp;i5;<br />2 sizeof 返回某个便赖宁嘎或数据类型的长度<br />3 引用<br /> 1 引用变量<br />  int i=1;<br />  int &amp; r_i=i;<br />5 名空间<br />  1 namespace<br />   namespace car<br />  {<br />    int model;<br />    int length;<br />    int width;<br />  }<br />  namespace plane<br />  {<br />    int model;<br />    namespace size<br />    {<br />      int lenth;<br />      int width;<br />    }<br />  }<br />  namespace car //添加名空间的成员<br />  {<br />    char * name;<br />  }<br />  namespqce c=car; //定义别名<br />  int Time //外不变量属于全局名空间<br />  <br />  car::length=3;<br />  plane::size::length=70;<br />  <br />  int Time=1996;<br />  ::Time=1997;</p>
		<p> 2 using <br />  void main()<br /> {<br />   using namespace car;<br />   length;<br />   using namespace phane;<br />   model;</p>
		<p> }<br />6 new 与delete 运算符<br /> double * pd;   // define pointer variable<br /> pd=new double; // allocate memory <br /> if(pd!=null)<br /> {<br />   ...<br />  delete pd;  // free memory<br /> }</p>
		<p> double1=null <br /> * pds=new double[100];<br /> if(pds)<br /> {<br />   ....<br />  delete[] pds;</p>
		<p> }</p>
		<p> 如果是使用new 进行内存空间分配没有成功，则返回空指针null<br /> 释放一个数组分配的内存是，常常采用带[]的形式<br />7 void 指针 它指向一个地址值，但是不说名数据类型，可以使用void 指针创建一个通用的函数，在使用<br /> 的时候将指针类型转化成相应的具体类型。<br /> void ShowHex(void *pv,int siae)<br />{<br /> ....<br /> ((unsigned char *)pv)[i]<br />}<br />void main()<br />{<br />  void *pv=null;<br />  unsigned char c1=0x12;<br />  pv=&amp;c1;<br />  ShowHex(pv,sizeof(c1));<br /> }<br />9 typeid 运算符 用来求得变量或队形爱女嘎的数据类型，返回一个type_info 类型的对象，通过该对象<br />  可以获取数据类型的名称</p>
		<p>  include&lt;typeinfo.h&gt;<br />  int i;<br />  const type_info &amp;t0=typeid(i);<br />  t0.name();<br />10 函数<br />   1 模板函数<br />  template&lt;class T&gt; T min(R &amp;x,Tv&amp;y)<br /> {<br />   return x&lt;y?x:y;<br /> }<br />  2 指向函数的指针<br />   int max(int x,int y)<br />  {<br />    ...<br />  }<br />  int min(int x,int y)<br />  {<br />     ...<br />  }<br />  int (* m_pfunction)(int,int);<br />  void main()<br />  {<br />    m_pfunction=max;<br />    (*m_pfunction)(2,3);<br />     m_pfunction=min;<br />    (*m_pfunction)(2,3);<br />  }<br />11 类与对象<br /> 1 构在函数和析构函数<br /> #include &lt;iostream.h&gt;<br />#include &lt;string.h&gt;<br />#include &lt;conio.h&gt;<br />class Book<br />{<br />private: <br />  char * pBookName;<br />public:<br />  int PageNum;<br />public:<br />  Book(char * pBN=NULL);<br />  ~ B o o k ( v o i d ) ;<br />  void SetBookName(char * pBN);<br />  int GetBookName(char * pBN,unsigned int MaxSize);<br />} ;</p>
		<p>Book:Book(char *PBN)<br />{<br />  cout&lt;&lt;"构造函数"&lt;&lt;endl;<br />  pBookName=null;<br />  if(oBN!=null)<br />  {<br />     pBookName=new char[strlen(pBN)+1];<br />     if(pBookName!=null)<br />         strcyp(pBookName,pBN);<br />  }<br />}</p>
		<p>Book::~Book()<br />{<br />  delete[] pBookName;<br />}</p>
		<p>void Book::SetBookName(char * pBN)<br />{<br />  if(pBookName！＝null)<br />    delete[] pBookName;<br />   pBookName=new char[strlen(pBN)+1];<br />     if(pBookName!=null)<br />         strcyp(pBookName,pBN);<br />}</p>
		<p>int Book::GetBookName(char * pBN,unsigned intmaxSize)<br />{<br /> if((pBookName!=null))&amp;&amp;(MaxSize&gt;strlen(pBookName))<br />{<br />  strcpy(pBN,pBookName);<br />  retrun strlen(strlen(pBookName));<br /> }<br />}</p>
		<p>// 使用<br />Book b1;<br />b1.SetBookName("test");<br />Book b2(test1);<br /> 2 对象的引用参数传递<br />  void Add(Book b)<br />  void AddRef(Book &amp; b);<br /> 3 静态成员变量 是一个公共变量<br /> 在初始化 的时候利用作用运算符:: 对私有类型的静态成员变量可以向公有类型的静态成变量一样赋值<br /> 但不能直接引用<br /> 3 const 类型成员函数与mutable<br /> class CDate<br /> {<br />   public:<br />    int year;<br />    mutable int month;<br />    CDate(int y=2000,int m=1)<br />    {<br />       year=y;<br />       month=m;<br />    };<br />    int BetMonth() const ;//read only<br />    void SetMonth(int m);// write only<br /> }<br />  void CDate::SetMonth(int m)<br /> {<br />   month=m;<br /> }<br /> void main()<br /> {<br />   CDate d1;<br />   <br /> }</p>
		<p> 在const 类型的成员函数定义中，不可一直接或简介的修改普通类型的成员变量<br /> 如果象修改const 类型对象的成员函数，可以使用关键字mutable 对该成员变量进行修改<br /> 5 对象的初始化与初始化行<br /> 将参数类表中的数值赋予成员变量时，不仅可以在构造函数的函数体中进行，以阿宽衣在初始化行中进行<br /> 在初始化处惊醒初始化的情况有：<br />  1 分层类的构在函数可以调用它的任何一个子对象的构造函数<br />  2 对常量const 类型的成员变量的初始化必须在初始化行上<br />  3 对于引用类型的成员变量的初始化必须在初始化行上<br />  <br /> class CPoint<br /> {<br />   public：<br />    int x,y;<br />    CPoint(int ax=0,int ay=0)<br />    {<br />      x=ax;<br />      y=ay;<br />    }<br /> };</p>
		<p> class CRect<br /> {<br />   private:<br />    CPoint low_right;<br />    CPoint up_left;<br />   public:<br />    int &amp; CenterX;<br />    const int CenterY;<br />    CRect(int x1,int y1,int x2,int y2,int &amp;x3,int y3)<br />     :up_left(x1,y1),low_right(x2,y2),CenterX(x3),CenterY(y3)<br />   {<br />     <br />   }<br /> };<br />void main()<br />{<br />  int cx=5;<br />  int cy=6;<br />  CRect r1(1,2,3,4,cx,cy);<br />}<br /> 6 拷贝构造函数<br />  拷贝构造函数与普通构造函数的差别在与棋参数类表，参数列表中有一个对象，该对象的数据类型是<br /> 本类的一个引用，而且一般情况下，该对象还应该是一个const 类型的对象。<br />  如果在类的定义是不是显示的定义一个拷贝函数，则编译器会自动的定义一个拷贝构造函数<br /> <br /> class CPoint<br /> {<br />   public:<br />     int x,y;<br />     CPoint(int m_x=0,ubt m_y=0);   // default constructor <br />     cPoint(const CPoint &amp;c);      //copy consrucotr<br /> };<br /> CPoint::CPoint(int m_x,int m_y)<br /> {<br />   <br /> }<br /> <br /> CPoint::CPoint(const CPoint &amp;c)<br /> {<br />   x=c.y;<br />   y=c.x;<br /> }</p>
		<p> void main()<br /> {<br />   CPoint c1(1,2);  //invoke default constructor<br />   CPoint c2-c1;    // invoke copy constructor<br /> }<br />7 template class<br /> template&lt;class t,int Size&gt;class Array // template class<br /> {<br />   private:<br />     T arr[Size];<br />     int CurSize;<br />   public:<br />    Array(T * date,int n)<br />    {<br />      CurSize=n&lt;Size?n;Size;<br />      for(int i=0;i&lt;CurSize;i++)<br />      {<br />        Arr[i]=data[i];  <br />      }<br />    }<br />}</p>
		<p>void main()<br />{<br />  int i1[10]={1,2,3,4,5,6,7,8,9};<br />  Array&lt;int,6&gt;array_i1(i1,i0);<br />}<br /><br />1 友员类和友员函数<br />  #include &lt;iostream.h&gt;<br />  #include&lt;string.h&gt;<br />  #include&lt;conio.h&gt;</p>
		<p>class SoftWareEngineer; //先对SoftwareEngineer 类进行显示说明一下</p>
		<p>class Computer // 定义Computer 类<br />{<br />   private:<br />     int Price;<br />   public:<br />     Computer(int p){Price=p};<br />    friend class SoftwareEngineer; //将友员类载这里声明<br />    frined void Judge(Computer &amp;c,SoftwareEngineer  &amp; s) //友员函数<br />    <br />}；</p>
		<p>class SoftwareEngineer<br />{<br />   int Salary;<br />   char Name[20];<br />  public:<br />    SoftwareEngineer(int s,char *n //构造函数)<br />  {<br />     Salary=s;<br />     strcpy(Name,n);<br />  }<br />int GetComputerPrice(Computer &amp;c){return c.Price} //访问Computer 的思友变量<br />friend void Judge(Computer &amp;c,SoftwareEngineer &amp; s) //友员函数<br />}；<br />//判断一个软件工程师是否可以用一个月的薪水买的器一台电脑<br />void Judge(Computer &amp;c,SoftwareEngineer &amp;s) //桥友员函数<br />{<br />   if(c.Price&gt;s.Salary)<br />     cout&lt;&lt;"软件工程师"&lt;&lt;s.Name&lt;&lt;"的一个月薪水买不起一台电脑"&lt;&lt;endl;<br />   else<br />     cout&lt;&lt;"软件工程师"&lt;&lt;s.Name&lt;&lt;"的一月薪水买的起一台电脑"&lt;&lt;endl; <br />} <br />void main()<br />{<br />  Computer c1(100);<br />SoftwareEngineer s1(120,"user1");<br />Judge(c1,s1);</p>
		<p>SiftwareEngineer s2(80,"user2")<br />Judge(c1,s2);<br />getch();<br />}<br />2运算符重载 <br />#include&lt;iostream.h&gt;<br />#include&lt;conio.h&gt;<br />#include&lt;iomanip.h&gt;</p>
		<p>class TValue{<br /> private:<br />     int value;<br /> public:<br />   TValue(int i){value=i}<br /> //成员函数重载运算符<br />  TValue operator!();<br />  TValue operator+(TValue &amp; rv);<br /> // 友员函数重载运算符<br />  friend ostream &amp; operator&lt;&lt;{ostream &amp; os,TValue &amp; rv};<br />}</p>
		<p>TValue Tvalue::operator!()<br />{<br />  return TValue(!value); <br />}</p>
		<p>TValue TValue:operator+(TValue&amp; rv)<br />{<br />  return TValue(value+rv.value);<br />}</p>
		<p>ostream &amp; operator&lt;&lt;(ostream &amp; os,TValue rv)<br />{<br />   os&lt;&lt;setw(5)&lt;&lt;rv.value;<br />  return os;<br />}</p>
		<p>void main()<br />{<br />   TValue v1(3),v2(5);<br />   cout&lt;&lt;<br />}<br />3 类的派生和继承<br />1class Box{<br />   public:<br />     int width,height;<br />     void SetWidth(int w){width=w};<br />     void SetWidth(int h){height=h;};<br /> };<br /> class TColorBox::public Box{<br />  public:<br />     int color;<br />     void SetColor(int c){color=c;};<br /> };</p>
		<p>void main(){<br />    TColorBox cb;<br />    cb.SetColor(255); //声明非继承类的对象<br />    cb.SetWidth(100);//声明继承类的对象<br />    cb.SetHeight(100); //声明继承类的对象<br />   <br />  }<br /> 2 不能被继承的成员<br />  构造函数，析构函数，用户定义的新操作符，用户定义的赋值操作，友员关系<br /> 3 构造函数，析构函数的调用顺序<br />class A{<br /> int a,b,c;<br /> public:<br />  A(int x,int y,int z)<br />{<br />   a=x;<br />   b=y;<br />  c=z;<br />}</p>
		<p>};<br />class B{<br />  int d;<br />  public :<br />   B(int xx):A(xx,xx+1,xx+2)(d=xx);//内联构造函数<br />   B(int x,int y,int z,int xx);//非内联构造函数<br />  B:B(int x,int y,int z,int xx):A(x,y,z)<br />{<br />   d=xx;<br />}<br />}<br />  <br />实例<br />class Currency<br />{<br />  poublic:<br />    double par_value;<br />    Currency(){per_value=0.0;}<br />    Currency(double d){per_value=d;}<br />    Currency(Currency &amp;rc){par_value=rc.par_value;}<br />   <br />   Currency &amp; operator={Currency &amp; c}<br />   {<br />      par_valuc=c.par_value;<br />      return * this;<br />   }<br />   Currency &amp; operator+(Currency &amp; c)<br />  {<br />     par_value+=c.par_value;<br />    return *this;<br />  }<br />}</p>
		<p>//人民币<br /> class RMB:public Currency<br /> {<br />   public:<br />   RMB():Currency(){};//调用派生类的构造函数前，需要调用器基类的工造函数<br />   RMB(double d):Currency(d){};<br />   RMB(Currency&amp; c)：Currency(c){};<br />   RMB(RMB&amp; rmb):Currency(rnb){};<br />   friend ostream&amp; operator&lt;&lt;{ostream&amp; os,RMB&amp; rnb};//派生类的附加功能<br /> }；<br /> ostream&amp; operator&lt;&lt;{ostream&amp; os, RMB&amp; rmb} //output 运算符不能是一个类的成员函数<br />{<br />  os&lt;&lt;"￥"&lt;&lt;setiosflags(ios::shwopoint|ios:fixed)&lt;&lt;setprecision(2)rmb.par_value;<br /> return os;<br />}</p>
		<p>void main()<br />{<br />   RMB r_income(5000);<br />   RMB r_balance;<br />   r_balance=r_income=r_expense;<br />  cout&lt;&lt;"income"&lt;&lt;r_income&lt;&lt;endl;<br />} <br />4 将iostream 运算符重载<br /> 1)ostream &amp; operator&lt;&lt; (ostream &amp;os,const Triangular &amp; ths)<br />{<br />   os&lt;&lt;"("&lt;&lt;rhs.beg_pos()&lt;&lt;","&lt;&lt;rhs.length()&lt;&lt;")";<br />  rhs.display(rhs.length(),rhs.beg_pos(),os);<br />}<br />ouutput 运算符不能够设计成member function<br /> 2)istream&amp; operator&gt;&gt;(istream &amp;is,Triangular &amp;rhs)<br /> {<br />    char ch1,ch2;<br />    int bp,len;<br />   //输入 ch1='(',bp=3,ch3=',' len=6<br />   is&gt;&gt;ch1&gt;&gt;bp&gt;&gt;ch2&gt;&gt;len;<br />   rhs.beg_pos(bp);<br />   rhs.length(len);<br />   rhs.next_reset();<br />   return is;<br /> }<br />Triangular tris;<br />cin&gt;&gt;tri2<br />4 虚基类<br />载继承关系中，同一基类被继承多次，不仅会引器歧异，而起可能浪费空间<br /> class A<br />{<br />   public:<br />     int value;<br />};<br />class B:public virtual A(); //虚基类 编译器只产生一个基类版本。如果不定义为virtual 编译器不知到调用那个value 了，当然<br />class C:public virtual A();//  也可以return B::value;<br />class D:public b.public c<br />{<br />  public <br />   int GetValue(){return value;}<br />};</p>
		<p>void main()<br />{<br />  D dd;<br />  dd.GetValue();<br />} </p>
		<p>5 多态形，和虚函数<br />class TFirst<br />{<br />   public virtual void Display();<br />};</p>
		<p>void TFirst::Display()<br />{<br />   cout&lt;&lt;"first "<br />}</p>
		<p>class TSecond:public TFirst<br />{<br />   public:<br />     virtual void Display();<br />};</p>
		<p>void TSecond::Display()<br />{<br />   cout&lt;&lt;"second"<br />}</p>
		<p>void Display(TRist * pFirst)<br />{<br />   pFisrt=&gt;Display();<br />}</p>
		<p>void main()<br />{<br />   TFirst * pFirst=new TFirst;<br />   TSecond * pSecond=new TSecond;<br />   pFirst-&gt;Display();<br />   pSecond-&gt;Display();<br />   Display(pFirst);<br />   Display(pSecond);<br />    delet pfirst ；<br />    delet pSecond;<br />   <br />    getch();<br />}</p>
		<p>c++ builder 中的集合的<br />  1 集合的概念基本<br />   Set&lt;type,minval,maxval&gt; <br />   Set&lt;char,'A','C'&gt; s1<br />   <br />   tydefef Set(char,1,255) UPPERCASet;<br />   UPPERCASESet s1;<br />   s1&lt;&lt;'A'&lt;&lt;'B'&lt;&lt;'C';<br />   sysset.h 直接包含载vcl.h 中<br />  2 集合的操作<br />   #include&lt;iostream&gt;<br />   #include&lt;system.hpp&gt;<br />   #include&lt;conio.h&gt;<br />   <br />   using namespace std;<br />  typedef Set&lt;char,'B','Z'&gt; UpperSet;<br />  typedef Set&lt;char,'a','z'&gt; LoverSet;<br />  typeder Set&lt;char,'a','j'&gt; HalfLowerSet;</p>
		<p>  void set_example()<br />  {<br />      LowerSet ae,ae2,ac,de;<br />      UpperSet AE,AE2,AC,DE;<br />     HalfLowerSet aj;<br />  }</p>
		<p>异常处理<br />1 c++ 的异常处理<br /> #include &lt;iostream.h&gt;<br /> #include &lt;conio.h&gt;</p>
		<p> class Crange<br /> {<br />    public:<br />      int index;<br />      CRange(int i){index=i;}<br />}<br />   class CString<br />   {<br />        char a[100];<br />        int len;<br />        public:<br />         CString(char * s)<br />        {<br />             Len=strlen(s);<br />             strcpy(a,s);<br />        }  <br />       char &amp; operator[](int i)<br />      {<br />           if(i&gt;0 &amp;&amp; i&lt;len) return a[i];<br />           else throw CRange(i);<br />      }<br />     <br />      void DisplayNoCatch(CString &amp; s)<br />     {<br />             int j;<br />             for(j=0lj&lt;20;j++)<br />               cout&lt;&lt;s[j]&lt;&lt;""endl;;<br />     }<br />    <br />     int DisplayHasCatch(CString &amp; s)<br />    {<br />        try{<br />               DisplayNoCatch(s);<br />        }catch(CRange r)<br />        {<br />                cerr&lt;&lt;r.index&lt;&lt;endl;<br />        }<br />         return 99;<br />    }<br /> }<br /> 2 多路捕捉<br />  #include&lt;iostream.h&gt;<br />  #include&lt;conio.h&gt;<br />  void MultiException()<br />  {<br />      int i;<br />      double d;<br />     <br />      int no;<br />      cout&lt;&lt;"(&gt;0 and &lt;5)";<br />      cin&gt;&gt;no;<br />      tyr<br />      {<br />         switch(no)<br />        {<br />            case 1;<br />               throw 123;<br />               break;<br />             case 2:<br />               throw i;<br />               break;<br />             case 3:<br />               throw d;<br />               break;<br />             case 4:<br />               throw "Error";<br />                break;<br />              default:<br />                cout&lt;&lt;"error";<br />        }<br />      }<br />      catch(int ie)<br />     {<br />          cout&lt;&lt;"int"&lt;&lt;endl;<br />     }<br />     catch(double de)<br />     {<br />         cout&lt;&lt;"double"&lt;&lt;endl;<br />     }<br />     catch(char* se)<br />    {<br />          cout&lt;&lt;"char"&lt;&lt;endl;<br />    }<br />  }</p>
		<p> 3 bcb中的异常类<br />   1)却省vcl 异常处理 无try catch 自动处理<br />     int i=4,j=0;k;<br />     k=i/k;<br />     //下一句永远不会执行<br />    ShowMessage(k);<br />   2) 引发vcl 异常类<br />    try{<br />    }<br />    catch(Exception &amp;e)<br />    {<br />          ShowMessage(e.,Message);<br />    }<br />   不能引发如int double 等简单类型的异常<br />   3) c++ 异常处理<br />    try<br />    {<br />         throw 2222;<br />    }<br />    catch(int ErrorCode)<br />   {<br />         ShowMessage(ErrorCode);<br />    }<br /> }<br /> 4 单一捕捉异常<br />   try{<br />      k=10/0;<br />   }catch(EDivByZero &amp; E)<br />   {<br />      MessageDlg()  <br />   }<br />  5 捕捉所有异常<br />   try<br />   {<br />   }catch(...) or(EExternalException &amp;E)</p>
<img src ="http://www.blogjava.net/kevinfriend/aggbug/65808.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kevinfriend/" target="_blank">康文</a> 2006-08-25 15:28 <a href="http://www.blogjava.net/kevinfriend/archive/2006/08/25/65808.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>c++泛型编程</title><link>http://www.blogjava.net/kevinfriend/archive/2006/08/21/64892.html</link><dc:creator>康文</dc:creator><author>康文</author><pubDate>Mon, 21 Aug 2006 13:40:00 GMT</pubDate><guid>http://www.blogjava.net/kevinfriend/archive/2006/08/21/64892.html</guid><wfw:comment>http://www.blogjava.net/kevinfriend/comments/64892.html</wfw:comment><comments>http://www.blogjava.net/kevinfriend/archive/2006/08/21/64892.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kevinfriend/comments/commentRss/64892.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kevinfriend/services/trackbacks/64892.html</trackback:ping><description><![CDATA[
		<p>1 Standard Template Library （STL）主要有两种组件构成，一时容器container 另一种组件是 操作<br /> 这些容器类的泛型算法<br /> 1 vector and list 是序列是容器<br /> 2 map 是一对keyvalue 组合 3 set 其中仅含有key 我们对它惊醒查询操作。主要是判断某之是否存在其中。<br />2 指针的算术运算 <br /> 当数组被传递给函数，仅有第一个元素的地址会被传递<br /> template &lt;typename elemType&gt;<br /> elemType * find(const elemType *array,int size,const elemType &amp;value)<br /> {<br />   if(!array||size&lt;1)<br />    return 0;<br />   for(int i=0;i&lt;size;i++)<br />   {<br />      if(array[i]==value)<br />       return &amp;array[i];<br />   }<br />   /*<br />    for(int i=0;i&lt;size;i++,array++)<br />   {<br />      if(*array==value)<br />       return array;<br />   }<br />   */<br />   return 0;<br /> }<br /> template &lt;typename elemType&gt;<br /> elemType * find(const elemType *first,const elemType *last,const elemType &amp;value)<br /> {<br />   if(!fist||!last)<br />    return 0;<br />   for(;first!=last;first++)<br />    if(*first==value)<br />    return first;<br />   return 0;<br /> } <br /> 由于vector 和array 相同，<br />array[2] equals *(array+2) 2 指两个elemType 单位<br /> 由于vector 和array 相同，都是以一块赖宁许内存存储所有元素，所以我们可以使用和array 一样的处理处理<br /> 方式<br /> vector&lt;string&gt; svec<br /> find(&amp;vec[]，&amp;svec[vec.size()].serch_value);<br />3 了解Iterator<br />  1 template&lt;typename elemType&gt;<br />  void display(const vector&lt;elemType&gt;&amp;vec,ostream &amp;os)<br />  {<br />     vector&lt;elemType&gt;::const_iterator iter=vec.begin();<br />     vector&lt;elemType&gt;::const_iterator end_it=vec.end();<br />     for(;iter !=end_it;++iter)<br />     {<br />       os&lt;&lt;"ite3r"&lt;&lt;endl;<br />     }<br />  }<br />   2 find <br />   template&lt;typename IteratorTypes,typename elemType&gt;<br />   IteratorType<br />   find(IteratorType first,IteratorType last,count elemType &amp;values)<br />   {<br />      for(;first!=last;++first)<br />       if(value==*first)<br />         return first;<br />   }<br />        return last;<br />   const int siz3=9;<br />   int ia[size]={1,2,3,4,5,6,7,8,9};<br />   vector&lt;int&gt; vec=(ia,ia+size);<br />   list&lt;int&gt; list=(ia,ia+size);<br />   <br />   int *pia=find(ia,ia+size,3);<br />   if(pia!=ia+size)<br />     //find...;</p>
		<p>   vector&lt;int&gt;::iterator it;<br />   it=find(vec.begin(),vec.end,1024);<br />   if(it!=vec.end())<br />    //find...</p>
		<p>   list&lt;int&gt;::iterator it;<br />   it=find(list.first().list.end,4);<br />4 所有容器的共同操作<br /> equality(==),assignment(=),empty(),size(),clear()<br /> begin() 返回一个iterator,只向容器的第一个元素<br /> end()   返回一个iterator,只向容器的最后 一个元素<br />5 使用序列容器<br /> 1 vector 和list 是两个最主要的序列式容器 vector 式一块连续的内存。取数据效率较高，但是存数据<br /> 但是如果在任意位置插入或删除数据，效率就比较低。（但是在最后一位添加和删除数据效率都比较高）<br /> 2 list 系以双向连接来存储内存，可以任意执行前进或后退操作，可以在任意位置安插或删除数据。<br /> 3 deque 以连续的内存存储元素，但是deque 在最前端元素的安插和删除操作更有效,末端相同<br /> 4 #include&lt;vector&gt;<br />   #include&lt;list&gt;<br />   #include&lt;deque&gt;<br /> 5 产生空的容器<br />   list&lt;string&gt; slist;<br />   vector&lt;int&gt; ivec;<br /> 6 产生特定大小的容器，每个元素都以千默认的值作为初值<br />   list&lt;int&gt; ilist(1024);<br />   vector&lt;string&gt; svec(32);<br /> 7产生特定大小的容器，并为每个软速制定初值<br />  vector&lt;int&gt; ivec(10,-1)<br />  list&lt;string&gt; slist(16,'unassigned');<br /> 8 int ia[9]={1,2,3,4,5,6,7,8,9};<br />   vector&lt;int&gt; fib(ia,ia+8);<br /> 9 根据某个容器产生新容器，复制软来容器的元素，作为新容器的初值<br />  list&lt;string&gt; slist;<br />  list&lt;string&gt; slist2&lt;slist&gt;<br /> 10 push_back(),pob_back() 在容器末尾进行安插和删除操作。在deque和list可以用push_front<br />  和pop_front 在最前面天加和删除操作。<br /> 11 fornt（） 和back()可以取回最前和最后的值 <br /> 12 iterator insert(iterator position,elemType value)将value 安插于position 之前，返回一个iterator<br /> 指向被安插的元素<br />  list&lt;int&gt;ilist;<br />  list&lt;int&gt;:: it=ilist.begin();<br />  while(it!=ilist.end())<br />   if(*it&gt;=ival)<br />   {<br />      ilist.inert(it,ival);<br />      break;<br />   }<br />  if(it==ilist.end())<br />    ilist.pushi_back(ival); </p>
<img src ="http://www.blogjava.net/kevinfriend/aggbug/64892.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kevinfriend/" target="_blank">康文</a> 2006-08-21 21:40 <a href="http://www.blogjava.net/kevinfriend/archive/2006/08/21/64892.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>esential c++ 学习笔记1－－c＋＋ 编程基础.txt</title><link>http://www.blogjava.net/kevinfriend/archive/2006/08/14/63395.html</link><dc:creator>康文</dc:creator><author>康文</author><pubDate>Mon, 14 Aug 2006 01:27:00 GMT</pubDate><guid>http://www.blogjava.net/kevinfriend/archive/2006/08/14/63395.html</guid><wfw:comment>http://www.blogjava.net/kevinfriend/comments/63395.html</wfw:comment><comments>http://www.blogjava.net/kevinfriend/archive/2006/08/14/63395.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/kevinfriend/comments/commentRss/63395.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/kevinfriend/services/trackbacks/63395.html</trackback:ping><description><![CDATA[
		<p>1 class 的定义，一般来说分为两部分，其中一个是所谓的头文件，用来声明class 所提供的各种操作行为<br />  另一个是文件，程序代码文件，用来包含这些行为的实现内容。预使用class 不许在程序中含入其头文件<br />2 using namespace std<br />3 template class 机制使程序员直到使用template class 时才决定真正的数据类别。先使用一个代名，<br />  稍后才绑定至实际的数据类别<br />4 Arrays 要定义array 我们必须指定array 的元素类型，名称，并指定其尺度的大小<br />  array 的尺度必须是个常量表达式<br />  const int seq_size=18;<br />  int pell_seql seq_size=1;<br />5 vector 必须首先含如vector 的头文件。在角括号中指定其元素类型，其尺度则写作小括号内，不一定<br />  是常量表达式<br />  #include&lt;vector&gt;<br />  vector&lt;int&gt; pell_seq(seq_size); <br />6 初始化数组和vector<br />  1 初始化数组<br />   int elem_seq[seq_size]={1,2,3,4} ;<br />   int elem_swq[]={1,2,3,4};由编译其根据初始值自动算出array 的值<br />  2 初始化vector <br />   1) vector&lt;int&gt; elem_seq(seq_size);<br />      elem_seq[0]=1;<br />      elem_seq[1]=2;<br />      .....<br />      elem_seq[[17]==22;<br />    2) 利用一个以初始化的array<br />     int elem_val[seq_size]={1,2,3,4}<br />     vector&lt;int&gt;elem_seq(elem_val,elem_val+seq_size); 其中elem_val 为内存地址<br />7 array 和 vector 的使用<br />   vector 知道自己的大小，而array 不知道<br />   for(int i=0;i&lt;elem_seq.size();i++){<br />      cout&lt;&lt;elem_seq[[i]&lt;&lt;'';</p>
		<p>    }<br />8指针 指针为程序引入了一层间接性，我们可以操作指针（代表某特定内存地址），而不再直接操控对象。<br /> 指针主要形成两件事，可以增加程序本身的弹性，但同时也增加了直接操控对象时所没有的复杂度<br /> 1 int ival=1024<br />   int *p=&amp;ival; 其中*p 指int型对象的地址<br /> 2 指针所具有的双重性，既可以让我们操控指针内含的内存地址，也可以让我们操作指针所指定的对象值<br />   pi  指定pi所含有的内存地址<br />  *pi  核定ival的值<br /> 3 指针的提领（dereference）<br />  如果pi 寻址到某个对象，则执行提领操作，如果pi 不指定任何对象，提领会导致未知的执行结果<br />  一个为只想任何对象的指针，其内含地址为0，我们称为null，任何指针都可以被初始话，或是令值为0<br />  if(pi&amp;&amp;...)<br />  只有pi含一个非0值时，其结果为true<br />  vector&lt;int&gt; *pv=0; <br />  const int seq_cnt=6;<br />  vector&lt;int&gt; *seq_addres[seq_cnt]={<br />    &amp;fibonacci,&amp;lucas,&amp;pell...<br />  };<br />  一个指针数组，容量为seq_cnt，每个指针都指向vector&lt;int&gt;   <br />  4 #include&lt;cstdlib&gt;<br />  rand(seed) 返回一个介于0和seed 之间的随机数<br />  5 对象指针<br />  if(!fibonacci.empty()&amp;&amp;....){<br />     pv.empty()..<br />   }<br />9 文件写<br />  对文件的读写，首先的含入fstream<br />  #include&lt;fstream&gt;<br />  1 ofstream outfile("seq_data.txt"); 如果文件不存在，产生一个文件，如果文件已经存在，这个文件<br />  被开启作为输出只用，但是源文件中的数据会输调<br />  2 ofstream outfile("seq_data.txt",ios_base::app) 追加模式<br />  3 oufile 为false 表示文件未开启成功<br />10 文件读<br />  ifstream 将文件名传人，如果文件未能开启成功，ifstream 对象被核定为false ，如果成功，为true<br />  ifstream infile("seq_data.txt");<br />  int num_tries=0;<br />  int num_cor=0;<br />  if(!infile){<br />    //由于某种原因，文件无法开启<br />  }<br />  else<br />  {<br />   string name;<br />   int nt;<br />   int nc;<br />   while(infile&gt;&gt;name)<br />   { <br />     // 一旦读到到文件尾，infile 尾false <br />     // infile&gt;&gt;name&gt;&gt;nt&gt;&gt;nc  ,把文件 anna 24 19 分别读到name，nt，nc 中<br />     infile&gt;&gt;nt&gt;&gt;nc;<br />     if(name==usr_name)<br />     {<br />       //find hime<br />       count&lt;&lt;"Welcome back,"&lt;&lt;usr_name<br />            &lt;&lt;"\nYour current score is" &lt;&lt;nc<br />            &lt;&lt;" out of " &lt;&lt;nt&lt;&lt;"\nGood Luck"!\n";<br />            num_tries=nt;<br />            num_cor=nc;    <br />     }<br />   }<br />11 同时读写同一个文件<br />  fstream iofile("seq_data.txt",ios_base::in|ios_base::app);<br />  if(!iofile)<br />   ...<br />  else<br />  {<br />   iofile.seekg(0); 将文件重新定位的文件的最末端 <br />  }<br />12<br />  #include&lt;iostream&gt;<br />#include&lt;string&gt;<br />using namespace std;<br />//---------------------------------------------------------------------------</p>
		<p>#pragma argsused<br />int main()<br />{<br />   string username;<br />   cout&lt;&lt;"Please enter your name:";<br />   cin&gt;&gt;username;<br />   switch(username.size()){<br />     case 0:<br />       cout&lt;&lt;"with no name";<br />       break;<br />     case 1:<br />       cout&lt;&lt;"with one character";<br />       break;<br />     default:<br />       cout&lt;&lt;"hollo ,"&lt;&lt;username&lt;&lt;endl;<br />       break;<br />   }<br />        return 0;<br />}<br />13<br />#include&lt;iostream&gt;<br />#include&lt;vector&gt;<br />#include&lt;string&gt;<br />using namespace std;<br />//---------------------------------------------------------------------------<br />int main()<br />{<br />  vector&lt;int&gt; ivec;<br />  string str;<br />  int val;<br />  while(cin&gt;&gt;val){<br />       ivec.push_back(val);<br />  }<br />  int sum=0;<br />  for(int i=0;i&lt;ivec.size();i++){<br />      sum+=ivec[i];<br />  }<br />  int average=sum/ivec.size();<br />  cout&lt;&lt;"sum of "&lt;&lt;ivec.size()<br />      &lt;&lt;"elements "&lt;&lt;sum<br />      &lt;&lt;"average "&lt;&lt;average&lt;&lt;endl;<br />        return 0;<br />}<br />14<br />//---------------------------------------------------------------------------</p>
		<p>#include&lt;iostream&gt;<br />#include&lt;vector&gt;<br />#include&lt;string&gt;<br />using namespace std;<br />//---------------------------------------------------------------------------<br />int main()<br />{<br /> const int array_size=120;<br /> int la[array_size];<br /> int ival,icnt=0;</p>
		<p> while(cin&gt;&gt;ival&amp;&amp;icnt&lt;array_size){<br />   la[icnt++]=ival;<br /> }<br /> int sum=0;<br /> for(int i=0;i&lt;array_size;i++){<br />   sum+=la[i];<br /> }<br /> int average=sum/array_size;<br /> cout&lt;&lt;array_size<br />     &lt;&lt;"\n"&lt;&lt;sum<br />     &lt;&lt;"\n"&lt;&lt;average&lt;&lt;endl;<br />}<br />//---------------------------------------------------------------------------</p>
		<p>
				<br />//---------------------------------------------------------------------------</p>
		<p>#include&lt;iostream&gt;<br />#include&lt;vector&gt;<br />#include&lt;string&gt;<br />#include&lt;fstream&gt;<br />#include&lt;algorithm&gt;<br />using namespace std;<br />//---------------------------------------------------------------------------<br />int main()<br />{<br />  ifstream in_file ("D:\inputfile.txt");<br />  ofstream out_file("D:\outputfile.txt");<br />  if(!in_file){<br />     cerr&lt;&lt;"unable to open the inputfile" ;<br />  }<br />  if(! out_file){<br />     cerr&lt;&lt;"unable to open the outputfile" ;<br />  }<br />  string word;<br />  vector&lt;string&gt; text;<br />  while(in_file&gt;&gt;word)<br />  {<br />     text.push_back(word);<br />  }<br />  cout&lt;&lt;"unsort file";<br />  for(int i=0;i&lt;text.size();++i)<br />  {<br />  cout&lt;&lt;text[i]&lt;&lt;" "&lt;&lt;endl;<br />  }<br />  cout&lt;&lt;"sort file";<br />  sort(text.begin(),text.end());// sort the vector<br />  for(int i=0;i&lt;text.size();++i)<br />  {<br />  out_file&lt;&lt;text[i]&lt;&lt;" "&lt;&lt;endl;<br />  }<br />}</p>
		<p> </p>
<img src ="http://www.blogjava.net/kevinfriend/aggbug/63395.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/kevinfriend/" target="_blank">康文</a> 2006-08-14 09:27 <a href="http://www.blogjava.net/kevinfriend/archive/2006/08/14/63395.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>