Author Topic: 64 Bit SDK Programming  (Read 14573 times)

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1996
    • zapsolution
Re: 64 Bit SDK Programming
« Reply #15 on: October 04, 2021, 07:35:49 pm »
Fred

Here is my 6 Kb VS2019 Mini version using TCLib.

This is the same code i am using with all the programming languages from my soap box.

VS 2019 version

Code: [Select]
#include <windows.h>

constexpr auto CLIENT_WIDTH   = 640;
constexpr auto CLIENT_HEIGHT  = 480;

struct PROP {
    HWND        hMain;
    long        minTrackSizeW;
    long        minTrackSizeH;
};

static PROP gP;

long Width(IN RECT &r) {
    return r.right - r.left;
}

long Height(IN RECT &r) {
    return r.bottom - r.top;
}

LRESULT CALLBACK WndProc(IN HWND hWnd, IN UINT uMsg, IN WPARAM wParam, IN LPARAM lParam) {
    LRESULT nRet = -1;
    MINMAXINFO* pMM;
    switch (uMsg) {
    case WM_CREATE:
        break;

    case WM_GETMINMAXINFO:
        pMM = (MINMAXINFO*)lParam;
        pMM->ptMinTrackSize.x = gP.minTrackSizeW;
        pMM->ptMinTrackSize.y = gP.minTrackSizeH;
        break;

    case WM_SIZE:
        InvalidateRect(hWnd, NULL, TRUE);
        nRet = 0;
        break;

    case WM_PAINT:
        {
        PAINTSTRUCT ps;
        HDC hDC = BeginPaint(hWnd, &ps);
        // Paint the window content here
        EndPaint(hWnd, &ps);
        nRet = 0;
        }
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        nRet = 0;
        break;
    }

    if (nRet == -1) nRet = DefWindowProc(hWnd, uMsg, wParam, lParam);
    return nRet;
}

int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) {
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    int nRet = 0;
    WCHAR szClass[] = L"MINI";

    // Detect process without using a Mutex
    HWND hFound = FindWindow(szClass, NULL);
    if (hFound) {
        if (IsIconic(hFound)) { ShowWindow(hFound, SW_RESTORE); }
        SetForegroundWindow(hFound);
        return nRet;
    }

    WNDCLASSEX wcx = { 0 };
    wcx.cbSize = sizeof(wcx);
    long IsInitialized = GetClassInfoEx(hInstance, szClass, &wcx);
    if (!IsInitialized) {
        wcx.style         = CS_HREDRAW | CS_VREDRAW;
        wcx.lpfnWndProc   = WndProc;
        wcx.cbClsExtra    = 0;
        wcx.cbWndExtra    = 0;
        wcx.hInstance     = hInstance;
        wcx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
        wcx.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wcx.hbrBackground = (HBRUSH)COLOR_BTNSHADOW;
        wcx.lpszMenuName  = 0;
        wcx.lpszClassName = szClass;
        wcx.hIconSm       = wcx.hIcon;
        if (RegisterClassEx(&wcx)) { IsInitialized = -1; }
    }

    if (IsInitialized) {
        DWORD dwExStyle = 0;
        DWORD dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
        RECT lpr = { 0 }; SetRect(&lpr, 0, 0, CLIENT_WIDTH, CLIENT_HEIGHT);
        AdjustWindowRectEx(&lpr, dwStyle, FALSE, dwExStyle);
        gP.minTrackSizeW = Width(lpr);
        gP.minTrackSizeH = Height(lpr);
        long x = max((GetSystemMetrics(SM_CXSCREEN) - gP.minTrackSizeW) / 2, 0);
        long y = max((GetSystemMetrics(SM_CYSCREEN) - gP.minTrackSizeH) / 2, 0);

        gP.hMain = CreateWindowEx(dwExStyle, szClass, szClass, dwStyle, x, y, gP.minTrackSizeW, gP.minTrackSizeH, 0, 0, hInstance, NULL);
        if (gP.hMain) {

            ShowWindow(gP.hMain, nCmdShow);
            SetForegroundWindow(gP.hMain); // Slightly Higher Priority

            MSG msg = { 0 };
            while (GetMessage(&msg, NULL, 0, 0)) {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            nRet = (int) msg.wParam;
        }
        UnregisterClass(wcx.lpszClassName, hInstance);
    }
    return nRet;
}

WinDev version

Code: [Select]
//+--------------------------------------------------------------------------+
//|                                  MINI                                    |
//|                                                                          |
//|                  Create an empty SDI SDK popup window                    |
//|                                                                          |
//+--------------------------------------------------------------------------+
//|                         Author Patrice TERRIER                           |
//|                            copyright(c) 2014                             |
//|                           www.zapsolution.com                            |
//|                        pterrier@zapsolution.com                          |
//+--------------------------------------------------------------------------+

CONSTANT
    ClientW         = 640
    ClientH         = 480
END

PROP is Structure
    hMain           is system int
    MinTrackSizeW   is int
    MinTrackSizeH   is int
END

gP is PROP // Global class properties

// This is the main entry code (but with with WinDev the main entry code is always in the "project code")
// C'est le point d'entrée du programme (mais avec WinDev le point d'entrée principal est le "code du projet")
FUNCTION MainWindow()
wcx is WNDCLASSEXA
szClass is string ASCIIZ on 16 = "FLAT_API_POPUP" // The class name of our popup window.

wcx.cbSize = Dimension(wcx)
IsInitialized is int =  GetClassInfoExA(Instance, szClass, wcx)
IF IsInitialized = 0 THEN
    wcx.style         = CS_HREDRAW | CS_VREDRAW
    wcx.lpfnWndProc   = &WndProc
    wcx.cbClsExtra    = 0
    wcx.cbWndExtra    = 0 // Extend_cbWndExtra * 4
    wcx.hInstance     = Instance
    wcx.hIcon         = Null
    wcx.hCursor       = API(USER32, "LoadCursorA", Null, IDC_ARROW)
    wcx.hbrBackground = GetStockObject(WHITE_BRUSH)
    wcx.lpszMenuName  = Null
    wcx.lpszClassName = &szClass
    wcx.hIconSm       = Null
    IF RegisterClassExA(wcx) THEN IsInitialized = True
END

IF IsInitialized THEN
    nRet is unsigned int = 0
    r is RECT
    uMsg is TagMSG
    dwExStyle is unsigned int = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE
    dwStyle is unsigned int = WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
   
    SetRect(r, 0, 0, ClientW, ClientH)
    AdjustWindowRectEx(r, dwStyle, False, dwExStyle)
   
    gP.MinTrackSizeW = r.nRight - r.nleft
    gP.MinTrackSizeH = r.nBottom - r.nTop
    x is int = Max((GetSystemMetrics(SM_CXSCREEN) - gP.MinTrackSizeW) / 2, 0)
    y is int = Max((GetSystemMetrics(SM_CYSCREEN) - gP.MinTrackSizeH) / 2, 0)
    sCaption is string = "Popup window "
    IF In64bitMode() THEN sCaption += "64-bit" ELSE sCaption += "32-bit"
    gP.hMain = CreateWindowExA(0, szClass, sCaption, dwStyle, x, y, gP.MinTrackSizeW, gP.MinTrackSizeH, 0, 0, Instance, 0)
   
    IF gP.hMain THEN
       
        ShowWindow(gP.hMain, SW_SHOW)
        SetForegroundWindow(gP.hMain) // Slightly Higher Priority
       
        // Main message pump.       
        WHILE GetMessageA(uMsg, Null, 0, 0)
            TranslateMessage(uMsg)
            DispatchMessageA(uMsg)
        END

        nRet =uMsg.wParam
       
    END

END
RESULT nRet

// WinMain callback function.
// Function callback utilisée par la fenêtre principale.
FUNCTION WndProc(LOCAL hWnd is system int, LOCAL uMsg is unsigned int, LOCAL wParam is unsigned int, LOCAL lParam is system int)
ps is PAINTSTRUCT;
nRet is int = -1; // -1 means let Windows perform the default message processing
hDC is system int;

SWITCH uMsg
   
    CASE WM_GETMINMAXINFO:
        pMM is MINMAXINFO
        Transfer(&pMM, lParam, Dimension(pMM));
        pMM.ptMinTrackSize.x = gP.MinTrackSizeW;
        pMM.ptMinTrackSize.y = gP.MinTrackSizeH;
        Transfer(lParam, &pMM, Dimension(pMM));
       
    CASE WM_SIZE
        InvalidateRect(hWnd, Null, True);
        nRet = 0;
       
    CASE WM_COMMAND
        wmId is int    = LOINT(wParam);
        wmEvent is int = HIINT(wParam);
//      SWITCH (wmId)
//
//      END
        nRet = 0;
       
    CASE WM_PAINT
        hDC = BeginPaint(hWnd, ps);
        // Paint the window content here
        EndPaint(hWnd, ps);
        nRet = 0;
               
    CASE WM_DESTROY
        PostQuitMessage(0);
        nRet = 0;
    OTHER CASE
       
END

IF nRet = -1 THEN
    nRet = DefWindowProcA(hWnd, uMsg, wParam, lParam);
END
RESULT nRet;
« Last Edit: October 04, 2021, 07:38:48 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)

Frederick Harris

  • Newbie
  • *
  • Posts: 47
Re: 64 Bit SDK Programming
« Reply #16 on: October 05, 2021, 04:06:13 am »
Thanks Patrice!

I need to add /Gy back into my build strings James.  Thanks for mentioning it.  At one time way back I used to use that, but somehow I dropped it and forgot about it.

Patrice Terrier

  • Administrator
  • *****
  • Posts: 1996
    • zapsolution
Re: 64 Bit SDK Programming
« Reply #17 on: October 05, 2021, 11:36:14 am »
I wonder why you don't use the VS environment, and only the command line.
The VS editor is really very good, and could help to detect the most obvious bugs.
Patrice
(Always working with the latest Windows version available...)

Frederick Harris

  • Newbie
  • *
  • Posts: 47
Re: 64 Bit SDK Programming
« Reply #18 on: October 06, 2021, 05:10:01 pm »
Quote
I wonder why you don't use the VS environment, and only the command line.
The VS editor is really very good, and could help to detect the most obvious bugs.

I seem to create a lot of real small test code programs Patrice.  It seems that over the years, as Microsoft's Visual Studio got bigger and bigger, slower and slower, I started to gravitate to using Notepad.exe to write a few lines of code, and just build from the command line, rather than opening Visual Studio, creating a project, navigating the cumbersome IDE to set compiler and linker options, changing Visual Studio defaults I didn't like, filling in preprocessor equates to stop Visual Studio from complaining about deprecated functions I nontheless insisted on using, etc.  Another thing is, I only use laptops - sometimes old ones, which sometimes just about choke on the heavy burdens Visual Studio puts on them.  It really is SLOW for me at times, unless I'm using my newest and fastest laptop.  I know you always use cutting edge hardware cuz you are into advanced graphics, but like I said, Visual Studio sometimes barely works on my older machines.  I tend to develop on several machines at a time - always changing from one to another. 

My first version of Visual Studio I ever used was from Visual Studio 98 - Windows 95, 98 and Win2000 times.  I used that a lot for many years.  I had the Enterprise version of it, which came with tremendous COM/OLE documentation.  My next version was Visual Studio 2008 (I used to joke that I'd get a new version of Visual Studio every ten years, whether I needed it or not).  That's where I started having difficulties with it and started to gravitate to command line building, although I used the IDE a lot too - especially for my big projects at work.  I needed VStudio 2008 because it had support for embedded systems (the handheld data collectors running Windows CE at work), and I never quite figurred out how to get the command line compiling working for that.  So I do use IDEs some.  Really though, I tended to use the 'lighter weight' IDEs such as CodeBlocks or Dev-C++, simply because they ran better on my slower hardware.

As I mentioned the other day though, I was tickled (english colloquialism meaning it made me happy) when I found it was easy to use my TCLib from within the latest version of Visual Studio.  I've used Visual Studio more in the last week than I have in the last five years!  But then I'm mostly using it on a brand new high end laptop which is pretty fast.  If I had to pick out my biggest complaint about Visual Studio I guess it would be all the directories/folders it creates and all the bloat in the extra files it needs.  Philosophically I'm a 'minimalist'  in just about everything I do and think, so command line compiling kind of works for me.

One project I'm hoping to undertake this winter is work on my own lightweight IDE.  Started on it last winter, but got hung up on splitter controls.  Somehow or other I've never used or coded these, so I'm only about halfway through understanding the code and how to code them.  I think it would be cool to have my own I made myself.  I want a tabbed interface with a project/symbol explorer though, so I'll need a splitter control. 

I'd be curious why James does command line building, if he cares to jump in.  Far as I know, it's just him and I.  Not too many coders seem interested in it.  By the way Patrice, I tried your "MINI" template.  That's neat.  Saw some stuff in there I never used.  Thanks for posting it.  It came in 5,120 bytes for me.  Had to add this to it though to get it to build from the command line....

Code: [Select]
#ifndef UNICODE
   #define UNICODE
#endif
#ifndef _UNICODE
   #define _UNICODE
#endif

Reason is, and getting back to our discussion of why I do a lot of command line building, the Visual Studio IDE sets by default the character encoding to wide character, and when one builds from the command line there are few if any defaults set - that would kind of defeat the purpose of command line compiling/linking I guess.  So it interpreted everything to the 'A' versions of the WinApi functions, which threw all kinds of errors for me.  That's why in all my code I use the preprocessor directives above.  I know that James and I are the only folks in the whole world that do command line compiling - everybode in this world and likely beyond uses Visual Studio, so I use those #defines.  I use the #ifndef (If Not Defined macro) because when I post code in online forums I know everybody is using Visual Studio, and if I simply put....

#define UNICODE
#define _UNICODE

...that would generate duplicate preprocessor warnings, cuz, like I said, the Visual Studio IDE already defines it.
« Last Edit: October 06, 2021, 05:17:30 pm by Frederick Harris »

Frederick Harris

  • Newbie
  • *
  • Posts: 47
Re: 64 Bit SDK Programming
« Reply #19 on: October 06, 2021, 05:30:57 pm »
By the way, I finally looked up WinDev on the internet.  I really didn't know what it was, although over the years I heard you mention it many, many times in the PowerBASIC forum, Jose's Forum, and here.  I even wondered if it was a product of yours which you sold/marketed.  So I checked it out.  Must be from a French company?  Anyway, there was no english version of a Wikipedia article on it.  I checked out their website - didn't check out the price though.  I'm assummng it's fairly high.  The code you posted using it looked reasonable to me though. 

A couple weeks ago I was checking out the Purebasic website.  I was kind of thinking of buying it so as to have a 64 bit basic compiler that actually generated it's own binaries rather than using C or C++ as a back end.  I was really put off by the syntax though.  It didn't look to me anything like any version of basic I ever saw.  I was so turned off by it I immediately dropped the idea of buying it.

I'm guessing WinDev must be something like old pre-.NET Visual Basic, or Delphi?  Shares same market niche I guess?   

James Fuller

  • Newbie
  • *
  • Posts: 41
Re: 64 Bit SDK Programming
« Reply #20 on: October 06, 2021, 08:03:09 pm »
Fred,
  I use the command line because I don't write c/c++ source. I use BCX. I don't like the c++ nomanclature but love the power. I started with Atari BASIC then Visual Basic 3,4,5;  PowerBASIC; My bc9Basic fork of BCX; and now just BCX. I LOVE BASIC.
I only have Vusual Studio Community 2019 BuildTools on my current everyday coding box. No IDE
I do have the full 2022 preview IDE but have not used the ide.
I have another machine with the 2019 IDE primarily to run Patrice's code.

I have batch files for all the compilers I test.
Visual Studio
Nuwen distro
TDM
Clang
Mingw
Pelles
and shell to them from the BcxAdp editor.
Code: [Select]
$ONEXIT "ASTYLE.EXE -pn $FILE$.CPP"
'Unicode
'$ONEXIT "ULEX.EXE $FILE$.CPP"
'$ONEXIT "VC_Only.EXE $FILE$.CPP"
'$ONEXIT "VSCPP.BAT $FILE$ -m64 con"
'$ONEXIT "CLANGCPP.BAT $FILE$ -m64 con"
'------------------------------------------------------------------------------
'ansi
'$ONEXIT "NUWENGPP.BAT $FILE$ con"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con"
'$ONEXIT "MINGWGPP.BAT $FILE$ -m64 con"
$ONEXIT "LVM_MINGWGPP.BAT $FILE$ -m64 con"
'unicode
'$ONEXIT "ULEX.EXE $FILE$.CPP TCHARXLATER_VC.TXT"
'$ONEXIT "NUWENGPP.BAT $FILE$ con -municode"
'$ONEXIT "MINGWGPP.BAT $FILE$ -m64 con -municode"
'$ONEXIT "TDMGPP.BAT $FILE$ -m64 con -municode"

This is the setup section of my VSCPP.BAT file with the versions in descending order from newest to oldest.
Code: [Select]
SET XTYPE=x64
:setup
IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio\2022\Preview\VC\Auxiliary\Build\vcvarsall.bat" (
  ECHO Using Visual Studio Cummunity 2022 Preview
  CALL "C:\Program Files (x86)\Microsoft Visual Studio\2022\Preview\VC\Auxiliary\Build\vcvarsall.bat" %XTYPE%
  GOTO got_tools
)
IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" (
  ECHO Using Visual Studio BuildTools 2019
  CALL "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" %XTYPE%
  GOTO got_tools
)       
IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" (
  ECHO Using Visual Studio Cummunity 2019
  CALL "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat" %XTYPE%
  GOTO got_tools
)
IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" (
  ECHO Using Visual Studio BuildTools 2017
  CALL "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\VC\Auxiliary\Build\vcvarsall.bat" %XTYPE%
  GOTO got_tools
)
IF EXIST "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" (
  ECHO Using Visual Studio Cummunity 2017
  CALL "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" %XTYPE%
  GOTO got_tools
)

