同系列文章:
注意:
- 针对使用C/C++版本的FaceId.dll
- 本代码仅适合PowerBuilder 9.0及以下版本
- 代码仅供参考,需要根据具体的设备做调整
运行环境
下载安装Visual C++ Redistributable Packages for Visual Studio 2013
演示程序源代码下载:
程序界面:
示例代码(版本:PowerBuilder 9.0)
全局外部函数声明
FUNCTION ULong FaceId_Start(ref Blob Address, UInt Port, Int AddressFamily) LIBRARY "FaceId.dll" FUNCTION Long FaceId_SetSecretKey(ULong SocketHandle, ref Blob SecretKey) LIBRARY "FaceId.dll" FUNCTION ULong FaceId_GetSecretKey(ULong SocketHandle) LIBRARY "FaceId.dll" FUNCTION Long FaceId_Execute(ULong SocketHandle, ref Blob InBuffer, ref ULong hOutBuffer, ULong CodePage) LIBRARY "FaceId.dll" SUBROUTINE FaceId_Close(ULong SocketHandle) LIBRARY "FaceId.dll" FUNCTION Long MultiByteToWideChar(ULong CodePage, ULong dwFlags, ref String lpMultiByteStr, Long cbMultiByte, ref Blob lpWideCharStr, Long cchWideChar) LIBRARY "kernel32.dll" FUNCTION Long WideCharToMultiByte(ULong CodePage, ULong dwFlags, ULong lpWideCharStr, Long cchWideChar, ref String lpMultiByteStr, Long cbMultiByte, ref String lpDefaultChar, ref Boolean lpUsedDefaultChar) LIBRARY "kernel32.dll"
全局变量定义
constant ULong DeviceCodePage = 936
公共函数定义
forward prototypes public function blob MultiByteStringToWideCharBlob(ref string source, unsignedlong codepage) public function string WideCharBlobToMultiByteString(unsignedlong source, unsignedlong codepage) end prototypes public function blob MultiByteStringToWideCharBlob(ref string Source, unsignedlong Codepage);Blob lpWideCharStr SetNull(lpWideCharStr) Long cchWideChar cchWideChar = MultiByteToWideChar(CodePage, 0, ref Source, -1, ref lpWideCharStr, 0) IF cchWideChar > 0 THEN String Buffer Buffer = space(cchWideChar * 2) lpWideCharStr = Blob(Buffer) MultiByteToWideChar(CodePage, 0, ref Source, -1, ref lpWideCharStr, cchWideChar) END IF return lpWideCharStr end function public function string WideCharBlobToMultiByteString(unsignedlong Source, unsignedlong Codepage);String lpMultiByteStr SetNull(lpMultiByteStr) String lpDefaultChar SetNull(lpDefaultChar) Boolean lpUsedDefaultChar lpUsedDefaultChar = false Long cbMultiByte cbMultiByte = WideCharToMultiByte(CodePage, 0, Source, -1, ref lpMultiByteStr, 0, ref lpDefaultChar, ref lpUsedDefaultChar) IF cbMultiByte > 0 THEN lpMultiByteStr = Space(cbMultiByte) WideCharToMultiByte(CodePage, 0, Source, -1, ref lpMultiByteStr, cbMultiByte, ref lpDefaultChar, ref lpUsedDefaultChar) END IF return lpMultiByteStr end function
清空命令按钮clicked()事件
mle_answer.Text = ''
执行命令按钮clicked()事件
// 设备地址 Blob DeviceIP DeviceIP = MultiByteStringToWideCharBlob(ref sle_deviceip.Text, DeviceCodePage) // 设备端口 Integer DevicePort DevicePort = Integer(sle_deviceport.Text) // 连接设备 ULong SocketHandle SocketHandle = FaceId_Start(ref DeviceIP, DevicePort, 2) IF SocketHandle = 0 THEN MessageBox('警告', '请输入正确的设备地址和通信端口!', Exclamation!, OK!) RETURN ELSE // 注意:通信密码要和设备中的密码一致 String AnsiKey AnsiKey = '' Blob SecretKey SecretKey = MultiByteStringToWideCharBlob(ref AnsiKey, DeviceCodePage) IF FaceId_SetSecretKey(SocketHandle, ref SecretKey) <> 0 THEN FaceId_Close(SocketHandle) MessageBox('警告', '设置通信密码失败!', Exclamation!, OK!) RETURN END IF END IF // 清空原有文本 mle_answer.Text = '' // 获取命令 Blob Command Command = MultiByteStringToWideCharBlob(ref sle_command.Text, DeviceCodePage) // 执行命令 ULong hOutBuffer Long ReceivedChars ReceivedChars = FaceId_Execute(SocketHandle, ref Command, ref hOutBuffer, DeviceCodePage) IF ReceivedChars > 0 THEN String Answer Answer = WideCharBlobToMultiByteString(hOutBuffer, DeviceCodePage) mle_answer.Text = Answer ELSE MessageBox('警告', '执行命令失败!', Exclamation!, OK!) END IF // 断开连接 FaceId_Close(SocketHandle)