ethical.blue Magazine

// Cybersecurity clarified.

Playing The Prodigal Son with WinAPI Beep();

2022-03-28   Dawid Farbaniec
...
The Beep function can be found in the Windows Application Programming Interface called WinAPI. It allows you to play the sound on the system speaker. The sound played is simple, but you can set the frequency (first parameter) and the duration in milliseconds (second parameter). The function prototype looks like this:
BOOL WINAPI Beep(

  _In_ DWORD dwFreq,
  _In_ DWORD dwDuration
);

Playing The Prodigal Son Melody

The Visual C++ code is:
#include <iostream>

#include <vector>
#include <Windows.h>

class N
{
public:
    int frequency;
    int duration;

    N(int a, int b)
    {
        frequency = a;
        duration = b;
    }
};

int main(int argc, char* argv[])
{
    std::vector<N> melody(
        {
            N(1044, 237), N(1044, 475), N(1044, 237), N(1044, 475), N(833, 475), N(934, 475),
            N(934, 950), N(934, 475), N(934, 475), N(934, 237), N(1044, 475), N(1175, 237),
            N(1044, 950), N(1044, 237), N(1044, 475),N(1044, 237), N(1044, 475), N(833, 475),
            N(934, 475), N(934, 950), N(833, 475), N(833, 237), N(833, 237), N(783, 475),
            N(783, 475), N(698, 950), N(1044, 950),N(833, 475), N(934, 475), N(1044, 950),
            N(1175, 237), N(1044, 475), N(934, 950), N(934, 950), N(783, 475), N(833, 475),
            N(934, 950), N(1044, 475), N(833, 237), N(783, 475), N(698, 950)
        });

    for (auto const& x : melody)
    {
        std::cout << " Beep(" << x.frequency << "," << x.duration << ");" << std::endl;
        ::Beep(x.frequency, x.duration);
    }

    std::cout << "-== ethical.blue ==-" << std::endl;

    return EXIT_SUCCESS;
}

Listen To The Melody

Bibliography

https://docs.microsoft.com/en-us/windows/win32/api/utilapiset/nf-utilapiset-beep [access: 2022-03-28]