maandag 26 februari 2024

Everything about windows DLL's

This presentation from CppCon 2017 is really helpful for understanding how DLL's are loaded and functions are exported.

You can read the live debug log from the loader, by setting the SLS on an executable.

gflags /i PrintGreeting.exe +sls
cdb PrintGreeting.exe
gflags /i PrintGreeting.exe -sls

You can export data using:
extern "C" __declspec(dllimport) int const One = 1;
extern "C" __declspec(dllimport) int const Two = 2;

link Constants.obj /dll /out:Constants.dll /noentry /nodefaultlib /export:One,DATA /export:Two,DATA


You can have thread variables using:
Thread local storage:
__declspec(thread) int x = 10;

Dumpbin.exe is more powerful than you might think

dumpbin.exe /exports msvcrt.dll > msvcrt.exports
dumpbin.exe /headers hello.dll
dumpbin.exe /rawdata /section:.text hello.dll
dumpbin.exe /disasm /section:.text hello.dll

dumpbin.exe /dependents PrintGreeting.exe     -> which DLL's are loaded as dependencies
dumpbin.exe /imports PrintGreeting.exe        -> which functions are used in the dependencies

dumpbin.exe /exports hello.lib

MSVCRT.dll will remain in Windows

When creating a small C program, it is necessary to skip the shipping of the C runtime DLL's. Why do you need all those DLL's when you just want to printf something or write a file?

There is only one DLL needed, and that is the old MSVCRT.DLL. This DLL is available since Windows95 or something. It is available on ALL Microsoft Windows systems.

And it is still updated!  I saw an update in August 2023 and October 2023. Why is it still updated? Well it is an KNOWN WINDOWS CORE DLL!

Yes, it is special, even to Microsoft. A lot of system software relies on this DLL and they won't stop shipping the DLL.

There is a list of these special known DLL's. Open the registry editor and go to:

Computer\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs

And in the list with entries like kernel32.dll, ole32.dll, user32.dl and gdi32.dll, you see MSVCRT.dll. Great! Something we can build on.


dinsdag 6 februari 2024

Call Angular method from Javascript

There are situations in which your context becomes javascript and you
need to get back to the Angular context.

Do this with a CustomEvent.

In Javascript do:
function (theDetails){
	var event = new CustomEvent("uniquenamefor.thecustomevent", { 
       detail: theDetails, bubbles: true, cancelable: true
    });    
    window.dispatchEvent(event);
}

This event will be send in the webbrower and we need Anuglar to pick it up.

In Angular do:
import { HostListener, Component } from '@angular/core';

    @HostListener('window:uniquenamefor.thecustomevent', ['$event']) 
    async onUniqueCustomEvent(event) {
    	alert(event.detail);
    }
    
The Angular framework only allows HostListeners on components/directives, so
if you want to catch the CustomEvent in a service you need to resort back
to good old Javascript:
   window.addEventListener('uniquenamefor.thecustomevent', 
       this.onUniqueCustomEvent.bind(this));