DLL中GDIPlus初始化和退出时注意的问题

39 篇文章 1 订阅
转载的别人的文章,因为自己也在实际工程中遇到类似的问题。

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.
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
GdiPlus是一个Windows图形库,可以用于处理图形、图像的显示、打印等操作。使用Golang调用GdiPlus dll打印可以按照以下步骤进行: 1. 导入"syscall"包,使用"LoadLibrary"函数加载GdiPlus.dll库文件。 ```go import ( "syscall" ) var ( gdiplus = syscall.NewLazyDLL("GdiPlus.dll") ) ``` 2. 使用"GetProcAddress"函数获取所需函数的地址。 ```go var ( gdiplusStartup = gdiplus.NewProc("GdiplusStartup") gdiplusShutdown = gdiplus.NewProc("GdiplusShutdown") gdiplusCreateFont = gdiplus.NewProc("GdipCreateFontFamilyFromName") gdiplusGetDC = gdiplus.NewProc("GdipGetDC") gdiplusReleaseDC = gdiplus.NewProc("GdipReleaseDC") gdiplusPrint = gdiplus.NewProc("GdipPrint") // 其他函数... ) ``` 3. 调用"GdiplusStartup"函数初始化GdiPlus,传入指针类型的"ULONG_PTR"变量作为输出参数。 ```go var token uintptr gdiplusStartup.Call(uintptr(unsafe.Pointer(&token)), 0, 0) defer gdiplusShutdown.Call(token) ``` 4. 使用"GdipCreateFontFamilyFromName"函数创建字体,该函数需要传入字体名称、语言、字体句柄等参数。 ```go var ( fontName = "Arial" lang = "en-US" fontHdc uintptr fontPtr uintptr ) gdiplusCreateFont.Call( uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(fontName))), uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lang))), uintptr(unsafe.Pointer(&fontPtr)), ) defer gdiplusReleaseFont.Call(fontPtr) ``` 5. 使用"GdipGetDC"函数获取打印机设备上下文句柄。 ```go var printerName = "Microsoft Print to PDF" var hdc uintptr gdiplusGetDC.Call( uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(printerName))), uintptr(unsafe.Pointer(&hdc)), ) defer gdiplusReleaseDC.Call(hdc) ``` 6. 使用"GdipPrint"函数将图像打印到指定设备上下文。 ```go gdiplusPrint.Call( hdc, uintptr(unsafe.Pointer(&image)), uintptr(width), uintptr(height), uintptr(x), uintptr(y), ) ``` 以上步骤仅供参考,具体操作需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值