TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Gaurav Chinchankar
NA
40
1.7k
C# - Exception when C++ COM dll's used in reflection
Dec 20 2017 1:12 AM
Whenever my supporting COM dll's is called through reflection technique in C#, It gives me "Exception has been thrown by the target of an invocation."
My Code:
public class ExecuteAssembly { IAssembly objIAssembly = new Xml_Operations.XmlRetrive();
public void StartExecution()
{
try
{
Type classType = LoadDll();
// create instance of class Calculator
//object classInstance = Activator.CreateInstance(classType);
dynamic classInstance = (dynamic) Activator.CreateInstance(classType); <-- ERROR GETS FIRED
classInstance.Visible = true;
// get info about method: public
MethodInfo methodCall = classType.GetMethod(objIAssembly.MethodName);
ParameterInfo[] parameters = methodCall.GetParameters();
objIAssembly.MethodOutput = methodCall.Invoke(classInstance, objIAssembly.ParametersArray);
Xml_Operations.XmlStore.OutputToXmlFile(objIAssembly);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private Type LoadDll()
{
string[] dirSplit=objIAssembly.DllPath.Split('\\');
string dllName = dirSplit[dirSplit.Length - 1].Substring(0, dirSplit[dirSplit.Length - 1].Length - 4); ;
int count = dirSplit[dirSplit.Length - 1].Count();
string dirPath = objIAssembly.DllPath.Substring(0, (objIAssembly.DllPath.Length - count));
var files = Directory.GetFiles(dirPath ,"*.dll");
Type[] types;
Type classType = null;
foreach (var file in files)
{
try
{
types = Assembly.LoadFrom(file).GetTypes();
if (types[0].AssemblyQualifiedName.Contains(dllName))
classType = types[0];
}
catch
{
continue; // Can't load as .NET assembly, so ignore
}
}
return classType;
}
}
Reply
Answers (
3
)
How can i register com+ application using msi?
How to make wrapper of EDSDK API To handle Canon Camera