Problem rewriting vb6 to c#
I have this code I'm trying to rewrite to c# from a vbs script.
VB has an array function.
So the VB line of code looks like this (In reality it is an LDAP call):
dGrp.Put "ShowInAddressBook", Array("val", "val")
I would think the c# equiv would be:
dGrp.GetType().InvokeMember("Put",
InvokeMemberFlags,
null, dGrp, new object[] { "ShowInAddressBook", addressBookvalues.Split(',') });
But thi开发者_如何转开发s doesn't work.
I can't call the PUT method multiple times either, because it overwrites previous values. It's expecting all the values.
The script works, but the c# produces this:
Unhandled Exception: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Runtime.InteropServices. COMException (0x80004005): Unspecified error (Exception from HRESULT: 0x80004005 (E_FAIL))
Any help would be good, thanks!
EDIT: Sorry for not explicitly mentioning the solution needs to remain COM based. So using managed code alternatives is not an option (in this case).
Binding flags are:
private static BindingFlags setCOMBindings()
{
return BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.InvokeMethod;
}
I think this was better as a comment but I don't have enough reputation, so here it goes:
I'm not familiar with LDAP, therefore I don't know the objects you're talking about, but wouldn't be possible that the problem is the way you're marshaling the array of values?
If the method is expecting a VARIANT array, maybe the array of address book values should be an array of object instead of an array of strings.
Maybe it would be important if someone could get the signature, in COM way, of this Put method.
Something like
using System.DirectoryServices;
...
DirectoryEntry entry = new DirectoryEntry("LDAP://<your_dn_here>");
object[] entries = addressBookValues.Split(',');
entry.Properties["ShowInAddressBook"].AddRange(entries);
try
{
entry.CommitChanges();
Console.WriteLine("Success!!");
}
catch(Exception e)
{
Console.WriteLine(e);
}
精彩评论