473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Uncompressing using unrar.dll P/Invoke what do I do wrong?

Hello !

I am trying to get the unrar.dll working in C#... it seems that I correctly
imported the functions as the first 2 function work without problem
(RAROpenArchive & RARGetDLLVersion)... however always if I want to execute
the RARReadHeader Function I get "Object reference not set to an instance
of an object"

What do I wrong, I used the unrar.dll documentation and I really dont find
any mistakes but why do I always get this error message and why do i only
get it by calling the function RARReadHeader?

Thanks in advance,
Oliver

------------------------- 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
{
public string ArcName;
public string ArcNameW;
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();
static void Main(string[] args)
{
IntPtr lHandle;
int iStatus;
RARHeaderData uHeader=new RARHeaderData();
RAROpenArchiveData uRAR=new RAROpenArchiveData();

uRAR.ArcName = "D:\\adddas.rar";
uRAR.CmtBuf = string.Empty.PadLeft(16384,' ');
uRAR.CmtBufSize = 16384;
uRAR.OpenMode=RAR_OM_LIST;

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);
}
}
}
Nov 15 '05 #1
4 10478
Oliver <gr***@stratoz.at> wrote in
news:Xn****************************@207.46.248.16:
Hello !

I am trying to get the unrar.dll working in C#... it seems that
I correctly imported the functions as the first 2 function work
without problem (RAROpenArchive & RARGetDLLVersion)... however
always if I want to execute the RARReadHeader Function I get
"Object reference not set to an instance of an object"

What do I wrong, I used the unrar.dll documentation and I really
dont find any mistakes but why do I always get this error
message and why do i only get it by calling the function
RARReadHeader?


Oliver,

The RARHeaderData structure has two string parameters which are
supposed to represent fixed length arrays of characters (e.g. "char
arcName[260];" in C). When using P/Invoke, a string instead of a
character array is typically used. However, the fixed-length nature
of the string must be explicitly stated using the MarshalAs
attribute:

public struct RARHeaderData
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string ArcName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string FileName;
...
}

I added these two attributes and ran your code. It seemed to work
OK.

Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #2
Hi Chris!!!

Thank you very much for you help! That was exactly which was missing.
Didn't know how I could make them fixed size since string ArcName[260] or
similar didnt work!

So thanks for taking the time to test it and give me the correct answer. I
really appreciate that! I sat 4 hours and searched the internet but didnt
find any answer to this :)

Thanks,
Oliver
Nov 15 '05 #3
gcl
How did you get the info for that RAR stuffs like its api
calls?
-----Original Message-----
Hi Chris!!!

Thank you very much for you help! That was exactly which was missing.Didn't know how I could make them fixed size since string ArcName[260] orsimilar didnt work!

So thanks for taking the time to test it and give me the correct answer. Ireally appreciate that! I sat 4 hours and searched the internet but didntfind any answer to this :)

Thanks,
Oliver
.

Nov 15 '05 #4
"gcl" <ln*******@comcast.net> wrote in
news:0a****************************@phx.gbl:
How did you get the info for that RAR stuffs like its api
calls?


http://www.rarlab.com/rar_add.htm
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: boxim | last post by:
hi all, I'm having a few problems whereby my application is hanging when using the Invoke method of a form's control. Basically, when a user clicks a button on the form, it calls a remote...
2
by: mmmobasher | last post by:
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 ...
4
by: gilad | last post by:
If a DLL has been created to be thread-safe, and you create a wrapper class around it in C# that is non-static (i.e. you must instantiate objects from it), what are the implications of creating...
0
by: Ken Yee | last post by:
Anyone know how to do this in C#? It's pretty trivial in VB, but is being a PITA in C#. I can call the Start/Stop methods w/o any problems, but I can't figure out how to read the current status...
3
by: Charles Denny | last post by:
I'm trying to invoke CertFindCertificateInStore to find all certificates that have the Code Signing enhanced key attribute. I'd just like to find 1 cert (not even all at this point), however, I...
3
by: Charles Denny | last post by:
I'm trying to call CertFindCertificateInStore to find all certificates in the store that have the Code Signing enhanced key usage. I'm running into problems marshalling the array of OIDs in...
1
by: geri.gan | last post by:
I have C API just like this: enum void getinfor(const struct inputinfor *a, const struct outputinfor ** b) i use p/invok to translate it to internal static extern void getinfor(ref...
1
by: harter.jim | last post by:
I am currently calling a 3rd party external library using P/Invoke. The library gives me a handful of functions that I need to call. I have been successful at calling many of them, but I am some...
2
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
I am trying to find a good example of SendInput. Doing a search on google I found two, one is incomplete and vague from the start and the other I copied the code and tried running it and it was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.