[Help:] Please, how rewrite these 20 lines in Pascal to C++?

Discussion in 'Mixed Languages' started by moderate, May 22, 2016.

  1. moderate

    moderate MDL Guru

    Aug 31, 2009
    2,652
    2,177
    90
    #1 moderate, May 22, 2016
    Last edited by a moderator: Apr 20, 2017
    Hello,

    could anybody help me and convert these 20 lines in Pascal to C++?
    (As I need to compile it by VS as desktop win32 arm EXE for jail-broken W8-1 RT.)
    Thanks.

    Code:
    program Shutdown;
    {$apptype GUI} 
    {$mode objfpc}{$H+} 
     
    uses 
      {$IFDEF UNIX}{$IFDEF UseCThreads} 
      cthreads, 
      {$ENDIF}{$ENDIF} 
      ComObj 
      { you can add units after this }; 
     
    var 
    shell: Variant; 
     
    {$R *.res} 
     
    begin 
    shell := CreateOleObject('Shell.Application'); 
    shell.ShutdownWindows; 
    end.
    
     
  2. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,646
    3,941
    120
    #2 Michaela Joy, Aug 22, 2016
    Last edited: May 5, 2017
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. vyvojar

    vyvojar MDL Novice

    Aug 10, 2016
    21
    13
    0
    #3 vyvojar, Aug 22, 2016
    Last edited by a moderator: Apr 20, 2017
    Code:
    #define COBJMACROS
    #include <windows.h>
    #include <shldisp.h>
    #include <shlguid.h>
    
    int main()
    {
            IShellDispatch *shell;
            CoInitialize(NULL);
            CoCreateInstance(&CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, &IID_IShellDispatch, (void **)&shell);
            // In C++ may become shell->ShutdownWindows()
            IShellDispatch_ShutdownWindows(shell);
    }
    
     
  4. vyvojar

    vyvojar MDL Novice

    Aug 10, 2016
    21
    13
    0
    #4 vyvojar, Aug 22, 2016
    Last edited by a moderator: Apr 20, 2017
    And for the sake of completeness, C++ is almost the same:

    Code:
    #include <windows.h>
    #include <shldisp.h>
    #include <shlguid.h>
    
    int main()
    {
            IShellDispatch *shell;
            CoInitialize(NULL);
            CoCreateInstance(CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, IID_IShellDispatch, (void **)&shell);
            shell->ShutdownWindows();
    }
    
    tip: Try to avoid C++ in Windows, it's not pretty and it will never will. Either use C and Win32 if you need to do something low level. VB/Pascal/C# for everything fancy, including COM interfaces.
     
  5. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,646
    3,941
    120
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. Tiger-1

    Tiger-1 MDL Guru

    Oct 18, 2014
    3,768
    4,052
    120
    #6 Tiger-1, Dec 13, 2016
    Last edited by a moderator: Apr 20, 2017
    thanks a lot for very useful info dude :worthy:
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. Erik B

    Erik B MDL Junior Member

    Oct 10, 2008
    82
    18
    0
    Nice translation!

    "Try to avoid C++ in Windows" ??
    Why would you say this? Choice of language is a very personal matter if the choice is free. Almost all Windows desktop programming is done in C++, more or less in ATL style. Just look at the Chrome code base for example. Not to mention almost all games. Classic Windows system code is a mix of C code from the 80s and C++ from 90s and 00s. Implementing COM in C? No thanks. VB/Pascal/C# all have performance issues, but I guess you mention these because of the GUI implementation. However, I agree with you C code is cleaner than C++ and perhaps more esthetic appealing. :D:D
     
  8. Michaela Joy

    Michaela Joy MDL Crazy Lady

    Jul 26, 2012
    3,646
    3,941
    120
    LOL Yup. With Pascal calling conventions no less. :D
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. Erik B

    Erik B MDL Junior Member

    Oct 10, 2008
    82
    18
    0
    I never understood why though. stdcall being standard in COM? The WDK build assumed stdcall no matter what. :cool:

    I have no idea how new Windows code looks like, but my guess is there's a lot of modern C++ with a C interface to conform to older code. Except for PowerShell, was there ever any windows system code written in C# or .NET? WinRT still feels like a thin layer in Windows 10, so my guess is Windows 10 is still 99% the same old C/C++ base written in the 80s-90s, although patched numerous times. But few know for sure. :D:D