Skip to content

Some More About Windows

Justin Scherer edited this page May 23, 2018 · 1 revision

Windows COM/OLE Programming is NOT Fun

There may be some that disagree with this statement, but after working with the system for a little over a month and a half, I can clearly state that the Window's programming model is not fun to program. A good example of this is the type SAFEARRAY that is we will need to get access to when we are passing arrays from the JavaScript runtime to our C++ code. Here is an example of this type of code:

				Buffer* buffers;
				SafeArrayAccessData(args, (void**)&buffers);
				long lb, ub;
				SafeArrayGetLBound(args, 1, &lb);
				SafeArrayGetUBound(args, 1, &ub);
				long numElements = ub - lb + 1;
				long byteCount = 0;
				for (int i = 0; i < numElements; i++) {
					byteCount += sizeof(buffers[i].internArr->getBase());
				}
				byte* nBuf = new byte[byteCount];
				byteCount = 0;
				for (int i = 0; i < numElements; i++) {
					memcpy(nBuf, buffers[i].internArr->getBase(), sizeof(buffers[i].internArr->getBase()));
					nBuf += sizeof(buffers[i].internArr->getBase());
				}
				std::unique_ptr<Buffer> buf = std::make_unique<Buffer>(nBuf);
				result->vt = VT_DISPATCH;
				result->pdispVal = buf.release();
				SafeArrayUnaccessData(args);```

There is quite a bit going on here, but the basic idea is that we want to change this SAFEARRAY type object to a basic byte array. Buffers in Node should be treated as just bytes, so that is how we handle the data we are sent. But, because of the SAFEARRAY needing to be safe for multi-threaded applications, these are the steps we have to go to to get access to the data. 

## Why Show This
Many may not believe this, but programming for an OS is usually fun. I quite enjoy the POSIX style of programming. Even doing work in Qt or Gnome was fun. Windows did not adopt a lot of these ideas until much later (Windows 10 and the UWP system). Because of the need to support different Window's OSs, we need to program in the base system.

So by showcasing this, I am hoping to set a precedent that it is not the easiest to deal with and I am hoping to abstract all of this away. Currently, all of this code is inline when we are making various calls to the C++ backend. New Utility classes are going to be made to make working with these objects a bit easier. I am hoping with these helper classes and some strict testing, it should be make getting more developers into the system. 
Clone this wiki locally