DirectoryEntry.Invoke() throws error on "ChangePassword" call - c#

DirectoryEntryObject.Invoke("ChangePassword", new object[] { oldPassword, newPassword } ); throws the following error:
"System.Reflection.TargetInvocationException:
Exception has been thrown by the
target of an invocation.
---> System.Runtime.InteropServices.COMException
(0x80020005): Type mismatch.
(Exception from HRESULT: 0x80020005
(DISP_E_TYPEMISMATCH))
--- End of inner exception stack trace ---
at System.DirectoryServices.DirectoryEntry.Invoke(String
methodName, Object[] args)
Is this something to do with any settings in the AD or I am missing anything?

There's an MSDN page titled Managing User Passwords that has some examples of how to call ChangePassword from C#. The specific sample shows the following syntax:
usr.Invoke("ChangePassword", OldSecurelyStoredPassword, NewSecurelyStoredPassword);
I suspect it's because you're calling it and passing in an object array, rather than passing two strings explicitly (the documentation you've linked to in your comment indicates that it expects to be passed two strings, one as an input parameter and one as an output parameter. Try this:
var oldPassword = "TheOldPassword";
var newPassword = "TheNewPassword";
DirectoryEntryObject.Invoke("ChangePassword", oldPassword, newPassword);

Related

GetToastCollectionManager crash

I already have Toast Notifications working, but I need to create a collection for them.
Following the code example in : https://learn.microsoft.com/en-us/windows/apps/design/shell/tiles-and-notifications/toast-collections
I get a crash on the GetToastCollectionManager call.
System.Exception
HResult=0x80070490
Message=Element not found. (0x80070490)
Source=<Cannot evaluate the exception source>
StackTrace:
<Cannot evaluate the exception stack trace>
This exception was originally thrown at this call stack:
WpfApp1.MainWindow.CreateToastCollection() in MainWindow.xaml.cs
I'm not sure if I'm missing something obvious , but I cannot find examples or tutorials on Toast Notifications.
Any Ideas ?
public async void CreateToastCollection()
{
ToastNotificationManagerForUser defaultManager = ToastNotificationManager.GetDefault();
ToastCollectionManager collectionManager = defaultManager.GetToastCollectionManager();
string displayName = "Is Potato";
string launchArg = "NavigateToPotato";
System.Uri iconURI = new System.Uri("ms-appx:///Assets/icon.png");
ToastCollection licensingManagerToastCollection = new ToastCollection(
"MyToastCollection",
displayName,
launchArg,
iconURI);
// Calls the platform to create the collection
await collectionManager.SaveToastCollectionAsync(licensingManagerToastCollection);
}

C# Invoking Exception TargetSite

I have an application that can sometimes throw an exception, most of these are handled but I want the unhandled ones to be recoverable. So I'm trying to invoke the method that caused the exception by using the exception's targetsite like so:
Exception ex = Global.ThrownException;
MethodBase mB = ex.TargetSite;
try
{
mB.Invoke(mB, null);
}
catch(Exception exc)
{
System.Windows.MessageBox.Show(exc.Message);
}
I'm doing this to make sure that the exception was a one time error before showing the window to the user again.
The test method (and the exception targetsite) I'm trying to invoke is this:
public void testMethod()
{
throw new System.IO.IOException("test");
}
When I run this, an exception is thrown with the message "Object does not match target type" but since testMethod doesn't have any parameters this shouldn't happen.
Any ideas?

Exception thrown by a function, where that function called when exception thrown in it

I have a function "ReturnString":
public static string ReturnString(string sa, string sb)
{
try
{
...
...
return "xyz";
}
catch (Exception ex)
{
throw new clsException(ex.Message);
}
}
it is call by more than 600 times from other more then 40 classes and win farms Mean's it has more than 600 references in more then 40 classes and win farms.
When Exception thrown by it, I want to know what is the it's last calling ref. when exception happen?
Please help me to solve this without changing function arguments.
You should initialize an instance of StackTrace class -
https://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace(v=vs.110).aspx
Then, get the first StackFrame -
https://msdn.microsoft.com/en-us/library/system.diagnostics.stackframe(v=vs.110).aspx
Finally, get the MethodBase of this frame; Its "Name" property is what you need -
https://msdn.microsoft.com/en-us/library/system.reflection.methodbase(v=vs.110).aspx
Try this:
public static string ReturnString(string sa, string sb)
{
try
{
//...
//...
return "xyz";
}
catch (Exception ex)
{
StackTrace oStackTrace = new StackTrace();
string sMethodName = oStackTrace.GetFrame(1).GetMethod().Name;
//It's not a good practice to keep only the error message (you may need other exception details later)
throw new clsException(string.Format("{0}: {1}", sMethodName, ex.Message));
}
}
Your problem is here:
throw new clsException(ex.Message);
As others have mentioned, ex already contains the info you want inside the StackTrace property (check this link for more info).
But when you throw a new exception, you are only throwing the message, and ignoring all the info you want to get.
Just throw without a new exception, or include ex as the inner exception of your clsException.
I want to know what is the it's last calling ref. when exception
happen?
Then check the exception StackTrace, that will let you know the entire call stack and the latest one responsible for exception. Also the innerException property if any.
Check the documentation on Exception class. It has a property StackTrace which you should check.
In your case, the exception object should have it ex.StackTrace
You may also want to get the TargetSite property value from your exception object saying ex.TargetSite

Converting string into exception

I want to convert a string into exception but not able to find any thing on google.
I am using C# .Net 2.0.
Reason is because third party client has a method that is logging method and only takes exception and i have a scenario where i must need to log something but using that method. so must need to convert string into exception.
Exceptions are created as any other object, using the new keyword. You can provide it a message argument that you can store your string in:
Exception e = new Exception("Your string goes here");
If you are using Try...Catch() then you can do following to add your customize message and original exception together
try{
//your code block
}
catch(Exception e)
{
var exception = new Exception("Your message: ");
//Display "exception" to users
//Log "e" for further investigation
}

OutOfMemory exception on Exception.ToString()

Below is some logging output from a .NET application.
Error in MainFunction.
Message: Exception of type 'System.OutOfMemoryException' was thrown.
InnerException:
StackTrace: at System.Text.StringBuilder.ToString()
at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat)
at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
at System.Exception.GetStackTrace(Boolean needFileInfo)
at System.Exception.ToString(Boolean needFileLineInfo)
at System.Exception.ToString()
[the rest of the trace is removed]
Which corresponds to the following line of application code. The following is in a catch block, and returns the string to the method that actually throws:
private void MainFunction()
{
...
try
{
string doc = CreateXMLDocument(); // <- Out of Memory throws here
}
catch (Exception ex)
{
CoreLogging("Error in MainFunction.", ex);
}
}
private string CreateXMLDocument()
{
try
{
//Some basic and well constrained XML document creation:
...
}
catch (Exception ex)
{
return "Exception message: " + ex.ToString(); // <- This is the last line of the trace
}
}
What should I make of this? Clearly Exception.Message should be used instead of Exception.ToString(), but I'd still like to understand this. Does
at System.Text.StringBuilder.ToString()
at System.Diagnostics.StackTrace.ToString(TraceFormat traceFormat)
mean that the stack trace of the exception in CreateXMLDocument was so mammoth caused OutOfMemory? I'm curious to see how that would occur as there's definitely no circular calls in CreateXMLDocument, which is the only thing I can think of that could cause an enormous stack trace.
Has anyone else encountered a similar situation?
I little bit of guessing:
1) CLR rises a OutOfMemoryException. 2) You catch this exception and call .ToString on it
3) ToString() tries to allocate memory to the stack trace but... there is no memory and another OutOfMemoryException is rised.
In the comments you said that the XML documents have a few hundreds of kbytes, this could be a/the problem if your server run on 32bits, because of the fragmentation of the LOH.

Categories

Resources