:got_tools


My verison of TCLib and it's includes produce slightly larger exe's. I needed to tweak it a bit so it would play nice with BCX.
My version of your Demo2 -> 6656 Bytes

I am not a real minimalist. It's fun but I really like all the cool stuff in the STL regardless of size. Like Patrice I always (until Win 11) have a powerful machine to code on. Range based for loops of std:c++11 and now I am investigating the std:c++17 <filesystem> not to mention vectors and regex.

I found out another item that I think might be MS only?
The way BCX works all the translated Basic -> c++ code is place in one file. Similar to PowerBASIC's Dead Code Removal, only the functions called are added to your exe. Classes are always included in the exe and so is c++ code that is Included (#include myfile.cpp). I find this far superior to creating a MyOftenUsedCode.lib and linking to it. If I need to make changes or add code to MyOftenUsedCode I don't have to recompile.

James


Patrice Terrier

  • Administrator
  • *****
  • Posts: 1996
    • zapsolution
Re: 64 Bit SDK Programming
« Reply #21 on: October 06, 2021, 08:21:54 pm »
Fred

WinDev is very special, it is not a programming tool, but a development tool using p-code and a huge framework written in C++.

Its main advantage is how fast you can build a complex project, even without knowledge of the Windows API.

This is a press button development tool intended to be used by... anyone.

The main problem of WinDev is its statibility, because they produce a new version each year to please their shareholders, but this doesn't give enough time to the programming team to fix the recurrent bugs.
But when you are a real programmer you can work around the bugs, and speed up the code with your own DLL(s).

I have written amazing applications with it in monthes, that would have taken years with any other tool.

WinDev is the first development tool in the "french speaking countries", there is also an english version and even a version in Mandarin for the chinese market.

BTW: Have you seen the dedicated section I made for you ?
« Last Edit: October 09, 2021, 12:27:56 pm by Patrice Terrier »
Patrice
(Always working with the latest Windows version available...)