Windows API support class
The class below simplifies use of Windows API function by providing methods to convert data between VFP types and Windows API types/structures
Most of the code was borrowed from Heap allocation class (clsheap) by late Ed Rauh.
| This is sample code. Add error handling and adjust to your requirements as necessary. |
DEFINE CLASS WinApiSupport AS Custom * Converts VFP number to the Long integer FUNCTION Num2Long(tnNum) LOCAL lcStringl lcString = SPACE(4) =RtlPL2PS(@lcString, BITOR(tnNum,0), 4) RETURN lcString ENDFUNC * Convert Long integer into VFP numeric variable FUNCTION Long2Num(tcLong) LOCAL lnNum lnNum = 0 = RtlS2PL(@lnNum, tcLong, 4) RETURN lnNum ENDFUNC * Return Number from a pointer to DWORD FUNCTION Long2NumFromBuffer(tnPointer) LOCAL lnNum lnNum = 0 = RtlP2PL(@lnNum, tnPointer, 4) RETURN lnNum ENDFUNC * Convert Short integer into VFP numeric variable FUNCTION Short2Num(tcLong) LOCAL lnNum lnNum = 0 = RtlS2PL(@lnNum, tcLong, 2) RETURN lnNum ENDFUNC * Retrieve zero-terminated string from a buffer into VFP variable FUNCTION StrZFromBuffer(tnPointer) LOCAL lcStr, lnStrPointer * Allocate 4KB buffer. * Increase the size of the buffer if string could be longer * or use lstrlen to get actual length of the string lcStr = SPACE(4096) lnStrPointer = 0 = RtlP2PL(@lnStrPointer, tnPointer, 4) lstrcpy(@lcStr, lnStrPointer) RETURN LEFT(lcStr, AT(CHR(0),lcStr)-1) ENDFUNC * Return a string from a pointer to LPWString (Unicode string) FUNCTION StrZFromBufferW(tnPointer) Local lcResult, lnStrPointer, lnLen lnStrPointer = This.Long2NumFromBuffer(tnPointer) lnLen = lstrlenW(lnStrPointer) * 2 lcResult = Replicate(chr(0), lnLen) = RtlP2PS(@lcResult, lnStrPointer, lnLen) lcResult = StrConv(StrConv(lcResult, 6), 2) RETURN lcResult ENDFUNC * Retrieve zero-terminated string FUNCTION StrZCopy(tnPointer) LOCAL lcStr, lnStrPointer * Allocate 4KB buffer. * Increase the size of the buffer if string could be longer * or use lstrlen to get actual length of the string lcStr = SPACE(4096) lstrcpy(@lcStr, tnPointer) RETURN LEFT(lcStr, AT(CHR(0),lcStr)-1) ENDFUNC ENDDEFINE *------------------------------------------------------------------------ FUNCTION RtlPL2PS(tcDest, tnSrc, tnLen) DECLARE RtlMoveMemory IN WIN32API AS RtlPL2PS STRING @Dest, Long @Source, Long Length RETURN RtlPL2PS(@tcDest, tnSrc, tnLen) FUNCTION RtlS2PL(tnDest, tcSrc, tnLen) DECLARE RtlMoveMemory IN WIN32API AS RtlS2PL Long @Dest, String Source, Long Length RETURN RtlS2PL(@tnDest, @tcSrc, tnLen) FUNCTION RtlP2PL(tnDest, tnSrc, tnLen) DECLARE RtlMoveMemory IN WIN32API AS RtlP2PL Long @Dest, Long Source, Long Length RETURN RtlP2PL(@tnDest, tnSrc, tnLen) FUNCTION RtlP2PS(tcDest, tnSrc, tnLen) DECLARE RtlMoveMemory IN WIN32API AS RtlP2PS STRING @Dest, Long Source, Long Length RETURN RtlP2PS(@tcDest, tnSrc, tnLen) FUNCTION lstrcpy (tcDest, tnSrc) DECLARE lstrcpy IN WIN32API STRING @lpstring1, INTEGER lpstring2 RETURN lstrcpy (@tcDest, tnSrc) FUNCTION lstrlenW(tnSrc) DECLARE Long lstrlenW IN WIN32API Long src RETURN lstrlenW(tnSrc) FUNCTION lstrlen(tnSrc) DECLARE Long lstrlen IN WIN32API Long src RETURN lstrlen(tnSrc)
Added StrZCopy method
Added StrZCopy method that allows to copy a zero terminated string from a Win API structure.