I'm having some difficulties using a struct to call some Windows kernel32 function. I'm trying to call the function FindFirstStreamW, which return, among others, a WIN32_FIND_STREAM_DATA struct.
I use this struct in C# to represent it :
[StructLayout(LayoutKind.Sequential)]
public unsafe struct WIN32_FIND_STREAM_DATA
{
public long StreamSize;
public fixed char StreamName [MAX_PATH + 32];
}
But I have problems using the StreamName afterward. After a call to the function, the StreamName buffer seems to contains a semicolon (which is expected) and then only random data.
I experimented by replacing the fixed buffer by consecutives char such as s1, s2, s3 etc... and it worked (s1 contained ':', then s2 '$' etc... all correct !).
[StructLayout(LayoutKind.Sequential)]
public unsafe struct WIN32_FIND_STREAM_DATA
{
public long StreamSize;
public char s1;
public char s2;
public char s3;
}
I must therefore miss something but can't find what and after 8 firefox windows filled with google researches, I'm pretty desperate.
Here is my DLLImport :
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr FindFirstStreamW(
[MarshalAs(UnmanagedType.LPTStr)] string filename,
StreamInfoLevels infoLevel,
out WIN32_FIND_STREAM_DATA data,
int reserved = 0
);
And there is my test code :
WIN32_FIND_STREAM_DATA data = default(WIN32_FIND_STREAM_DATA);
IntPtr search = FindFirstStreamW(D_TMP_TESTHANDLE_TXT, StreamInfoLevels.FindStreamInfoStandard, out data);
Debug.WriteLine(data.StreamSize);
Debug.WriteLine(new string(data.StreamName));
FindClose(search);
Thanks in advance for your help.