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.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103

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) 

Comments

Added StrZCopy method that allows to copy a zero terminated string from a Win API structure.