Apache webserver error on startup

I don’t use Apache that often, (not as often as I used to), but when I do I sometime get an error telling me that

(OS 10048)Only one usage of each socket address (protocol/network address/port) is normally permitted. : make_sock: could not bind to address 0.0.0.0:443 no listening sockets available, shutting down Unable to open logs

Now in my case the error was because of skype.exe that was using port 443

But if you need to know who is using it simply open the command line and type

netstat -ano

And, somewhere in the list, you will give see something that is using 0.0.0.0:443, but you will not see the name, you will see the pid, (the process id).

BTW: to get the error message, you can get it via the logs or you can open a command line and run the Apache server manually.

httpd.exe -k start

What I had forgotten about WM_CLOSE

When I was working on the next version of Piger I would sometime get an issue when using the this.bye command, (to close Piger).

What happens in the background is I call WM_CLOSE to close the active window, this message is sent to all the windows.

When it is processed by the message pump no other messages are been processed, this is a problem in case some windows are still dishing out messages, (like a fading dialog box in our example).

So remember, once you call WM_CLOSE, all bets are off and your message pump is as good as dead.

Best to have your own ‘Close()’ that does all the housekeeping in the background before you actually post WM_CLOSE to the main thread…

Otherwise your app my appear to hang.

Preventing embedded python from killing your app

I was looking at a defect in Piger where a Python script could close Piger itself, it wasn’t a crash, but rather a graceful exit.

import am
am.say( "Bye", 1, 1 );
exit(1);

The problem was with the way I was calling the script rather than anything wrong with Piger or Python itself.

Somewhere in the Python virtual machine I had something like…

...
if( -1 == PyRun_SimpleString( ... ) )
{
...
}
...

And, while that works fine in most cases, if I have an exit( xyz ) in my script, piger would close.
So the answer was to replace it with …


...
PyObject * PyRes = PyRun_String(s, Py_file_input, main_dict, main_dict);
PyObject* ex = PyErr_Occurred();
if( NULL != ex)
{
...
}
...

The one last problem was, how to tell if the error was because of an exit(…) or because of an error.


...
PyObject * PyRes = PyRun_String(s, Py_file_input, main_dict, main_dict);
PyObject* ex = PyErr_Occurred();
if( NULL != ex)
{
// if we exit(...) then 'ex' is not NULL, so we must check for that.
if (!PyErr_ExceptionMatches(PyExc_SystemExit))
{
// this is a real coding error.
}
}
...

Now you can catch all the real errors and swallow all the exist code and so on.

See the exceptions handling in Python and embedding in general.

Released piger 0.3.2

I released the first version of Piger on Github after moving from sourceforge.net

This new release is pretty much the same as the previous one just brought to the 21st century …

The changes are

  1. Python 3.5.1
  2. Python is not required on the host machine, (something that was causing a break before!)
  3. Lua 5.2
  4. x64 or x86 build, (not for memory reasons, but for env. variable reasons).
  5. Moved from Sourceforge.net, (I added a readme.md to redirect to github). Please Google for some well documented reasons to move away from them…

Please try it and tell me if you see something cool that should be added.

I am also looking for cool scripts to be added to my list, so please, send them my way!

Embed Python in your C++ application without Python installed on guest machine

For my Piger application, I wanted my C++ app to parse Python scripts but I quickly became aware of the fact that the user must have Python installed on their machine, not only that they needed the same version as mine, (version 3.5).

The normal call would be something like…

#include <Python.h>
...
// This will cause an error if the user does not have the same version of Python as the one we want.
// or if they do not have Python installed at all.
Py_Initialize();
...

But that throws an error when the user does not have Python installed.
And if they have an older version installed, it _might_ work, but the scripts might not work as expected.

The solution is to embed the Python you want to run in your app.

  1. Go to the Python website and download the Embeddable zip file for your app, (x64 or x86)
  2. Extract the python35.zip located inside that zip file
  3. Copy it somewhere where it can be referenced.
  4. Add the code below.
#include <Python.h>
...    
std::wstring exe_dir = L"\\exe\\path";
std::wstring python_path;
python_path += exe_dir + L"python35.zip";
Py_SetPath(python_path.c_str());

// Now we can call Initialize
Py_Initialize();
...

Now your app will work using version Python 3.5.1

Installing Eclipse C++ on Windows

The steps below are for installing C++ on eclipse and window.

I am using Eclipse 4.5 and Windows 10, but it doesn’t really mater, it should work for any version.

  • First things first, close everything, whatever version of Eclipse you have open.
    To make your life easier, close things you do not need as well, like notepads and so on.

MinGW

  • Go to mingw and download it, (MinGW stands for Minimalist GNU for Windows).
    The version I got was version 0.6.2
    Go to the website, (link above), and click on the “Download Installer” button, (it is not obvious at first where the button is).
  • Run the install as an Administrator.
    Pay attention to the install folder, (For me the default was C:\MinGW).
  • Wait for the install to complete …
  • When this is all done, add the ‘bin’ folder to your PATH environment variable, (Google how to do it), in my case I added “C:\MinGW\bin” to the PATH.
  • Open a command line window and type ‘mingw-get install gcc g++ gdb mingw32-make‘.
  • Wait for the install to complete …
  • Check that everything will work type “gcc –version“, if you get an error, something went wrong.
    Otherwise you should see the gcc version number.

Eclipse

  • Open your version of Eclipse
  • Go to Help > Eclipse Marketplace …
  • Type C++ and select the CDT option
  • or add “http://download.eclipse.org/tools/cdt/builds/mars/milestones” as a software site and select CDT main features.
  • Download and Update everything.

Others

  • Make sure that the right tool chain is selected
    • Project > Properties …
    • C/C++ Build
    • Tool Chain Editor and select “MinGW GCC”

Include Google test to your vs2015 project

To include Google test to your visual studio project you just need to follow the steps below

  1. Download Googletest from Github and copy all the file in it’s own folder.
    You only need the folder called “googletest” other files are for github and so on.
  2. Create a library
    1. Add a new empty project
      Right click the solution, Add > New Project …, “Win 32 project”
    2. Give it a name “googletest”
    3. Make sure that the directory is relative to your project, (the default is somewhere weird and wonderful in your %appdata% I think).
    4. Select the option “Static Library”, “empty project” and it should have no files in it.
    5. Uncheck the “Precompiled header” and press “Finish”
    6. Add gtest_main.cc and gtest-all.cc to that project, (they are located in the googletest\src\ folder of the files you just downloaded.
    7. Compile that project, and note where the googletest.lib file is created.
  3. In your test project,
    1. Right click > Properties > C++ > General
    2. In the part for “Additional include libraries” add the path to the google test folder and include folder.
      For example, “..\googletest\include;..\googletest\”.
    3. There might already be a folder called “%(AdditionalIncludeDirectories)”.
    4. Right click > Properties > Linker > General
    5. In the part for “”Additional Library directories” add the directory where the googletest.lib file was created, (the library, not the file).
    6. Finaly, add a reference, (Right Click the project > References and add googletest).

Be sure to set the references and include libraries for all your configurations, “Release”, “Debug”, x32, x64 and so on.

NB: Of course you can just build the lib and include it as a library, but that way when Googletest does an update, you just need to replace the code, and life is good again!