Sunday, August 28, 2011

Never mix business with pleasure

So on the RWA forums I was a bit to gabby with my experience as a programmer and was challenged to put my money where my mouth is.

The problem that was being discusses was why RW always launches in a different place, or better said as, why doesn't it remember its last position next time you launch it. This problem bothers me as much as everyone else, and to be clear this issue has been a source of annoyance for years and not just with RW, but mostly any application that was not written by Microsoft, but I digress. In the course of my rant on RWA I said:
What bugs me is that this is so easy to fix. If you register the app with the OS (no we are not talking about COM registration) then its handled for you automatically.
Apparently someone thought I was either, boasting about my uber-skillz at coding or blowing smoke. I begin to suspect that this person is one of my Coding Brethren, reaching out across the ether of the Internet to determine if I really am who I claim, or some lowly imposter. Now before anyone screams "shenanigans" and begins a flaming-finger-pointing-he-said-she-said-troll-war I want to opt for a third descriptor: "Humble enough to eat my own hat if I'm wrong" and thus I am now going to explain as best as I can without going into swaths of C++ or C# code why "its so easy to fix", or that I am flat out wrong. Lets get started.

First off I want to look at the application and identify what run time language I am dealing with. I looked at RailWorksProc2.exe and found is a MS VC 7.1 (Visual Studio 2003) application.


This tells me that I don't need to delve in any .Net languages.  For the curious you can start by reading up on Application Settings Classes, which handles windows positioning and a plethora of other application settings. However be warned that .Net handles this very differently (and more richly) than Win32 executables. Also looking at the application folder I see that the MFC runtime was included. This means that some of the CWnd classes could have been used. Getting heavy yet? Let me explain.

MFC applications can remember their window placement and their Most Recently Used (MRU) file list automatically. This is all handled in when you call the functions SetRegistryKey and  LoadStdProfileSettings. This will place your application's settings in the registry under this key:
HKEY_CURRENT_USER\Software\<company name>\<application name>
To make matters simpler the Application Wizard in the Visual Studio environment adds these two APIs to your InitInstance handler. This supports my "easy to fix" claim because if you didn't use the wizard you can add these yourself... for free. If RW isn't using MFC we are still in luck. They could add a small function to center the window (say with CenterWindow... oops that MFC) when not in full screen mode. Another method would be to call GetWindowRect and then save the co-ordinates to the regsitry when the application closes to be read in on the next launch. There are numerous examples on the web on how to do this.

Ta-da! Done! Lets all go home for dinner...

But lets step back for a moment and look at the problem again and make sure I didn't forget anything. Its clear that RailWorksProc2.exe isn't remember anything about itself. The registry key I mentioned is not getting created, there is no INI file that it seems to be reading from (the old way this use to happen). There is a file called config.xml which does contains some settings, just not the ones we are looking for (if there are its not documented). So what is happening? If I run the Process Monitor I can tell for certain that its not trying to read its application settings from the registry, and I confirmed that the only file that remotely looks like it reads its settings from the config.xml (which RailWorks.exe configures).

Sadly it looks like the end of the road for trying to find a workaround. Do I eat my hat yet? I'll let the forum judge me (this could hurt).

So instead of running trains or repainting them, or even laying some more track down on the CC, I wrote this blog post. I'm not upset about this... well, not too much. Its always good to get back to basics, but I would rather play the game than QA it. So the moral of the story is "Never mix business with pleasure"... or "Just keep my big mouth shut".

No comments:

Post a Comment