不同 include file 但同一個結構名稱需注意事項

第一個是要注意 struct 的定義要相同。


比如說 a.exe 中有個 struct abc,然後在 b.dll 中也有個 struct abc。其中 a.exe 會呼叫 b.dll 的一個函式,並帶入 struct abc 在 b.dll 中修改完畢後回傳,這時候必需注意


a.exe 與 b.dll 對 struct abc 的定義必需相同


因為回傳的結構值基本上就是在記憶體,然後使用 a.exe 中的定義來 parse。假如說 a.exe 中的定義是


struct abc


{


   int n1;


   int n2;


   unsigned short n3;


};


但 b.dll 中的定義卻是


struct abc


{


   int n1;


   unsigned short n2;


   unsigned short n3;


};


這時候在 a.exe 的回值內容,n2 佔了 4 bytes (32 bits),就會把在 b.dll 中的 n2/n3 給吃掉了。那麼在這例中的 a.exe 的 n3,就是會 undefined value。


而第二個要注意的,是 VS 設定中的 struct member alignment (在 C/C++ -> Code Generation)。預設的值是 8 bytes alignment。如果在專案中這個值定義不同的話,那就有可能在跳不同專案程式時,會出現值 mapping 錯誤的問題。


所以,無特殊需求的話,這個設定還是都把它設成 default 吧!


留言

熱門文章