Archive

Posts Tagged ‘Path’

Path of other process

April 24, 2010 Leave a comment

A few days ago I try to find a way to get path of a specific process, well here is snipped  code do this work. you need Process ID to use with CreateToolhelp32Snapshot API and then get process path with Module32First API

 DWORD dwPID;	// PID of process
 MODULEENTRY32 module = {sizeof(MODULEENTRY32)};

 HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, dwPID);
 if ( Module32First(hSnap, &module) )
   MessageBox(hWnd, module.szExePath, "MsG", MB_OK);

 CloseHandle(hSnap);

it’s very simple, isn’t? :)

Work with Path easier

April 17, 2010 Leave a comment

For today I want talk about a bunch of API make work of path very easy, I know everyone in his/her life work on a program that need of handling  path, i mean combine two path, remove file spec, get file spec, check file extension, replace extension,  etc.

for example maybe some of you for separating file spec of path use kinda loop like  “while loop” and  traversing the path reverse with pointer that point to end of path to encounter first slash and get file name, so if i say simply you can use PathFindFileName API and this return you pointer to first character of file name you maybe wonder of this.

you need to add shlwapi.h and shlwapi.lib to your project and use one of Path relate API I describe here, this API are for both ASCII and Unicode path:

for more info and example see MSDN website.

PathAppend : Appends one path to the end of another.  

PathCombine : Concatenates two strings that represent properly formed paths into one path; also concatenates any relative path pieces.  

PathFileExists : Determines whether a path to a file system object such as a file or directory is valid.

PathFindExtension : Searches a path for an extension.

PathFindFileName : Searches a path for a file name.  

PathIsDirectory : Verifies that a path is a valid directory.  

PathIsFileSpec : Searches a path for any path-delimiting characters (for example, ‘:’ or ‘\’ ). If there are no path-delimiting characters present, the path is considered to be a File Spec path.

PathRemoveExtension : Removes the file extension from a path, if one is present.

PathRemoveFileSpec : Removes the trailing file name and backslash from a path, if they are present.  

PathStripToRoot : Removes all parts of the path except for the root information.

here is my example :

#include <windows.h>
#include <shlwapi.h>
#include <tchar.h>
#include <stdio.h>

#pragma comment(lib, "shlwapi.lib")

int main()
{
 TCHAR lpPath[MAX_PATH] = _T("C:\\windows");

 printf("windows path : %s\n", lpPath);

 PathAppend(lpPath, _T("system32"));
 printf("system32 path : %s\n", lpPath);

 PathAppend(lpPath, _T("calc.exe"));
 if ( PathFileExists(lpPath) )
 printf("calc.exe is exist\n");

 PathRemoveFileSpec(lpPath);
 PathAppend(lpPath, _T("notepad.exe"));
 if ( PathFileExists(lpPath) )
 printf("notepad.exe is exist\n");

 printf("file name of \n  %s\t%s\n", lpPath, PathFindFileName(lpPath));
 printf("extension of \n  %s\t%s\n", lpPath, PathFindExtension(lpPath));

 return 0;
}

Follow

Get every new post delivered to your Inbox.