Pb10.5 中用API取打印机列表的问题

hard_blue 2008-04-06 11:27:07
一个打印模块,其实要用到API取 打印机纸张,打印机列表等信息
在PB9下是好用的
但升级到PB10.5下就不行了,取出来的全是乱码

下面是取打印机信息的代码

/*************************************************************************
* 函 数 名: of_GetPrinterList
* 访问级别: Public
* 参 数: 传址 返回打印机各项列表
*as_printerNameList[] -- 打印机名列表数组
*as_PortNameList[] -- 打印机端口名列表数组
*as_DriverNameList[] -- 打印机驱动名列表数组
*as_CommentList[] -- 打印机备注列表数组
* 返 回 值: Integer 成功--打印机个数,错误--返回-1
**************************************************************************
* 说 明: 获取所安装的打印机列表
**************************************************************************/

Blob lbl_pPrinterEnum, lbl_EachPrinterInfo
Long li_cbBufSize, ll_pcbNeeded, ll_pcReturned
String ls_Name
Integer li_StrLevel, li_i, li_PrinterNumber
str_PRINTER_INFO_2 lstr_PrinterInfo

SetNull (ls_Name)

//使用两步法枚举所有安装在本地机器上的打印机
l_GetPrinter:
lbl_pPrinterEnum = Blob(Space (li_cbBufSize))

If Not EnumPrinters( PRINTER_ENUM_LOCAL, ls_Name, 2, lbl_pPrinterEnum, &
li_cbBufSize, ll_pcbNeeded, ll_pcReturned ) Then
If li_cbBufSize < ll_pcbNeeded Then
li_cbBufSize = ll_pcbNeeded
Goto l_GetPrinter
End If
End If

//返回打印机的个数
li_PrinterNumber = ll_pcReturned

If li_PrinterNumber >= 1 Then
//将打印机各项信息保存到数组中
For li_i = 1 To li_PrinterNumber
lbl_EachPrinterInfo = BlobMid ( lbl_pPrinterEnum, (li_i -1 ) * PRINTER_INFO_2_LEN + 1 )
//将缓冲区信息拷贝到结构中
CopyMemory ( lstr_PrinterInfo, lbl_EachPrinterInfo, PRINTER_INFO_2_LEN )
//获取结构中的打印机名、端口名、驱动程序名和打印机备注列表
//注意:访问指定类型为long字符串时,需加上关键字"address"
as_printerNameList[li_i] = String ( lstr_PrinterInfo.PrinterName, "address" )
as_PortNameList[li_i] = String ( lstr_PrinterInfo.PortName, "address" )
as_DriverNameList[li_i] = String ( lstr_PrinterInfo.DriverName, "address" )
as_CommentList[li_i] = String ( lstr_PrinterInfo.Comment, "address" )
Next
Return li_PrinterNumber
Else
Return -1
End If


api声明

//获取或设置打印机API函数
FUNCTION Boolean EnumPrinters(Long flags, String Name, Long level, REF blob buffer, Long cbbuf, REF Long cbneeded, REF Long returned) LIBRARY "winspool.drv" alias for "EnumPrintersA"
FUNCTION Boolean EnumJobs (Long hPrinter, Long FirstJob, Long NoJobs, Long Level, REF Blob pJob, Long cdBuf, REF Long pcbNeeded, REF Long pcReturned ) LIBRARY "winspool.drv" ALIAS FOR "EnumJobsA"

FUNCTION Boolean OpenPrinter ( String PrinterName, REF Long hPrinter, Long SetToNULL) LIBRARY "winspool.drv" ALIAS FOR "OpenPrinterA"
FUNCTION Boolean OpenPrinter ( String PrinterName, REF Long hPrinter, REF str_printer_defaults SetToSetToNULL) LIBRARY "winspool.drv" ALIAS FOR "OpenPrinterA"
FUNCTION Boolean ClosePrinter ( Long hPrinter ) LIBRARY "winspool.drv"
FUNCTION Boolean GetPrinter ( Long hPrinter, Long level, REF blob PrinterInfo, Long cbbuf, REF Long cbNeeded ) LIBRARY "winspool.drv" ALIAS FOR "GetPrinterA"
FUNCTION Boolean SetPrinter ( Long hPrinter, Long level, REF blob PrinterInfo, Long command ) LIBRARY "winspool.drv" ALIAS FOR "SetPrinterA"
FUNCTION Boolean SetPrinter ( Long hPrinter, Long level, REF str_PRINTER_INFO_2 Printer, Long command ) LIBRARY "winspool.drv" ALIAS FOR "SetPrinterA"
FUNCTION Boolean PrinterProperties ( Long hwnd, Long hprinter ) LIBRARY "winspool.drv"

