Solved

VFP and API

Posted on 2010-08-25
11
1,484 Views
Last Modified: 2012-05-10
Hi All,

I have recently started programming in VFP (OOP). I'm stuck with a few things at the moment and would need your help.

How to use WMI Classes with VFP? (Event Classes, Monitor Classes, etc) with an example

How to use Win32 API with VFP? with an example.

   To be honest, I have no idea about Win32 API at all. Could you please redirect me to a link which has good information about
   Win32 API?

Thanks!
SP


0
Comment
Question by:Nick Sui
  • 4
  • 4
  • 3
11 Comments
 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
WMI
===

a Short sample:

strComputer = "."
objSWbemServices = GetObject("winmgmts:\\" + strComputer)
colServices = objSWbemServices.InstancesOf("Win32_Service")

* or use WQL, eg
* colServices = objSWbemServices.ExecQuery("Select * From Win32_Service Where Name Like '%sql%'")

For Each objService In colServices
    ? objService.Name
    ? objService.DisplayName
    ? objService.Description
    ? objService.PathName
    ? objService.StartMode
    ? objService.State
    ? objService.AcceptStop
    ? objService.AcceptPause
Next

You can adapt scripts in the WMI script repository at http://technet.microsoft.com/en-us/scriptcenter/default.aspx (search for WMI), WMI scripting primer http://technet.microsoft.com/en-us/library/ee156560.aspx

In regard to events: http://msdn.microsoft.com/en-us/library/aa392396(v=VS.85).aspx
An overview over WMI classes: http://msdn.microsoft.com/en-us/library/aa394554(VS.85).aspx

You find tons of references googling.


Win API
======

The command supporting the use of C functions in DLLs is DECLARE, A very good resource for Win API declarations and sample code in foxpro is http://www.news2news.com/vfp/index.php

Bye, Olaf.

0
 
LVL 27

Expert Comment

by:CaptainCyril
Comment Utility
There are samples in solution.app that ship with FoxPro.
0
 
LVL 29

Expert Comment

by:Olaf Doschke
Comment Utility
True, Cyril.

While the Windows API "chapter" of solutions is far from being complete, it has nice samples on how to use API functions needing a struct or a pointer to an array.

@SystmProg - you find the solution samples via tools -> task pane, it's a toolbar button there. It also can answer many other basic foxpro questions.

Bye, Olaf.
0
 
LVL 35

Author Comment

by:Nick Sui
Comment Utility
Well, what I need on Win32 API is a Guide which explains:

    What a Win32 API is?
    What a DLL is?
    How to use DLL Functions?

I think the question is not related to VFP in anyway but I would still a link or a book if you can suggest.

Thanks!
SP
0
 
LVL 27

Expert Comment

by:CaptainCyril
Comment Utility
An API is Application Programming Interface

http://www.google.com/search?hl=en&rlz=1C1GPCK_enLB349LB359&defl=en&q=define:Api&sa=X&ei=YA11TK-WJJLW4gbkmczTBg&ved=0CBYQkAE

Windows offers a lot of API functions which some are in the form of DLL (Dynamic Link Library) to save space for applications and time in development.

You need to DECLARE an API in FoxPro to be able to use it as if you are calling a native/local function.

You don't need a book. You just need to know what it is and how to search for one.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 35

Author Comment

by:Nick Sui
Comment Utility
Cyril, You're right - I don't need a book...but how about a beginner coding a program which utilizes APIs?

The issue is that I don't understand the parameters associated with an API, for example; I don't know what a hWND is and how touse it.

For example: (I copied this from news2news.com)

DECLARE INTEGER GetMonitorContrast IN dxva2;
    INTEGER hMonitor,;
    LONG @pdwMinimumContrast,;
    LONG @pdwCurrentContrast,;
    LONG @pdwMaximumContrast

it says "GetMonitorContrast" IN dxva2 - and next it says INTEGER hMonitor, LONG @pdwMinimumContrast , etc..

the question is/are:

    Is dxva2 is a DLL which contains GetMonitorContrast function and hMonitor is a parameter or @pdwMinimumContrast is a parameter?

   How do I know this function is defined in this DLL and I would need to DECLARE in this DLL? Is there any list of DLLs with functions/paras associated with it?

   When using a Function from a Windows DLL, we would need to know the parameters associated with it - once we know we need to use them - the question is how we use them in VFP? is it same as storing a value in the variable? e.g. in this case; MyMonitorMinContrast = GetMonitorContrast.

I hope you're clear on what I want...

Thanks!
SP

   
0
 
LVL 27

Assisted Solution

by:CaptainCyril
CaptainCyril earned 200 total points
Comment Utility
dxva2 is most probably a dll in your hard disk.

The API info you find with the hardware manual or online. I usually search for Windows API info online. I used to have a 12MB manual of all API calls but since there was the internet, it is easier and faster and usually you find sample code as well.

