static Variant writeSafeArray(String string) { // Create a one dimensional safearray containing two VT_UI1 values // where VT_UI1 is an unsigned char // Define cDims, fFeatures and cbElements short cDims = 1; short FADF_FIXEDSIZE = 0x10; short FADF_HAVEVARTYPE = 0x80; short fFeatures = (short) (FADF_FIXEDSIZE | FADF_HAVEVARTYPE); int cbElements = 1; // Create a pointer and copy the data into it int count = string.length(); char[] chars = new char[count + 1]; string.getChars(0, count, chars, 0); int cchMultiByte = OS.WideCharToMultiByte(CodePage, 0, chars, -1, null, 0, null, null); if (cchMultiByte == 0) return null; long /*int*/ pvData = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, cchMultiByte); OS.WideCharToMultiByte(CodePage, 0, chars, -1, pvData, cchMultiByte, null, null); int cElements1 = cchMultiByte; int lLbound1 = 0; // Create a safearray in memory long /*int*/ pSafeArray = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, SAFEARRAY.sizeof); SAFEARRAY safeArray = new SAFEARRAY(); safeArray.cDims = cDims; safeArray.fFeatures = fFeatures; safeArray.cbElements = cbElements; safeArray.pvData = pvData; SAFEARRAYBOUND safeArrayBound = new SAFEARRAYBOUND(); safeArray.rgsabound = safeArrayBound; safeArrayBound.cElements = cElements1; safeArrayBound.lLbound = lLbound1; OS.MoveMemory(pSafeArray, safeArray, SAFEARRAY.sizeof); // Create a variant in memory to hold the safearray long /*int*/ pVariant = OS.GlobalAlloc(OS.GMEM_FIXED | OS.GMEM_ZEROINIT, Variant.sizeof); short vt = (short) (OLE.VT_ARRAY | OLE.VT_UI1); OS.MoveMemory(pVariant, new short[] {vt}, 2); OS.MoveMemory(pVariant + 8, new long /*int*/[] {pSafeArray}, OS.PTR_SIZEOF); // Create a by ref variant Variant variantByRef = new Variant(pVariant, (short) (OLE.VT_BYREF | OLE.VT_VARIANT)); return variantByRef; }