Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pass struct to WinAPI

Status
Not open for further replies.

JimmyK

Programmer
Sep 8, 2000
142
VN
Some winapi functions ' parameters is struct. How can i invoke that kind of function in visual Fox?

Tnanks

Jimmy
[sig][/sig]
 
Example 1
I am calling Win32Api kernel32.dll function [red]WinExec[/red] in the return line

lparameters pcExeName, pcParameters, [red]pnWindowStat[/red]
* first parameter required, other - optional
if Parameters() < 2.5 .or. type(&quot;pnWindowStat&quot;) != &quot;N&quot; .or. empty(pnWindowStat)
pnWindowStat = 1 && see Win32Api function ShowWindow for values of pnWindowStat
endif
local lcRunString
[red]m.lcRunString[/red] = '&quot;'+ allt(m.pcExeName) + '&quot;' + iif(parameters() > 1 , + &quot; &quot; + m.pcParameters , &quot;&quot;)
[red]declare integer WinExec in kernel32 string lpCmdLine, integer uCmdShow[/red]
return [red]WinExec(m.lcRunString, m.pnWindowStat)[/red] > 31
endproc

Example 2
Here I'm call Win32Api advapi32.dll function [red]RegOpenKey[/red]
*procedure IsFileInReg
lparameters pcExeName
* returns fill path of EXE file taking it from Windows registry, or empty string
if empty(m.pcExeName) .or. type(&quot;m.pcExeName&quot;) != &quot;C&quot; .or. parameter() < 0.5
return ' '
endif
m.pcExeName = justfname(m.pcExeName)
...more code

local lnErrorRes, phkResult, lpSubKey, lpValue, lpcbValue
#define [red]HKEY_LOCAL_MACHINE[/red] -2147483646 && bitset(0,31)+2
[red]phkResult[/red] = 0
[red]lpSubKey[/red] = 'SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths'
[red]lpValue[/red] = .null.
[red]declare long RegOpenKey in advapi32 long hKey, string lpSubKey, long @phkResult[/red]
lnErrorRes =[red] RegOpenKey(HKEY_LOCAL_MACHINE, lpSubKey, @phkResult)[/red]
if lnErrorRes # 0 or phkResult = 0 && Cannot open registry key - unknown error
= messagebox('Cannot open Windows registry.')
return ''
endif
... more code

Does This Help You ? [sig]<p>David W. Grewe<br><a href=mailto:Dave@internationalbid.net>Dave@internationalbid.net</a><br>[/sig]
 
Hi Jimmy,
IMO, you've just stumbled on one of VFP's few weaknesses. A struct, AISI, is nothing more than sequentially allocated memory space.
Handling structs from VFP requires creating a &quot;buffer&quot; to return the values, passing that buffer to the function by reference, and parsing the values out of that buffer. Here's an example of the GetSystemTime function:

DECLARE GetSystemTime IN WIN32API STRING @lcBuffer

*!* typedef struct _SYSTEMTIME {
*!* WORD wYear; && 2 bytes
*!* WORD wMonth; && 2 bytes
*!* WORD wDayOfWeek; && 2 bytes
*!* WORD wDay; && 2 bytes
*!* WORD wHour; && 2 bytes
*!* WORD wMinute; && 2 bytes
*!* WORD wSecond; && 2 bytes
*!* WORD wMilliseconds; && 2 bytes
*!* } SYSTEMTIME, *PSYSTEMTIME;

lcBuffer=SPACE(16) && total struct length is 16 bytes

GetSystemTime(@lcBuffer)

lcYear=Str2Word(SUBSTR(lcBuffer,1,2))
lcMonth=Str2Word(SUBSTR(lcBuffer,3,2))
lcDOW=Str2Word(SUBSTR(lcBuffer,5,2))
lcDay=Str2Word(SUBSTR(lcBuffer,7,2))
lcHour=Str2Word(SUBSTR(lcBuffer,9,2))
lcMinute=Str2Word(SUBSTR(lcBuffer,11,2))
lcSecond=Str2Word(SUBSTR(lcBuffer,13,2))
lcMilli=Str2Word(SUBSTR(lcBuffer,15,2))

*********************
FUNCTION Str2Word
*********************

PARAMETERS m.wordstr

PRIVATE i, m.retval

m.retval = 0
FOR i = 0 TO 8 STEP 8
m.retval = m.retval + (ASC(m.wordstr) * (2^i))
m.wordstr = RIGHT(m.wordstr, LEN(m.wordstr) - 1)
NEXT
RETURN m.retval

Str2Word is used to convert the returned value to WORD data type. [sig]<p>Jon Hawkins<br><a href=mailto: jonscott8@yahoo.com> jonscott8@yahoo.com</a><br><a href= > </a><br>Carpe Diem! - Seize the Day![/sig]
 
Thanks for all respones,
Jon's example is exactly what i wanted
happy.gif
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top