Advertisement
CODE_TOLD_FAST

ISO_C/03/M.c

Jan 19th, 2020
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.87 KB | None | 0 0
  1.  
  2. //:         VIDEO: https://www.youtube.com/watch?v=TnrRkzC1_RU
  3. //:       CHANNEL: CODE_TOLD_FAST
  4. //: PASTEBIN_NAME: ISO_C/03/M.c
  5. //:  PASTEBIN_URL: ( See Youtube Video Description For Link )
  6.  
  7. //: SECTION: [Headers/Libraries] and types: /////////////////://
  8.  
  9.  
  10.  
  11. #include <stdio.h>   //:printf(...)
  12. #include <windows.h> //:LoadLibraryA, GetProcAddress
  13.  
  14.  
  15.  
  16. struct DAT_ShellExecuteInfoW { /////////////////////////////////
  17.     unsigned long     cbSize; //:---------------: U_32 / 8_BYTES
  18.     unsigned long      fMask; //:---------------: U_32 / 8_BYTES
  19.              void*      hwnd; //:---------------: VOID
  20.  
  21.     const unsigned short *         lpVerb; //:--: U_16 / 2_BYTES
  22.     const unsigned short *         lpFile; //:--: U_16 / 2_BYTES
  23.     const unsigned short *   lpParameters; //:--: U_16 / 2_BYTES
  24.     const unsigned short *    lpDirectory; //:--: U_16 / 2_BYTES
  25.  
  26.     int       nShow;
  27.     void*  hInstApp;
  28.     void*  lpIDList;
  29.                     const unsigned short *   lpClass;
  30.                     void*                  hkeyClass;  
  31.                     unsigned long           dwHotKey;
  32.     union /** anonymous **/ {
  33.         void*    hIcon;
  34.         void* hMonitor;
  35.     } dummy_union_member_aka_DUMMYUNIONNAME;
  36.  
  37.     void*    hProcess;
  38. }; /////////////////////////////////////////////////////////////
  39.  
  40.  
  41.  
  42. typedef
  43.     int
  44.     (*PFN_ShellExecuteExW)(
  45.         struct DAT_ShellExecuteInfoW *pExecInfo
  46.     );
  47.  
  48. typedef
  49.     unsigned long
  50.     (*PFN_WaitForSingleObject)(
  51.         /**/    void* hHandle
  52.         ,       unsigned long  dwMilliseconds
  53.     );
  54.  
  55. typedef
  56.     int
  57.     (*PFN_CloseHandle)(
  58.         void* hObject
  59.     );
  60.  
  61. int main( void ){
  62.     printf("[BEG:main]\n");
  63.  
  64.     //:SECTION: Get Function Pointers ///////////////////////://
  65.  
  66.    
  67.  
  68.     //:Load DLL (Dynamic Link Library) Files:
  69.     void* dll_Shell32  = LoadLibraryA( "shell32.dll"   );
  70.     void* dll_Kernel32 = LoadLibraryA( "kernel32.dll"  );
  71.    
  72.     //:Pull function pointer(s) from DLL(s):
  73.     ////////////////////////////////////////////////////////////
  74.     #define T1 PFN_ShellExecuteExW      // ################## //
  75.     #define T2 PFN_WaitForSingleObject  // ################## //
  76.     #define T3 PFN_CloseHandle          // ################## //
  77.  
  78.     T1 pfn_ShellExecuteExW=(T1)(GetProcAddress(dll_Shell32,
  79.           "ShellExecuteExW" ));;
  80.  
  81.     T2 pfn_WaitForSingleObject=(T2)(GetProcAddress(dll_Kernel32,
  82.           "WaitForSingleObject" ));;
  83.  
  84.     T3 pfn_CloseHandle=(T3)(GetProcAddress(dll_Kernel32,
  85.           "CloseHandle" ));
  86.  
  87.     #undef  T1   // ######################################### //
  88.     #undef  T2   // ######################################### //
  89.     #undef  T3   // ######################################### //
  90.     ////////////////////////////////////////////////////////////  
  91.  
  92.     //:SECTION: Create Argument List ////////////////////////://
  93.  
  94.    
  95.  
  96.     //:Check for null function pointers:
  97.     if( NULL == pfn_ShellExecuteExW     ){ printf("[01]\n"); };
  98.     if( NULL == pfn_WaitForSingleObject ){ printf("[02]\n"); };
  99.     if( NULL == pfn_CloseHandle         ){ printf("[03]\n"); };
  100.  
  101.     //:Create Argument List (Using WIDE strings)
  102.     const short* lpParameters=(
  103.         L"ARG01"
  104.         L" "     //:SPACE_BETWEEN_ARGS!
  105.         L"ARG02"
  106.         L" "     //:SPACE_BETWEEN_ARGS!
  107.         L"ARG03"
  108.         L"\0" //:Extra null terminator to be safe
  109.     );;  
  110.  
  111.     //:SECTION: Build+Invoke: ShellExecuteExW Call //////////://
  112.  
  113.    
  114.  
  115. //[ SEE_MASK_NOCLOSEPROCESS (0x00000040) --------------------]//
  116. //| Makes ShellExecuteExW put process handle into hProcess   |//
  117. //| so you can detect when launched program terminates.      |//
  118.  
  119. struct DAT_ShellExecuteInfoW info = { 0 };
  120.  
  121. info      .cbSize = sizeof( struct DAT_ShellExecuteInfoW );
  122. info       .fMask = 0x00000040 /** SEE_MASK_NOCLOSEPROCESS **/;
  123. info      .lpFile = L"S.exe"; //:L for "Wide String"
  124. info.lpParameters = lpParameters;
  125.  
  126. //:DONT_NEED_TO_SUPPLY, but here for completeness:
  127. info      .lpVerb = L"open"; //:Defaults to "open" if not given.
  128. info        .hwnd =    NULL; //:No parent window.
  129. info .lpDirectory =    NULL; //:For: working dir != current dir
  130. info       .nShow =    0   ; //:Not windowed program
  131. info    .hInstApp =    NULL; //:hInstApp >= 32 if success
  132. info    .lpIDList =    NULL; //:Identify exe to run via PIDL
  133. info     .lpClass =    NULL; //:Misc  String. SEE_MASK_CLASSNAME
  134. info   .hkeyClass =    NULL; //:Registry Key. SEE_MASK_CLASSKEY
  135. info    .dwHotKey =    0   ; //:Shortcut Key. SEE_MASK_HOTKEY
  136. info    .hProcess =    NULL; //:output. SEE_MASK_NOCLOSEPROCESS  
  137.  
  138.     //:SECTION: Spinlock till S.exe finished ////////////////://
  139.  
  140.        
  141.  
  142.     //:Launch Sub Program : S.exe
  143.     pfn_ShellExecuteExW( &info );
  144.  
  145.     //: The handle must have the SYNCHRONIZE access right.
  146.     //: -MSDN: WatiForSingleObject
  147.     if(NULL == info.hProcess ){
  148.         printf("[ShellExecuteInfoW:Problems]");
  149.         fflush(stdout); exit(666);
  150.     };;
  151.  
  152.     //:SpinLock
  153.     pfn_WaitForSingleObject(
  154.         info.hProcess
  155.     ,   0xFFffFFff /** INFINITE : winbase.h **/
  156.     );;
  157.  
  158.     pfn_CloseHandle( info.hProcess );  
  159.  
  160.     //:SECTION: Read from file created by S.exe /////////////://
  161.  
  162.        
  163.  
  164.     //:Because we are certain that the sub program is
  165.     //:finished, we can read the file that sub program
  166.     //:created without introducing a race condition.
  167.  
  168.     //:Open File:
  169.     FILE* file = fopen( "txt.txt", "r" /** r:read **/ );
  170.     if(NULL==file){
  171.         printf("[FailedToOpen:txt.txt]\n");
  172.         fflush(stdout); exit(666);
  173.     }else{
  174.         printf("[FileOpened:txt.txt]\n");
  175.         fflush(stdout);
  176.     };;
  177.  
  178.     //:Print contents and close file:
  179.     char line[64] = { 0 };
  180.     int  max_len  = 64;
  181.     while( fgets(line,max_len,file) != NULL ){
  182.         printf("[LN]:%s\n", line);
  183.     };;
  184.     fclose( file );  
  185.                                  
  186.     printf("[END:main]\n");
  187. }//[;main;]//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement