#include "FTD2XX.h" // FTDI typedef FT_STATUS (*pFT_Open) (int, FT_HANDLE *); // " FT_OPEN" // ... HMODULE hMod = LoadLibrary ("FTD2XX.dll"); // - . . pFT_Open pOpen = GetProcAddress (hMod, "FT_Open"); // - . . // ... FT_STATUS st = pOpen (0, &hDev); // // ... FreeLibrary (hMod); //
typedef FT_STATUS (*pFT_Open) (int, FT_HANDLE *); // " " pFT_Open pOpen; // " " pFT_Open pOpen = GetProcAddress (hMod, "FT_Open"); // "FT_Open"
Funct2<FT_STATUS, int, FT_HANDLE *> Open; // " 2 " Open = GetProcAddress (hMod, "FT_Open"); // "FT_Open" // ... FT_STATUS st = Open (0, &hDev); //
template <typename Ret, typename Arg1, typename Arg2, typename Except = FunctPtrExceptionType, Except Value = FunctPtrExceptionDefValue> class Funct2 { public: typedef Ret (*tfPtr) (Arg1, Arg2); tfPtr fPtr; public: Funct2 (tfPtr Ptr = 0): fPtr (Ptr) {} Funct2 &operator= (void *Ptr) { fPtr = reinterpret_cast<tfPtr> (Ptr); return *this; } Ret operator () (Arg1 A1, Arg2 A2) throw (Except) { if (!fPtr) throw Except (Value); return fPtr (A1, A2); } }; // class Funct2
#include < Windows.h > // GetProcAdress #include < stdio.h > // printf // ** // ** // ** #define FunctPtrExceptionType int // #define FunctPtrExceptionDefValue 0 // // ** // ** // ** template < typename Ret = void, typename Except = FunctPtrExceptionType, Except Value = FunctPtrExceptionDefValue > class Funct0 { public: typedef Ret (*tfPtr) (void); tfPtr fPtr; public: Funct0 (tfPtr Ptr = 0): fPtr (Ptr) {} Funct0 &operator= (tfPtr Ptr) { fPtr = Ptr; return this; } Ret operator () (void) throw (Except) { if (!fPtr) throw Except (Value); return fPtr (); } }; // ** // ** 1 // ** template < typename Ret, typename Arg1, typename Except = FunctPtrExceptionType, Except Value = FunctPtrExceptionDefValue > class Funct1 { public: typedef Ret (*tfPtr) (Arg1); tfPtr fPtr; public: Funct1 (tfPtr Ptr = 0): fPtr (Ptr) {} Funct1 &operator= (void *Ptr) { fPtr = reinterpret_cast<tfPtr> (Ptr); return *this; } Ret operator () (Arg1 A1) throw (Except) { if (!fPtr) throw Except (Value); return fPtr (A1); } }; // ** // ** 2 // ** template < typename Ret, typename Arg1, typename Arg2, typename Except = FunctPtrExceptionType, Except Value = FunctPtrExceptionDefValue > class Funct2 { public: typedef Ret (*tfPtr) (Arg1, Arg2); tfPtr fPtr; public: Funct2 (tfPtr Ptr = 0): fPtr (Ptr) {} Funct2 &operator= (void *Ptr) { fPtr = reinterpret_cast<tfPtr> (Ptr); return *this; } Ret operator () (Arg1 A1, Arg2 A2) throw (Except) { if (!fPtr) throw Except (Value); return fPtr (A1, A2); } }; // ** // ** // ** int add (const int A, const int *B) { int C; C = A + *B; printf (" int add (const int %d, const int %d) = %d\n", A, *B, C); return C; } void prn (void) { printf (" void prn (void)\n"); } // ** // ** // ** void main (void) { int i, i2; double d; long l; Funct0<> prner (prn); Funct1< double, long *, int, 2 > longer; Funct2< int, const int, const int * > adder; adder = add; longer = GetProcAddress (0, "Longer"); try { prner (); i2 = 6; i = adder (5, &i2); d = longer (&l); } catch (int val) { switch (val) { case 2: printf (" *** !\n"); break; default: printf (" *** !\n"); break; } }
Source: https://habr.com/ru/post/111680/
All Articles