2013年3月31日 星期日

C/C++ : Data structure alignment

在宣告資料結構的時候,ARM 或是 某些編譯器會自動做對齊,但是對齊的方式是每 4 Byte 做一個單位 ( entries )。這樣很多空間都會在對齊的時候浪費掉。對於現在都是以 Giga 等級的記憶體作為應用的美好時代來說當然沒有差。但是如果有一天真的要到對記憶體錙銖必較的時候就要知道這種用法。

在struct的前後加上 ...



#pragma pack(push) /* push current alignment to stack */
#pragma pack(1) /* set alignment to 1 byte boundary */
typedef struct _test
{
    char ch0; //byte 1
    char ch1; //byte 1
    int in; //byte 4
    short sh; //byte 2
}test;
#pragma pack(pop) /* restore original alignment from stack */


參考資料:

1. http://changeway.pixnet.net/blog/post/7669656-data-structure-alignment
2. http://en.wikipedia.org/wiki/Data_structure_alignment

2013年3月20日 星期三

C# - 如何取得環境變數


string os = Environment.GetEnvironmentVariable("OS");

資料來源:http://richielin-programer.blogspot.tw/2008/04/c.html

Windows 7 環境參數列表

最近小素需要控制到 Windows 下的環境參數,因此稍微筆記一下。

%ALLUSERSPROFILE%:All Users的資料夾路徑。
%APPDATA%:目前使用者的Application Data資料夾路徑。
%CommonProgramFiles%:Common Files的資料夾路徑。
%COMPUTERNAME%:電腦名稱。
%ComSpec%::命令提示字元的路徑。
%HOMEDRIVER%:使用者目錄的磁碟機。
%HOMEPATH%:使用者家目錄。
%LOCALAPPDATA%:目前使用者的Local資料夾
%LOGONSERVER%:目前使用者所登入的網路控制器名稱。
%NUMBER_OF_PROCESSORS%:CPU的處理器數量。
%OS%:作業系統名稱。
%Path%:執行檔的搜尋路徑。
%PATHEXT%:執行檔的副檔名。
%PROCESSOR_ARCHITECTURE%:處理器的架構名稱,如x86。
%PROCESSOR_IDENTIFIER%:處理器的文字。
%PROCESSOR_LEVEL%:處理器的model number。
%PROCESSOR_REVISION%:處理器的Revision number。
%ProgramData%:與%ALLUSERSPROFILE%相同。
%ProgramFiles%:應用程式目錄,預設是C:\Program Files。
%PROMPT%:目前解譯程式的命令提示字串。
%PSModulePath%:PowerShell Module的路徑。
%PUBLIC%:公用資料夾的路徑,預設為C:\Users\Public
%SESSIONNAME%:連上終端伺服器的Session names。
%SystemDrive%:系統磁碟機,預設為C:。
%SystemRoot%:系統磁碟機的根目錄,預設為C:\Windows。
%Temp%:暫存資料夾。
%Tmp%:暫存資料夾。
%USERDNSDOMAIN%:DNS網域名稱。
%USERDOMAIN%:Netbios網域名稱。
%USERNAME%:目前使用者的帳號。
%USERPROFILE%:目前使用者的資料夾。
%windir%:系統磁碟機上的Windows資料夾,預設為C:\Windows。

資料來源:http://mrynlin.blogspot.tw/2012/04/windows-7.html

2013年3月18日 星期一

C# - 如何在靜態函數(static function)中呼叫表單物件?

為了要讓C# 裡面的 static function 可以存取表單裡面的物件需要...
1. 先把表單的 instance 傳進 thread 中。
2. 當 thread 收到這個 instance的時候再傳進 Static function。
3. Static function 收到後利用這個 instance 呼叫一個中繼的非 static function。
4. 這個中繼的非 static function 再利用委任(BeginInvoke) 的方法呼叫一個非靜態的 function 去更改表單物件。

PS. 繞了半天...終於成功了。一開始靜態函數無法直接更改所以改用委任。但是因為是靜態函數所以無法呼叫 BeginInvoke 。所以先藉由外部傳進來的 instace 呼叫一個非靜態的函數來委任。網路上一般都會利用傳event的方法或也可以再做一隻Thread 不斷的去更新。忽然靈機一動...把委任跟把 thread 傳進 instance的方法結合在一起終於成功。

namespace ShowStringDemo

{
    public partial class Demoform : Form

    {

        /* function */

        delegate void ShowDataToFormDelegate(string show_string);

        ShowDataToFormDelegate ShowDataToFormDelegateFunc;   

        /* thread define */

        public Thread DemoThread;

        public Demoform()

        {

            InitializeComponent();         

        }

        private void Demoform_Load(object sender, EventArgs e)

        {

            DemoThread= new Thread(new ParameterizedThreadStart(DemoThreadMain));

            DemoThreadMain.Start(this);

        }

        public static void DemoThreadMain(object sender)

        {

            while(true)

            {

                StaticShow(sender);

                Thread.sleep(500);

            }

        }

        public static void StaticShow(object sender)

        {

            Demoform instance = ( Demoform ) sender;

            instance.ShowStates("test");

        }

        public void ShowStates(string show_string)

        {

            ShowDataToFormDelegateFunc = new ShowDataToFormDelegate(OutputToLabStatus);

            BeginInvoke(ShowDataToFormDelegateFunc, show_string);

        }

        void OutputToLabStatus(string show_string)

        {

            LabStatus.Text = show_string;

        }

    }

}

參考資料 : http://social.msdn.microsoft.com/Forums/zh-TW/233/thread/5cabca0a-b8cc-4aaf-a77e-c45cb309f5db/