DllImport Issues

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1877 Views - Last Post: 02 June 2015 - 09:03 PM Rate Topic: -----

#1 Verequies   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-March 14

DllImport Issues

Posted 02 June 2015 - 05:14 AM

Hey Guys

Over the past few days I've been developing a custom installer that uses the FreeArc unarc.dll as its unarchiving engine. Just trying to learn how to use DllImport properly, so far I think I get it however everytime I compile and run the below code, my console window just stops on a black window with flashing underscore.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace Unarc
{
    class UnsafeNativeMethods
    {
        public delegate int cbtype(string what, int int1, int int2, string str);

        [DllImport("unarc.dll", CharSet=CharSet.Unicode)]

        public static extern int FreeArcExtract(cbtype callback, string cmd1, string cmd2);

        [DllImport("unarc.dll", CharSet = CharSet.Unicode)]
        public static extern void UnloadDLL();
    }
    class UnarcTest
    {
        static int callback(string what, int int1, int int2, string str)
        {
            if (what.Equals("password?"))
            {
                Console.WriteLine("Enter Password: ");
                str = Console.ReadLine();
                return 1;
            }
            else if (what.Equals("overwrite?"))
            {
                Console.WriteLine("Overwrite? ");
                str = Console.ReadLine();
                return 1;
            }
            else if (what.Equals("error"))
            {
                Console.WriteLine("error: ");
                str = Console.ReadLine();
                return 1;
            }
            else
                return 1;
        }

        static int Main(string[] args)
        {
            int result;
            result = UnsafeNativeMethods.FreeArcExtract(callback, "x", "data.vq");

            if (result == 1)
            {
                Console.WriteLine("Hello");
                return 1;
            }
            UnsafeNativeMethods.UnloadDLL();
            return 0;
        }
    }
}




If anyone can help, that would be great.

Thanks,
Hamish

Is This A Good Question/Topic? 0
  • +

Replies To: DllImport Issues

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: DllImport Issues

Posted 02 June 2015 - 05:57 AM

Quote

my console window just stops on a black window with flashing underscore.

Why would you expect anything different? In other words... Where are you telling it to write something? If you don't tell, its not going to happen by magic.

You only write something if the result == 1. So what gets displayed on screen if the result == 0?

Put a breakpoint on line 52 and check the value of result at run-time. Basic debugging.


See FAQ # 2 "How do I debug my program?"

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated June 2015
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 Verequies   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-March 14

Re: DllImport Issues

Posted 02 June 2015 - 06:10 AM

I don't actually need the Console.WriteLine("Hello"); That was just a test to see if it error'd out. However since I added the "error" callback, it is no longer needed. After doing some debugging of my own which I forgot to mention. The code reaches the call to FreeArcExtract, but nothing happens after that.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: DllImport Issues

Posted 02 June 2015 - 06:26 AM

Quote

The code reaches the call to FreeArcExtract,

SO line 50?

Wow - calling some 3rd party dll causes a problem - and you can't identify it because its not your code. <sarcasm> How surprising </sarcasm>
That's not a comment on your code. Just a comment on my experience with such things. WAY too often I've seen this and there is little you can do if the guy that wrote that other code didn't do a good job. If the DLL is hung, you're fraked. Now you have to write robust code to wrap the calls to the dll.

So you spin off another thread to your dll wrapper class.
The wrapper calls the DLL. If the wrapper doesn't give you a response back after 3 seconds you know its hung up and you will have to abort it. Ouch.

Also double check some basics. Is the dll 32bit and your program is running 64 bit? Because that doesn't work. You need 64bit dll's for a 64 bit program. Either shift your program down to 32bit, or get 64bit dlls, or write a seperate 32bit process to wrap the 32bit dlls that your 64bit application can communicate with. I do this with some DSLR camera dll's because they are all 32bit but I want a 64bit program. So I wrote a 32bit process to be a server for the camera.

If you're just 'unarchiving' - is it a standard archive type like .zip? Because there are tons of libraries for that. Try another library to see if it gives you the same hang up. Maybe this library doesn't cope well with corrupted archives for example.

Check the output window while debugging. Are you seeing exceptions? Maybe something like "bad image format" which means the dlls don't match the bitness of the application.
Make sure CLR exceptions are ticked on in Visual Studio exception handling.

This post has been edited by tlhIn`toq: 02 June 2015 - 06:24 AM

Was This Post Helpful? 1
  • +
  • -

#5 Verequies   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-March 14

Re: DllImport Issues

Posted 02 June 2015 - 06:32 AM

As far as I know the .dll is fine. I've tested it with the pre-compiled example executable. My data.vq isn't corrupted, I've extracted it quite a number of times with success. After studying the example C++ & C# code, & the readme that was with the .dll I believe I could create a simple program that could extract the data.vq. And this is where I am now. I should mention that the .dll was coded with C++, and that the source code is freely available.
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7139
  • View blog
  • Posts: 24,245
  • Joined: 05-May 12

Re: DllImport Issues

Posted 02 June 2015 - 06:42 AM

Could you point us to the source code?

As noted above, are you sure the bitness matches? If you are running on a 64-bit OS, are you compiling the unarc.dll to be a 64-bit binary; or are you compiling your program to target 32-bits if the unarc.dll is 32-bits?
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: DllImport Issues

Posted 02 June 2015 - 07:29 AM

View PostVerequies, on 02 June 2015 - 07:32 AM, said:

As far as I know the .dll is fine.


What does 'fine' mean in this context?

If you don't know what we mean when we are talking about 32bit versus 64bit then just say so.
The default for Visual Studio is "Any CPU" so if you are building on a 64 bit OS then you are going to make a 64 bit application. Is you OS 64bit?
Do you know how to check if your Visual Studio solution is 64 bit or 32 bit? You should check the sample solution to see if it is specifying 32bit - I'll bet it is.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,599
  • Joined: 12-June 08

Re: DllImport Issues

Posted 02 June 2015 - 07:55 AM

I'll bite - what is "data.vq", and where did it come from?
Was This Post Helpful? 0
  • +
  • -

#9 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: DllImport Issues

Posted 02 June 2015 - 01:16 PM

I'd suspect the p/invoke signature first. Where did you get it for that dll?
Was This Post Helpful? 1
  • +
  • -

#10 Verequies   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-March 14

Re: DllImport Issues

Posted 02 June 2015 - 04:07 PM

Okay, so FreeArc is built for a 32-bit Architecture, however there is talk that there may be a 64-bit version in development. I have been compiling it for the x86 Architecture within Visual Studio 2013. 'data.vq' is compressed by FreeArc 0.67 with the following arguments:

'arc create data.vq -r "./compress/*" -mx -ld1600m --encryption=aes:r100+serpent/cfb:r100+twofish:r100 -hp"password" -p"password"'

The '.vq' extension is basically just a rename, instead of creating the standard 'data.arc', I just changed it to be short for my alias 'Verequies'. It still works exactly the same. As for the DLL and the Source Code. You can get FreeArc 0.67 from here. For the Source Code, download the 'Source code package'. I'm using the pre-compiled DLL which is located in "C:\Program Files (x86)\FreeArc\Addons\Unarc". You'll have to install FreeArc using the 'Windows installer' to get the pre-compiled DLL.

This post has been edited by Verequies: 02 June 2015 - 04:14 PM

Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: DllImport Issues

Posted 02 June 2015 - 04:26 PM

View PostVerequies, on 02 June 2015 - 05:07 PM, said:

Okay, so FreeArc is built for a 32-bit Architecture, however there is talk that there may be a 64-bit version in development.


Ok. So the problem is identified.

Making a 64 bit version of the utility is probably not an option for you. No disrespect to you but...
  • If you are new enough to coding to have not recognized this fairly common issue *AND*
  • even the developer who knows all their work hasn't worked this out to be 64 bit...
Then you making a 64 bit version of the DLL is probably a task beyond your current skill level.

So that leaves you a couple options:
  • Change your application to be 32bit. Is there any reason your application has to be 64bit? This is the fastest fix.
  • Change libraries. There are LOTS of archiving libraries out there. Change to a different one that is 64 bit.

Was This Post Helpful? 0
  • +
  • -

#12 Verequies   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-March 14

Re: DllImport Issues

Posted 02 June 2015 - 04:31 PM

I already knew FreeArc was a 32-bit application, and I'm using this compression library as with FreeArc I'm able to compress a 1.3 GB folder down to a 580 MB file. Not looking to develop my own DLL at this point, I have some experience, but I'm not that experienced. I was already going to develop my application as 32-bit.
Was This Post Helpful? 0
  • +
  • -

#13 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,599
  • Joined: 12-June 08

Re: DllImport Issues

Posted 02 June 2015 - 05:08 PM

Your app is a "1.3 GB folder"?! Wowzers.. what does it do?
Was This Post Helpful? 0
  • +
  • -

#14 Verequies   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 19-March 14

Re: DllImport Issues

Posted 02 June 2015 - 05:14 PM

My app isn't, but I'm developing a custom installer that uses the FreeArc engine to decompress the archives. I'm just testing it with a game on my system at the moment. I was originally using ProcessStartInfo with arc.exe and the previously said arguments to extract 'data.vq', that was working fine. However I thought just for learning purposes and the ability to easily create progress bars, I decided to try and get the unarc.dll working.
Was This Post Helpful? 0
  • +
  • -

#15 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15359
  • View blog
  • Posts: 61,599
  • Joined: 12-June 08

Re: DllImport Issues

Posted 02 June 2015 - 05:23 PM

Okay - so I am confused. Your app is a custom installer for a game on your computer?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2