Sean's Note: 2011

2011年12月16日 星期五

[Debug] printf


撰寫程式時,我們常會透過 printf 來 debug,但 release 的時候又

需要把這些 printf 註解掉,造成了不少麻煩。這時我們可以利用

Maco 定義我們的 printf 函數,當有定義 _DEBUG_ 時才印出東西,

否則不作任何事,_DEBUG_  就像一個開關一樣。

範例如下:

#define _DEBUG_

#ifdef _DEBUG_
  #define DPRINTF(...) { printf(__VA_ARGS__); }
#else
  #define DPRINTF(...)
#endif

DPRINTF ("123\n");
DPRINTF ("%d\n", 123);

WM_SIZE & WM_WINDOWPOSCHANGED

當我們建立很多視窗和子視窗時,常會利用 WM_SIZE 來改變子視窗的位移狀態,

但有時候會發現子視窗並無收到 WM_SIZE 的訊息,導致程式執行的結果不如預期

在 MSNDN 論壇上看到,有人建議在處裡父視窗時用 WM_SIZE,處理子視窗時用

WM_WINDOWPOSCHANGED。

那也許都用 WM_WINDOWPOSCHANGED 也可以 ?

PS: 視窗大小改變的觸發順序:

WM_WINDOWPOSCHANGING -> WM_WINDOWPOSCHANGED -> WM_SIZE

資料參考 :
1. http://social.msdn.microsoft.com/forums/en-US/windowsuidevelopment/thread/25181bd5-394d-4b94-a6ef-06e3e4287527/

2. http://blogs.msdn.com/b/alejacma/archive/2008/11/20/controls-won-t-get-resized-once-the-nesting-hierarchy-of-windows-exceeds-a-certain-depth-x64.aspx

2011年12月14日 星期三

Inside COM - 讀後筆記


COM (Component Object Model)

Benefits:
1. Application Customization, each component can be replaced with a different component that better meets the needs of the user.
2. Component Libraries, choose components from a components library like Lego building blocks to form applications.
3. Distributed Components, the application has been divided into functional parts that can be located remotely.

Requirements:
1. dynamic linking
2. information hiding(Encapsulate), the more the interface is isolated from implementation details, the less likely the
   interface will change as a result of changing the client or the component.

 
COM doesn't compete with or replace DLLs. COM uses DLLs to provide components with the ability to dynamically link.

For COM, an interface is a specific memory structure containing an array of function pointers.

A class is not a component. You can implement a single COM component using several C++ classes or C structures.

The COM client doesn't know the interface supported by a component. To find out whether the component supports a
particular interface, the client asks the component for that interface at run time.

In QueryInterface, you can't use a case statement because the interface identifier is a structure and not a number.

IID is type defined as a 128-bit(16-byte) structure called a GUID(Globally Unique Identifier).
In COM, a GUID used to identified a components is called a class identifier(CLSID).

2011年12月12日 星期一

WinDbg

微軟提供的 WinDbg 提供了許多除錯的功能,常用的例子如:

當我們想 Trace 哪些程式使用了 Windows 的 SetCursor API,

我們可以先打 x USER32!*setcursor

x:              查詢 symbol name 的指令
user32:     欲查詢的 Module name
!:               分隔符號
*:              萬用字元
setcursor: 欲查詢的 API

搜尋結果:

777ff34b USER32!ReaderSetCursor = <no type information>
777b3075 USER32!NtUserSetCursor = <no type information>

再設定中斷點:
bp USER32!NtUserSetCursor "k; g"

即可攔截該 API 。


2011年12月6日 星期二

[Android] 環境架設

按此跳轉自[Android] 環境架設 (2014 更新版)