FUNCTION Boolean GetDefaultPrinter ( REF Blob pszBuffer, REF Long pcchBuffer ) LIBRARY "winspool.drv" ALIAS FOR "GetDefaultPrinterA"
FUNCTION Boolean SetDefaultPrinter( String pszPrinter ) LIBRARY "winspool.drv" ALIAS FOR "SetDefaultPrinterA"

//打印机属性函数
FUNCTION Long DocumentProperties(Long hwnd,Long hPrinter,String pDeviceName,REF str_DEVMODE pDevModeOutput,REF str_DEVMODE pDevModeInput,Long fMode) LIBRARY "winspool.drv" ALIAS FOR "DocumentPropertiesA"
FUNCTION Long DocumentProperties(Long hwnd,Long hPrinter,String pDeviceName,REF blob DevModeOutput,REF blob DevModeOutput,Long fMode) LIBRARY "winspool.drv" ALIAS FOR "DocumentPropertiesA"
FUNCTION Long DocumentProperties(Long hwnd,Long hPrinter,String pDeviceName,Long SetToNULL,Long SetToNULL,Long SetToNULL) LIBRARY "winspool.drv" ALIAS FOR "DocumentPropertiesA"
FUNCTION Long DocumentProperties ( Long hwnd, Long hprinter, String SetToNULL, REF blob DevModeOutput, Long SetToNULL_1, Long fMode ) LIBRARY "winspool.drv" ALIAS FOR "DocumentPropertiesA"
FUNCTION Long DocumentProperties ( Long hwnd, Long hprinter, String SetToNULL, Long SetToNULL_1, REF str_DEVMODE pDevModeInput , Long fMode ) LIBRARY "winspool.drv" ALIAS FOR "DocumentPropertiesA"
FUNCTION Long DocumentProperties ( Long hwnd, Long hprinter, String SetToNULL, Long SetToNULL_1, REF blob pDevModeInput , Long fMode ) LIBRARY "winspool.drv" ALIAS FOR "DocumentPropertiesA"

//设备性能函数
FUNCTION Long DeviceCapabilities( String lpDeviceName, String lpPort,Long fwCapability, REF str_POINT pOutput[200], Long devmode) LIBRARY "winspool.drv" ALIAS FOR "DeviceCapabilitiesA"
FUNCTION Long DeviceCapabilities( String lpDeviceName, String lpPort,Long fwCapability, REF Long pOutput[20], Long devmode) LIBRARY "winspool.drv" ALIAS FOR "DeviceCapabilitiesA"
FUNCTION Long DeviceCapabilities( String lpDeviceName, String lpPort,Long fwCapability, REF int pOutput[200], Long devmode) LIBRARY "winspool.drv" ALIAS FOR "DeviceCapabilitiesA"
FUNCTION Long DeviceCapabilities ( String Device, String Port, Long fwCapability, REF blob pOutput, Long SetToNULL ) LIBRARY "winspool.drv" ALIAS FOR "DeviceCapabilitiesA"
FUNCTION Long DeviceCapabilities ( String Device, String Port, Long fwCapability, Long SetToNULL, Long SetToNULL ) LIBRARY "winspool.drv" ALIAS FOR "DeviceCapabilitiesA"
FUNCTION Long DeviceCapabilities ( String Device, String Port, Long fwCapability, REF blob pOutput[], Long SetToNULL ) LIBRARY "winspool.drv" ALIAS FOR "DeviceCapabilitiesA"

//内存拷贝函数
FUNCTION Long CopyMemory ( REF str_POINT dest, REF Long source, Long length ) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"
FUNCTION Long CopyMemory ( REF str_PRINTER_INFO_1 dest, REF blob source, Long length ) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"
FUNCTION Long CopyMemory ( REF str_PRINTER_INFO_2 dest, REF blob source, Long length ) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"
FUNCTION Long CopyMemory ( REF blob source, REF str_PRINTER_INFO_2 dest, Long length ) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"
FUNCTION Long CopyMemory ( REF str_DEVMODE dest, REF blob src, Long length ) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"
FUNCTION Long CopyMemory ( REF str_DEVMODE dest, Long src, Long length ) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"
FUNCTION Long CopyMemory ( REF blob src, REF str_DEVMODE dest, Long length ) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"
FUNCTION Long CopyMemory ( Long src, REF str_DEVMODE dest, Long length ) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"
FUNCTION Long CopyMemory ( REF String dest, blob source, Long length ) LIBRARY "kernel32" ALIAS FOR "RtlMoveMemory"

//设备场景函数
FUNCTION Long CreateDC ( Long driver, String device, Long output, Long devmode ) LIBRARY "gdi32" ALIAS FOR "CreateDCA"
FUNCTION Long ResetDC(Long hdc,REF blob lpInitData) LIBRARY "gdi32.dll" ALIAS FOR "ResetDCA"
FUNCTION Boolean DeleteDC ( Long hdc ) LIBRARY "gdi32"

//Win.ini文件操作函数
FUNCTION Long GetProfileString( String lpAppName, String lpKeyName, String lpDefault, REF String lpReturnedString,Long nSize) LIBRARY "kernel32.dll" ALIAS FOR "GetProfileStringA"
FUNCTION Long WriteProfileString( String lpszSection, String lpszKeyName, String lpszString) LIBRARY "kernel32.dll" ALIAS FOR "WriteProfileStringA"

//发送消息函数
FUNCTION Long SendMessageTimeout ( Long hwnd, Long msg, Long wParam, Long lParam, Long fuFlags, Long uTimeout, Long lpdwResult) LIBRARY "user32" ALIAS FOR "SendMessageTimeoutA"



谁能给解决一下啊
...全文
762 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
langrs 2010-08-25
  • 打赏
  • 举报
回复
你好,我也碰到了同样的问题,经过查找资料后终于解决了。以下是从pb10的帮组中查到的内容
Handling ANSI data
Since this function does not have an encoding argument to allow you to specify the encoding of the data, the string returned can contain garbage characters if the data has ANSI encoding. You can handle this by converting the ANSI string returned from the String function to a Unicode blob, and then converting the ANSI string in the blob to a Unicode string, using the encoding parameters provided in the Blob and String
conversion functions:

ls_temp = String(long, "address" )

lb_blob = blob(ls_temp) //EncodingUTF16LE! is default

ls_result = string(lb_blob, EncodingANSI!)

出现乱码的原因是pb10和旧版本对ANSI的支持不一样导致的,你需要先转成blob,再转码成string就可以了
方法上面有解释了
hard_blue 2008-04-07
  • 打赏
  • 举报
回复
我加了;ANSI也一样不好用啊。。
pcwe2002 2008-04-06
  • 打赏
  • 举报
回复
PB10编码方式改变了,在函数后面加上;ansi
比如
FUNCTION Boolean OpenPrinter ( String PrinterName, REF Long hPrinter, Long SetToNULL) LIBRARY "winspool.drv" ALIAS FOR "OpenPrinterA;ansi"
hard_blue 2008-04-06
  • 打赏
  • 举报
回复
我再顶
shmilypiaoye 2008-04-06
  • 打赏
  • 举报
回复
FromUnicode 这个函数可能有用
shmilypiaoye 2008-04-06
  • 打赏
  • 举报
回复
PB9和PB10的字符编码好像不是同一种
hard_blue 2008-04-06
  • 打赏
  • 举报
回复
没人回答,自己顶一下

1,076

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 相关问题讨论
社区管理员
  • 基础类社区
  • WorldMobile
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