It is very important to know what type of string. No need to be concerned if Integer or Double or Numeric since FoxPro can treat them the same. Just be concerned with Numeric, String.

You need to declare the function once and you can call it as many times as you wish.

here is a sample


FUNCTION WindowsDirectory
LOCAL cBuff, nLen
DECLARE INTEGER GetWindowsDirectory IN kernel32 STRING @, INTEGER
cBuff = SPACE(255)
nLen = GetWindowsDirectory(@cBuff,LEN(cBuff))
CLEAR DLLS
RETURN ALLTRIM(LEFT(cBuff,nLen))
0
 
LVL 27

Expert Comment

by:CaptainCyril
Comment Utility
hwnd is a Window handler just like a file handler. You can get is from oForm.hwnd where oForm is the variable to your form.
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 300 total points
Comment Utility
"a Win32 API" - you seem to misunderstand that there are many Win32 APIs. There is only one Win32 API. Win32 API is standing for all C functions of Windows DLLs, there surely are more APIs in windows, eg WMI is a different API and there are much more things windows offers as interface to make use of it. Cyril already pointed you towards several definitions of the term API. Win32API is a API, but despite of the different versions of it because of the different windows versions, there is only one Win32 API.

Win32 API is naming all the functions windows offers to any programming language to make use of in their applications. By using foxpro you already use many of the API functions indirectly, eg a foxpro form is a windows form and is created by the os with many Win API functions.

If you like you won't need many of the functions windows offers and can do applications with the foxpro language alone, but there surely are things worth investigating, no question.

The official documentation is from the creator of windows and it's API: Microsoft. It's called the MSDN Library. For example you find Microsofts description of the GetMonitorContrast Function here: http://msdn.microsoft.com/en-us/library/dd692942(VS.85).aspx

In regard of how to know what hMonitor is: In general a function is part of a family of functions of a DLLs of windows. MSDN often does better describe the parameters and their meaning, constants and structues involved etc. For example the meaning of hMonitor is described by both news2news and MSDN:

Handle to a physical monitor. To get the monitor handle, call GetPhysicalMonitorsFromHMONITOR or GetPhysicalMonitorsFromIDirect3DDevice9.

So this simply is the result of another API function. There are certain naming conventions and nomenclatures, eg a parameter beginning with h typically is a handle, hwnd is a handle of a window. a handle typically is an integer value standing representive for an object instead of an object reference to the object adress in memory.

And from the DELARE perspective the semantics of the command are as the foxpro help says:

DECLARE [cFunctionType] FunctionName IN LibraryName [AS AliasName] [cParamType1 [@] ParamName1, cParamType2 [@] ParamName2, ...]

So you define the type of the function result, then the function name within the DLL and then the list of parameters and their type, and if they are by value or referential parameters.

Yes, there are many things and terms to learn, but you can and should perhaps keep it to a minimum of what you really need additional to what foxpro offers itself. GetMonitorContrast for example has Vista or Win2008 Server as minimum requirement and even though a VFP9 application with the vfp9 runtime can run on win98, me, xp, vista and win7, you limit it on Vista or win7 by making use of that function.

DECLARE is also capable to declare functions from other DLLs not part of Windows, not part of the Win32 API. There are many third party vendors of DLLs, commercial and open source. DECLARE can make use of such DLLs with c functions, in fact it's not important the function is written in C/C++, it can also be a pascal DLL, as long as it's a functional DLL in contrast to a DLL with COM Servers in it or an .NET Assembly DLL.

I surely opened a can of worms now, keep calm and just see the general idea of API is to enable external programs to make use of functions and to extend capabilities of a language or product. You can but not necessarily need to make use of that. DECLARE is one way out of foxpro to include functions from DLLs, CREATEOBJECT(Oleclassname) is another way to make use of OLE classes, COM Servers in DLLs or OCX files, There are further things like Webservices and many more more to extend foxpro.

Put into one word this is interoperability. Don't complain there is no documentation or reference to it, you could also explain there is no book explaining life. ;)

Bye, Olaf.
0
 
LVL 35

Author Comment

by:Nick Sui
Comment Utility
A very clear and nice explanation on what is what!!!!!

You know your stuff, Olaf :)

>>>I surely opened a can of worms now, keep calm and just see the general idea of API is to enable external programs to make use of functions and to extend capabilities of a language or product.

I concur...

Thanks!
SP
0
 
LVL 35

Author Closing Comment

by:Nick Sui
Comment Utility
Thanks!
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: http://msdn.microsoft.com/en-us/vfoxpro/default.aspx (http://msd…
Learn to move / copy / export exchange contacts to iPhone without using any software. Also see the issues in configuration of exchange with iPhone to migrate contacts.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now