Figure 1 GetKeyState
////////////////////////////////////////////////////////////////
// MSDN Magazine ?August 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
// CtlTest illustrates how to test for a special control-key combination
// when your program starts.
#include "StdAfx.h"
#include "MainFrm.h"
#include "StatLink.h"
#include "resource.h"
class CMyApp : public CWinApp {
public:
virtual BOOL InitInstance();
} theApp;
BOOL CMyApp::InitInstance()
{
BOOL bCtl = GetKeyState(VK_CONTROL)<0;
BOOL bF8 = GetKeyState(VK_F8)<0;
if (bCtl && bF8) {
if (AfxMessageBox(_T("Enter special startup mode?"),
MB_YESNO)==IDYES)
// Enter special startup mode here.
MessageBeep(0);
}
}
Figure 2 tstring
////////////////////////////////////////////////////////////////
// MSDN Magazine ?August 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
// TSTRING shows how to implement a tstring class that uses STL string or
// wstrings depending on the setting of _UNICODE, similar to TCHAR,
// _tprintf and all the other "t" versions of functions in the C runtime.
//
// To see the difference, compile both debug and release versions. The
// debug version uses Unicode; the release uses MBCS. Then run each
// program and compare the output files.
//
#include "stdafx.h"
#include "resource.h"
using namespace std;
// tstring is either string or wstring, depending on _UNICODE.
// This works too, but may produce an extra class:
//
// typedef basic_string,
// allocator > tstring;
//
#ifdef _UNICODE
#define tstring wstring
#else
#define tstring string
#endif
static void WriteString(HANDLE f, LPCTSTR lpsz, int len);
void _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
// process args
if (argc != 2) {
_tprintf(_T("Usage: tstring [filename]\n"));
_tprintf(_T(" writes test message to [filename]\n"));
return;
}
// CreateFile will create Unicode or MBCS string
// depending on value of _UNICODE.
LPCTSTR filename = argv[1];
HANDLE f = CreateFile(filename, ...);
if (f!=INVALID_HANDLE_VALUE)
{
if (GetFileType(f) == FILE_TYPE_DISK)
{
// create STL tstring
tstring s = _T("Hello, world");
WriteString(f, s.c_str(), s.length());
} else {
tprintf(_T("ERROR: the specified file '%s' is not a disk file\n"),
filename);
}
CloseHandle(f); // close file
} else {
_tprintf(_T("ERROR: can't open '%s'\n"), filename);
}
}
////////////////
// write string to file.
//
void WriteString(HANDLE f, LPCTSTR lpsz, int len)
{
DWORD nWrite = len * sizeof(TCHAR);
DWORD nActual;
if (WriteFile(f, lpsz, nWrite, &nActual, NULL)) {
// display results.
_tprintf(_T("%d bytes written\n sizeof(TCHAR)=%d\n"), nActual,
sizeof(TCHAR));
} else {
_tprintf(_T("ERROR %d writing\n"), GetLastError());
}
}
Figure 4 utest
////////////////////////////////////////////////////////////////
// MSDN Magazine ?August 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
// This C# program tests the union defined in MCLib.cpp.
//
using System;
using MCLib; // home-brew MC++ class lib
class MyApp {
// main entry point
[STAThread]
static int Main(string[] args) {
CUnionClass obj = new CUnionClass();
// Note that this code accesses the embedded union both directly
// through obj.val and indirectly through the properties i and d.
obj.uval.i = 17;
Console.WriteLine("Set i=17:");
Console.WriteLine(" i={0}", obj.i);
Console.WriteLine(" d={0}", obj.d);
obj.uval.d = 3.14159;
Console.WriteLine();
Console.WriteLine("Set d=3.14159:");
Console.WriteLine(" i={0}", obj.i);
Console.WriteLine(" d={0}", obj.d);
return 0;
}
}
Figure 5 C-Style Unions
////////////////////////////////////////////////////////////////
// MSDN Magazine ?August 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
// This module shows how to export a C-style union as a managed __value
// struct using StructLayout and FieldOffset. To compile this program and
// the C# test program that uses it, cd to this directory and type
//
// NMAKE
//
#using < mscorlib.dll >
using namespace System;
using namespace System::Runtime::InteropServices;
// Handy definitions to save typing ugly __'s.
#define DOTNET __gc
#define PROPERTY __property
#define VALUE __value
namespace MCLib {
//////////////////
// Here's a typical C/C++ union:
//
// union {
// int i;
// double d;
// };
//
// And here's how to implement it for consumption by .NET:
//
[StructLayout(LayoutKind::Explicit)]
public VALUE struct MyUnion {
[FieldOffset(0)] int i;
[FieldOffset(0)] double d;
};
// This exported class contains the union as a member.
// The C# program test.cs shows how to use it.
//
public DOTNET class CUnionClass {
public:
// can access this directly because it's public
MyUnion uval;
// additional access through properties
PROPERTY int get_i() { return uval.i; }
PROPERTY void set_i(int i) { uval.i = i; }
PROPERTY double get_d() { return uval.d; }
PROPERTY void set_d(double d) { uval.d = d; }
};
}
Figure 6 CSpecialFolder
////////////////////////////////////////////////////////////////
// MSDN Magazine ?August 2004
// If this code works, it was written by Paul DiLascia.
// If not, I don't know who wrote it.
// Compiles with Visual Studio .NET 2003 on Windows XP. Tab size=3.
//
// Class to get the path name of a special folder. To use:
//
// CSpecialFolder sf(CSIDL_MYPICTURES)
//
// Now the string holds the path name.
//
class CSpecialFolder : public CString
{
protected:
BOOL GetSpecialFolder(int nFolder) {
Empty();
BOOL bRet = SHGetSpecialFolderPath(NULL, GetBuffer(MAX_PATH),
nFolder, FALSE);
ReleaseBuffer();
return bRet;
}
public:
CSpecialFolder(int nFolder) {
GetSpecialFolder(nFolder);
}
CSpecialFolder& operator=(int nFolder) {
GetSpecialFolder(nFolder);
}
};