我正在使用找到的代码here直接从outlook拖放.msg文件。 FileDescriptorA
类的实现方式如下:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public sealed class FILEDESCRIPTORA
{
public uint dwFlags;
public Guid clsid;
public SIZEL sizel;
public POINTL pointl;
public uint dwFileAttributes;
public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
public uint nFileSizeHigh;
public uint nFileSizeLow;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string cFileName;
}
获取文件名的代码是:
MemoryStream fgdStream =
(MemoryStream)e.Data.GetData("FileGroupDescriptor");
byte[] fgdBytes = new byte[fgdStream.Length];
fgdStream.Read(fgdBytes, 0, fgdBytes.Length);
fgdStream.Close();
//copy the file group descriptor into unmanaged memory
IntPtr fgdaPtr = Marshal.AllocHGlobal(fgdBytes.Length);
Marshal.Copy(fgdBytes, 0, fgdaPtr, fgdBytes.Length);
int numFiles = Marshal.ReadInt32(fgdaPtr);
string[] fileNames = new string[numFiles];
//get the pointer to the first file descriptor
IntPtr fdPtr = (IntPtr)((int)fgdaPointer + Marshal.SizeOf(fgdaPointer));
//loop for the number of files acording to the file group descriptor
for(int fdIndex = 0;fdIndex < numfiles;fdIndex++)
{
//marshal the pointer to the file descriptor as a FILEDESCRIPTORA struct
object fdObj = Marshal.PtrToStructure(fdPtr,
typeof(NativeMethods.FILEDESCRIPTORA));
NativeMethods.FILEDESCRIPTORA fd = (NativeMethods.FILEDESCRIPTORA)fdObj;
//get file name of file descriptor and put in array
fileNames[fdIndex] = fd.cFileName;
//move the file descriptor pointer to the next file descriptor
fdPtr = (IntPtr)((int)fdPtr + Marshal.SizeOf(fd));
}
这一切都很有效,除了返回的文件名总是缺少前几个字符。有谁知道会导致什么和/或如何修复它?
答案 0 :(得分:1)
在the codeproject page的评论中,另一个人提到了同样的问题 - 在这种情况下,数组项的偏移量是从结构的开头计算出来的,没有考虑到包含的第一个32/64位。数组中的项目数:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public sealed class FILEGROUPDESCRIPTORA
{
public uint cItems;
public FILEDESCRIPTORA[] fgd;
}