-
Notifications
You must be signed in to change notification settings - Fork 9
Linking into Window's system
I plan on doing a longer write up at some point when/if I decide to actually write articles on what I have been going through to implement this native system. I want to do this, but again, it is all about time balance and currently I am spending a lot of time coding instead of being able to talk about what I have been developing.
But, this does not mean that this knowledge needs to be only held by me, I want others to know how to do this so we can have more developers working on projects similar to this!
So, after deciding to build this project, I set out to learn how Window's handles Internet Explorer (can't use Edge since that is only Win10 and I want this to be compatible all supported Window OS).
My C++ has been very rusty, and so going into this head first may not have been the smartest move, but it has been moving forward and I am using the new features when I feel that I grasp them fairly well.
So stepping away from that anecdote, I can tell you that the documentation on any of this in Windows is pretty bad. From browsing for hours on end, there does not seem to be a lot of development for pure C++ applications written for Windows. This made it quite a challenge, but after piecing together several articles (I will link them below), I was able to cobble together a running system.
First, was getting the basic window up and running. This mean using the COM system that Windows uses for its low level talking. For these types of applications your starting point is the WinMain function found in quark.cpp. Here you can find the basic pieces of me setting up a window and then running their message loop.
Second, it was figuring out how to embed a web browser in this controlled window. After monkeying around and googling non stop, I found a nice CodeProject that gave a nice shell for me to use. But, just having this shell is not enough, understanding what was going on is needed to actually utilize the thing.
COM is a system where you can query about various objects in the system. This means implementing a lot of interfaces that Windows expects. This also means using Windows types that are all over the code. At some point, I really want to go through and document these, but for the most part the MSDN documentation was not to bad. A good example of this is Embed.cpp and its corresponding header file. These will show the interfaces that I had to override and to actually get a browser context in
From the browser, it was now trying to figure out how to grab the actual scripting instance. This meant learning about the IDispatch system. Essentially, it is the way to event in Windows. Think of it like the addEventListener or 'on' method you see in a lot of JavaScript code. We set up handlers for DISPIDs which are the names. These can handle various flags such as DISPATCH_METHOD (making a function call). You then can grab the arguments from this in the DISPPARAMS struct that you are given. I will not go into the details here, but it is essentially all the information and types that you will need and that are passed from the JavaScript context.
By figuring out this piece, it was now figuring out eventing in JavaScript and how that complies with C++ and this IDispatch system. Well, come to find out, that a callback that you pass a function is just an IDispatch object that is passed to you! You call the Invoke method on this guy to call the callback. You can pass parameters through the DISPPARAMS structure yourself, and now you events setup!
After figuring out that this whole system relies on the IDispatch interface, I made a guess that when you call new that it is actually wanting to create a new IDispatch object in the background. Sure enough, this is what it does. If you have that property and new is called, you create an IDispatch object from a class you created. This is seen with the Test.cpp and corresponding header.
So now that the scripting system and window system is figured out in Windows, I want to make the error handling more robust. Currently, if you write a bad script, it just blows the Window up with an exception. I need to handle these the way a modern browser does. I will also like to link the debug console up so that you have access to that.
As stated above, I want to give a more in depth analysis and will most likely set something up over the weekend, but patience is key on that.