Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// <summary>
- /// Represent info about particular file within a .torrent file.
- /// </summary>
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct TorrentInfoType_FilesList
- {
- // Full file path within a .torrent
- [MarshalAs(UnmanagedType.LPWStr)]
- public string filepath;
- // Filename of a file within a .torrent
- [MarshalAs(UnmanagedType.LPWStr)]
- public string filename;
- // Filesize of a file within a .torrent
- public Int64 filesize;
- }
- /// <summary>
- /// Represent general information about a .torrent file.
- /// </summary>
- /// <see cref="DLL->TorrentInfo.cpp/h"/>
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
- public struct TorrentInfoType
- {
- // Is torrent valid?
- [MarshalAs(UnmanagedType.U1)]
- public bool IsValid;
- // Torrent name/title.
- [MarshalAs(UnmanagedType.LPWStr)]
- public string name;
- // Torrent sha1 hash.
- [MarshalAs(UnmanagedType.LPWStr)]
- public string hash;
- // Torrent creation date
- public Int64 creationDate;
- // Torrent creator (usually, software ะดัะปั uTorrent/3310)
- [MarshalAs(UnmanagedType.LPWStr)]
- public string creator;
- // Torrent author's comment.
- [MarshalAs(UnmanagedType.LPWStr)]
- public string comment;
- // Is torrent private?
- [MarshalAs(UnmanagedType.U1)]
- public bool IsPrivate;
- // Total number of bytes the torrent-file represents (all the files in it).
- public Int64 totalSize;
- // Files in torrent.
- public int filesCount;
- public int pieceLength;
- public int piecesCount;
- // Files list in .torrent
- [MarshalAs(UnmanagedType.LPArray)]
- public TorrentInfoType_FilesList[] files;
- }
- [DllImport(Globals.WRAPPER_DLL, CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
- private static extern TORRENT_ERROR dll_TorrentGetInfo(string filepath, IntPtr infoStruct);
- ...
- /// <summary>
- /// Retrieves .torrent information (creator, comment, files count and so on)
- /// </summary>
- /// <param name="filepath">full path to .torrent file (ex. C:\torr.torrent)</param>
- /// <returns></returns>
- public static bool GetTorrentInfo(string filepath, out TorrentInfoType info)
- {
- // Init structure with default values.
- info = new TorrentInfoType()
- {
- IsValid = false,
- name = "Unknown",
- hash = "",
- creationDate = 0,
- creator = "Unknown creator",
- comment = "",
- IsPrivate = false,
- totalSize = 0,
- filesCount = 0,
- pieceLength = 0,
- piecesCount = 0,
- files = new TorrentInfoType_FilesList[]
- {
- }
- };
- // Is .torrent exists?
- if (!File.Exists(filepath))
- {
- AppCore.ShowErr(
- String.Format(AppCore.LS("Error.FileNotExists"), filepath)
- );
- return false;
- }
- // Allocate structure to be ready marshalled in/out DLL.
- int tempSize = Marshal.SizeOf(typeof(TorrentInfoType));
- IntPtr pInfo = Marshal.AllocHGlobal(tempSize);
- Marshal.StructureToPtr(info, pInfo, false);
- // Query info.
- var err = dll_TorrentGetInfo(filepath, pInfo);
- if (err == TORRENT_ERROR.TE_INFO_INVALIDTORRENT)
- {
- Marshal.FreeHGlobal(pInfo);
- return false;
- }
- // Update info structure with received data.
- info = (TorrentInfoType)(Marshal.PtrToStructure(pInfo, typeof(TorrentInfoType)));
- // Free memory.
- Marshal.FreeHGlobal(pInfo);
- DumpTorrentInfoData(info);
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement