锘??xml version="1.0" encoding="utf-8" standalone="yes"?> BOOL PostThreadMessage( DWORD idThread, ===========Happy New Year============== 浠ュ墠鑻遍泟涓栫晫涔熸湁涓涓繖鏍風殑紼嬪簭錛屼絾鏄増鏈お鑰侊紝涓嶈兘鐢ㄤ簡銆?/P>
浣跨敤鐗堟湰:涓枃鐗?.1,鍏朵粬鐗堟湰娌℃湁嫻嬭瘯 浣跨敤鏂規硶:榪涘叆娓告垙鍚庯紝榪愯紼嬪簭 Download: http://www.shnenglu.com/Files/sandy/h3c.zip
]]>
]]>
]]>
]]>
]]>
MSDN涓婇潰鏈変竴孌墊枃瀛楁槸榪欐牱璇寸殑
If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. Messages sent between threads are processed only when the receiving thread executes message retrieval code. The sending thread is blocked until the receiving thread processes the message. However, the sending thread will process incoming nonqueued messages while waiting for its message to be processed. To prevent this, use
緲昏瘧涓涓嬶細
SendMessage : 濡傛灉鎸囧畾紿楀彛鐢辮皟鐢ㄧ嚎紼嬪垱寤猴紝閭d箞紿楀彛榪囩▼浼氳褰撴垚涓涓瓙紼嬪簭绔嬪嵆璋冪敤銆傚鏋滄寚瀹氱獥鍙g敱鍙﹀涓涓嚎紼嬪垱寤猴紝閭d箞緋葷粺浼氬垏鎹㈠埌閭d釜綰跨▼錛屽茍涓旇皟鐢ㄥ悎閫傜殑紿楀彛榪囩▼銆傚湪綰跨▼涔嬮棿浼犻掔殑娑堟伅浠呬粎褰撴帴鏀剁嚎紼嬫墽琛宮essage retrieval code鎵嶄細琚鐞嗐傚彂閫佺嚎紼嬩細琚牭濉炵洿鍒版帴鏀剁嚎紼嬪鐞嗗畬娑堟伅銆?strong>浣嗘槸錛屽彂閫佺嚎紼嬪湪絳夊緟鐨勫悓鏃朵細澶勭悊鏀跺埌鐨刵onqueued messages 銆備負浜嗛樆姝㈣繖涓鐐癸紝浣跨敤甯︽湁SMTO_BLOCK鍙傛暟 鐨凷endMessageTimeout .
=================================鍗庝附鐨勫垎鍓茬嚎===========================
鎴戞浘緇忛亣鍒拌繖涓棶棰橈紝鎴戣皟鐢⊿endMessage鍚戝彟澶栦竴涓嚎紼嬬獥鍙e彂message錛屾湰鏉ヤ互涓轟粬浼氫竴鐩碽lock浣忥紝浣嗘槸浠栧嵈璋冪敤浜嗗彟澶栦竴涓秷鎭殑澶勭悊紼嬪簭錛屽鑷翠簡琛屼負涓嶆紜?strong>鎵浠ヤ竴瀹氳灝忓績浣跨敤SendMessage鍙戠粰鍏朵粬綰跨▼鐨勭獥鍙c?/strong>
鎴戜慨鏀逛簡涓涓嬶紝鎶?br />pWnd->SendMessage(MSG_LOG_MESSAGE, nMsgType, (LPARAM)(LPCTSTR)m_cstrMessage);
鏀規垚浜?br />HWND hWnd = pWnd->GetSafeHwnd();
::SendMessageTimeout(hWnd,MSG_LOG_MESSAGE, nMsgType, (LPARAM)(LPCTSTR)m_cstrMessage,SMTO_BLOCK,15000,0);
瑙e喅浜嗚繖涓猙ug.
]]>SYSTEMTIME/WINDOWSTIME
SYSTEMTIME鐨勫畾涔夋槸榪欐牱
鍏堟彁涓棶棰橈紝濡備綍璁$畻褰撳墠鏃墮棿100澶╀互鍚庣殑鏃墮棿錛屽湪win32涓?/font>
1.SYSTEMTIME
聽聽聽聽{
聽聽聽聽WORD聽wYear;
聽聽聽聽WORD聽wMonth;
聽聽聽聽WORD聽wDayOfWeek;
聽聽聽聽WORD聽wDay;
聽聽聽聽WORD聽wHour;
聽聽聽聽WORD聽wMinute;
聽聽聽聽WORD聽wSecond;
聽聽聽聽WORD聽wMilliseconds;
聽聽聽聽}聽SYSTEMTIME;
寰堝ぇ錛屾誨叡16涓瓧鑺傘備綘鍙互璋冪敤GetSystemTime鏉ュ彇寰楀綋鍓嶇殑鏃墮棿
::GetSystemTime(&st);
2. FILETIME
FILETIME鐨勫畾涔夋槸榪欐牱鐨?br />
聽聽聽聽{
聽聽聽聽DWORD聽dwLowDateTime;
聽聽聽聽DWORD聽dwHighDateTime;
聽聽聽聽}聽FILETIME;
//or
__int64聽d2;
memcpy(&d2,&st,sizeof(d2));
杞寲涓篲_int64鏄竴涓互100綰崇涓哄崟浣嶇殑鍊?br />琛ヤ範涓涓嬫椂闂村崟浣?http://blog.vckbase.com/zaboli/archive/2005/06/29/8969.aspx)1ms (姣) 1姣=0.001縐?10-3縐?millisecond) 1渭s (寰) 1寰=0.000001=10-6縐?microsecond) 1ns (綰崇) 1綰崇=0.000000001縐?10-9縐掞紙nanosecond) 1ps (鐨) 1鐨=0.000000000001縐?10-12縐?/font> 1fs (椋炵) 1椋炵=0.00000000000001縐?10-15縐?/font>
SYSTEMTIME鍙互閫氳繃SystemTimeToFileTime鏉ヨ漿鍖栦負FILETIME
3.Window Time
::GetTickCount()鍙互榪斿洖褰撳墠鐨勪互寰涓哄崟浣嶇殑鏃墮棿錛岀敤鍦ㄧ簿搴﹁姹備笉楂樼殑鍦哄悎錛岃繑鍥炵殑鍙槸涓涓狣WORD錛屽洓瀛楄妭銆傞珮涓鐐圭殑浣跨敤timeGetTime
濡備綍璁$畻褰撳墠鏃墮棿100澶╀互鍚庣殑鏃墮棿錛屽湪win32涓?br />搴旇榪欐牱鍐?br />
SYSTEMTIME聽st;
::GetSystemTime(&st);
FILETIME聽f;
::SystemTimeToFileTime(&st,&f);
ULARGE_INTEGER聽now;
memcpy(&now,&f,sizeof(now));
now聽+=聽100*nano100SecInDay;
memcpy(&f,&now,sizeof(f));
FileTimeToSystemTime(&f,&st);
鏈鍚庯細闄勪笂鎴戝啓鐨勪竴涓皬宸ュ叿錛屽垹闄ゆ寚瀹氭枃浠跺す涓嬮潰鐨勮繃鏈熸枃浠剁殑涓涓伐鍏鳳紝騫跺彲浠ュ浠斤紝鏀寔瀛愭枃浠跺す宓屽銆?br />
#include聽<cstdio>
#include聽<string>
using聽namespace聽std;
const聽__int64聽nano100SecInDay=(__int64)10000000*60*60*24;
ULARGE_INTEGER聽now;
int聽days聽=聽14;
bool聽backup聽=聽false;
string聽dst_dir;
string聽backup_dir;
void聽make_sure_dir_exist(string聽&聽file)
{
聽聽聽聽int聽tag聽=聽2;
聽聽聽聽while(true)
聽聽聽聽{
聽聽聽聽聽聽聽聽tag聽=聽file.find_first_of("\\",tag+1);
聽聽聽聽聽聽聽聽if(tag!=-1)
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽string聽tmp聽=聽file.substr(0,tag);
聽聽聽聽聽聽聽聽聽聽聽聽::CreateDirectory(tmp.c_str(),NULL);
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽break;
聽聽聽聽聽聽聽聽}
聽聽聽聽}
}
void聽xdel(const聽char聽*dir)
{
聽聽聽聽char聽tempFileFind[1024];
聽聽聽聽sprintf(tempFileFind,"%s\\*.*",dir);
聽聽聽聽
聽聽聽聽WIN32_FIND_DATA聽ffd;
聽聽聽聽HANDLE聽hFind;
聽聽聽聽hFind聽=聽::FindFirstFile(tempFileFind,&ffd);
聽聽聽聽
聽聽聽聽if(hFind聽==聽INVALID_HANDLE_VALUE)
聽聽聽聽{
聽聽聽聽聽聽聽聽printf("can't聽find聽%s\n",dir);
聽聽聽聽聽聽聽聽return;
聽聽聽聽}
聽聽聽聽
聽聽聽聽while(true)
聽聽聽聽{
聽聽聽聽聽聽聽聽//printf("find聽%s\n",ffd.cFileName);
聽聽聽聽聽聽聽聽FILETIME聽&ft聽=聽ffd.ftLastWriteTime;
聽聽聽聽聽聽聽聽ULARGE_INTEGER聽ui;
聽聽聽聽聽聽聽聽memcpy(&ui,&ft,sizeof(ui));
聽聽聽聽聽聽聽聽__int64聽t聽=聽now.QuadPart-ui.QuadPart;
聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽if(ffd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽if(strcmp(ffd.cFileName,聽".")聽&&聽strcmp(ffd.cFileName,聽".."))
聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽char聽temp[1024];
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽sprintf(temp,"%s\\%s",dir,ffd.cFileName);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽xdel(temp);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(t>=nano100SecInDay*days)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(::RemoveDirectory(temp))
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽printf("del聽dir聽%s聽ok\n",temp);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽printf("del聽dir聽%s聽failed\n",temp);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽char聽temp[1024];
聽聽聽聽聽聽聽聽聽聽聽聽sprintf(temp,"%s\\%s",dir,ffd.cFileName);
聽聽聽聽聽聽聽聽聽聽聽聽
聽聽聽聽聽聽聽聽聽聽聽聽if(t>=nano100SecInDay*days)
聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(backup)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽string聽backup_file聽=聽temp;
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽backup_file.replace(0,dst_dir.length(),backup_dir.c_str());
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽make_sure_dir_exist(backup_file);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(::MoveFile(temp,backup_file.c_str()))
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽printf("backup聽file聽%s聽ok\n",temp);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽printf("backup聽file聽%s聽failed\n",temp);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽SetFileAttributes(temp,FILE_ATTRIBUTE_NORMAL);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽if(::DeleteFile(temp))
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽printf("del聽file聽%s聽ok\n",temp);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽printf("del聽file聽%s聽failed\n",temp);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽if聽(!FindNextFile(hFind,聽&ffd))聽
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽break;
聽聽聽聽聽聽聽聽}
聽聽聽聽}
聽聽聽聽FindClose(hFind);
}
int聽main(int聽argc,char聽**聽argv)
{
聽聽聽聽if(argc<2)
聽聽聽聽{
聽聽聽聽聽聽聽聽printf("usage:聽xdel聽directory聽/d=聽/m=\n");
聽聽聽聽聽聽聽聽printf("[optional]聽/d:聽config聽the聽expired聽days,default聽is聽14聽days\n");
聽聽聽聽聽聽聽聽printf("[optional]聽/m:聽config聽the聽backup聽directory\n");聽
聽聽聽聽聽聽聽聽printf("for聽example:聽xdel聽d:\\test聽/d=10聽/m=d:\\backup");
聽聽聽聽聽聽聽聽return聽1;
聽聽聽聽}
聽聽聽聽
聽聽聽聽for(int聽i=1;i<argc;++i)
聽聽聽聽{
聽聽聽聽聽聽聽聽string聽tmp聽=聽argv[i];
聽聽聽聽聽聽聽聽if(tmp.find("/d=")!=-1)
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽int聽d聽=聽atoi(tmp.substr(3).c_str());
聽聽聽聽聽聽聽聽聽聽聽聽if(d!=0)
聽聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽days聽=d;
聽聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽else聽if(tmp.find("/m=")!=-1)
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽backup聽=聽true;
聽聽聽聽聽聽聽聽聽聽聽聽backup_dir聽=聽tmp.substr(3);
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽else
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽dst_dir聽=聽tmp;
聽聽聽聽聽聽聽聽}
聽聽聽聽}
聽聽聽聽
聽聽聽聽//Get聽system聽time
聽聽聽聽SYSTEMTIME聽st;
聽聽聽聽::GetSystemTime(&st);
聽聽聽聽FILETIME聽f;
聽聽聽聽::SystemTimeToFileTime(&st,&f);
聽聽聽聽memcpy(&now,&f,sizeof(now));
聽聽聽聽
聽聽聽聽xdel(argv[1]);
聽聽聽聽
聽聽聽聽return聽0;
}
]]>
]]>
鎵浠ュ涔犲綰跨▼緙栫▼鏈閲嶈鐨勪笉鏄涔燗PI,鑰屾槸鐞嗚В浠涔堟墠鏄綰跨▼瀹夊叏鐨勪唬鐮?BR>
浠庝緥瀛愯璧?BR>
#include <process.h>
long global1 = 0;
volatile long global2 = 0;
class MyClass
{
public:
MyClass() : m(0)
{
++m;
}
int fun(int v)
{
return m+v; //-----------9
}
void set(int v)
{
m = v; //-------------10
}
int m;
};
MyClass global_object; //-------------8
unsigned int __stdcall thread_fun1(void *param)
{
static int static2 = 0;
static MyClass static_object; //--------6
int local1 = 0;
++local1; //-------1
++static2; //-------2
++global1; //-------3
++global2; //-------4
InterlockedIncrement(&global1); //--------5
local1 = global_object.fun(local1); //----------7
global_object.set(local1); //---------------11
return 0;
}
unsigned int __stdcall thread_fun2(void *param)
{
++global1; //-------3
++global2; //-------4
InterlockedIncrement(&global1); //--------5
global_object.set(1); //-----------11
return 0;
}
int main()
{
HANDLE thread1 = (HANDLE)_beginthreadex(0,0,&thread_fun1,0,0,0); //thread 1
HANDLE thread2 = (HANDLE)_beginthreadex(0,0,&thread_fun1,0,0,0); //thread 2
HANDLE thread3 = (HANDLE)_beginthreadex(0,0,&thread_fun2,0,0,0); //thread 3
WaitForSingleObject(thread1,INFINITE);
WaitForSingleObject(thread2,INFINITE);
WaitForSingleObject(thread3,INFINITE);
return 0;
}
1.灞閮ㄥ彉閲忓眬閮ㄤ嬌鐢ㄦ槸瀹夊叏鐨?/STRONG>
涓轟粈涔?鍥犱負姣忎釜thread 閮芥湁鑷繁鐨勮繍琛屽爢鏍堬紝鑰屽眬閮ㄥ彉閲忔槸鐢熷瓨鍦ㄥ爢鏍堜腑,澶у涓嶅共鎵般?BR>鎵浠ヤ唬鐮?
int local1;
++local1;
鏄畨鍏ㄧ殑
2.鍏ㄥ眬鍘熺敓鍙橀噺澶氱嚎紼嬭鍐欐槸涓嶅畨鍏ㄧ殑
鍏ㄥ眬鍙橀噺鏄湪鍫?heap)涓?BR>long global1 = 0;
++global2;
++榪欎釜鎿嶄綔鍏跺疄鍒嗕負涓ら儴錛屼竴涓槸璇伙紝鍙﹀涓涓槸鍐?BR> mov ecx,global
add ecx,1
mov global,ecx
鎵浠ヤ唬鐮?澶勬槸涓嶅畨鍏ㄧ殑
3.鍑芥暟闈欐佸彉閲忓綰跨▼璇誨啓涔熸槸涓嶅畨鍏ㄧ殑
閬撶悊鍚?
鎵浠ヤ唬鐮?澶勪篃鏄笉瀹夊叏鐨?BR>
4.volatile鑳戒繚璇佸叏灞鏁村艦鍙橀噺鏄綰跨▼瀹夊叏鐨勪箞
涓嶈兘銆?BR>volatile浠呬粎鏄憡璇玞ompiler涓嶈瀵硅繖涓彉閲忎綔浼樺寲錛屾瘡嬈¢兘瑕佷粠memory鍙栨暟鍊鹼紝鑰屼笉鏄粠register
鎵浠ヤ唬鐮?涔熶笉鏄畨鍏?BR>
5.InterlockedIncrement淇濊瘉鏁村瀷鍙橀噺鑷鐨勫師瀛愭?/STRONG>
鎵浠ヤ唬鐮?鏄畨鍏ㄧ殑
6.function static object鐨勫垵濮嬪寲鏄綰跨▼瀹夊叏鐨勪箞
涓嶆槸銆?BR>钁楀悕鐨凪eyer Singleton鍏跺疄涓嶆槸綰跨▼瀹夊叏鐨?BR>Object & getInstance()
{
static Object o;
return o;
}
鍙兘浼氶犳垚澶氭鍒濆鍖栧璞?BR>鎵浠ヤ唬鐮?澶勬槸涓嶅畨鍏ㄧ殑
7.鍦?2鏈哄櫒涓婏紝4瀛楄妭鏁村艦涓嬈ssign鏄師瀛愮殑
姣斿
i =10; //thread1
i=4; //thread2
涓嶄細瀵艱嚧i鐨勫煎浜庢湭鐭ョ姸鎬?瑕佷箞鏄?0瑕佷箞鏄?
鍏跺畠鐨勫ぇ瀹惰嚜宸卞幓浣撲細銆?BR>
鍐欏ソ澶氱嚎紼嬪畨鍏ㄧ殑娉曞疂灝辨槸灝佽錛屼嬌鏁版嵁鏈変繚鎶ょ殑琚闂埌
瀹夊叏鎬э細
灞閮ㄥ彉閲?gt;鎴愬憳鍙橀噺>鍏ㄥ眬鍙橀噺
]]>
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
PostThreadMessage鍙互鐢ㄤ簬綰跨▼涔嬮棿鐨勫紓姝ラ氳錛屽洜涓哄畠涓嶇敤絳夊緟璋冪敤鑰呰繑鍥烇紝
榪欎篃璁告槸綰跨▼閫氳涓渶綆鍗曠殑涓縐嶆柟娉曚簡銆?BR>
浣嗘槸瑕佹敞鎰忎互涓嬮棶棰?BR>1 .PostThreadMessage鏈夋椂浼氬け璐ワ紝鎶?444閿欒(Invalid thread identifier. )
鍏跺疄榪欎笉涓瀹氭槸綰跨▼涓嶅瓨鍦ㄧ殑鍘熷洜錛屼篃鏈夊彲鑳芥槸綰跨▼涓嶅瓨鍦ㄦ秷鎭槦鍒?message queue)閫犳垚鐨勩?BR>浜嬪疄涓婏紝騫朵笉鏄瘡涓猼hread閮芥湁message queue,閭e浣曡thread鍏鋒湁鍛紵
絳旀鏄紝鑷沖皯璋冪敤message鐩稿叧鐨刦unction涓嬈★紝姣斿GetMessage,PeekMessage銆?BR>
2.濡傛灉鏄痯ost鍔ㄦ佸垎閰嶇殑memory緇欏彟澶栦竴涓猼hread,瑕佹敞鎰忓唴瀛樼殑姝g‘閲婃斁銆?BR>
3.PostThreadMessage涓嶈兘澶焢ost WM_COPYDATE涔嬬被鐨勫悓姝ユ秷鎭紝鍚﹀垯浼氭姤閿?BR>
4.鏈濂戒笉瑕佷嬌鐢≒ostThreadMessage post message緇欎竴涓獥鍙?浣跨敤PostMessage鏇夸唬銆?BR>
涓嬮潰鏄垜鍐欑殑涓涓瘮杈冧弗鏁寸殑渚嬪瓙錛屼粎渚涘弬鑰冦?BR>
#include <cstdio>
#include <process.h>
#define MY_MSG WM_USER+100
const int MAX_INFO_SIZE = 20;
HANDLE hStartEvent; // thread start event
// thread function
unsigned __stdcall fun(void *param)
{
printf("thread fun start\n");
MSG msg;
PeekMessage(&msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
if(!SetEvent(hStartEvent)) //set thread start event
{
printf("set start event failed,errno:%d\n",::GetLastError());
return 1;
}
while(true)
{
if(GetMessage(&msg,0,0,0)) //get msg from message queue
{
switch(msg.message)
{
case MY_MSG:
char * pInfo = (char *)msg.wParam;
printf("recv %s\n",pInfo);
delete[] pInfo;
break;
}
}
};
return 0;
}
int main()
{
HANDLE hThread;
unsigned nThreadID;
hStartEvent = ::CreateEvent(0,FALSE,FALSE,0); //create thread start event
if(hStartEvent == 0)
{
printf("create start event failed,errno:%d\n",::GetLastError());
return 1;
}
//start thread
hThread = (HANDLE)_beginthreadex( NULL, 0, &fun, NULL, 0, &nThreadID );
if(hThread == 0)
{
printf("start thread failed,errno:%d\n",::GetLastError());
CloseHandle(hStartEvent);
return 1;
}
//wait thread start event to avoid PostThreadMessage return errno:1444
::WaitForSingleObject(hStartEvent,INFINITE);
CloseHandle(hStartEvent);
int count = 0;
while(true)
{
char* pInfo = new char[MAX_INFO_SIZE]; //create dynamic msg
sprintf(pInfo,"msg_%d",++count);
if(!PostThreadMessage(nThreadID,MY_MSG,(WPARAM)pInfo,0))//post thread msg
{
printf("post message failed,errno:%d\n",::GetLastError());
delete[] pInfo;
}
::Sleep(1000);
}
CloseHandle(hThread);
return 0;
}
]]>
灝辨槸璁╂垜鏂硅嫳闆勫浼氭墍鏈夋妧鑳斤紝鐪熺殑铔埥鐨?BR>瀛︿細28欏規妧鑳戒慨鏀規硶
銆銆棣栧厛錛屾妸浣犵殑鑻遍泟鐨勫+鍏佃皟鍒板墠闈㈠嚑鏍兼潵錛屾帴鐫鐢‵PE鏉ユ悳绱€備緥濡備綘鐨勫+鍏墊暟錛氱涓鏍?錛岀浜屾牸14錛岀涓夋牸4錛岄偅涔堝氨鐢?3錛?錛?4錛?錛?錛? 鏉ユ悳绱紝灝卞彲浠ユ壘鍒板湴鍧銆傛樉紺轟負錛?
銆銆澹叺縐嶇被錛?FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
銆銆 FF FF FF FF FF FF FF FF FF FF FF FF 03 00 00 00錛嶅+鍏墊暟閲?BR>銆銆 0E 00 00 00 04 00 00 00 00 00 00 00 00 00 00 00
銆銆 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08
銆銆 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
銆銆 25 26 27 28 01 02 03 04 05 06 07 08 00 00 00 00
銆銆 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
銆銆 04 00 00 00 00 00 00 00 FF FF FF FF FF FF FF FF
銆銆 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
銆銆 FF鎴?0浣滀負涓鏍鹼紝姣?鏍兼槸浠h〃涓縐嶅睘鎬э紝鐗╁搧錛?鐘舵佺瓑絳夈傚墠闈?8鏍兼槸澹叺鐨勭綾伙紝鎺ヤ笅鏉?8鏍兼槸澹叺鐨勬暟閲忋備緥濡? 鑻遍泟鍙甫7縐嶅+鍏碉紝浣犺姣忕閮芥槸澶╀嬌錛岄偅涔堟妸
銆銆 03 00 00 00 鍓嶉潰鐨凢F FF FF FF 鏀逛負0D 00 00 00銆?濡傛灉浣犺鏁伴噺100錛?閭d箞鎶?BR>銆銆 03 00 00 00 鏀逛負64 00 00 00銆?澹叺鐨勭綾繪槸榪欐牱鍒嗙殑錛?0 00 00 00 鏄灙鍏碉紝
銆銆 01 00 00 00 鏄?榪涘寲涓綰х殑鏋叺錛?2 00 00 00 鏄紦綆墜錛?3 00 00 00 鏄綆墜銆?BR>銆銆 濡傛綾繪帹銆?BR>銆銆
銆銆 浠?3閭d竴鏍煎紑濮嬫暟錛?8鏍煎悗灝辨槸鑻遍泟鐨勬妧鑳界瓑綰э紝鎺ョ潃鐨?8鏍兼槸鑻遍泟鐨勬妧鑳斤紝
銆銆 涔熷氨鏄粠01鍒?8浠h〃鐨勬槸鑻遍泟鐨勬妧鑳界瓑綰э紝鎶鑳界瓑綰ф渶澶氬彧鑳芥槸3銆?BR>銆銆 01 綆湳絳夌駭 02 瀵昏礬鏈瓑綰?03 鍚庡嫟瀛︾瓑綰?BR>銆銆 04 渚﹀療鏈瓑綰?05 澶栦氦鏈瓑綰?06 鑸搗鏈瓑綰?BR>銆銆 07 棰嗗鏈瓑綰?08 鏅烘収鏈瓑綰?09 紲炵鏈瓑綰?BR>銆銆 10 騫歌繍鏈瓑綰?11 寮歸亾鏈瓑綰?12 楣扮溂鏈瓑綰?BR>銆銆 13 鎷涢瓊鏈瓑綰?14 鐞嗚儲鏈瓑綰?15 鐏郴欖旀硶絳夌駭
銆銆 16 姘旂郴欖旀硶絳夌駭 17 姘寸郴欖旀硶絳夌駭 18 鍦熺郴欖旀硶絳夌駭
銆銆 19 瀛︽湳絳夌駭 20 鎴樻湳絳夌駭 21 鐐湳絳夌駭
銆銆 22 瀛︿範鑳藉姏絳夌駭 23 榪涙敾鏈瓑綰?24 闃插盡鏈瓑綰?
浣嗘槸姣忔浣跨敤FPE鐪熺殑寰堥夯鐑︼紝鎴戝張鏄緢鎳掔殑浜恒侳PE鍙互鍋氬埌鐨勪簨鎯咃紝鎴戜篃鍙互鍋氬埌銆?BR>鍏跺疄娓告垙淇敼涓嶈繃鏄嬌鐢?STRONG>ReadProcessMemory鍜?STRONG>WriteProcessMemory
鑺變簡鍗婂ぉ涓嶆柇鐨勮瘯楠岋紝緇堜簬鍐欏嚭涓涓彲浠ョ敤浜庤嫳闆勬棤鏁?.1鐗堢殑淇敼鍣紝娌℃湁鍐欑晫闈紙I'm lazy-:)錛夛紝榪愯涔嬪悗灝辨槸璁╂垜鏂硅嫳闆勫浼氭墍鏈夋妧鑳姐?BR>
#include <tlhelp32.h>
#include <cstdio>
const char MODULE_NAME[] = "Heroes3.exe";
void printError( TCHAR* msg )
{
DWORD eNum;
TCHAR sysMsg[256];
TCHAR* p;
eNum = GetLastError( );
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, eNum,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
sysMsg, 256, NULL );
// Trim the end of the line and terminate it with a null
p = sysMsg;
while( ( *p > 31 ) || ( *p == 9 ) )
++p;
do { *p-- = 0; } while( ( p >= sysMsg ) &&
( ( *p == '.' ) || ( *p < 33 ) ) );
// Display the message
printf( "WARNING: %s failed with error %d (%s)\n", msg, eNum, sysMsg );
}
DWORD findProcessId(const char *module)
{
DWORD result = -1;
HANDLE hProcessSnap;
// Take a snapshot of all processes in the system.
hProcessSnap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
if( hProcessSnap == INVALID_HANDLE_VALUE )
{
printError( "CreateToolhelp32Snapshot (of processes)" );
return result;
}
PROCESSENTRY32 pe32;
// Set the size of the structure before using it.
pe32.dwSize = sizeof( PROCESSENTRY32 );
// Retrieve information about the first process,
// and exit if unsuccessful
if( !Process32First( hProcessSnap, &pe32 ) )
{
printError( "Process32First" ); // Show cause of failure
CloseHandle( hProcessSnap ); // Must clean up the snapshot object!
return result;
}
// Now walk the snapshot of processes, and
// display information about each process in turn
do
{
if(stricmp(pe32.szExeFile,module)==0)
{
printf( "find process: %s\n", module );
result = pe32.th32ProcessID;
break;
}
} while( Process32Next( hProcessSnap, &pe32 ) );
CloseHandle( hProcessSnap );
return result;
}
int main(int argc,char *argv[])
{
DWORD h3pid = -1;
h3pid = findProcessId(MODULE_NAME);
if(h3pid == -1)
{
printf("can't find %s in memory,please make sure the program started!\n",MODULE_NAME);
return 1;
}
HANDLE h3 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, h3pid );
if( h3 == NULL )
{
printError( "OpenProcess" );
return 1;
}
unsigned long sideOffset=0x824994;
unsigned char side = 0xff;
if(!ReadProcessMemory(h3,(LPCVOID)sideOffset,&side,1,0)) //鏌ユ壘鎴戞柟鐨勯鑹?BR> {
printError( "ReadProcessMemory" );
return 1;
}
if(side!=0xff)
{
printf("find current side:%d\n",(int)side);
}
else
{
printf("can't find current side\n");
side = 0;
}
unsigned long heroBaseAddress = 0x15216ab; //hero name start
unsigned char name[20]={0};
unsigned long temp = heroBaseAddress-1;
char b[28]; //28縐嶆妧鑳?BR> int size = sizeof(b);
memset(b,3,size);
b[12]=0;//涓嶅鎷涢瓊鏈?BR>
for(int i=0;i<=155;++i) //涓鍏?56涓狧ero
{
if(!ReadProcessMemory(h3,(LPCVOID)temp,name,sizeof(name),0))
{
printError( "ReadProcessMemory" );
return 1;
}
if(name[0]==side)
{
printf("find:%s\t",name+1);
if(!WriteProcessMemory(h3,(LPVOID)(temp+0xA7),b,size,0))
{
printError( "WriteProcessMemory" );
return 1;
}
else
{
printf("update skill sucess!\n");
}
}
temp += 0x492;
}
CloseHandle(h3);
return 0;
}
闄勫姞:
璁╂垜鏂硅嫳闆勬墍鏈夎嫳闆勫浼氶櫎浜嗘嫑欖傛湳鐨勪互澶栫殑27縐嶆妧鑳界殑灝忕▼搴?/P>
]]>
鐪熺殑娌″姙娉曞垹闄よ嚜韜箞錛?BR>璇瘋繍琛屼笅闈㈢殑浠g爜錛?BR>
#include <shlobj.h>
BOOL SelfDelete()
{
SHELLEXECUTEINFO sei;
TCHAR szModule [MAX_PATH], szComspec[MAX_PATH], szParams [MAX_PATH];
// get file path names:
if((GetModuleFileName(0,szModule,MAX_PATH)!=0) &&
(GetShortPathName(szModule,szModule,MAX_PATH)!=0) &&
(GetEnvironmentVariable("COMSPEC",szComspec,MAX_PATH)!=0))
{
// set command shell parameters
lstrcpy(szParams,"/c del ");
lstrcat(szParams, szModule);
lstrcat(szParams, " > nul");
// set struct members
sei.cbSize = sizeof(sei);
sei.hwnd = 0;
sei.lpVerb = "Open";
sei.lpFile = szComspec;
sei.lpParameters = szParams;
sei.lpDirectory = 0;
sei.nShow = SW_HIDE;
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
// increase resource allocation to program
SetPriorityClass(GetCurrentProcess(),
REALTIME_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_TIME_CRITICAL);
// invoke command shell
if(ShellExecuteEx(&sei))
{
// suppress command shell process until program exits
SetPriorityClass(sei.hProcess,IDLE_PRIORITY_CLASS);
SetProcessPriorityBoost(sei.hProcess,TRUE);
// notify explorer shell of deletion
SHChangeNotify(SHCNE_DELETE,SHCNF_PATH,szModule,0);
return TRUE;
}
else // if error, normalize allocation
{
SetPriorityClass(GetCurrentProcess(),
NORMAL_PRIORITY_CLASS);
SetThreadPriority(GetCurrentThread(),
THREAD_PRIORITY_NORMAL);
}
}
return FALSE;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// on program exit
// close all handles etc.
if(!SelfDelete())
{
// add error messaging
}
return 0; // WinMain exit
}
紼嬪簭鐨勬濇兂鏄氳繃鍒涘緩涓涓彟澶栫殑榪涚▼(ShellExecuteEx)錛屽啀璧嬩簣鏈繘紼嬫瘮杈冮珮鐨勬潈闄?SetPriorityClass),
絳夎繖涓▼搴忛鍑轟互鍚庯紝閭d釜鏉榪涚▼鐨勮繘紼嬪氨鍙互鍒犻櫎紼嬪簭浜嗭紝鍙﹀紼嬪簭閫氳繃SHChangeNotify閫氱煡Explorer:紼嬪簭琚垹闄ゃ?BR>
鍏蜂綋API鐨勪嬌鐢ㄦ柟娉曡鐪婱SDN.
ps:榪欎釜紼嬪簭鏄垜鍦ㄨ佸鐨勭綉绔欎笂鎵懼埌鐨勶紝涓嶆槸鎴戝啓鐨勩傛垜鍦╒C6,Win2000 Professional涓婇潰璋冭瘯閫氳繃
]]>
#include <string>
#include <ctime>
enum Colors
{
BLACK = 0,
BLUE = 1,
DARK_GREEN = 2,
LIGHT_BLUE = 3,
RED = 4,
PURPLE = 5,
ORANGE = 6,
GREY = 7,
DARKER_GREY = 8,
MEDIUM_BLUE = 9,
LIGHT_GREEN = 10,
TEAL = 11,
RED_ORANGE = 12,
LIGHT_PURPLE = 13,
YELLOW = 14,
WHITE = 15
};
void set_cursor(short x, short y)
{
COORD point = {x, y};
::SetConsoleCursorPosition(::GetStdHandle(STD_OUTPUT_HANDLE), point);
}
void set_color(unsigned short color)
{
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void delay(unsigned int delay)
{
::Sleep(delay);
}
void set_title(std::string title)
{
::SetConsoleTitle(title.c_str());
}
void show_cursor(bool show, int size = 25)
{
CONSOLE_CURSOR_INFO cci;
if (size <= 0) size = 1;
if (size > 100) size = 100;
cci.dwSize = size;
cci.bVisible = show;
::SetConsoleCursorInfo(::GetStdHandle(STD_OUTPUT_HANDLE), &cci);
}
void clear_screen()
{
system("cls");
}
浠g爜寰堢畝鍗曪紝涓嶇敤澶氫綔瑙i噴浜?img src ="http://www.shnenglu.com/sandy/aggbug/1537.html" width = "1" height = "1" />
]]>