-
How can I get the MODULEENTRY32.szModule 's |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 8 replies
-
If this were using a UTF-16 character array, there would be a ToString() method on the struct that would produce what you want. This is how you can do it. It produces the string "abc": using System.Text;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.System.Diagnostics.ToolHelp;
MODULEENTRY32 m = default;
m.szModule._0 = (CHAR)(byte)'a';
m.szModule._1 = (CHAR)(byte)'b';
m.szModule._2 = (CHAR)(byte)'c';
unsafe
{
byte* pszModule = (byte*)&m.szModule;
string szModule = Encoding.UTF8.GetString(pszModule, GetLength(pszModule));
}
static unsafe int GetLength(byte* pszStr)
{
int len = 0;
while (*pszStr++ != 0)
{
len += 1;
}
return len;
} |
Beta Was this translation helpful? Give feedback.
-
C# 11 introduced a new feature called public static unsafe ReadOnlySpan<byte> AsNullTerminatedReadOnlySpan(this in __CHAR_256 char256)
{
fixed (CHAR* pszChar = &char256._0)
{
return MemoryMarshal.CreateReadOnlySpanFromNullTerminated((byte*)pszChar);
}
} |
Beta Was this translation helpful? Give feedback.
If this were using a UTF-16 character array, there would be a ToString() method on the struct that would produce what you want.
But as this is a
CHAR
inline array, you have to do it yourself. I filed #636 to improve this.This is how you can do it. It produces the string "abc":