1. 安裝 Android SDK。(http://developer.android.com/sdk/index.html)

2. 先裝好 Eclipse (目前的版號是 3.7 Indigo) 。

3. 到 Help -> Install New Software... ,

    就會彈出下載視窗,點選 Add ,在欄位裡輸入,

    Name:Android (可自訂名稱)
    Locationhttps://dl-ssl.google.com/android/eclipse/ (伺服器位置)

   即可下載 Android Developer Tools 套件,

   在這裡可能會遇到 missing “org.eclipse.wst.sse.core 0.0.0″ 的錯誤訊息,

    此時,一樣先下載 Eclipse 的 Mobile Application Development 套件

    NameIndigo
    Locationhttp://download.eclipse.org/releases/indigo/

   在安裝一次 Android Developer Tools 即可。

4. Restart Eclipse.

5. 開啟後,Android SDK Manager 會自動跳出協助下載 Android SDK。
   
   另外,也可以到 Android Developers 自行下載。

6. 到 "我的電腦" 設定環境變數,在 Path 後加上 SDK 的安裝路徑,

    如: C:\Program Files\Android\android-sdk

7. 即可開始建立第一支 Android Project 啦!


Note: Android SDK Tools 常會有更新,目前是 Rev.21 (2012.11.14),

在更新到 Rev.20 時,可能會發現某資料夾正在被使用或被防毒軟體擋下,

以至於檔案無法移動的錯誤訊息。這時,只能靠手動的方式進行,移動雖然失敗,

但更新檔已在: C:\Program Files\Android\android-sdk\temp\tools_r20-windows.zip,

只要將其解壓縮覆蓋到 C:\Program Files\Android\android-sdk\tools 即可。



2011年12月5日 星期一

工作管理員會拿走滑鼠資源

發現一個有趣的問題,當在執行自己開發的 A 程式時,

同時開著工作管理員,工作管理員會因為要一直更新,

而不斷的使用滑鼠資源,造成 A 程式一直收到 WM_MOUSEMOVE, 

所以造成 A 程式可能會有些不預期的狀況發生。

2011年11月23日 星期三

[DEBUG] _RPT & _RPTF


Visual C++ 提供了 _RPT 和 _RPTF 巨集來幫助使用者除錯,

其功能類似於 printf,但僅在 Debug 設定下會印出字串,

Release 設定下則會自動刪除。其可印出呼叫該巨集的程式檔案名稱與行數。

例如 :

_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);

_CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);

_RPT0(_CRT_WARN, "Test.\n");

// Output
Test

_RPT1(_CRT_WARN, "Test %d.\n", 1);

// Output
Test 1

_RPTF0(_CRT_WARN, "Test.\n");

// Output
d:\win32\assert\assert\assert.cpp(54) : Test

_RPTF1(_CRT_WARN, "Test %d.\n", 1);

// Output
d:\win32\assert\assert\assert.cpp(54) : Test 1


有無 F 差在要不要印出檔案名稱與行數,而數字代表要代入的參數個數,最多到 4 。


[DEBUG] _ASSERT & _ASSERTE

Visual C++ 提供了 _ASSERT 和 _ASSERTE 巨集來幫助使用者除錯,
其功能類似於 printf,但僅在 Debug 設定下會印出字串,
Release 設定下則會自動刪除。其可印出一布林運算式的結果。
用法例如 :

_ASSERT(3 == 3);
 // Output
d:\win32\assert\assert\assert.cpp(53) :

_ASSERT(3 == 2);
 // Output
d:\win32\assert\assert\assert.cpp(53) : Assertion failed!

_ASSERTE(3 == 3);
 // Output
當條件成立時不印出任何字串

_ASSERTE(3 == 2);
 // Output
d:\win32\assert\assert\assert.cpp(53) : Assertion failed : 3 == 2


記得要先設定為 _CRTDBG_MODE_FILE,才會顯示在 console 上,
否則預設為跳出視窗_CRTDBG_MODE_WNDW )

_CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
_CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);

2011年10月4日 星期二

處理路徑字串的常用函式

PathCombine__out LPTSTR lpszDest, __in_opt LPCTSTR lpszDir, __in LPCTSTR lpszFile )
in: C:
in: One\Two\Three
out: C:\One\Two\Three

PathRemoveExtension( __inout LPTSTR pszPath )
in: C:\TEST\sample.txt
out: C:\TEST\sample


PathFindExtension( __in PTSTR pszPath )
in: C:\TEST\sample.txt
out: .txt


PathRemoveFileSpec( __inout LPTSTR pszPath )
in: C:\TEST\sample.txt
out: C:\TEST

GetSystemDirectory( __out LPTSTR lpBuffer, __in UINT uSize )
out: C:\Windows\System32

PathStripPath(__inout LPTSTR pszPath); 
in: c:\\dir1\\file.txt
out: file.txt

2011年9月15日 星期四

[Win32] 如何修改 Edit Control 和 Static Control 的文字顏色和背景顏色

當 Edit Control (Edit Text) 和 Static Control (Static Text) 要被繪圖的時候,

視窗就會接受到 WM_CTLCOLORSTATIC 的訊息,

當接收到此訊息後,可以利用 wParam 和 lParam 分別取得元件的 hDC 和 handle,

再透過 SetTextColor 和 SetBkColor 函式,並建立背景筆刷 hBRUSH,

即可更改顏色,詳細內容與範例請參考 MSDN

Window Controls 和 .rc 檔

用 Visual Studio 在開發 win32 或 MFC 視窗程式的時候,

我們常常可以用拖拉的方式來建立許多元件 (Controls),

並由旁邊的屬性對話窗 (Properties Dialog),來更改一些屬性,

但並非所有屬性都會列於此,例如元件的長寬等等,

這時就必須透過程式或者 *.rc 資源檔來直接做修改。

而在 MSDN 裡有介紹了各種元件在 rc 檔的格式。

2011年9月13日 星期二

[VC] 用 dumpbin 來看 DLL 檔

在 Visual Studio 裡提供了 dumpbin 工具,可以用來查看
DLL 檔提供了哪些函式,dumpbin.exe 的位置於 :
C:\Program Files\Microsoft Visual Studio 8\VC\bin
如果執行時出現找不到 mspdb80.dll 時,
將 C:\Program Files\Microsoft Visual Studio 8\Common7\IDE 
加到系統環境變數的 Path 裡。

2011年9月8日 星期四

char, wchar_t and TCHAR


在 c 裡常見的 char 代表 1 個 byte,

後來為了引入寬字元, 而有了 2 個 bytes 的 wchar_t,

wchar_t 的定義如下 :

typedef unsigned short wchar_t;

平常我們所用的字串處理函式如下 :

char* str = "hello!"; // 7

len = strlen(str);

當然也有用來處理寬字元字串的函式 :

wchar_t* str = "hello!"; // 14

len = wcslen(str);

為了使程式既能按 ASCII 又能按 Unicode 編譯,而衍伸了通用變數 TCHAR,

在 tchar.h 裡可以看到,當有 #define _UNIOCODE 時

typedef wchar_t TCHAR

反之,

typedef char TCHAR

如此一來,使用通用變數,將會使得程式碼更為一致性。

而通用的字串處理函式如 : l + 函式名稱

len = lstrlen(str);
len = lstrcpy(str);
len = lstrcpyn(str);
len = lstrcat(str);

2011年6月19日 星期日

k-lite mega code 播 rmvb 沒有聲音

今天在 win7 上裝了最新的 k-lite mega code 7.2.0,結果播 rmvb 檔卻沒有聲音,

於是在官網的 F.A.Q. 上看到


Q: RMVB files play without sound
A: You are probably missing a decoder for RealAudio COOK. You can install that by following these steps:

  1. Download the Windows version of binary codec pack for MPlayer.
    http://www.mplayerhq.hu/design7/dload.html#binary_codec
    (filename = windows-essential-20071007.zip)
  2. Extract only these files from the ZIP archive:
    cook.dllpncrt.dllsipr3260.dlldrv33260.dlldrv43260.dll
  3. Put those files in the Windows system folder.
    That is C:\Windows\System32\ on a 32-bit version of Windows.
    That is C:\Windows\SysWOW64\ on a 64-bit version of Windows.

照著做就搞定了!

2011年3月30日 星期三

[DM6437] Pin Mux 的設定

可去官網抓 DM643x  Pin Mux Utility.exe 來用。

[DM6437] FlashBurn

平常透過 USB 在板子上模擬都是寫在 RAM 上,所以電源一關掉,

程式也就被洗掉了。這時候就可以使用 FlashBurn,

在 dvsdk 資料夾下可以搜尋到 DM643x Flashing Instructions.pdf,

照著 PDF 的步驟下載 FlashBurn DSK,即可把程式燒進 Flash,

每次把 DSP 電源開啟時就會跑自己燒進去的程式。


記得要呼叫這行 EVMDM6437_init();

因為利用 CCS 開發,startup 和 evmdm6437bsl.lib 會自動幫我們

初始一些環境,所以要手動加入,如果沒加這行有些功能就無法 work,

像是 UART。

2011年3月15日 星期二

[C/C++] extern 的用法

想要宣告一全域變數讓其他文件 (.c 或 .cpp) 共同使用,

必須在型態前加上 extern, 表示變數在文件之外, 如 :

int data; // 宣告在 file1.c

extern int data; // 則必須宣告在 flie1.h

當 file2.c 要使用變數時, 只要 include file1.h 即可,

不必再宣告該變數.

2011年2月15日 星期二

[Windows] 桌面上出現奇怪的IE圖示無法刪除

到 regedit 裡搜尋 {e17d4fc0-5564-11d1-83f2-00a0c90dc849}


這組機碼是負責搜尋功能的,大陸人很愛改寫這些機碼,


來綁架網頁和使搜尋功能壞掉,只要把這些機碼刪除,在重新修復即可。


修復搜索.reg