473,569 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 & RARGetDLLVersio n)... 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_ARCHIV E = 10;
public const int ERAR_NO_MEMORY = 11;
public const int ERAR_BAD_DATA = 12;
public const int ERAR_BAD_ARCHIV E = 13;
public const int ERAR_UNKNOWN_FO RMAT = 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 RAROpenArchiveD ata
{
public string ArcName;
public uint OpenMode;
public uint OpenResult;
public string CmtBuf;
public uint CmtBufSize;
public uint CmtSize;
public uint CmtState;
}

public struct RAROpenArchiveD ataEx
{
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;
};
[DllImportAttrib ute("unrar.dll" )]
public static extern IntPtr RAROpenArchive (ref
RAROpenArchiveD ata ArchiveData);
[DllImportAttrib ute("unrar.dll" )]
public static extern int RARCloseArchive (IntPtr hArcData);
[DllImportAttrib ute("unrar.dll" )]
public static extern int RARReadHeader (IntPtr hArcData, ref
RARHeaderData HeaderData);
[DllImportAttrib ute("unrar.dll" )]
public static extern IntPtr RAROpenArchiveE x(ref
RAROpenArchiveD ataEx ArchiveData);
[DllImportAttrib ute("unrar.dll" )]
public static extern int RARReadHeaderEx (IntPtr hArcData, ref
RARHeaderDataEx HeaderData);
[DllImportAttrib ute("unrar.dll" )]
public static extern int RARProcessFile( IntPtr hArcData, int
Operation, string DestPath, string DestName);
[DllImportAttrib ute("unrar.dll" )]
public static extern int RARGetDllVersio n();
static void Main(string[] args)
{
IntPtr lHandle;
int iStatus;
RARHeaderData uHeader=new RARHeaderData() ;
RAROpenArchiveD ata uRAR=new RAROpenArchiveD ata();

uRAR.ArcName = "D:\\adddas.rar ";
uRAR.CmtBuf = string.Empty.Pa dLeft(16384,' ');
uRAR.CmtBufSize = 16384;
uRAR.OpenMode=R AR_OM_LIST;

Console.WriteLi ne("DLL Version: {0}",RARGetDllV ersion());

lHandle = RAROpenArchive( ref uRAR);
Console.WriteLi ne("OpenResult : {0}",uRAR.OpenR esult);
Console.WriteLi ne("CmtState: {0}",uRAR.CmtSt ate);
iStatus=RARRead Header(lHandle, ref uHeader);
Console.WriteLi ne("Status: {0}",iStatus);

iStatus = RARCloseArchive (lHandle);
Console.WriteLi ne("Status: {0}",iStatus);
}
}
}
Nov 15 '05 #1
4 10487
Oliver <gr***@stratoz. at> wrote in
news:Xn******** *************** *****@207.46.24 8.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 & RARGetDLLVersio n)... 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(Unman agedType.ByValT Str, SizeConst = 260)]
public string ArcName;
[MarshalAs(Unman agedType.ByValT Str, 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*******@comc ast.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
4844
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 function, which in turn fires an event that is caught by the form. The form then need to add a value passed in the event to a form object, however,...
2
10514
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 using System; using System.Runtime.InteropServices;
4
2057
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 multiple objects from said wrapper class? There's only one DLL, so is it creating multiple instances of its functions when each object is making calls...
0
3044
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 in C#? Code looks like this: DirectoryEntry obDirEntry = new DirectoryEntry ("IIS://localhost/W3svc/1"); obDirEntry.Invoke("Stop", new object...
3
5896
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 keep coming up with the error - " Can not marshal parameter #5: Invalid managed/unmanaged type combination (this value type must be paired with...
3
5481
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 _CTL_USAGE. I keep getting a "This type can not be marshalled as a structure field." Does anyone have any ideas as to what I'm doing wrong? Here's the...
1
1534
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 inputinfor a, ref
1
1470
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 trouble calling a certain function that requires a pointer to a pointer. Here is the relevant header information: typedef struct _ITEM_INFO {...
2
9476
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 first of all incomplete (compiler errors) which I had to resolve by guessing on some things like how some variables were instantiated, and then finally...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7922
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8119
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5509
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2111
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.