| 
	2007年10月9日		  
	#
 
			
			
			
				 1 #coding=utf-82
 3 # locks.py zhangsk
 4 import threading, time
 5
 6 b = 50
 7 l = threading.Lock()
 8
 9 def threadcode():
 10     """This is run in the created threads"""
 11     global b
 12     print "Thread %s invoked" % threading.currentThread().getName()
 13     l.acquire()
 14     try:
 15         print "Thread %s running" % threading.currentThread().getName()
 16         time.sleep(1)
 17         b = b + 50
 18         print "Thread %s set b to %d" % (threading.currentThread().getName(), b)
 19     finally:
 20         l.release()
 21
 22 print "Value of b at start of program:", b
 23 childthreads = []
 24
 25 for i in range(1, 5):
 26     t = threading.Thread(target = threadcode, name = "Thread-%d" % i)
 27     t.setDaemon(1)
 28     t.start()
 29     childthreads.append(t)
 30
 31 for t in childthreads:
 32     t.join()
 33
 34 print "New Value of b:", b
 
 
	2007年9月29日		  
	#
 
			
			
			
				老勾的MSN上写了这样一句话,让我思考了很久!“程序员就像男人,编程语言就像女人,一般男人都想要很多女人,可没几个男人能真正了解一个女人”
 
   
 
			
			
			
				 1 #coding=utf-82 #!/user/bin/env python
 3 # connect.py 17:35 2007-9-28 zhangsk
 4
 5 import socket
 6
 7 print "Creating socket
  ", #加逗号,相当于c的print,不加逗号相当于c的println 8 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 9 print "done."
 10
 11 print "Connecting to remote host
  ", 12 s.connect(("www.google.com", 80))
 13 print "done."
 看到支持插入python的代码,特发个尝试一下。
			 
 
			
			
			
				请保留完整信息Delphi7远程调试
 张树坤 2007-09-29
 http://www.zhangsk.cn/
 http://www.sunmba.cn/
 
 上次写的delphi远程调试,有些步骤不是必须的。今整理如下,希望对大家有所帮助。
 
 自己的开发机器称为主机,运行程序的机器称为目标机;
 一、在主机编译执行程序
 1、project->options->linker中的EXE and DLL options选项组中的include remote debug symbols打上勾,
 这样就可以生成rsm为扩展名的文件,该文件名称于你的项目同名。
 2、project->options->Compiler->Debugging中的勾可以全部选上,这是在你的程序支持debug(正式发布产品时要去掉这些选项,Delphi默认设置是选则大部分的)
 3、Tools->Environment Options->Preferences的Compling and running选择组中选上Show compiler progress(可选项,在编译或者运行时显示编译过程,建议使用)
 二、拷贝Project1.exe和Project1.rsm到目标机器的运行目录(该目录可以是你的安装目录,也可以任意)
 注意:主机的代码不需和目标机的exe和rsm文件一致,就是说在进行第一步后不能改动你的代码
 三、目标机安装borland的远程调试工具rdebug,delphi7的光盘中就有,或者google一下。
 四、启动目标机的rdebug,启动后目标机的托盘图标中会出现一个小“虫子”debug的图标
 注意:远程调试工具不需正常运行才能进行远程调试
 五、主机的Delphi的远程调试设置
 1、Delphi中选择Run->Parameters->Remote
 2、Remote Path中输入目标机器的运行目录
 3、Remote Host中输入目标机IP
 4、选择Debug project on remote machine
 5、选择ok
 六、主机按F9调试即可
 
 
 
	2007年9月14日		  
	#
 
			
			
			
				在c++中switch(choice)
 {
 case 1:
 case 2:
 case 3:
 default:
 }
 如果这样的执行代码就会把所有的case走到(java中也是如此),所以不要忘记在case中加入break;
 switch(choice)
 {
 case 1: A
 break;
 case 2: B
 break;
 case 3: C
 break;
 default:       break;
 
 
 
 delphi中就不用了。
 
case I of   1..5: Caption := 'Low';   6..9: Caption := 'High';   0, 10..99: Caption := 'Out of range'; else   Caption := ''; end;
			 
 
			
			
			
				转载,虽然这篇不是自己写的,但是觉得不错,应该让大家看看。浅谈Object Pascal的指针
 Nicrosoft(nicrosoft@sunistudio.com) -- 2001.8.26
 http://www.sunistudio.com/nicrosoft/
 东日文档:http://www.sunistudio.com/asp/sunidoc.asp
     大家都认为,C语言之所以强大,以及其自由性,很大部分体现在其灵活的指针运用上。因此,说指针是C语言的灵魂,一点都不为过。同时,这种说法也让很多人产生误解,似乎只有C语言的指针才能算指针。Basic不支持指针,在此不论。其实,Pascal语言本身也是支持指针的。从最初的Pascal发展至今的 Object Pascal,可以说在指针运用上,丝毫不会逊色于C语言的指针。    以下内容分为八个部分,分别是    一、类型指针的定义 二、无类型指针的定义
 三、指针的解除引用
 四、取地址(指针赋值)
 五、指针运算
 六、动态内存分配
 七、字符数组的运算
 八、函数指针
   一、类型指针的定义。对于指向特定类型的指针,在C中是这样定义的: int *ptr;
 char *ptr;
 与之等价的Object Pascal是如何定义的呢?
 var ptr : ^Integer;
 ptr : ^char;
 其实也就是符号的差别而已。
   二、无类型指针的定义。C中有void *类型,也就是可以指向任何类型数据的指针。Object Pascal为其定义了一个专门的类型:Pointer。于是, ptr : Pointer;
 就与C中的
 void *ptr;
 等价了。
   三、指针的解除引用。要解除指针引用(即取出指针所指区域的值),C 的语法是 (*ptr),Object Pascal则是 ptr^。    四、取地址(指针赋值)。取某对象的地址并将其赋值给指针变量,C 的语法是 ptr = &Object;
 Object Pascal 则是
 ptr := @Object;
 也只是符号的差别而已。
   五、指针运算。在 C 中,可以对指针进行移动的运算,如: char a[20];
 char *ptr=a;
 ptr++;
 ptr+=2;
 当执行ptr++;时,编译器会产生让ptr前进sizeof(char)步长的代码,之后,ptr将指向a[1]。ptr+=2;这句使得ptr前进两个sizeof(char)大小的步长。同样,我们来看一下Object Pascal中如何实现:
 var a : array [1..20] of Char;
 ptr : PChar; //PChar 可以看作 ^Char
 begin
 ptr := @a;
 Inc(ptr); // 这句等价于 C 的 ptr++;
 Inc(ptr, 2); //这句等价于 C 的 ptr+=2;
 end;
   六、动态内存分配。C语言中,使用malloc()库函数分配内存,free()函数释放内存。如这样的代码: int *ptr, *ptr2;
 int i;
 ptr = (int*) malloc(sizeof(int) * 20);
 ptr2 = ptr;
 for (i=0; i<20; i++){
 *ptr = i; ptr++;
 }
 free(ptr2);
 Object Pascal中,动态分配内存的函数是GetMem(),与之对应的释放函数为FreeMem()(传统 Pascal中获取内存的函数是New()和 Dispose(),但New()只能获得对象的单个实体的内存大小,无法取得连续的存放多个对象的内存块)。因此,与上面那段C的代码等价的Object Pascal的代码为:
 var ptr, ptr2 : ^integer;
 i : integer;
 begin
 GetMem(ptr, sizeof(integer) * 20);
 //这句等价于C的 ptr = (int*) malloc(sizeof(int) * 20);
 ptr2 := ptr; //保留原始指针位置
 for i := 0 to 19 do
 begin
 ptr^ := i;
 Inc(ptr);
 end;
 FreeMem(ptr2);
 end;
 对于以上这个例子(无论是C版本的,还是Object Pascal版本的),都要注意一个问题,就是分配内存的单位是字节(BYTE),因此在使用GetMem时,其第二个参数如果想当然的写成 20,那么就会出问题了(内存访问越界)。因为GetMem(ptr, 20);实际只分配了20个字节的内存空间,而一个整形的大小是四个字节,那么访问第五个之后的所有元素都是非法的了(对于malloc()的参数同样)。
   七、字符数组的运算。C语言中,是没有字符串类型的,因此,字符串都是用字符数组来实现,于是也有一套str打头的库函数以进行字符数组的运算,如以下代码: char str[15];
 char *pstr;
 strcpy(str, "teststr");
 strcat(str, "_testok");
 pstr = (char*) malloc(sizeof(char) * 15);
 strcpy(pstr, str);
 printf(pstr);
 free(pstr);
 而在Object Pascal中,有了String类型,因此可以很方便的对字符串进行各种运算。但是,有时我们的Pascal代码需要与C的代码交互(比如:用Object Pascal的代码调用C写的DLL或者用Object Pascal 写的DLL准备允许用C写客户端的代码)的话,就不能使用String类型了,而必须使用两种语言通用的字符数组。其实,Object Pascal提供了完全类似C的一整套字符数组的运算函数,以上那段代码的Object Pascal 版本是这样的:
 var str : array [1..15] of char;
 pstr : PChar; //Pchar 也就是 ^Char
 begin
 StrCopy(@str, 'teststr');
 //在C中,数组的名称可以直接作为数组首地址指针来用
 //但Pascal不是这样的,因此 str前要加上取地址的运算符
 StrCat(@str, '_testok');
 GetMem(pstr, sizeof(char) * 15);
 StrCopy(pstr, @str);
 Write(pstr);
 FreeMem(pstr);
 end;
   八、函数指针。在动态调用DLL中的函数时,就会用到函数指针。假设用C写的一段代码如下: typedef int (*PVFN)(int); //定义函数指针类型
 int main()
 {
 HMODULE hModule = LoadLibrary("test.dll");
 PVFN pvfn = NULL;
 pvfn = (PVFN) GetProcAddress(hModule, "Function1");
 pvfn(2);
 FreeLibrary(hModule);
 }
 就我个人感觉来说,C语言中定义函数指针类型的typedef代码的语法有些晦涩,而同样的代码在 Object Pascal中却非常易懂:
 type PVFN = Function (para : Integer) : Integer;
 var fn : PVFN;
 //也可以直接在此处定义,如:fn : function (para:Integer):Integer;
 hm : HMODULE;
 begin
 hm := LoadLibrary('test.dll');
 fn := GetProcAddress(hm, 'Function1');
 fn(2);
 FreeLibrary(hm);
 end;
 
 
	2007年9月13日		  
	#
 
			
			
			
				被Delphi惯坏了,发现写一个原生的Form这么麻烦 vc版本
 #include <windows.h>
 LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
 
 int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
 PSTR szCmdLine, int iCmdShow)
 {
 static TCHAR szAppName[] = TEXT ("HelloWin") ;
 HWND         hwnd ;
 MSG          msg ;
 WNDCLASS     wndclass ;
 
 wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
 wndclass.lpfnWndProc   = WndProc ;
 wndclass.cbClsExtra    = 0 ;
 wndclass.cbWndExtra    = 0 ;
 wndclass.hInstance     = hInstance ;
 wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
 wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
 wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
 wndclass.lpszMenuName  = NULL ;
 wndclass.lpszClassName = szAppName ;
 
 if (!RegisterClass (&wndclass))
 {
 MessageBox (NULL, TEXT ("This program requires Windows NT!"),
 szAppName, MB_ICONERROR) ;
 return 0 ;
 }
 
 hwnd = CreateWindow (szAppName,                  // window class name
 TEXT ("The Hello Program"), // window caption
 WS_OVERLAPPEDWINDOW,        // window style
 CW_USEDEFAULT,              // initial x position
 CW_USEDEFAULT,              // initial y position
 CW_USEDEFAULT,              // initial x size
 CW_USEDEFAULT,              // initial y size
 NULL,                       // parent window handle
 NULL,                       // window menu handle
 hInstance,                  // program instance handle
 NULL) ;                     // creation parameters
 
 ShowWindow (hwnd, iCmdShow) ;
 UpdateWindow (hwnd) ;
 
 while (GetMessage (&msg, NULL, 0, 0))
 {
 TranslateMessage (&msg) ;
 DispatchMessage (&msg) ;
 }
 return msg.wParam ;
 }
 
 LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
 HDC         hdc ;
 PAINTSTRUCT ps ;
 RECT        rect ;
 
 switch (message)
 {
 case WM_CREATE:
 PlaySound (TEXT ("hellowin.wav"), NULL, SND_FILENAME | SND_ASYNC) ;
 return 0 ;
 
 case WM_PAINT:
 hdc = BeginPaint (hwnd, &ps) ;
 
 GetClientRect (hwnd, &rect) ;
 
 DrawText (hdc, TEXT ("Hello, Windows 98! By ZhangSK"), -1, &rect,
 DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
 
 EndPaint (hwnd, &ps) ;
 return 0 ;
 
 case WM_DESTROY:
 PostQuitMessage (0) ;
 return 0 ;
 }
 return DefWindowProc (hwnd, message, wParam, lParam) ;
 }
 Delphi版本
 program HelloWin;
 uses
 Windows,
 Messages,
 MMSystem,
 SysUtils;
 
 Const
 AppName:String = 'HelloWin';
 Null:Integer = 0;
 
 function WndProc(WindowHwnd:HWND;TheMessage:UINT;WPARAMS:wParam;LPARAMS:lParam):Integer;stdcall;
 var
 ClientDC:HDC;
 ps:TPaintStruct;
 ClientRect:TRect;
 sUser, sPower: string;
 begin
 case TheMessage of
 WM_CREATE: begin
 PlaySound('hellowin.wav',null,SND_FILENAME or SND_ASYNC);
 Result:=0;
 end;
 WM_PAINT: begin
 ClientDc:=BeginPaint(WindowHwnd,ps);
 GetClientRect(WindowHwnd,ClientRect);
 DrawText(ClientDc,PChar('Hello,Window98!'),-1,ClientRect,DT_SINGLELINE or
 DT_CENTER OR DT_VCENTER);
 sUser := 'ZhangSK''Testing
  '; sPower := 'POWERD BY DELPHI';
 TextOut(ClientDC, 5, 5, PChar(sUser), Length(sUser));
 TextOut(ClientDC, ClientRect.Right-200, ClientRect.Bottom-30, PChar(sPower), Length(sPower));
 Endpaint(Windowhwnd,ps);
 Result:=0;
 end;
 WM_DESTROY: begin
 PostQuitMessage(0);
 Result:=0;
 end;
 else
 Result:=DefWindowProc(WindowHwnd,TheMessage,WPARAMS,LPARAMS);
 end;
 end;
 
 
 
 var
 WinHwnd:HWND;
 WinMsg:MSG;
 WinClass:WNDCLASS;
 ECode:DWORD;
 EString:PChar;
 begin
 WinClass.style:=CS_HREDRAW OR CS_VREDRAW;
 WinClass.lpfnWndProc:=@WndProc;
 WinClass.cbClsExtra:=0;
 WinClass.cbWndExtra:=0;
 WinClass.hInstance:=hInstance;
 WinClass.hIcon:=LoadIcon(NULL,IDI_APPLICATION);
 WinClass.hCursor:=LoadCursor(Null,IDC_ARROW);
 WinClass.hbrBackground:=HBRUSH(GetStockObject(WHITE_BRUSH));
 WinClass.lpszMenuName:=nil;
 WinClass.lpszClassName:=PChar(AppName);
 
 if (RegisterClass(WinClass)=0) then
 begin
 MessageBox(null,'This application need WINDOWS platform','message',MB_ICONERROR);
 exit;
 end;
 
 WinHwnd:=CreateWindow(PChar(AppName),'First SDK Application',WS_OVERLAPPEDWINDOW,
 CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
 0,0,hInstance,nil);
 if Iswindow(WinHwnd)then
 begin
 ShowWindow(WinHwnd,SW_SHOWNORMAL);
 updateWindow(WinHwnd);
 end
 else begin
 ECode:=GetLastError;
 EString:=PChar(Inttostr(LoWord(ECode)));
 Messagebox(null,EString,'Error',MB_ICONERROR);
 exit;
 end;
 
 while(Getmessage(WinMsg,null,0,0))do
 begin
 TranslateMessage(WinMsg);
 DispatchMessage(WinMsg);
 end;
 
 UnregisterClass(PChar(AppName),hInstance);
 end.
 
 
 
 
	2007年9月11日		  
	#
 
			
			
			
				
    寻找的动力:看到公司产品中应用程序共享的功能很不错,在想想两年前为了做部队的桌面共享软件而实现的东西,简直不能比较,终于找到这个开源项目http://www.realvnc.com/使用感觉:目前是我见到最快的桌面共享软件编译源码:直接在VC6下就能编译,(记得不能Build All,只要在FileView选项卡下分别Build vncviewer files和winvnc files就可以)。学习计划:从今天2007-09-11开始学习、分析VNC的源代码。
 
 
	2007年9月4日		  
	#
 
			
			
			
				hi c++!
 不会吧,这个c++blog的插入代码竟然没有c++格式的 晕~~
 建议加入
 C++
 Delphi
 Python
 的代码格式化
 
 
 
 |