DLL中GDIPlus初始化和退出時注意的問題

轉載的別人的文章,因爲自己也在實際工程中遇到類似的問題。

DLL工程中使用GDIPlus
2009-08-04 14:51

前段時間項目的需求有所更改,因此我將工程中部分GDI改爲了GDI+,主要是爲了增加圖片格式的支持,沒時間看東西了,而因爲我以前對於GDI+比較熟,所以就直接用GDI+了。

一開始在幾個工程的Dllmain中添加Gdiplus的GdiplusStartup和GdiplusShutdown,不過發現在生成時 註冊輸出一步老卡在那裏不動,必須手動取消生成。在使用上倒沒遇到什麼大問題,因此也就暫時扔那裏了。

今天工作暫告一段落,就來分析這個問題。首先定位到是gdiplus引起的,而後搜索了不少資料,發現是對於GDI+的初始化有問題,在dll工程中不能在dllmain文件中進行GDI+的初始化。

詳見http://msdn.microsoft.com/en-us/library/ms534077(VS.85).aspx

Remarks 

You must call GdiplusStartup before you create any GDI+ objects, and you must delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown. 

You can call GdiplusStartup on one thread and call GdiplusShutdown on another thread as long as you delete all of your GDI+ objects (or have them go out of scope) before you call GdiplusShutdown. 

Do not call GdiplusStartup or GdiplusShutdown in DllMain or in any function that is called by DllMain. If you want to create a DLL that uses GDI+, you should use one of the following techniques to initialize GDI+: 


Require your clients to call GdiplusStartup before they call the functions in your DLL and to call GdiplusShutdown when they have finished using your DLL. 
Export your own startup function that calls GdiplusStartup and your own shutdown function that calls GdiplusShutdown. Require your clients to call your startup function before they call other functions in your DLL and to call your shutdown function when they have finished using your DLL. 
Call GdiplusStartup and GdiplusShutdown in each of your functions that make GDI+ calls.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章