Wrox Programmer Forums
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 23rd, 2004, 07:56 PM
Registered User
 
Join Date: Jan 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default using unrar.dll in c#

Dear sirs
after some googling i found some code to using unrar.dll, but i get runtime error
System.NullReferenceException in line iStatus=RARReadHeader(lHandle, ref uHeader);
hear is the code

using System;
using System.Runtime.InteropServices;

namespace Unrar
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        public const int ERAR_END_ARCHIVE = 10;
        public const int ERAR_NO_MEMORY = 11;
        public const int ERAR_BAD_DATA = 12;
        public const int ERAR_BAD_ARCHIVE = 13;
        public const int ERAR_UNKNOWN_FORMAT = 14;
        public const int ERAR_EOPEN = 15;
        public const int ERAR_ECREATE = 16;
        public const int ERAR_ECLOSE = 17;
        public const int ERAR_EREAD = 18;
        public const int ERAR_EWRITE = 19;
        public const int ERAR_SMALL_BUF = 20;

        public const int RAR_OM_LIST = 0;
        public const int RAR_OM_EXTRACT = 1;

        public const int RAR_SKIP = 0;
        public const int RAR_TEST = 1;
        public const int RAR_EXTRACT = 2;

        public const int RAR_VOL_ASK = 0;
        public const int RAR_VOL_NOTIFY = 1;

        public enum RarOperations
        {
            OP_EXTRACT = 0,
            OP_TEST = 1,
            OP_LIST = 2
        }

        public struct RARHeaderData
        {
            public string ArcName;
            public string FileName;
            public uint Flags;
            public uint PackSize;
            public uint UnpSize;
            public uint HostOS;
            public uint FileCRC;
            public uint FileTime;
            public uint UnpVer;
            public uint Method;
            public uint FileAttr;
            public string CmtBuf;
            public uint CmtBufSize;
            public uint CmtSize;
            public uint CmtState;
        }

        public struct RAROpenArchiveData
        {
            public string ArcName;
            public uint OpenMode;
            public uint OpenResult;
            public string CmtBuf;
            public uint CmtBufSize;
            public uint CmtSize;
            public uint CmtState;
        }

        public struct RAROpenArchiveDataEx
        {
            public string ArcName;
            public string ArcNameW;
            public uint OpenMode;
            public uint OpenResult;
            public string CmtBuf;
            public uint CmtBufSize;
            public uint CmtSize;
            public uint CmtState;
            public uint Flags;
            public uint Reserved;
        }
        public struct RARHeaderDataEx
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string ArcName;

            public string ArcNameW;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string FileName;

            public string FileNameW;
            public uint Flags;
            public uint PackSize;
            public uint PackSizeHigh;
            public uint UnpSize;
            public uint UnpSizeHigh;
            public uint HostOS;
            public uint FileCRC;
            public uint FileTime;
            public uint UnpVer;
            public uint Method;
            public uint FileAttr;
            public string CmtBuf;
            public uint CmtBufSize;
            public uint CmtSize;
            public uint CmtState;
            public uint Reserved;
        };


        [DllImportAttribute("unrar.dll")]
        public static extern IntPtr RAROpenArchive (ref
            RAROpenArchiveData ArchiveData);
        [DllImportAttribute("unrar.dll")]
        public static extern int RARCloseArchive(IntPtr hArcData);
        [DllImportAttribute("unrar.dll")]
        public static extern int RARReadHeader (IntPtr hArcData, ref
            RARHeaderData HeaderData);
        [DllImportAttribute("unrar.dll")]
        public static extern IntPtr RAROpenArchiveEx(ref
            RAROpenArchiveDataEx ArchiveData);
        [DllImportAttribute("unrar.dll")]
        public static extern int RARReadHeaderEx(IntPtr hArcData, ref
            RARHeaderDataEx HeaderData);
        [DllImportAttribute("unrar.dll")]
        public static extern int RARProcessFile(IntPtr hArcData, int
            Operation, string DestPath, string DestName);
        [DllImportAttribute("unrar.dll")]
        public static extern int RARGetDllVersion();
        [DllImportAttribute("unrar.dll")]
        public static extern int RARSetPassword(IntPtr hArcData,string Password);


        static void Main(string[] args)
        {

                IntPtr lHandle;
                int iStatus;
                RARHeaderData uHeader;
                RAROpenArchiveData uRAR;
                uint openresult=0;
                uint cmt=0;
                uRAR.ArcName = @"D:\1.rar";
                uRAR.CmtBuf = string.Empty.PadLeft(16384,' ');
                uRAR.CmtBufSize = 16384;
                uRAR.OpenMode=RAR_OM_LIST;
                uRAR.OpenResult=openresult;
                uRAR.CmtState=cmt;
                uRAR.CmtSize=4;
            ////////////////////////////
            string HeaderArcName=string.Empty.PadLeft(260);
            uHeader.ArcName=HeaderArcName;
            string HeaderCmtBuf=string.Empty.PadLeft(260);
                uHeader.CmtBuf=HeaderCmtBuf;
            uint HeaderCmtBufSize=0;
                uHeader.CmtBufSize=HeaderCmtBufSize;
            uint HeaderCmtSize=0;
                uHeader.CmtSize=HeaderCmtSize;
            uint HeaderCmtState=0;
                uHeader.CmtState=HeaderCmtState;
            uint HeaderFileAttr=0;
                uHeader.FileAttr=HeaderFileAttr;
            uint HeaderFileCRC=0;
                uHeader.FileCRC=HeaderFileCRC;
            string HeaderFileName=string.Empty.PadLeft(260);
                uHeader.FileName=HeaderFileName;
            uint HeaderFileTime=0;
                uHeader.FileTime=HeaderFileTime;
            uint HeaderFlags=0;
                uHeader.Flags=HeaderFlags;
            uint HeaderHostOS=0;
                uHeader.HostOS=HeaderHostOS;
            uint HeaderMethod=0;
                uHeader.Method=HeaderMethod;
            uint HeaderPackSize=0;
                uHeader.PackSize=HeaderPackSize;
            uint HeaderUnpSize=0;
                uHeader.UnpSize=HeaderUnpSize;
            uint HeaderUnpVer=0;
                uHeader.UnpVer=HeaderUnpVer;
                Console.WriteLine("DLL Version: {0}",RARGetDllVersion());

                lHandle = RAROpenArchive(ref uRAR);
                Console.WriteLine("OpenResult: {0}",uRAR.OpenResult);
                Console.WriteLine("CmtState: {0}",uRAR.CmtState);


                iStatus=RARReadHeader(lHandle, ref uHeader);
                Console.WriteLine("Status: {0}",iStatus);

                iStatus = RARCloseArchive(lHandle);
                Console.WriteLine("Status: {0}",iStatus);

        }
    }
}

