Using Win32 functions in Visual FoxPro Image Gallery
Code examples:
How to change display settings: screen resolution, screen refresh rate
Adding and deleting Scheduled Tasks using NetScheduleJob API functions
Custom GDI+ class
Converting Unicode data from the Clipboard to a character string using a given code page
Capturing keyboard activity of another application with the Raw Input API (VFP9)
Enumerating data formats currently available on the clipboard
Winsock: sending email messages (SMTP, port 25)
Detecting changes in connections to removable drives (VFP9)
Custom FTP Class for Visual FoxPro application
Splash Screen for the VFP application
How to download a file from the FTP server using FtpGetFile
How to activate Windows Calculator
Mapping and disconnecting network drives
Custom HttpRequest class (WinHTTP)
How to convert a bitmap file to monochrome format (1 bpp)
Enumerating raw input devices attached to the system (keyboard, mouse, human interface device)
Establishing connection using the SQLDriverConnect
Using Font and Text functions
GDI+: copying to the Clipboard (a) image of active FoxPro window/form, (b) image file
Displaying bitmap using the AlphaBlend function
Custom HttpRequest class (WinINet)
Using WM_COPYDATA for interprocess communication (VFP9)
Extensible Storage Engine class library
How to play AVI file on the _screen
How to assemble an array of strings and pass it to external function

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
More code examples    Listed functions    Add comment     W32 Constants      Translate this page Printer friendly version of this code sample
Versions:
click to open
Before you begin:
The PathFindOnPath function is anything but special. Whatever it does, can be accomplished by using FILE() and GETENV() FoxPro functions. Though it gives me a chance to demonstrate a technique of assembling an array of strings for being passed to external function.

More comments below the source code.
 
using System;
using System.Text;
using System.Runtime.InteropServices;
 
namespace WinAPI.SearchPath
{
    class Class1
    {
        /*
        EntryPoint="PathFindOnPathA" means that the function expects 
        strings in one byte per character format. PathFindOnPathW must 
        be used otherwise.
 
        System.Text.StringBuilder Class represents a string-like object.
        Its value can be modified once it has been created 
        by appending, removing, replacing, or inserting characters. 
        In the code below the upper limit of 260 characters is set 
        for the new instance of the StringBuilder.
 
        The last member of dirs, the array that stores directory names,
        must be null. Omitting this, triggers an exception, 
        System.NullReferenceException, if the file name is not found.
         */
 
        const int MAX_PATH = 260;
 
        [STAThread]
        static void Main()
        {
            StringBuilder f =  //file to be searched
                new StringBuilder("test.txt", MAX_PATH);
 
            //directories to be searched first
            string[] dirs = {"c:\\dir1",
                "c:\\dir2", "c:\\documents\\dir1", null};
 
            Console.WriteLine(PathFindOnPath(f, dirs) ?
                f.ToString() : "File not found");
 
            Console.ReadLine();
        }
 
        [DllImport("shlwapi.dll", EntryPoint="PathFindOnPathA") ]
        static extern bool PathFindOnPath(System.Text.StringBuilder pszFile,
            string[] ppszOtherDirs);
 
    }
}
 
 
 

User rating: 0/10 (0 votes)
Rate this code sample:
  • ~
1366 bytes  
Created: 2005-04-27 19:35:53  
Modified: 2005-04-28 09:32:16  
Visits in 7 days: 127  
Listed functions:
GlobalAlloc
GlobalFree
GlobalSize
PathFindOnPath
Printer friendly API declarations
My comment:
The second parameter of the PathFindOnPath is a memory address of null-terminated array of pointers to strings. Members of such array must be 4-byte numbers, format called UINT or DWORD. Each array member is an address of allocated in memory string that contains a directory name.

In this code sample, I want to make this function search for the file in three directories:
c:\dir1
c:\dir2
c:\documents\dir1



As the first step, I use the GlobalAlloc to allocate each directory name in memory. Three GlobalAlloc calls return three memory addresses.

To become a null-terminated array, these memory addresses must be put one after another to a memory buffer and padded by some null characters. As I said, each address must occupy four bytes.

So far these numbers are in FoxPro numeric format. This is something similar to managed objects in .Net. Technically I can not say how many bytes each numeric value takes and where exactly it resides in memory.

Somehow I need to convert them to 4-byte DWORDs. Three addresses, as three directories are searched, and five zero bytes, produce the total length of 17 bytes. I use num2dword() function to convert each number to 4-character string, which is the binary character representation of the DWORD format. In VFP9 you probably can use BINTOC() for this conversion.

So now I have created the binary character representation for the array -- a string 17 characters long. I could use the GlobalAlloc again to allocate this string in memory and obtain its address.

But more elegant solution is declaring this parameter as STRING @ -- a reference to a string, cPathArray in the code above. That makes the FoxPro to take care about all memory operations: memory allocation, passing the pointer to the PathFindOnPath and releasing the memory in the end.

PChar class hides certain complexity of memory API calls. In particular, it calls the GlobalFree, whenever a PChar instance is destroyed or re-allocated, preventing memory leaks.
Word Index links for this example:
Translate this page:
  Spanish    Portuguese    German    French    Italian  
FreeTranslation.com offers instant, free translations of text or web pages.
User Contributed Notes:
There are no notes on this subject.


Copyright © 2001-2013 News2News, Inc. Before reproducing or distributing any data from this site please ask for an approval from its owner. Unless otherwise specified, this page is for your personal and non-commercial use. The information on this page is presented AS IS, meaning that you may use it at your own risk. Microsoft Visual FoxPro and Windows are trade marks of Microsoft Corp. All other trademarks are the property of their respective owners. 

Privacy policy
Credits: PHP (4.4.9), an HTML-embedded scripting language, MySQL (5.1.55-log), the Open Source standard SQL database, AceHTML Freeware Version 4, freeware HTML Editor of choice.   Hosted by Korax Online Inc.
Last Topics Visited (184.72.91.94)
3.77 hrs.Function: 'AVIStreamTimeToSample'
 Function: 'PathFindFileName'
9.52 hrs.Function: 'SHGetFileInfo'
9.7 hrs.Example: 'Retrieving configuration information for the specified server (Win98/Me)'
 Example: 'Compressing and decompressing files with Windows API Runtime Library routines'
10.49 hrs.Example: 'How to download a file from HTTP server using URL Monikers functions'
Language: 'C#'
13.94 hrs.Function: 'SHFileOperation'
1 day(s)Function: 'GetMenuString'
 Example: 'Sending a standard message with one or more attached files using default email client'
 Example: 'Using Change Notification Objects to monitor changes to the printer or print server'
Google
Advertise here!