TODO: It is unclear what to do if the VBA code runs in a 32-bit environment but only a 64-bit MinGW compiler is available. Using the -m32 option of gcc resulted in a linker error.
Microsoft's (visual?) compiler
With Microsofts Compiler, I created the DLLs like so
I didn't find any option in cl to undecorate exported function names, therefore, I needed a dll.def file.
Referencing additional libraries
The returning a string example uses the SysAllocStringWinAPI call which is implemented in oleaut32.dll. Thus, at least with the GNU compiler, oleaut32.lib needs to be linked as well:
In order for the VBA run time environment to find the DLL, the directory path to the DLL needs to be either in the %PATH% environment variable or the full path to the dll needs to specified in the declare statement.
The directory path to the DLL can be added to the PATH variable in cmd.exe like so
This link contains some notes for developping DLLs for use with Visual Basic. It's a bit dated though (1996), and was written for VB 5.
Apparently, when strings are passed to DLL via the declare construct, the runtime of Visual Basic runtime does an automatic Unicode to Ansi and vice versa conversion. This seems to be the case even if the string is passed with an as any type.