Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

c# - PInvoke FbwfFindFirst - FbwfCacheDetail problems

I'm trying to create a PInvoke for FbwfFindFirst and am struggling with the struct FbwfCacheDetail.

In short, I'm not sure how to marshal WCHAR fileName[1]; seeing as it's a variable length array and a non-null terminated.

Any help would be welcomed

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Since the whole structure is of variable size, one way to do this is like this (I can't test it because I don't have this dll on my system):

string volume = "";

int size = 0;
// ask for whole structure size
FbwfFindFirst(volume, IntPtr.Zero, ref size); // this call should return ERROR_MORE_DATA which is ok

// allocate for the structure
var ptr = Marshal.AllocHGlobal(size);
try
{
    FbwfFindFirst(volume, ptr, ref size); // should not return error

    // get the easy part 
    var detail = Marshal.PtrToStructure<FbwfCacheDetail>(ptr);

    // compute filename offset and get the string
    // file name length is in bytes, per documentation
    var fileName = Marshal.PtrToStringUni(ptr + Marshal.OffsetOf<FbwfCacheDetail>("fileName").ToInt32(), detail.fileNameLength / 2);
}
finally
{
    Marshal.FreeHGlobal(ptr);
}

[DllImport("fbwflib", CharSet = CharSet.Unicode)]
static extern int FbwfFindFirst(string volume, IntPtr cacheDetail, ref int size);

[StructLayout(LayoutKind.Sequential)]
struct FbwfCacheDetail
{
    public int cacheSize;
    public int openHandleCount;
    public int fileNameLength;
    byte fileName; // don't use this member
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...