回调方法委托作为Dll函数的参数 [英] Callback Method Delegate as an Argument to Dll Functions

查看:68
本文介绍了回调方法委托作为Dll函数的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我需要一些帮助,将一个回调委托作为参数传递给

动态链接的Dll方法,这样它最终可以叫它。下面

是我正在处理的代码的显着部分。它全部编译和所有

其他动态链接的方法只传递常规参数

(字符串,整数,布尔,对象等)效果很好。但是,当这个代码运行并且点击了实际调用Dll方法的行时,

会引发以下异常:


对象类型无法转换为目标类型。


为什么我不允许传递回调委托:cbMyCallBack到Dll

方法?

当Dll被调用方法

和参数类型匹配时,为什么要尝试转换参数?

任何帮助我将不胜感激,TIA。

GloballyDelcared:


public delegate void NotifyCB();


Inside Object1:


public void SomeWorkerMethod()


{


....


Object2.DoWork(新NotifyCB(cbTheCallBack))


....


}


public static void cbTheCallBack()


{


//这里回调... < br $>

}


Inside Object2:


私有程序集m_DllAssembly = Assembly.Load(DllName);


private类型m_oTypeEngine = =

m_DllAssembly.GetType (" Namespace.ObjectName");


私有对象m_oActivator = Activator.CreateInstance(m_oTypeEngine);


private类型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);


....


}

在被调用的DLL中,Object3:


public void MyDllMethod(NotifyCB cbMyCallBack)


{


....


//是的ome DLL工作


....


cbMyCallBack();


}


-

John C. Bowman

软件工程师

Thermo Electron Scientific Instruments Div。

<在回复之前删除此> jo*********@thermo.com

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();

}

--
John C. Bowman
Software Engineer
Thermo Electron Scientific Instruments Div.
<Remove this before reply> jo*********@thermo.com

推荐答案

请注意:在我包含的

代码的复制和粘贴/清理过程中,我已经介绍了几个不属于

代码,例如= =不应该是,多个;等等。请

忽略它们。


TIA,


John


" John Bowman 乔********* @ thermo.com >" <<在回复之前删除此>写在

消息新闻:Om ************** @ TK2MSFTNGP09.phx.gbl ...
Please note: during the copying and pasting / sanitization process of the
code I included, I''ve introduced several typos that are not really part of
the code, such as = = where it should not be, multiple ;''s, etc. Please
ignore them.

TIA,

John

"John Bowman jo*********@thermo.com>" <<Remove this before reply> wrote in
message news:Om**************@TK2MSFTNGP09.phx.gbl...
你好,
我需要一些帮助,将一个回调委托作为参数传递给
动态链接的Dll方法,这样它最终可以调用它。下面
是我正在处理的代码的显着部分。它全部编译并且所有其他动态链接的方法只传递常规方法。争论
(字符串,整数,bools,对象等)工作得很好。但是,当这部分代码运行并命中实际调用Dll
方法的行时,它会抛出以下异常:

"对象类型无法转换为目标类型。

为什么我不允许传递回调委托:cbMyCallBack到
Dll方法?
为什么它在Dll时尝试转换参数callee方法
和参数类型匹配?

任何帮助将不胜感激,TIA。

GloballyDelcared:

public delegate void NotifyCB ();

Inside Object1:

public void SomeWorkerMethod()

{

...
Object2.DoWork(新NotifyCB(cbTheCallBack))

...

}
公共静态void cbTheCallBack()<
//
//在这里做回调......


Inside Object2:<私有程序集m_DllAssembly = Assembly.Load(DllName);

私有类型m_oTypeEngine = =
m_DllAssembly.GetType(" Namespace.ObjectName");

私有对象m_oActivator = Activator.CreateInstance(m_oTypeEngine);

私有类型m_TypeInterface = =
m_oTypeEngine.GetInterface(" InterfaceName");

私有MethodInfo m_miMyDllMethod =
m_TypeInterface.GetMethod(" MyDllMethod");;

.. 。

public void DoWork(NotifyCB cbMyCallBack)




对象[] oArgs = { cbMyCallBack};

bool bRtn =(bool)m_miMyDllMethod.Invoke(m_oActivator,oArgs);

...

}


在被调用的DLL中,Object3:

public void MyDllMethod(NotifyCB cbMyCallBack)



...

//做一些DLL工作



cbMyCallBack();

}

-
John C. Bowman
软件工程师
Thermo Electron Scientific Instruments Div。
<在回复之前将其删除> jo*********@thermo.com
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();

}

--
John C. Bowman
Software Engineer
Thermo Electron Scientific Instruments Div.
<Remove this before reply> jo*********@thermo.com



另一个请注意:


抛出的异常是一个带有消息的ArgumentException:


"对象类型无法转换为目标类型。

TIA ...再次,


John

John Bowman jo*********@thermo.com >" <<在回复之前删除此>写在

消息新闻:Om ************** @ TK2MSFTNGP09.phx.gbl ...
Another Please note:

The Exception thrown is an ArgumentException with the message:

"Object type cannot be converted to target type."
TIA... again,

John
"John Bowman jo*********@thermo.com>" <<Remove this before reply> wrote in
message news:Om**************@TK2MSFTNGP09.phx.gbl...
你好,
我需要一些帮助,将一个回调委托作为参数传递给
动态链接的Dll方法,这样它最终可以调用它。下面
是我正在处理的代码的显着部分。它全部编译并且所有其他动态链接的方法只传递常规方法。争论
(字符串,整数,bools,对象等)工作得很好。但是,当这部分代码运行并命中实际调用Dll
方法的行时,它会抛出以下异常:

"对象类型无法转换为目标类型。

为什么我不允许传递回调委托:cbMyCallBack到
Dll方法?
为什么它在Dll时尝试转换参数callee方法
和参数类型匹配?

任何帮助将不胜感激,TIA。

GloballyDelcared:

public delegate void NotifyCB ();

Inside Object1:

public void SomeWorkerMethod()

{

...
Object2.DoWork(新NotifyCB(cbTheCallBack))

...

}
公共静态void cbTheCallBack()<
//
//在这里做回调......


Inside Object2:<私有程序集m_DllAssembly = Assembly.Load(DllName);

私有类型m_oTypeEngine = =
m_DllAssembly.GetType(" Namespace.ObjectName");

私有对象m_oActivator = Activator.CreateInstance(m_oTypeEngine);

私有类型m_TypeInterface = =
m_oTypeEngine.GetInterface(" InterfaceName");

私有MethodInfo m_miMyDllMethod =
m_TypeInterface.GetMethod(" MyDllMethod");;

.. 。

public void DoWork(NotifyCB cbMyCallBack)




对象[] oArgs = { cbMyCallBack};

bool bRtn =(bool)m_miMyDllMethod.Invoke(m_oActivator,oArgs);

...

}


在被调用的DLL中,Object3:

public void MyDllMethod(NotifyCB cbMyCallBack)



...

//做一些DLL工作



cbMyCallBack();

}

-
John C. Bowman
软件工程师
Thermo Electron Scientific Instruments Div。
<在回复之前将其删除> jo*********@thermo.com
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();

}

--
John C. Bowman
Software Engineer
Thermo Electron Scientific Instruments Div.
<Remove this before reply> jo*********@thermo.com



你对GloballyDeclared究竟是什么意思?我猜你已经获得了2个项目,每个项目都有自己的副本公共代表无效

NotifyCB();然后运行时将其视为2个不同的委托类型

没有转换。我相信正确的修复将是一个或

其他项目定义NotifyCB,然后另一个项目需要

引用它。看看你的整体布局,我猜想NotifyCB的定义

应该存在于Object2的项目中。


-

--Grant

此帖子按原样提供。没有保证,也没有授予任何权利。

John Bowman jo ***** ****@thermo.com >" <<在回复之前删除此>写在

消息新闻:uW ************** @ TK2MSFTNGP09.phx.gbl ...
What exactly do you mean by "GloballyDeclared"? I''m guessing that you''ve
got 2 projects each with their own copy of "public delegate void
NotifyCB();" which the runtime then treats as 2 distinct delegates types
that have no conversion. I believe the proper fix would be for one or the
other project to define NotifyCB, and then the other project needs to
reference it. Looking at your overall layout, I would guess the definition
of NotifyCB should exist in Object2''s project.

--
--Grant
This posting is provided "AS IS" with no warranties, and confers no rights.
"John Bowman jo*********@thermo.com>" <<Remove this before reply> wrote in
message news:uW**************@TK2MSFTNGP09.phx.gbl...
另一个请注意:<抛出的异常是一个带有消息的ArgumentException:

对象类型无法转换为目标类型。

TIA ...再次,

John

" John Bowman jo *** ******@thermo.com >" <<在回复之前删除此>在消息新闻中写道:Om ************** @ TK2MSFTNGP09.phx.gbl ...
Another Please note:

The Exception thrown is an ArgumentException with the message:

"Object type cannot be converted to target type."
TIA... again,

John
"John Bowman jo*********@thermo.com>" <<Remove this before reply> wrote in
message news:Om**************@TK2MSFTNGP09.phx.gbl...
你好,
我需要一些帮助,将一个回调委托作为参数传递给一个动态链接的Dll方法,这样它最终可以调用它。
以下是我正在处理的代码的显着部分。它全部编译和
所有其他动态链接的方法只传递常规争论
(字符串,整数,bools,对象等)工作得很好。但是,当这部分代码运行并命中实际调用Dll
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,

会引发以下异常:

对象类型无法转换为目标类型。

为什么我不允许传递回调委托:cbMyCallBack到
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


Dll

方法?
当Dll callee
方法和参数类型匹配时,为什么要尝试转换参数?

任何帮助都会受到赞赏, TIA。

GloballyDelcared:

public delegate void NotifyCB();

Inside Object1:

public void SomeWorkerMethod( )

对象2.DoWork(新NotifyCB(cbTheCallBack))

...

}

public static void cbTheCallBack()

//
//这里回调......

}

Inside Object2:

privat e Assembly m_DllAssembly = Assembly.Load(DllName);

private类型m_oTypeEngine = =
m_DllAssembly.GetType(" Namespace.ObjectName");

私有对象m_oActivator = Activator.CreateInstance(m_oTypeEngine);

private类型m_TypeInterface = =
m_oTypeEngine.GetInterface(" InterfaceName");

私有MethodInfo m_miMyDllMethod =
m_TypeInterface.GetMethod(" MyDllMethod");;

...

公共无效DoWork(NotifyCB cbMyCallBack)

{

...

对象[] oArgs = {cbMyCallBack};

bool bRtn =(bool)m_miMyDllMethod.Invoke(m_oActivator,oArgs); <在受调用的DLL中,对象3:

public void MyDllMethod(< b / b) NotifyCB cbMyCallBack)

{

...

//做一些DLL工作

...

cbMyCallBack();

}

-
John C. Bowman
软件工程师
Thermo Electron Scientific Instruments Div。
<在回复之前将其删除> jo*********@thermo.com
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();

}

--
John C. Bowman
Software Engineer
Thermo Electron Scientific Instruments Div.
<Remove this before reply> jo*********@thermo.com




这篇关于回调方法委托作为Dll函数的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