Search notes:

Accessing and calling DLLs from VBA: returning an array of strings

dll.c

//
//   This DLL must be linked to OleAut32:
//
//       cl /LD dll.c oleAut32.lib /Fethe.dll /link /def:dll.def
//

#include <windows.h>

__declspec(dllexport)  SAFEARRAY* __stdcall stringArray() {

 // Specifiy the bounds of ONE dimenision of the SAFEARRAY:
    SAFEARRAYBOUND dim;

    SAFEARRAY     *ret;
    LONG           index = 0;

    dim.lLbound   = 0; // We want the array to start with index 0
    dim.cElements = 3; // We return 3 values

    ret = SafeArrayCreate(
         VT_BSTR,
         1      , // Array to be returned has one dimension …
        &dim      // … which is described in this (dim) parameter
    );


    SafeArrayPutElement(ret, &index, SysAllocString(L"foo")); index ++;
    SafeArrayPutElement(ret, &index, SysAllocString(L"bar")); index ++;
    SafeArrayPutElement(ret, &index, SysAllocString(L"baz"));

    return ret;
}
Github repository VBA-calls-DLL, path: /return-string-array/dll.c

cal-the-dll.bas

option explicit

declare function stringArray lib "c:\github\VBA-calls-DLL\return-string-array\the.dll" () as string()

sub main() ' {

    dim ary() as string
    dim i     as integer

    ary = stringArray()

    for i = lBound(ary) to uBound(ary)
        debug.print i & ": " & ary(i)
    next i

end sub ' }
Github repository VBA-calls-DLL, path: /return-string-array/call-the-dll.bas

dll.def

LIBRARY the
EXPORTS
  stringArray
Github repository VBA-calls-DLL, path: /return-string-array/dll.def

See also

Determining loaded modules from VBA is similar to this example but demonstrates how a two dimensional array is returned from a DLL to VBA.
Function returning an array.
Accessing and calling DLLs from VBA (Visual Basic for Applications)

Index