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上寫(xiě)了這樣一句話(huà),讓我思考了很久! “程序員就像男人,編程語(yǔ)言就像女人,一般男人都想要很多女人,可沒(méi)幾個(gè)男人能真正了解一個(gè)女人”
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 ", #加逗號(hào),相當(dāng)于c的print,不加逗號(hào)相當(dāng)于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的代碼,特發(fā)個(gè)嘗試一下。
請(qǐng)保留完整信息 Delphi7遠(yuǎn)程調(diào)試 張樹(shù)坤 2007-09-29 http://www.zhangsk.cn/ http://www.sunmba.cn/
上次寫(xiě)的delphi遠(yuǎn)程調(diào)試,有些步驟不是必須的。今整理如下,希望對(duì)大家有所幫助。
自己的開(kāi)發(fā)機(jī)器稱(chēng)為主機(jī),運(yùn)行程序的機(jī)器稱(chēng)為目標(biāo)機(jī); 一、在主機(jī)編譯執(zhí)行程序 1、project->options->linker中的EXE and DLL options選項(xiàng)組中的include remote debug symbols打上勾, 這樣就可以生成rsm為擴(kuò)展名的文件,該文件名稱(chēng)于你的項(xiàng)目同名。 2、project->options->Compiler->Debugging中的勾可以全部選上,這是在你的程序支持debug(正式發(fā)布產(chǎn)品時(shí)要去掉這些選項(xiàng),Delphi默認(rèn)設(shè)置是選則大部分的) 3、Tools->Environment Options->Preferences的Compling and running選擇組中選上Show compiler progress(可選項(xiàng),在編譯或者運(yùn)行時(shí)顯示編譯過(guò)程,建議使用) 二、拷貝Project1.exe和Project1.rsm到目標(biāo)機(jī)器的運(yùn)行目錄(該目錄可以是你的安裝目錄,也可以任意) 注意:主機(jī)的代碼不需和目標(biāo)機(jī)的exe和rsm文件一致,就是說(shuō)在進(jìn)行第一步后不能改動(dòng)你的代碼 三、目標(biāo)機(jī)安裝borland的遠(yuǎn)程調(diào)試工具rdebug,delphi7的光盤(pán)中就有,或者google一下。 四、啟動(dòng)目標(biāo)機(jī)的rdebug,啟動(dòng)后目標(biāo)機(jī)的托盤(pán)圖標(biāo)中會(huì)出現(xiàn)一個(gè)小“蟲(chóng)子”debug的圖標(biāo) 注意:遠(yuǎn)程調(diào)試工具不需正常運(yùn)行才能進(jìn)行遠(yuǎn)程調(diào)試 五、主機(jī)的Delphi的遠(yuǎn)程調(diào)試設(shè)置 1、Delphi中選擇Run->Parameters->Remote 2、Remote Path中輸入目標(biāo)機(jī)器的運(yùn)行目錄 3、Remote Host中輸入目標(biāo)機(jī)IP 4、選擇Debug project on remote machine 5、選擇ok 六、主機(jī)按F9調(diào)試即可
2007年9月14日
#
在c++中 switch(choice) { case 1: case 2: case 3: default: } 如果這樣的執(zhí)行代碼就會(huì)把所有的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;
轉(zhuǎn)載,雖然這篇不是自己寫(xiě)的,但是覺(jué)得不錯(cuò),應(yīng)該讓大家看看。 淺談Object Pascal的指針
Nicrosoft(nicrosoft@sunistudio.com) -- 2001.8.26 http://www.sunistudio.com/nicrosoft/ 東日文檔:http://www.sunistudio.com/asp/sunidoc.asp
大家都認(rèn)為,C語(yǔ)言之所以強(qiáng)大,以及其自由性,很大部分體現(xiàn)在其靈活的指針運(yùn)用上。因此,說(shuō)指針是C語(yǔ)言的靈魂,一點(diǎn)都不為過(guò)。同時(shí),這種說(shuō)法也讓很多人產(chǎn)生誤解,似乎只有C語(yǔ)言的指針才能算指針。Basic不支持指針,在此不論。其實(shí),Pascal語(yǔ)言本身也是支持指針的。從最初的Pascal發(fā)展至今的 Object Pascal,可以說(shuō)在指針運(yùn)用上,絲毫不會(huì)遜色于C語(yǔ)言的指針。
以下內(nèi)容分為八個(gè)部分,分別是
一、類(lèi)型指針的定義 二、無(wú)類(lèi)型指針的定義 三、指針的解除引用 四、取地址(指針賦值) 五、指針運(yùn)算 六、動(dòng)態(tài)內(nèi)存分配 七、字符數(shù)組的運(yùn)算 八、函數(shù)指針
一、類(lèi)型指針的定義。對(duì)于指向特定類(lèi)型的指針,在C中是這樣定義的: int *ptr; char *ptr; 與之等價(jià)的Object Pascal是如何定義的呢? var ptr : ^Integer; ptr : ^char; 其實(shí)也就是符號(hào)的差別而已。
二、無(wú)類(lèi)型指針的定義。C中有void *類(lèi)型,也就是可以指向任何類(lèi)型數(shù)據(jù)的指針。Object Pascal為其定義了一個(gè)專(zhuān)門(mén)的類(lèi)型:Pointer。于是, ptr : Pointer; 就與C中的 void *ptr; 等價(jià)了。
三、指針的解除引用。要解除指針引用(即取出指針?biāo)竻^(qū)域的值),C 的語(yǔ)法是 (*ptr),Object Pascal則是 ptr^。
四、取地址(指針賦值)。取某對(duì)象的地址并將其賦值給指針變量,C 的語(yǔ)法是 ptr = &Object; Object Pascal 則是 ptr := @Object; 也只是符號(hào)的差別而已。
五、指針運(yùn)算。在 C 中,可以對(duì)指針進(jìn)行移動(dòng)的運(yùn)算,如: char a[20]; char *ptr=a; ptr++; ptr+=2; 當(dāng)執(zhí)行ptr++;時(shí),編譯器會(huì)產(chǎn)生讓ptr前進(jìn)sizeof(char)步長(zhǎng)的代碼,之后,ptr將指向a[1]。ptr+=2;這句使得ptr前進(jìn)兩個(gè)sizeof(char)大小的步長(zhǎng)。同樣,我們來(lái)看一下Object Pascal中如何實(shí)現(xiàn): var a : array [1..20] of Char; ptr : PChar; //PChar 可以看作 ^Char begin ptr := @a; Inc(ptr); // 這句等價(jià)于 C 的 ptr++; Inc(ptr, 2); //這句等價(jià)于 C 的 ptr+=2; end;
六、動(dòng)態(tài)內(nèi)存分配。C語(yǔ)言中,使用malloc()庫(kù)函數(shù)分配內(nèi)存,free()函數(shù)釋放內(nèi)存。如這樣的代碼: 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中,動(dòng)態(tài)分配內(nèi)存的函數(shù)是GetMem(),與之對(duì)應(yīng)的釋放函數(shù)為FreeMem()(傳統(tǒng) Pascal中獲取內(nèi)存的函數(shù)是New()和 Dispose(),但New()只能獲得對(duì)象的單個(gè)實(shí)體的內(nèi)存大小,無(wú)法取得連續(xù)的存放多個(gè)對(duì)象的內(nèi)存塊)。因此,與上面那段C的代碼等價(jià)的Object Pascal的代碼為: var ptr, ptr2 : ^integer; i : integer; begin GetMem(ptr, sizeof(integer) * 20); //這句等價(jià)于C的 ptr = (int*) malloc(sizeof(int) * 20); ptr2 := ptr; //保留原始指針位置 for i := 0 to 19 do begin ptr^ := i; Inc(ptr); end; FreeMem(ptr2); end; 對(duì)于以上這個(gè)例子(無(wú)論是C版本的,還是Object Pascal版本的),都要注意一個(gè)問(wèn)題,就是分配內(nèi)存的單位是字節(jié)(BYTE),因此在使用GetMem時(shí),其第二個(gè)參數(shù)如果想當(dāng)然的寫(xiě)成 20,那么就會(huì)出問(wèn)題了(內(nèi)存訪問(wèn)越界)。因?yàn)镚etMem(ptr, 20);實(shí)際只分配了20個(gè)字節(jié)的內(nèi)存空間,而一個(gè)整形的大小是四個(gè)字節(jié),那么訪問(wèn)第五個(gè)之后的所有元素都是非法的了(對(duì)于malloc()的參數(shù)同樣)。
七、字符數(shù)組的運(yùn)算。C語(yǔ)言中,是沒(méi)有字符串類(lèi)型的,因此,字符串都是用字符數(shù)組來(lái)實(shí)現(xiàn),于是也有一套str打頭的庫(kù)函數(shù)以進(jìn)行字符數(shù)組的運(yùn)算,如以下代碼: 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類(lèi)型,因此可以很方便的對(duì)字符串進(jìn)行各種運(yùn)算。但是,有時(shí)我們的Pascal代碼需要與C的代碼交互(比如:用Object Pascal的代碼調(diào)用C寫(xiě)的DLL或者用Object Pascal 寫(xiě)的DLL準(zhǔn)備允許用C寫(xiě)客戶(hù)端的代碼)的話(huà),就不能使用String類(lèi)型了,而必須使用兩種語(yǔ)言通用的字符數(shù)組。其實(shí),Object Pascal提供了完全類(lèi)似C的一整套字符數(shù)組的運(yùn)算函數(shù),以上那段代碼的Object Pascal 版本是這樣的: var str : array [1..15] of char; pstr : PChar; //Pchar 也就是 ^Char begin StrCopy(@str, 'teststr'); //在C中,數(shù)組的名稱(chēng)可以直接作為數(shù)組首地址指針來(lái)用 //但Pascal不是這樣的,因此 str前要加上取地址的運(yùn)算符 StrCat(@str, '_testok'); GetMem(pstr, sizeof(char) * 15); StrCopy(pstr, @str); Write(pstr); FreeMem(pstr); end;
八、函數(shù)指針。在動(dòng)態(tài)調(diào)用DLL中的函數(shù)時(shí),就會(huì)用到函數(shù)指針。假設(shè)用C寫(xiě)的一段代碼如下: typedef int (*PVFN)(int); //定義函數(shù)指針類(lèi)型 int main() { HMODULE hModule = LoadLibrary("test.dll"); PVFN pvfn = NULL; pvfn = (PVFN) GetProcAddress(hModule, "Function1"); pvfn(2); FreeLibrary(hModule); } 就我個(gè)人感覺(jué)來(lái)說(shuō),C語(yǔ)言中定義函數(shù)指針類(lèi)型的typedef代碼的語(yǔ)法有些晦澀,而同樣的代碼在 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慣壞了,發(fā)現(xiàn)寫(xiě)一個(gè)原生的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日
#
- 尋找的動(dòng)力:看到公司產(chǎn)品中應(yīng)用程序共享的功能很不錯(cuò),在想想兩年前為了做部隊(duì)的桌面共享軟件而實(shí)現(xiàn)的東西,簡(jiǎn)直不能比較,終于找到這個(gè)開(kāi)源項(xiàng)目http://www.realvnc.com/
- 使用感覺(jué):目前是我見(jiàn)到最快的桌面共享軟件
- 編譯源碼:直接在VC6下就能編譯,(記得不能Build All,只要在FileView選項(xiàng)卡下分別Build vncviewer files和winvnc files就可以)。
- 學(xué)習(xí)計(jì)劃:從今天2007-09-11開(kāi)始學(xué)習(xí)、分析VNC的源代碼。
2007年9月4日
#
hi c++!
不會(huì)吧,這個(gè)c++blog的插入代碼竟然沒(méi)有c++格式的 暈~~ 建議加入 C++ Delphi Python 的代碼格式化
|