Having trouble debugging a System.Runtime.InteropServices.COMException on Application.Run - c#

I have an Excel function that I'm calling from C# as follows:
string result = xlApp.Run("'ExcelBook.xlsm'!mainFromBatch",
"http://someSite/TestFile.xlsm",
false,
"T:\\somePath");
When run, this raises an error:
An exception of type 'System.Runtime.InteropServices.COMException' occurred
in BatchConverter.exe but was not handled in user code
Additional information: Exception from HRESULT: 0x800ADF09
If there is a handler for this exception, the program may be safely continued.
The function header for mainFromBatch is:
Function mainFromBatch(sourceBkPath As String, Flag As Boolean, _
Optional outputPathFromConfig As String) As String
When I put a breakpoint at this function header in the VBA code of ExcelBook.xlsm, the C# code raises the error, but the Excel breakpoint is reached and Excel pauses there, as though no error had occurred and it's ready to execute the rest of the code.
I'm able to use C# to call other functions from this workbook without a problem.
I tried Googling the HRESULT code but didn't find anything.
Any idea what's going on here?

Related

How to fix random FileSystemEnumerableIterator.MoveNext Error. The handle is invalid

I just started randomly getting the error on a program that has been running for years. all this program does is look at a set of directories and delete files based on their last write time.
Here is the error and stack trace.
Error: The handle is invalid.
Stack Trace: at System.IO.__Error.WinIOError(Int32 errorCode, String
maybeFullPath) at System.IO.FileSystemEnumerableIterator`1.MoveNext()
at DirectoryCleaner.Program.GetNewFiles()
Here is the code
DirectoryInfo di = new DirectoryInfo(sourcePath);
foreach (FileSystemInfo file in di.EnumerateFileSystemInfos(variablePattern == true ? "*" : SearchPattern, searchSubDirectories == true ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly))
{
creates queue of records to delete. I removed the logic as it is rather long...
}
I have spent several hours searching. I found
System.IO.IOException: The handle is invalid, when using Directory.EnumerateDirectories but it does not have an answer.
The stack trace shows that it is in the MoveNext method which I do not directly call. I assume the foreach loop calls it. How can I correct what is causing this?
This error occurs because the various file/directory EnumerateX methods are effectively wrappers around the native Win32 FindFirstFile function, and as per its documentation:
If the function fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate. To get extended error information, call the GetLastError function.
If we look at the relevant portion of the Framework Reference Source, we see that any error not handled explicitly is surfaced as an IOException. This would include INVALID_HANDLE_VALUE.
In other words, this is an expected potential exception while calling an EnumerateX method and your code should handle it correctly by catching the generic IOException. The docs for the various EnumerateX methods fail to call this out, which IMO is an omission, and as such I've opened an issue to get that fixed.

Can I get the method that was first called in an error routine?

I am trying to get the method that called a method in an assembly that caused an error.
I have an error routine that logs errors. I would like to get the calling assembly and method along with the assembly and method where the error occurred. I got 3 out of 4, I just cant figure out how to get the calling method.
Say I have a form and a button is clicked that calls a method in another asssembly that errors. I want the assembly and method where the error occurs along with the form and click event. The click event is what I can't find.
VB
Dim Trace As System.Diagnostics.StackTrace
Trace = New System.Diagnostics.StackTrace(ex, True)
'Returns the assembly where the error occurred
Trace.GetFrame(Trace.FrameCount - 1).GetMethod.ReflectedType.FullName
'Returns the calling assembly
Assembly.GetCallingAssembly.FullName.Split(","c)(0)'Would be nice to get the class too but I can live without it.
'Method where the error occurs
ex.TargetSite.Name
'Initial calling method
???
C#
System.Diagnostics.StackTrace Trace;
Trace = New System.Diagnostics.StackTrace(ex, True);
//Returns the assembly where the error occurred
Trace.GetFrame(Trace.FrameCount - 1).GetMethod().ReflectedType.FullName;
//Returns the calling assembly
Assembly.GetCallingAssembly().FullName.Split(',')[0];//Would be nice to get the class too but I can live without it.
//Method where the error occurs
ex.TargetSite.Name;
//Initial calling method
???
I can see the source in the stack trace (Main.btnCancel_CLick but can not get it. My stacktrace frame count is 1 and I have one method in there, the one with the try catch block. Unless there's some other way to get to it that I can't figure out. Anyone know?

Exit Method without ArgumentException

i have a Cancel button on a Progressbar, which is used to Show the Progress of Uploading e-Mails,
now i want this button to Exit the method which Uploads e-Mails.
My plan, once the button gets pressed, make bool cancelUpload true.
i have been unlucky to exit the method with the use of break or simply through an if Statement.
but now i found online that i could throw an Argument Exception,
which i implemented as follows:
if (cancelUpload)
{
throw new ArgumentException("SomeText");
}
but the Problem i have with this, is that once the User clicks on Cancel, he gets an Exception, which Looks like an Error or something went wrong, is there a way to get out of the method, without it looking as though something went wrong?(similiar to ArgumentException)
Thanks a lot in Advance!
Edit: The Method (void Method) is to big to be shown,
but when i tried to return;, i got an Error in Visual Studio saying:
TargetInvocationException was unhandled Exception has been thrown by the target of invocation

Attempted to read or write protected memory after extern function

this message is appearing in C# code after calling a function from dll
code is like this
function();
int i = 0;
on second line it says that there is an unhandled exception of type "System.AccessViolationException"... Attempted to read or write protected memory
If function is external there's something wrong with the declaration.
This happens because your return value or your parameters types aren't of the wrong size, and an error occurs with data "popped" from stack

What does the ComponentMetaData.FireError method do in an SSIS script component

I have gone through MSDN.But could not understand properly about the method mentioned below.
What does the below code do if it is included in an SSIS script destination component?
bool Error = false;
this.ComponentMetaData.FireError(0, "myScriptComponent",
"`A Transformation error occurred. Check the corresponding Text File ",
"", 0, out Error);`
The FireError method allows you to raise an error that is consistent with the built in error handling methods used elsewhere in SSIS. I.e. the above code raises an error that is picked up by the OnError event.
The parameters that follow the FireError method are described on BOL.
This can be used to provide adequate error handling (which you always should do when writing any custom code). E.g.:
Try
'Your Code Here
Catch
'Error handling here
Me.ComponentMetadata.FireError(...)
end try
In addition to .FireError, the additional .Fire... methods allow you to fire similar events that will be picked up by SSIS, e.g. .FireInformation allows you to write messages to the output window.

Categories

Resources