2007年10月9日
#
1 #coding=utf-8 2 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-8 2 #!/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 的代碼格式化
|