Figure 4 ListProc-cons////////////////////////////////////////////////////////////////
// ListProc.cpp shows how to write an app that can run from a single EXE
// as either a console or GUI app. To run in GUI mode, just type the name
// of the program with command-line args. To run in console/batch mode,
// type the name followed by any option, such as -help.
//
#include "stdafx.h"
#include "EnumProc.h"
static void help();
static BOOL bBare=FALSE;
static BOOL bClassName=FALSE;
static BOOL bTitle=FALSE;
// check for switch: / or -
inline BOOL isswitch(TCHAR c) { return c==L'/' || c==L'-'; }
/////////////////
// Main entry point for console app.
//
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
TRACE("hcon=%p\n",hcon);
if (argc <= 1) {
// Invoked with no command-line args: run in GUI mode.
TCHAR lpExeName[_MAX_FNAME];
GetModuleFileName(NULL, lpExeName, _MAX_FNAME);
LPTSTR ext = lpExeName + _tcslen(lpExeName) - 3;
_tcscpy(ext,_T("exe"));
ShellExecute(NULL, _T("open"), lpExeName, NULL, NULL, SW_SHOWNORMAL);
return 0;
}
// Parse command line. Switches can come in any order.
for (int i=1; i<argc; i++) {
if (isswitch(argv[i][0])) {
for (UINT j=1; j<_tcslen(argv[i]); j++) {
switch(tolower(argv[i][j])) {
case 'b':
bBare=TRUE;
break;
case 'c': // console : do nothing
break;
case 'n':
bClassName=TRUE;
break;
case 't':
bTitle=TRUE;
break;
case '?':
default:
help();
return 0;
}
}
} else {
help();
return 0;
}
}
// command-line mode: printf process list
_tprintf(FormatProcessList(bClassName, bTitle, bBare));
return 0;
}
void help()
{
_tprintf(_T("lp: List top-level proceses.\n"));
_tprintf(_T(" Copyright 2002 Paul DiLascia.\n\n"));
_tprintf(_T("Format: lp [-bcnt]\n\n"));
_tprintf(_T("Options:\n"));
_tprintf(_T(" None: run GUI version\n"));
_tprintf(_T(" -b(are) no header\n"));
_tprintf(_T(" -c(onsole) display on console (no GUI)\n"));
_tprintf(_T(" -n(className) show window class names\n"));
_tprintf(_T(" -t(itle) show window titles\n"));
_tprintf(_T("\n"));
}
Figure 6 IniFile using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
////////////////////
// .NET wrapper for an initialization file.
//
namespace IniProfile {
public class IniFile
{
[DllImport("Kernel32.dll")]
private static extern bool WritePrivateProfileString(
String section, String key, String val, String filename);
[DllImport("Kernel32.dll")]
private static extern int GetPrivateProfileInt(
String section, String key, int dflt, String filename);
[DllImport("Kernel32.dll")]
private static extern int GetPrivateProfileString(
String section, String key, String dflt,
StringBuilder buf, int nBufLen, String filename);
private String iniFileName; // .INI file name
private Hashtable settings = new Hashtable();
private String currentSection = "Settings";
public String Section
{
get { return currentSection; }
set { currentSection = value; }
}
// ctor: initialize file name
public IniFile(String fn, bool useAppDataPath) {
if (useAppDataPath) {
iniFileName = Application.UserAppDataPath + "\\" + fn;
} else {
iniFileName = fn;
}
}
public int GetIntVal(String key, int dflt)
{
return GetPrivateProfileInt(currentSection, key, dflt,
iniFileName);
}
public String GetStrVal(String key, String dflt)
{
StringBuilder sb = new StringBuilder(256);
GetPrivateProfileString(currentSection, key, dflt, sb,
sb.Capacity, iniFileName);
return sb.ToString();
}
public void SetVal(String key, String val)
{
WritePrivateProfileString(currentSection, key, val, iniFileName);
}
public void SetVal(String key, int val)
{
WritePrivateProfileString(currentSection, key, val.ToString(),
iniFileName);
}
public void RestoreWinPos(Form form, String section)
{
this.Section = section;
form.Location = new Point(
GetIntVal("X",form.Location.X),
GetIntVal("Y",form.Location.Y));
form.Size = new Size(
GetIntVal("Width", form.Size.Width),
GetIntVal("Height",form.Size.Height));
form.StartPosition = FormStartPosition.Manual; // important!
}
public void SaveWinPos(Form form, String section)
{
this.Section = section;
SetVal("X",form.Location.X);
SetVal("Y",form.Location.Y);
SetVal("Width", form.Size.Width);
SetVal("Height",form.Size.Height);
}
}
}
|