Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using unrar.dll in c#

Status
Not open for further replies.

mmmobasher

Programmer
Jan 18, 2004
8
IR
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(&quot;unrar.dll&quot;)]
public static extern IntPtr RAROpenArchive (ref
RAROpenArchiveData ArchiveData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern int RARCloseArchive(IntPtr hArcData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern int RARReadHeader (IntPtr hArcData, ref
RARHeaderData HeaderData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern IntPtr RAROpenArchiveEx(ref
RAROpenArchiveDataEx ArchiveData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern int RARReadHeaderEx(IntPtr hArcData, ref
RARHeaderDataEx HeaderData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern int RARProcessFile(IntPtr hArcData, int
Operation, string DestPath, string DestName);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern int RARGetDllVersion();
[DllImportAttribute(&quot;unrar.dll&quot;)]
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 = @&quot;D:\1.rar&quot;;
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(&quot;DLL Version: {0}&quot;,RARGetDllVersion());

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


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

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

}
}
}

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


 
The error is generated because the lHandle is null and that value is returned by :
lHandle = RAROpenArchive(ref uRAR);
Yes, will work if you put the file in another folder but you have to qualify the path in the DllImport constructors as follows:
[DllImportAttribute(&quot;C:\\Myfolder\\unrar.dll&quot;)]

-obislavu-
 
thanks for your response but which line cause this to return null
 
The problem is this line:
lHandle = RAROpenArchive(ref uRAR);
Maybe the file you try to open doesn't exist or it is not an rar file or something else depending on what you set in the uRAR structure.
-obislavu-
 
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(&quot;unrar.dll&quot;)]
public static extern unsafe IntPtr RAROpenArchive (ref RAROpenArchiveData ArchiveData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern unsafe int RARCloseArchive(IntPtr hArcData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern unsafe int RARReadHeader (IntPtr hArcData, ref
RARHeaderData HeaderData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern unsafe IntPtr RAROpenArchiveEx(ref
RAROpenArchiveDataEx ArchiveData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern unsafe int RARReadHeaderEx(IntPtr hArcData, ref
RARHeaderDataEx HeaderData);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern unsafe int RARProcessFile(IntPtr hArcData, int
Operation, string DestPath, string DestName);
[DllImportAttribute(&quot;unrar.dll&quot;)]
public static extern unsafe int RARGetDllVersion();
[DllImportAttribute(&quot;unrar.dll&quot;)]
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 = @&quot;D:\b.rar&quot;;
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(&quot;OpenResult: {0}&quot;,uRAR.OpenResult);
Console.WriteLine(&quot;CmtState: {0}&quot;,uRAR.CmtState);
String s = @&quot;d:&quot;;
String n=string.Empty;
RARProcessFile(lHandle,RAR_EXTRACT,s,n);
iStatus=RARReadHeader(lHandle, ref uHeader);
Console.WriteLine(&quot;Status: {0}&quot;,iStatus);

iStatus = RARCloseArchive(lHandle);

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

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

Is there some way you can post just the relevant portions of your code, instead of the whole thing? It's a little hard to read the thread...

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
hi folks
you are correct i should post relevent code, thanks ;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top