Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / basic / source / runtime / dllmgr-x64.cxx
blobd36e7238e20fd9029bf373540db071f07c316702
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #if defined(_WIN32)
23 #include <prewin.h>
24 #include <postwin.h>
25 #endif
27 #include <algorithm>
28 #include <cstddef>
29 #include <map>
30 #include <vector>
32 #include <basic/sbx.hxx>
33 #include <basic/sbxvar.hxx>
34 #include <runtime.hxx>
35 #include <osl/thread.h>
36 #include <osl/diagnose.h>
37 #include <rtl/ref.hxx>
38 #include <rtl/string.hxx>
39 #include <rtl/ustring.hxx>
40 #include <sal/log.hxx>
41 #include <salhelper/simplereferenceobject.hxx>
42 #include <o3tl/char16_t2wchar_t.hxx>
44 #undef max
46 #include "dllmgr.hxx"
48 using namespace css;
50 /* Open issues:
52 Missing support for functions returning structs (see TODO in call()).
54 Missing support for additional data types (64 bit integers, Any, ...; would
55 trigger OSL_ASSERT(false) in various switches).
57 It is assumed that the variables passed into SbiDllMgr::Call to represent
58 the arguments and return value have types that exactly match the Declare
59 statement; it would be better if this code had access to the function
60 signature from the Declare statement, so that it could convert the passed
61 variables accordingly.
64 namespace {
66 char * address(std::vector< char > & blob) {
67 return blob.empty() ? nullptr : blob.data();
70 ErrCode convert(OUString const & source, OString * target) {
71 return
72 source.convertToString(
73 target, osl_getThreadTextEncoding(),
74 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
75 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))
76 ? ERRCODE_NONE : ERRCODE_BASIC_BAD_ARGUMENT;
77 //TODO: more specific errcode?
80 ErrCode convert(char const * source, sal_Int32 length, OUString * target) {
81 return
82 rtl_convertStringToUString(
83 &target->pData, source, length, osl_getThreadTextEncoding(),
84 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
85 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
86 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))
87 ? ERRCODE_NONE : ERRCODE_BASIC_BAD_ARGUMENT;
88 //TODO: more specific errcode?
91 struct UnmarshalData {
92 UnmarshalData(SbxVariable * theVariable, void * theBuffer):
93 variable(theVariable), buffer(theBuffer) {}
95 SbxVariable * variable;
96 void * buffer;
99 struct StringData: public UnmarshalData {
100 StringData(SbxVariable * theVariable, void * theBuffer, bool theSpecial):
101 UnmarshalData(theVariable, theBuffer), special(theSpecial) {}
103 bool special;
106 class MarshalData {
107 public:
108 MarshalData() = default;
109 MarshalData(const MarshalData&) = delete;
110 const MarshalData& operator=(const MarshalData&) = delete;
112 std::vector< char > * newBlob() {
113 blobs_.push_back(std::vector< char >());
114 return &blobs_.back();
117 std::vector< UnmarshalData > unmarshal;
119 std::vector< StringData > unmarshalStrings;
121 private:
122 std::vector< std::vector< char > > blobs_;
125 std::size_t align(std::size_t address, std::size_t alignment) {
126 // alignment = 2^k for some k >= 0
127 return (address + (alignment - 1)) & ~(alignment - 1);
130 char * align(
131 std::vector< char > & blob, std::size_t alignment, std::size_t offset,
132 std::size_t add)
134 std::vector< char >::size_type n = blob.size();
135 n = align(n - offset, alignment) + offset; //TODO: overflow in align()
136 blob.resize(n + add); //TODO: overflow
137 return address(blob) + n;
140 template< typename T > void add(
141 std::vector< char > & blob, T const & data, std::size_t alignment,
142 std::size_t offset)
144 *reinterpret_cast< T * >(align(blob, alignment, offset, sizeof (T))) = data;
147 std::size_t alignment(SbxVariable const * variable) {
148 OSL_ASSERT(variable != nullptr);
149 if ((variable->GetType() & SbxARRAY) == 0) {
150 switch (variable->GetType()) {
151 case SbxINTEGER:
152 return 2;
153 case SbxLONG:
154 case SbxSINGLE:
155 case SbxSTRING:
156 return 4;
157 case SbxDOUBLE:
158 return 8;
159 case SbxOBJECT:
161 std::size_t n = 1;
162 SbxObject* pobj = dynamic_cast<SbxObject*>(variable->GetObject());
163 assert(pobj);
164 SbxArray* props = pobj->GetProperties();
165 for (sal_uInt16 i = 0; i < props->Count(); ++i) {
166 n = std::max(n, alignment(props->Get(i)));
168 return n;
170 case SbxBOOL:
171 case SbxBYTE:
172 return 1;
173 default:
174 OSL_ASSERT(false);
175 return 1;
177 } else {
178 SbxDimArray * arr = dynamic_cast<SbxDimArray*>( variable->GetObject() );
179 assert(arr);
180 int dims = arr->GetDims();
181 std::vector< sal_Int32 > low(dims);
182 for (int i = 0; i < dims; ++i) {
183 sal_Int32 up;
184 arr->GetDim32(i + 1, low[i], up);
186 return alignment(arr->Get32(low.data()));
190 ErrCode marshal(
191 bool outer, SbxVariable * variable, bool special,
192 std::vector< char > & blob, std::size_t offset, MarshalData & data);
194 ErrCode marshalString(
195 SbxVariable * variable, bool special, MarshalData & data, void ** buffer)
197 OSL_ASSERT(variable != nullptr && buffer != nullptr);
198 OString str;
199 ErrCode e = convert(variable->GetOUString(), &str);
200 if (e != ERRCODE_NONE) {
201 return e;
203 std::vector< char > * blob = data.newBlob();
204 blob->insert(blob->begin(), str.getStr(), str.getStr() + str.getLength() + 1);
205 *buffer = address(*blob);
206 data.unmarshalStrings.push_back(StringData(variable, *buffer, special));
207 return ERRCODE_NONE;
210 ErrCode marshalStruct(
211 SbxVariable const * variable, std::vector< char > & blob, std::size_t offset,
212 MarshalData & data)
214 OSL_ASSERT(variable != nullptr);
215 SbxObject* pobj = dynamic_cast<SbxObject*>(variable->GetObject());
216 assert(pobj);
217 SbxArray* props = pobj->GetProperties();
218 for (sal_uInt16 i = 0; i < props->Count(); ++i) {
219 ErrCode e = marshal(false, props->Get(i), false, blob, offset, data);
220 if (e != ERRCODE_NONE) {
221 return e;
224 return ERRCODE_NONE;
227 ErrCode marshalArray(
228 SbxVariable const * variable, std::vector< char > & blob, std::size_t offset,
229 MarshalData & data)
231 OSL_ASSERT(variable != nullptr);
232 SbxDimArray * arr = dynamic_cast<SbxDimArray*>( variable->GetObject() );
233 assert(arr);
234 int dims = arr->GetDims();
235 std::vector< sal_Int32 > low(dims);
236 std::vector< sal_Int32 > up(dims);
237 for (int i = 0; i < dims; ++i) {
238 arr->GetDim32(i + 1, low[i], up[i]);
240 for (std::vector< sal_Int32 > idx = low;;) {
241 ErrCode e = marshal(
242 false, arr->Get32(idx.data()), false, blob, offset, data);
243 if (e != ERRCODE_NONE) {
244 return e;
246 int i = dims - 1;
247 while (idx[i] == up[i]) {
248 idx[i] = low[i];
249 if (i == 0) {
250 return ERRCODE_NONE;
252 --i;
254 ++idx[i];
258 // 8-aligned structs are only 4-aligned on stack, so alignment of members in
259 // such structs must take that into account via "offset"
260 ErrCode marshal(
261 bool outer, SbxVariable * variable, bool special,
262 std::vector< char > & blob, std::size_t offset, MarshalData & data)
264 OSL_ASSERT(variable != nullptr);
266 SbxDataType eVarType = variable->GetType();
267 bool bByVal = !(variable->GetFlags() & SbxFlagBits::Reference);
268 if( !bByVal && !SbiRuntime::isVBAEnabled() && eVarType == SbxSTRING )
269 bByVal = true;
271 if (bByVal) {
272 if ((eVarType & SbxARRAY) == 0) {
273 switch (eVarType) {
274 case SbxINTEGER:
275 add(blob, variable->GetInteger(), outer ? 8 : 2, offset);
276 break;
277 case SbxLONG:
278 add(blob, variable->GetLong(), outer ? 8 : 4, offset);
279 break;
280 case SbxSINGLE:
281 add(blob, variable->GetSingle(), outer ? 8 : 4, offset);
282 break;
283 case SbxDOUBLE:
284 add(blob, variable->GetDouble(), 8, offset);
285 break;
286 case SbxSTRING:
288 void * p;
289 ErrCode e = marshalString(variable, special, data, &p);
290 if (e != ERRCODE_NONE) {
291 return e;
293 add(blob, p, 8, offset);
294 break;
296 case SbxOBJECT:
298 align(blob, outer ? 8 : alignment(variable), offset, 0);
299 ErrCode e = marshalStruct(variable, blob, offset, data);
300 if (e != ERRCODE_NONE) {
301 return e;
303 break;
305 case SbxBOOL:
306 add(blob, variable->GetBool(), outer ? 8 : 1, offset);
307 break;
308 case SbxBYTE:
309 add(blob, variable->GetByte(), outer ? 8 : 1, offset);
310 break;
311 default:
312 OSL_ASSERT(false);
313 break;
315 } else {
316 ErrCode e = marshalArray(variable, blob, offset, data);
317 if (e != ERRCODE_NONE) {
318 return e;
321 } else {
322 if ((eVarType & SbxARRAY) == 0) {
323 switch (eVarType) {
324 case SbxINTEGER:
325 case SbxLONG:
326 case SbxSINGLE:
327 case SbxDOUBLE:
328 case SbxBOOL:
329 case SbxBYTE:
330 add(blob, variable->data(), 8, offset);
331 break;
332 case SbxSTRING:
334 void * p;
335 ErrCode e = marshalString(variable, special, data, &p);
336 if (e != ERRCODE_NONE) {
337 return e;
339 std::vector< char >* blob2 = data.newBlob();
340 add(*blob2, p, 8, 0);
341 add(blob, address(*blob2), 8, offset);
342 break;
344 case SbxOBJECT:
346 std::vector< char > * blob2 = data.newBlob();
347 ErrCode e = marshalStruct(variable, *blob2, 0, data);
348 if (e != ERRCODE_NONE) {
349 return e;
351 void * p = address(*blob2);
352 if (outer) {
353 data.unmarshal.push_back(UnmarshalData(variable, p));
355 add(blob, p, 8, offset);
356 break;
358 default:
359 OSL_ASSERT(false);
360 break;
362 } else {
363 std::vector< char > * blob2 = data.newBlob();
364 ErrCode e = marshalArray(variable, *blob2, 0, data);
365 if (e != ERRCODE_NONE) {
366 return e;
368 void * p = address(*blob2);
369 if (outer) {
370 data.unmarshal.push_back(UnmarshalData(variable, p));
372 add(blob, p, 8, offset);
375 return ERRCODE_NONE;
378 template< typename T > T read(void const ** pointer) {
379 T const * p = static_cast< T const * >(*pointer);
380 *pointer = static_cast< void const * >(p + 1);
381 return *p;
384 void const * unmarshal(SbxVariable * variable, void const * data) {
385 OSL_ASSERT(variable != nullptr);
386 if ((variable->GetType() & SbxARRAY) == 0) {
387 switch (variable->GetType()) {
388 case SbxINTEGER:
389 variable->PutInteger(read< sal_Int16 >(&data));
390 break;
391 case SbxLONG:
392 variable->PutLong(read< sal_Int32 >(&data));
393 break;
394 case SbxSINGLE:
395 variable->PutSingle(read< float >(&data));
396 break;
397 case SbxDOUBLE:
398 variable->PutDouble(read< double >(&data));
399 break;
400 case SbxSTRING:
401 read< char * >(&data); // handled by unmarshalString
402 break;
403 case SbxOBJECT:
405 data = reinterpret_cast< void const * >(
406 align(
407 reinterpret_cast< sal_uIntPtr >(data),
408 alignment(variable)));
409 SbxObject* pobj = dynamic_cast<SbxObject*>(variable->GetObject());
410 assert(pobj);
411 SbxArray* props = pobj->GetProperties();
412 for (sal_uInt16 i = 0; i < props->Count(); ++i) {
413 data = unmarshal(props->Get(i), data);
415 break;
417 case SbxBOOL:
418 variable->PutBool(read< sal_Bool >(&data));
419 break;
420 case SbxBYTE:
421 variable->PutByte(read< sal_uInt8 >(&data));
422 break;
423 default:
424 OSL_ASSERT(false);
425 break;
427 } else {
428 SbxDimArray * arr = dynamic_cast<SbxDimArray*>( variable->GetObject() );
429 assert(arr);
430 int dims = arr->GetDims();
431 std::vector< sal_Int32 > low(dims);
432 std::vector< sal_Int32 > up(dims);
433 for (int i = 0; i < dims; ++i) {
434 arr->GetDim32(i + 1, low[i], up[i]);
436 for (std::vector< sal_Int32 > idx = low;;) {
437 data = unmarshal(arr->Get32(idx.data()), data);
438 int i = dims - 1;
439 while (idx[i] == up[i]) {
440 idx[i] = low[i];
441 if (i == 0) {
442 goto done;
444 --i;
446 ++idx[i];
448 done:;
450 return data;
453 ErrCode unmarshalString(StringData const & data, SbxVariable const & result) {
454 OUString str;
455 if (data.buffer != nullptr) {
456 char const * p = static_cast< char const * >(data.buffer);
457 sal_Int32 len;
458 if (data.special) {
459 len = static_cast< sal_Int32 >(result.GetULong());
460 if (len < 0) { // i.e., DWORD result >= 2^31
461 return ERRCODE_BASIC_BAD_ARGUMENT;
462 //TODO: more specific errcode?
464 } else {
465 len = rtl_str_getLength(p);
467 ErrCode e = convert(p, len, &str);
468 if (e != ERRCODE_NONE) {
469 return e;
472 data.variable->PutString(str);
473 return ERRCODE_NONE;
476 struct ProcData {
477 OString name;
478 FARPROC proc;
481 ErrCode call(
482 OUString const & dll, ProcData const & proc, SbxArray * arguments,
483 SbxVariable & result)
485 if (arguments && arguments->Count() > 20)
486 return ERRCODE_BASIC_NOT_IMPLEMENTED;
488 std::vector< char > stack;
489 MarshalData data;
491 // For DWORD GetLogicalDriveStringsA(DWORD nBufferLength, LPSTR lpBuffer)
492 // from kernel32, upon return, filled lpBuffer length is result DWORD, which
493 // requires special handling in unmarshalString; other functions might
494 // require similar treatment, too:
495 bool special =
496 dll.equalsIgnoreAsciiCase("KERNEL32.DLL") &&
497 (proc.name == OString("GetLogicalDriveStringsA"));
498 for (int i = 1; i < (arguments == nullptr ? 0 : arguments->Count()); ++i) {
499 ErrCode e = marshal(
500 true, arguments->Get(i), special && i == 2, stack, stack.size(),
501 data);
502 if (e != ERRCODE_NONE) {
503 return e;
505 align(stack, 8, 0, 0);
508 stack.resize(20*8);
510 // We fake all calls as being to a varargs function,
511 // as this means any floating-point argument among the first four
512 // ones will end up in a XMM register where the callee expects it.
513 sal_Int32 (*proc_i)(double d, ...) = reinterpret_cast<sal_Int32 (*)(double, ...)>(proc.proc);
514 double (*proc_d)(double d, ...) = reinterpret_cast<double (*)(double, ...)>(proc.proc);
516 sal_Int64 iRetVal = 0;
517 double dRetVal = 0.0;
519 switch (result.GetType()) {
520 case SbxEMPTY:
521 case SbxINTEGER:
522 case SbxLONG:
523 case SbxSTRING:
524 case SbxOBJECT:
525 case SbxBOOL:
526 case SbxBYTE:
528 auto const st = stack.data();
529 iRetVal =
530 proc_i(*reinterpret_cast<double *>(st + 0),
531 *reinterpret_cast<double *>(st + 1*8),
532 *reinterpret_cast<double *>(st + 2*8),
533 *reinterpret_cast<double *>(st + 3*8),
534 *reinterpret_cast<sal_uInt64 *>(st + 4*8),
535 *reinterpret_cast<sal_uInt64 *>(st + 5*8),
536 *reinterpret_cast<sal_uInt64 *>(st + 6*8),
537 *reinterpret_cast<sal_uInt64 *>(st + 7*8),
538 *reinterpret_cast<sal_uInt64 *>(st + 8*8),
539 *reinterpret_cast<sal_uInt64 *>(st + 9*8),
540 *reinterpret_cast<sal_uInt64 *>(st + 10*8),
541 *reinterpret_cast<sal_uInt64 *>(st + 11*8),
542 *reinterpret_cast<sal_uInt64 *>(st + 12*8),
543 *reinterpret_cast<sal_uInt64 *>(st + 13*8),
544 *reinterpret_cast<sal_uInt64 *>(st + 14*8),
545 *reinterpret_cast<sal_uInt64 *>(st + 15*8),
546 *reinterpret_cast<sal_uInt64 *>(st + 16*8),
547 *reinterpret_cast<sal_uInt64 *>(st + 17*8),
548 *reinterpret_cast<sal_uInt64 *>(st + 18*8),
549 *reinterpret_cast<sal_uInt64 *>(st + 19*8));
550 break;
552 case SbxSINGLE:
553 case SbxDOUBLE:
555 auto const st = stack.data();
556 dRetVal =
557 proc_d(*reinterpret_cast<double *>(st + 0),
558 *reinterpret_cast<double *>(st + 1*8),
559 *reinterpret_cast<double *>(st + 2*8),
560 *reinterpret_cast<double *>(st + 3*8),
561 *reinterpret_cast<sal_uInt64 *>(st + 4*8),
562 *reinterpret_cast<sal_uInt64 *>(st + 5*8),
563 *reinterpret_cast<sal_uInt64 *>(st + 6*8),
564 *reinterpret_cast<sal_uInt64 *>(st + 7*8),
565 *reinterpret_cast<sal_uInt64 *>(st + 8*8),
566 *reinterpret_cast<sal_uInt64 *>(st + 9*8),
567 *reinterpret_cast<sal_uInt64 *>(st + 10*8),
568 *reinterpret_cast<sal_uInt64 *>(st + 11*8),
569 *reinterpret_cast<sal_uInt64 *>(st + 12*8),
570 *reinterpret_cast<sal_uInt64 *>(st + 13*8),
571 *reinterpret_cast<sal_uInt64 *>(st + 14*8),
572 *reinterpret_cast<sal_uInt64 *>(st + 15*8),
573 *reinterpret_cast<sal_uInt64 *>(st + 16*8),
574 *reinterpret_cast<sal_uInt64 *>(st + 17*8),
575 *reinterpret_cast<sal_uInt64 *>(st + 18*8),
576 *reinterpret_cast<sal_uInt64 *>(st + 19*8));
577 break;
579 default:
580 break;
583 switch (result.GetType()) {
584 case SbxEMPTY:
585 break;
586 case SbxINTEGER:
587 result.PutInteger(static_cast< sal_Int16 >(iRetVal));
588 break;
589 case SbxLONG:
590 result.PutLong(static_cast< sal_Int32 >(iRetVal));
591 break;
592 case SbxSINGLE:
593 result.PutSingle(static_cast< float >(dRetVal));
594 break;
595 case SbxDOUBLE:
596 result.PutDouble(dRetVal);
597 break;
598 case SbxSTRING:
600 char const * s1 = reinterpret_cast< char const * >(iRetVal);
601 OUString s2;
602 ErrCode e = convert(s1, rtl_str_getLength(s1), &s2);
603 if (e != ERRCODE_NONE) {
604 return e;
606 result.PutString(s2);
607 break;
609 case SbxOBJECT:
610 //TODO
611 break;
612 case SbxBOOL:
613 result.PutBool(bool(iRetVal));
614 break;
615 case SbxBYTE:
616 result.PutByte(static_cast< sal_uInt8 >(iRetVal));
617 break;
618 default:
619 OSL_ASSERT(false);
620 break;
622 for (int i = 1; i < (arguments == nullptr ? 0 : arguments->Count()); ++i) {
623 arguments->Get(i)->ResetFlag(SbxFlagBits::Reference);
624 //TODO: skipped for errors?!?
626 for (auto const& elem : data.unmarshal)
628 unmarshal(elem.variable, elem.buffer);
630 for (auto const& elem : data.unmarshalStrings)
632 ErrCode e = unmarshalString(elem, result);
633 if (e != ERRCODE_NONE) {
634 return e;
637 return ERRCODE_NONE;
640 ErrCode getProcData(HMODULE handle, OUString const & name, ProcData * proc)
642 OSL_ASSERT(proc != nullptr);
643 if (name.getLength() != 0 && name[0] == '@') { //TODO: "@" vs. "#"???
644 sal_Int32 n = name.copy(1).toInt32(); //TODO: handle bad input
645 if (n <= 0 || n > 0xFFFF) {
646 return ERRCODE_BASIC_BAD_ARGUMENT; //TODO: more specific errcode?
648 FARPROC p = GetProcAddress(handle, reinterpret_cast< LPCSTR >(n));
649 if (p != nullptr) {
650 proc->name = "#" + OString::number(n);
651 proc->proc = p;
652 return ERRCODE_NONE;
654 } else {
655 OString name8;
656 ErrCode e = convert(name, &name8);
657 if (e != ERRCODE_NONE) {
658 return e;
660 FARPROC p = GetProcAddress(handle, name8.getStr());
661 if (p != nullptr) {
662 proc->name = name8;
663 proc->proc = p;
664 return ERRCODE_NONE;
666 sal_Int32 i = name8.indexOf('#');
667 if (i != -1) {
668 name8 = name8.copy(0, i);
669 p = GetProcAddress(handle, name8.getStr());
670 if (p != nullptr) {
671 proc->name = name8;
672 proc->proc = p;
673 return ERRCODE_NONE;
676 OString real("_" + name8);
677 p = GetProcAddress(handle, real.getStr());
678 if (p != nullptr) {
679 proc->name = real;
680 proc->proc = p;
681 return ERRCODE_NONE;
683 real = name8 + "A";
684 p = GetProcAddress(handle, real.getStr());
685 if (p != nullptr) {
686 proc->name = real;
687 proc->proc = p;
688 return ERRCODE_NONE;
691 return ERRCODE_BASIC_PROC_UNDEFINED;
694 struct Dll: public salhelper::SimpleReferenceObject {
695 private:
696 typedef std::map< OUString, ProcData > Procs;
698 virtual ~Dll() override;
700 public:
701 Dll(): handle(nullptr) {}
703 ErrCode getProc(OUString const & name, ProcData * proc);
705 HMODULE handle;
706 Procs procs;
709 Dll::~Dll() {
710 if (handle != nullptr && !FreeLibrary(handle)) {
711 SAL_WARN("basic", "FreeLibrary(" << handle << ") failed with " << GetLastError());
715 ErrCode Dll::getProc(OUString const & name, ProcData * proc) {
716 Procs::iterator i(procs.find(name));
717 if (i != procs.end()) {
718 *proc = i->second;
719 return ERRCODE_NONE;
721 ErrCode e = getProcData(handle, name, proc);
722 if (e == ERRCODE_NONE) {
723 procs.emplace(name, *proc);
725 return e;
728 OUString fullDllName(OUString const & name) {
729 OUString full(name);
730 if (full.indexOf('.') == -1) {
731 full += ".DLL";
733 return full;
738 struct SbiDllMgr::Impl{
739 private:
740 typedef std::map< OUString, ::rtl::Reference< Dll > > Dlls;
742 public:
743 Impl() = default;
744 Impl(const Impl&) = delete;
745 const Impl& operator=(const Impl&) = delete;
747 Dll * getDll(OUString const & name);
749 Dlls dlls;
752 Dll * SbiDllMgr::Impl::getDll(OUString const & name) {
753 Dlls::iterator i(dlls.find(name));
754 if (i == dlls.end()) {
755 i = dlls.emplace(name, new Dll).first;
756 HMODULE h = LoadLibraryW(o3tl::toW(name.getStr()));
757 if (h == nullptr) {
758 dlls.erase(i);
759 return nullptr;
761 i->second->handle = h;
763 return i->second.get();
766 ErrCode SbiDllMgr::Call(
767 OUString const & function, OUString const & library,
768 SbxArray * arguments, SbxVariable & result, bool cdeclConvention)
770 if (cdeclConvention) {
771 return ERRCODE_BASIC_NOT_IMPLEMENTED;
773 OUString dllName(fullDllName(library));
774 Dll * dll = impl_->getDll(dllName);
775 if (dll == nullptr) {
776 return ERRCODE_BASIC_BAD_DLL_LOAD;
778 ProcData proc;
779 ErrCode e = dll->getProc(function, &proc);
780 if (e != ERRCODE_NONE) {
781 return e;
783 return call(dllName, proc, arguments, result);
786 void SbiDllMgr::FreeDll(OUString const & library) {
787 impl_->dlls.erase(library);
790 SbiDllMgr::SbiDllMgr(): impl_(new Impl) {}
792 SbiDllMgr::~SbiDllMgr() {}
794 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */