-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
41 nativeaot cant marshal safearray (#42)
Replaced SafeArray marshalling with custom marshalling to get GetNames method working with NativeAOT and added missing method doc.
- Loading branch information
1 parent
b00d4ed
commit a717554
Showing
6 changed files
with
91 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace WmiLight | ||
{ | ||
internal static class SafeArray | ||
{ | ||
internal static IEnumerable<IntPtr> ForEach(IntPtr pSafeArray, uint nDim = 1) | ||
{ | ||
int lBound, uBound; | ||
|
||
HResult hResult = NativeMethods.SafeArrayGetLBound(pSafeArray, nDim, out lBound); | ||
|
||
if (hResult.Failed) | ||
throw (Exception)hResult; | ||
|
||
hResult = NativeMethods.SafeArrayGetUBound(pSafeArray, nDim, out uBound); | ||
|
||
if (hResult.Failed) | ||
throw (Exception)hResult; | ||
|
||
for (int i = lBound; i <= uBound; i++) | ||
{ | ||
hResult = NativeMethods.SafeArrayGetElement(pSafeArray, i, out IntPtr pElement); | ||
|
||
if (hResult.Failed) | ||
throw (Exception)hResult; | ||
|
||
yield return pElement; | ||
} | ||
} | ||
|
||
internal static IEnumerable<string> ForEachBSTR(IntPtr pSafeArray, uint nDim = 1) | ||
{ | ||
foreach (IntPtr pElement in ForEach(pSafeArray, nDim)) | ||
{ | ||
string stringValue = Marshal.PtrToStringBSTR(pElement); | ||
|
||
Marshal.FreeBSTR(pElement); | ||
|
||
yield return stringValue; | ||
} | ||
} | ||
|
||
internal static int GetLength(IntPtr pSafeArray, uint nDim = 1) | ||
{ | ||
int lBound, uBound; | ||
|
||
HResult hResult = NativeMethods.SafeArrayGetLBound(pSafeArray, nDim, out lBound); | ||
|
||
if (hResult.Failed) | ||
throw (Exception)hResult; | ||
|
||
hResult = NativeMethods.SafeArrayGetUBound(pSafeArray, nDim, out uBound); | ||
|
||
if (hResult.Failed) | ||
throw (Exception)hResult; | ||
|
||
return uBound - lBound + 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters