How to debug COM exceptions, caused by Excel Interop, in C# program? - c#

I'm working on a C# application, trying to use Excel features. In order to do that, I'm working with Interop as a library. I have an exception in following line of source code:
Range rng_Destination =
xlApp.get_Range(xlWrkSht.Cells[2,1],
xlWrkSht.Cells[xlWrkSht.Cells[1,1].CurrentArray.Rows.Count+1,
dt_main.Columns.Count]);
In order to investigate this, I've tried to add xlWrkSht.Cells[2,1] to Visual Studio's Watch window, but this just shows {System.__ComObject}, which is not very useful.
So, I tried another approach, using this kind of source code:
System.Diagnostics.Debug.WriteLine("xlWrkSht.Cells[2 ,1]: [%s]",
xlWrkSht.Cells[2, 1]);
This, however, causes following Exception:
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
HResult=0x80131500
Message=Cannot dynamically invoke method 'WriteLine' because it has
a Conditional attribute
Source=<Cannot evaluate the exception source>
StackTrace:<Cannot evaluate the exception stack trace>
So my question is very simple: how can I debug Interop related objects, like the mentioned xlWrkSht.Cells[2, 1]?
For your information: I already tried unchecking "Just my code" but this did not help until now (I found this somewhere on the internet, I have no idea what it does):
I've been looking for other resources about Interop debugging, but the information was far too general to be useful for my specific case.
I've also seen some posts on the internet, informing about the event viewer but there, I just have some "Information" and at most some "Warning" messages, no "Error" ones (I've been looking in "Windows Logs, Application/Security and System".
Edit after some more investigation
Meanwhile I've seen that I can find the value of that cell, using:
xlWrkSht.Cells[2, 1]
=> Dynamic View
FormulaLocal
That last attribute contains the value.
However, the following issue is that I'll need to get the Excel array this cell belongs to:
xlWrkSht.Cells[2, 1].CurrentArray.
This one seems not to be accessible, even not using "Dynamic view". Any ideas?

Related

HtmlAgilitypack fails to load certain html code. Causes unhandled .net Framework exception in w3wp.exe

Ive managed to finally isolate a issue I have with HtmlAgilityPack.
For a while, my w3wp has crashed causing a unhandled .net Framework exception to occur and the application pool restarts.
After using Debug Diagnostic i found that the problem occurs when trying to load the html result of this link:
https://tourette-syndrom.de/eingangsseite.htm
After viewing the source code i found that there are a big amount of <o:p> nodes that never closes, this causes the parser to fail.
I use the LoadHtml, this is something I need to use.
If i make use of htmlDoc.Load and pass the URI link instead of the html string,
it generates a error im able to catch.
System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks
The error tells me nothing really, but the w3wp application does not crash!
Ive tested these option to both true and false but application still crashes.
htmlDoc.OptionFixNestedTags
htmlDoc.OptionAutoCloseOnEnd
htmlDoc.OptionWriteEmptyNodes
If I simply replace the nodes <o:p> like this htmlDoc.LoadHtml(htmlString.Replace("<o:p>","")) then everything goes well. Im not looking for a simple replace function here, plus, this happends on several badly formatted websites, so was hoping this is something I can resolve by a proper setting or extension methods. Anyone else that have the same issue?

Where are .NET's internal resource strings stored

I'm decompiling some .NET code for diagnosing an issue but the object is very large. To that end, I've been trying to see where .NET would raise the error I'm looking for by resource string as the exception type is fairly generic but the error message is specific to ultimate issue I'm looking into.
Decompiling the code, I see that it uses a string resource manager to pull out the text but without being able to match the resource key to the text, I cannot trace from the error message to the point in decompiled code.
E.g., exceptions are generated like the following in the decompiled code:
throw new ArgumentOutOfRangeException("value", SR.GetString("net_io_timeout_use_ge_zero"));
Where would I find the .NET's localized error text and associated resource key (e.g. "net_io_timeout_use_ge_zero")?
I've tried using ResHacker on System.dll and System.Net.dll (the assembly with the object I'm investigating) but both of these yield nothing. Likewise, while there's a culture subfolder of en-US under %windir%\Microsoft.NET\Framework\v4.0.30319 and the 64 equivalent but these don't show any text bar the assembly details and some icons in the System.dll
This is really PetSerAl's answer (if he posts one, I'll mark it as such) but to find the resource strings you can use ILSpy to load up the assembly and the resources can be found on the Resources node like so;
Going further, while ILSpy does list the resources, it doesn't provide a direct way to search for them. Saving saves out the compiled details which are not easily readable (there's a github request for this). In the meantime, you can select the contents the grid by selecting a single item, CTRL+A to select all, CTRL+C to copy the grid to the clipboard. From there you can paste into the likes of Excel or Notepad and search for the text you need.

LoadLibrary() error code 998