and also if we copy unrar.dll to local directory not system32 is that work?
I will appreciate to your oponions.



 
Old January 25th, 2004, 07:31 AM
Registered User
 
Join Date: Jan 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

finaly this code seems to work but it doesn't extract anything and flags returns success, any oponion?


using System;
using System.Runtime.InteropServices;

namespace Unrar
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        public const int ERAR_END_ARCHIVE = 10;
        public const int ERAR_NO_MEMORY = 11;
        public const int ERAR_BAD_DATA = 12;
        public const int ERAR_BAD_ARCHIVE = 13;
        public const int ERAR_UNKNOWN_FORMAT = 14;
        public const int ERAR_EOPEN = 15;
        public const int ERAR_ECREATE = 16;
        public const int ERAR_ECLOSE = 17;
        public const int ERAR_EREAD = 18;
        public const int ERAR_EWRITE = 19;
        public const int ERAR_SMALL_BUF = 20;

        public const int RAR_OM_LIST = 0;
        public const int RAR_OM_EXTRACT = 1;

        public const int RAR_SKIP = 0;
        public const int RAR_TEST = 1;
        public const int RAR_EXTRACT = 2;

        public const int RAR_VOL_ASK = 0;
        public const int RAR_VOL_NOTIFY = 1;

        public enum RarOperations
        {
            OP_EXTRACT = 0,
            OP_TEST = 1,
            OP_LIST = 2
        }

        public struct RARHeaderData
        {

            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string ArcName;


            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string FileName;

            public uint Flags;
            public uint PackSize;
            public uint UnpSize;
            public uint HostOS;
            public uint FileCRC;
            public uint FileTime;
            public uint UnpVer;
            public uint Method;
            public uint FileAttr;
            public string CmtBuf;
            public uint CmtBufSize;
            public uint CmtSize;
            public uint CmtState;
        }

        public struct RAROpenArchiveData
        {
            public string ArcName;
            public uint OpenMode;
            public uint OpenResult;
            public string CmtBuf;
            public uint CmtBufSize;
            public uint CmtSize;
            public uint CmtState;
        }

        public struct RAROpenArchiveDataEx
        {
            public string ArcName;
            public string ArcNameW;
            public uint OpenMode;
            public uint OpenResult;
            public string CmtBuf;
            public uint CmtBufSize;
            public uint CmtSize;
            public uint CmtState;
            public uint Flags;
            public uint Reserved;
        }
        public struct RARHeaderDataEx
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string ArcName;

            public string ArcNameW;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
            public string FileName;

            public string FileNameW;
            public uint Flags;
            public uint PackSize;
            public uint PackSizeHigh;
            public uint UnpSize;
            public uint UnpSizeHigh;
            public uint HostOS;
            public uint FileCRC;
            public uint FileTime;
            public uint UnpVer;
            public uint Method;
            public uint FileAttr;
            public string CmtBuf;
            public uint CmtBufSize;
            public uint CmtSize;
            public uint CmtState;
            public uint Reserved;
        };


        [DllImportAttribute("unrar.dll")]
        public static extern unsafe IntPtr RAROpenArchive (ref RAROpenArchiveData ArchiveData);
        [DllImportAttribute("unrar.dll")]
        public static extern unsafe int RARCloseArchive(IntPtr hArcData);
        [DllImportAttribute("unrar.dll")]
        public static extern unsafe int RARReadHeader (IntPtr hArcData, ref
            RARHeaderData HeaderData);
        [DllImportAttribute("unrar.dll")]
        public static extern unsafe IntPtr RAROpenArchiveEx(ref
            RAROpenArchiveDataEx ArchiveData);
        [DllImportAttribute("unrar.dll")]
        public static extern unsafe int RARReadHeaderEx(IntPtr hArcData, ref
            RARHeaderDataEx HeaderData);
        [DllImportAttribute("unrar.dll")]
        public static extern unsafe int RARProcessFile(IntPtr hArcData, int
            Operation, string DestPath, string DestName);
        [DllImportAttribute("unrar.dll")]
        public static extern unsafe int RARGetDllVersion();
        [DllImportAttribute("unrar.dll")]
        public static extern unsafe int RARSetPassword(IntPtr hArcData,string Password);



        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            IntPtr lHandle;
            int iStatus;
            RARHeaderData uHeader;
            RAROpenArchiveData uRAR;
            uint openresult=0;
            uint cmt=0;
            uRAR.ArcName = @"D:\b.rar";
            uRAR.CmtBuf = string.Empty.PadLeft(16384,' ');
            uRAR.CmtBufSize = 16384;
            uRAR.OpenMode=RAR_OM_EXTRACT;
            uRAR.OpenResult=openresult;
            uRAR.CmtState=cmt;
            uRAR.CmtSize=4;
            ////////////////////////////
            uHeader=new RARHeaderData();

            lHandle = RAROpenArchive(ref uRAR);
            Console.WriteLine("OpenResult: {0}",uRAR.OpenResult);
            Console.WriteLine("CmtState: {0}",uRAR.CmtState);
            String s = @"d:";
            String n=string.Empty;
            RARProcessFile(lHandle,RAR_EXTRACT,s,n);
            iStatus=RARReadHeader(lHandle, ref uHeader);
            Console.WriteLine("Status: {0}",iStatus);

            iStatus = RARCloseArchive(lHandle);

            Console.WriteLine("Status: {0}",iStatus);
            Console.WriteLine("HostOs:{0}",uHeader.FileName);
            Console.ReadLine();

            //
            // TODO: Add code to start application here
            //
        }
    }
}


 
Old October 10th, 2006, 12:55 AM
Registered User
 
Join Date: Oct 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to agrath
Default

Having the exact same problem, could someone post the solution?






Similar Threads
Thread Thread Starter Forum Replies Last Post
Install problems - VS 6.0 on XP SCRRUN.DLL PDM.DLL jeff4444 Visual C++ 0 December 6th, 2006 08:48 PM
How to convert .NET dll to COM dll used by VB 6.0 gvprashanth .NET Framework 1.x 0 December 4th, 2006 05:05 AM
The database dll crdb_oracle.dll could not be load Yeliz Crystal Reports 0 October 18th, 2006 08:12 AM
Using unrar niick_123 C++ Programming 0 November 12th, 2004 01:28 AM
DLL & Excel - keep having to reset DLL reference! James Diamond Pro VB 6 2 May 25th, 2004 03:37 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.