J
John Bowman
Hello,
I need some help getting a callback delegate passed as an argument to a
dynamically linked Dll method so it in turn, can eventually call it. Below
is the salient portions of code I'm dealing with. It all compiles and all
other dynamically linked methods that only pass "regular" arguments
(strings, ints, bools, objects, etc.) work great. However, when this
portion of code runs and hits the line that acutally invokes the Dll method,
it throws the following exception:
"Object type cannot be converted to target type."
Why am I not allowed to pass the callback delegate: cbMyCallBack to the Dll
method?
Why is it trying to convert the argument at all when the Dll callee method
and the argument type match?
Any help would be appreciated, TIA.
GloballyDelcared:
public delegate void NotifyCB();
Inside Object1:
public void SomeWorkerMethod()
{
....
Object2.DoWork(new NotifyCB(cbTheCallBack))
....
}
public static void cbTheCallBack()
{
//Do the callback here ...
}
Inside Object2:
private Assembly m_DllAssembly = Assembly.Load(DllName);
private Type m_oTypeEngine = =
m_DllAssembly.GetType("Namespace.ObjectName");
private object m_oActivator = Activator.CreateInstance(m_oTypeEngine);
private Type m_TypeInterface = =
m_oTypeEngine.GetInterface("InterfaceName");
private MethodInfo m_miMyDllMethod =
m_TypeInterface.GetMethod("MyDllMethod");;
....
public void DoWork(NotifyCB cbMyCallBack)
{
....
object[] oArgs = {cbMyCallBack};
bool bRtn = (bool)m_miMyDllMethod.Invoke(m_oActivator, oArgs);
....
}
Inside the Called DLL, Object3:
public void MyDllMethod(NotifyCB cbMyCallBack)
{
....
//Do some DLL work
....
cbMyCallBack();
}
I need some help getting a callback delegate passed as an argument to a
dynamically linked Dll method so it in turn, can eventually call it. Below
is the salient portions of code I'm dealing with. It all compiles and all
other dynamically linked methods that only pass "regular" arguments
(strings, ints, bools, objects, etc.) work great. However, when this
portion of code runs and hits the line that acutally invokes the Dll method,
it throws the following exception:
"Object type cannot be converted to target type."
Why am I not allowed to pass the callback delegate: cbMyCallBack to the Dll
method?
Why is it trying to convert the argument at all when the Dll callee method
and the argument type match?
Any help would be appreciated, TIA.
GloballyDelcared:
public delegate void NotifyCB();
Inside Object1:
public void SomeWorkerMethod()
{
....
Object2.DoWork(new NotifyCB(cbTheCallBack))
....
}
public static void cbTheCallBack()
{
//Do the callback here ...
}
Inside Object2:
private Assembly m_DllAssembly = Assembly.Load(DllName);
private Type m_oTypeEngine = =
m_DllAssembly.GetType("Namespace.ObjectName");
private object m_oActivator = Activator.CreateInstance(m_oTypeEngine);
private Type m_TypeInterface = =
m_oTypeEngine.GetInterface("InterfaceName");
private MethodInfo m_miMyDllMethod =
m_TypeInterface.GetMethod("MyDllMethod");;
....
public void DoWork(NotifyCB cbMyCallBack)
{
....
object[] oArgs = {cbMyCallBack};
bool bRtn = (bool)m_miMyDllMethod.Invoke(m_oActivator, oArgs);
....
}
Inside the Called DLL, Object3:
public void MyDllMethod(NotifyCB cbMyCallBack)
{
....
//Do some DLL work
....
cbMyCallBack();
}