Advertisement
KanjiCoder

GFT.C11

Nov 28th, 2022 (edited)
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.19 KB | None | 0 0
  1.  
  2.     //: This File On Paste Bin : https://pastebin.com/8CvECT98
  3.     //:
  4.     //: NOTE: theddmage says page 56 , 6.3.2.3 Pointers
  5.     //:       https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1548.pdf
  6.  
  7.     #include <windows.h>
  8.     #include <stdint.h>  
  9.     #include <stdio.h>  
  10.  
  11.     //: PROBLEM :
  12.     //:     You there is no gaurantee that casting
  13.     //:     a function to void* will give you the
  14.     //:     same address back when you cast the
  15.     //:     void* back to it's original type.
  16.     //:
  17.     //:     I interpret this to mean "void*" is
  18.     //:     a "generic data pointer" because making
  19.     //:     it point to the address of a function is
  20.     //:     asking for trouble.
  21.     //:
  22.     //: SOLUTION :
  23.     //:
  24.     //:     I think I am just trying to do something
  25.     //:     that can't be done. Function addresses
  26.     //:     cannot be implicitly casted to another
  27.     //:     function type.
  28.     //:
  29.     //:     SEE: NOTICE_ME_SENPAI
  30.  
  31.     /** Okay, so it's not a "generic function pointer"   **/
  32.     /** type. There is no such thing. There is a         **/
  33.     /** generic data pointer ( void* ) , but no          **/
  34.     /** such equivalent with function pointers.          **/
  35.     /**                                                  **/
  36.     /** You can cast between "void*" and back with       **/
  37.     /** data and be gauranteed to get the same address.  **/
  38.     /**                                                  **/
  39.     /** You cannot cast between a function pointer       **/
  40.     /** and "void*" and be gauranteed to get the         **/
  41.     /** same address.                                    **/
  42.    
  43.     typedef  void ( defacto_generic_function )( void );
  44.  
  45.  
  46.  
  47.  
  48.     /** Takes unspecified number of arguments,           **/
  49.     /** returns a signed 32 bit integer                  **/
  50.     /** Your job to call it with correct # of args.      **/
  51.     /** Have I been writing too much JavaScript ? Maybe. **/
  52.  
  53.     typedef  uint32_t  ( f_i32 )(       );
  54.  
  55.  
  56.  
  57.  
  58.    
  59.     defacto_generic_function* GETPROCADDRESS(
  60.        
  61.         void*      dll_handle
  62.     ,   const char* func_name
  63.     ){
  64.  
  65.         defacto_generic_function* func_ptr =(
  66.             (defacto_generic_function*)(
  67.             GetProcAddress( dll_handle , func_name )
  68.         ));;
  69.  
  70.         return( func_ptr );
  71.     }
  72.  
  73.     int main( void ){
  74.  
  75.         void* dll_handle = LoadLibraryA( "gdi32.dll" );
  76.  
  77.         //: function pointers require cast ://
  78.  
  79.         defacto_generic_function* f_1 =(
  80.             (defacto_generic_function*)
  81.             (GetProcAddress( dll_handle  , "ChoosePixelFormat" )) );
  82.        
  83.         f_i32* f_2 =(
  84.  
  85.             (f_i32*) //:<--- THIS CAST MUST BE HERE. < < < < < < < < <  NOTICE_ME_SENPAI
  86.                      //:     But, I wish I didn't have to have it here.
  87.  
  88.             (GETPROCADDRESS( dll_handle  , "ChoosePixelFormat" )) );
  89.  
  90.         //: data pointer doesn't require a cast ://
  91.  
  92.             const char* look_mom_no_casting =malloc( 30 ); //: < < < < < NOTICE_ME_SENPAI
  93.      
  94.  
  95.         if( f_1 ){ /** NOOP **/ };
  96.         if( f_2 ){ /** NOOP **/ };
  97.         if( look_mom_no_casting ){ /** NOOP **/ };
  98.  
  99.         printf( "[Hey_Hey_What_Do_You_Say?]\n" );
  100.     }
  101.  
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement