7

I'm writing a C# .NET 3.5 program wich uses the latest MediaInfoLib Dll.
It seems that it causes an exception for some files.

I want to catch those exceptions and ensure my program continues running,
but for some reason I can't catch it with a simple try/catch statement.

PInvoke Methods:

    [DllImport("MediaInfo.dll")]
    private static extern IntPtr MediaInfo_New();
    [DllImport("MediaInfo.dll")]
    private static extern IntPtr MediaInfo_Open(IntPtr Handle,MarshalAs(UnmanagedType.LPWStr)] string FileName);

Usage:

    Handle = MediaInfo_New();
    try{
        MediaInfo_Open(Handle, FileName)
    } catch { } 

Calling MediaInfo_Open(Handle, FileName) might cause an exception.
Instead of catching the error with the try/catch statement, my program exits and "vshost32-clr2.exe" crashes. (It also crashes as a release build and with no debugger attached)
After searching the web, I found someone who suggested to check "Enable unmanaged code debugging", which only resulted in my program exiting without vshost32-clr2.exe crashing.

Any idea how I can catch the exception?

Arokh
  • 614
  • 1
  • 10
  • 18
  • Are you running this on a x64 OS? – ahawker Feb 13 '11 at 22:58
  • 1
    according to the documentation MediaInfo_Open does not return any pointer but size_t. It actually returns 1 when everything went fine and 0 when there was an error, so there's your answer on how to do error handling. (As this seems to be originally a C++ library you might be happier with C++/CLI then with PInvoke, at least I would.) – DaVinci Feb 13 '11 at 23:01
  • On the project page there is also a cs wrapper. Why don't you use that one: http://mediainfo.svn.sourceforge.net/viewvc/mediainfo/MediaInfoLib/trunk/Source/MediaInfoDLL/MediaInfoDLL.cs?revision=3789&view=markup – rene Feb 13 '11 at 23:04
  • Win7 x64, building as 32bit and using 32bit MediaInfoLib. @rene I just stripped the code I posted here, I'm actually using the wrapper. – Arokh Feb 13 '11 at 23:08
  • @Arokh, do you know hot to invoke the dll,am using media info.dll but when i deploy in my test server (64bit) it say's unable to load the mediainfo library ? any idea or how to work around please. – Usher Mar 05 '12 at 05:21

2 Answers2

7

If the unmanaged DLL is causing the crash (rather than just returning an error code of some kind), then there's no way to catch it. Once you've gone outside of the .NET runtime's control, it's entirely up to the unmanaged code; there's nothing the .NET runtime can do.

Adam Robinson
  • 182,639
  • 35
  • 285
  • 343
  • 1
    Well, I don't really want to catch the exception. I just don't want my program to crash. Is that impossible too? – Arokh Feb 13 '11 at 23:14
  • @Arokh: I don't have experience with that library, but I would certainly imagine that it's possible! ;) The question that you asked (and what will likely bring others to this question in the future) was whether or not--and how--you could catch the error as an exception, and the answer to that is "no". – Adam Robinson Feb 13 '11 at 23:24
  • I should've been more careful phrasing the question, he he. So, in short, if the unmanaged lib wants to crash my program, I have no means to prevent it? – Arokh Feb 13 '11 at 23:27
  • 1
    @Arokh: Unfortunately, no; unmanaged libraries can do anything that Windows will allow them to, including blowing up your application in a fiery blaze of glory. – Adam Robinson Feb 13 '11 at 23:33
  • Well I tried annotate a method which calls the native one with attribute [HandleProcessCorruptedStateExceptions] which handles CSE like AccessViolationException and it works fine, but I am looking for a solution how to catch and log all exceptions (which may be thrown anytime from DLL) – Lukáš Koten Jan 14 '19 at 20:43
0

I've had a similar problem (with BSTR specifically) but hopefully this will help.

There is a bug in .NET (fixed in 4.0) when internally marshalling strings back from unmanaged code. More Info

The work-around is to change your P/Invoke signature to use an IntPtr and do the string marshalling yourself.

[DllImport("MediaInfo.dll", EntryPoint = "MediaInfo_Open")]
private static extern IntPtr _MediaInfo_Open(IntPtr handle, IntPtr filename);

internal static extern IntPtr MediaInfo_Open(IntPtr handle, string filename)
{
     IntPtr stringPtr = Marshal.StringToBSTR(filename);
     return _MediaInfo_Open(handle, stringPtr);
}
ahawker
  • 3,306
  • 24
  • 23