I’ve got a c++ dll written in visual studio 2012. I’d like to load it in a C# program(VS2012 too) with using of LoadLibrary function.
I set the SetLastError = true to get the probabilistic returned error code by LoadLibrary. When I run my C# program I always receive 998 error code but I can’t understand what’s wrong with it!
Should you help me to resolve this problem,please?
Any help would be appreciated.
(It should be mentioned that I saw some posts about error code 998 but none of them couldn't give me a solution.)
In response to this comment "please tell me how can I do this instruction":
To troubleshoot the LoadLibrary() failure, run the application under a
debugger and enable first chance exception handling for the C0000005
Access Violation exception. If an access violation occurs when the
LoadLibrary() function is called, the application will break into the
debugger. The debugger's call stack can then be used to trace where
the exception occurred. The stack trace should help you narrow down
the actual problem related to the exception being encountered.
Open your project in Visual Studio
In the menu, click Debug > Exceptions
In the Exceptions window, click Find... and enter C0000005 click Ok
Check the box next to the highlighted exception under the column Thrown.
Now, when you debug your program and the exception is thrown, it will break and you should be able to at least inspect the exception details of the thrown exception.
It's likely you'll be thrown into disassembly window so you may not see any readable code. If the exception detail isn't enough, you could try decompiling with Dot Peek.
Here's a tutorial on enabling Dot Peek as a symbol server. Doing this will hopefully decompile the library on-the-fly so you can start inspecting the line of code that has caused the exception.

Import to Excel does not work

I am using the Office Integration Pack
After following instruction correctly I am still not able to get the Excel to Import working
My Visual Studio Lightswitch 2011 application is configured to host on IIS Server and use's Easy Shell (so its the default Shell provided by MS).
So far I have tried is calling the
OfficeIntegration.Excel.Import(
this.States,
#"C:\Users\Mr_Mia_Gie\My Documents\ExcelSheet.xls",
"Sheet1",
"A1:C3");
on _Execute event of a button (the button does not live on the Shell Command Bar)
The exception I get back is "Object variable or With block variable not set."
Any solution or suggestion will be highly appreciated
Cheers
I agree with Nevyn (& I'd vote up his answer, but it's embedded in the question as an edit so I can't).
As Nevyn has pointed out, there are really only three objects in that particular line of code that can be causing a null exception:
the OfficeIntegration object
the OfficeIntegration.Excel object
or, the this.States collection (unlikely though)
As was also pointed out for you, it's most likely that one of those objects isn't correctly initialised. You need to check what the value of those three objects are by putting a break point on that line & checking what their values actually are at that point.
You could also put a guard clause in your code (just above that line):
if (OfficeIntegration == null) || (OfficeIntegration.Excel == null) return;
It won't neccesarily "fix" the problem, but it will stop the null exception from occuring (but this shouldn't be a problem in an Execute method). But it's good programing practice to put a guard clause any time you're referencing an object whose value could be null.
Failing that, the only other advice any of us can give you is to post a question in the questions section of the gallery page, where you downloaded the extension from. The autor of the extension should be able to help you.
Is this a web application, if so I dont think it supports it. It requires an extenstion. I dug up an article for you, try it out:
http://blogs.msdn.com/b/lightswitch/archive/2011/04/13/how-to-import-data-from-excel.aspx
The office integration Pack is for Visual Studio LightSwitch windows application and does work for application that are hosted on IIS hence the below code throws exception
OfficeIntegration.Excel.Import(
this.States,
#"C:\Users\Mr_Mia_Gie\My Documents\ExcelSheet.xls",
"Sheet1",
"A1:C3");
The link show that the extension does not support LS WebBrowser application http://officeintegration.codeplex.com/discussions/374585
Also the extension Import data from Excel does work for IIS hosted LS application that runs in webbrowser

Traceback StackOverflow Exception

I've got an Umbraco site using a fair ammount of usercontrols in which, somewhere - somehow, a stackoverflow exception gets thrown every now and then.
Since the SOE doesn't occur when testing is has something to do with a user posting or getting some information (fair enough: something I've missed).
How can I trace back where my stackoverflow exception took place inside my code? Are there tools available to check my sources to see if I've missed some recursive method? Or how can I debug the running process?
I have found that debugging the application (sometimes having to attach VS to the correct w3wp.exe process) and setting VS to break on exception is the most helpful. You could still see the stack trace which is full of the same series of method calls.
If the application crashes, enable this registry setting to create a crashdump for all crashing apps:
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Windows Error Reporting\LocalDumps]
"DumpFolder"="C:\\TEMP"
"DumpCount"=dword:00000010
"DumpType"=dword:00000002
It will create a crash dump in your C:\Temp folder. Either open this dump file in a newer version of Visual Studio which will show you the exact code it crashed (just like if you where debugging and you got an unhandled exception).
Or an extremely useful MS tool for all your dump analysis needs: Debug Diagnostics Tools (https://www.microsoft.com/en-us/download/details.aspx?id=49924). Video walkthrough here: https://channel9.msdn.com/Shows/Defrag-Tools/Defrag-Tools-123-DebugDiag-Part-3
This will analyse your crashed application and show you all the threads and their managed and unmanaged stacktraces.
It will likely point out exactly the issue of your crash and where it occurred so you can check that stacktrace.
If however the program did not crash, you can open task manager and rightclick the process and create a dump file manually. Sending this dump file through Debug Diagnostics on a crash analysis it will show you the last X exceptions that did occur in your application with stacktraces. I use this method to find the actual error of user applications when the user gets a nice simplified error message. As an alternative to creating a dump from taskmanager you can also use procdump.exe or Debug Diagnostics itself to create rules on when to create dump files.
I really believe more people should be aware of how incredible easy a DUMP file together with Debug Diagnostics is to find any error that occurred in a production environment, where we don't have our handy development tools available.

Categories