함수 또한 변수와 마찬가지로 메모리의 일부영역을 차지하고 있어 참조가 가능하고 이를 파라미터로 넘길 수도 있다. 다음예제는 OS를 구분하여 함수를 구분하여 실행할 수 있도록 함으로써 함수 내부에서는 OS버전에 관계없이 함수를 호출할 수 있게 해준다.
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 | #include "stdafx.h" #include <iostream> #include <windows.h> using namespace std; typedef void (*FUNCTION_STORE) (); void FunctionA(); void FunctionB(); void TestFunc(FUNCTION_STORE pFunctionStore); BOOL IsCurrentProcess64bit(); BOOL IsCurrentProcessWow64(); BOOL Is64BitWindows(); int _tmain( int argc, _TCHAR* argv[]) { FUNCTION_STORE pFunction; if (Is64BitWindows()) { // 64비트의 경우 FunctionA를 참조 pFunction = &FunctionA; } else { // 64비트가 아닐경우 FunctionB를 참조 pFunction = &FunctionB; } // 구분된 함수를 인자로 전달 TestFunc(pFunction); return 0; } void FunctionA() { cout << "Call Function A - 64bit OS.\n\n" ; } void FunctionB() { cout << "Call Function B - 32bit OS.\n\n" ; } void TestFunc(FUNCTION_STORE pFunctionStore) { // OS 버전과 관계없이 동일 코드를 실행한다. (*pFunctionStore)(); } BOOL IsCurrentProcess64bit() { #if defined(_WIN64) return TRUE; #else return FALSE; #endif } BOOL IsCurrentProcessWow64() { BOOL bIsWow64 = FALSE; typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS)( HANDLE , PBOOL ); LPFN_ISWOW64PROCESS fnIsWow64Process; fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress(GetModuleHandle(TEXT( "kernel32" )), "IsWow64Process" ); if (!fnIsWow64Process) return FALSE; return fnIsWow64Process(GetCurrentProcess(), &bIsWow64) && bIsWow64; } BOOL Is64BitWindows() { if (IsCurrentProcess64bit()) return TRUE; return IsCurrentProcessWow64(); } |