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 10 3619
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... 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
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... 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
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... 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... 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
Grant,
Thanks for the info. & response.What I meant by Globally Declared was that I
have a .CS file that defines it and that file is shared in both projects as
you surmised. But I did not realize the runtime would treat them as 2
distinct delegate types.
I'll take another look at it in the morning and try your idea and let you
know if it works.
Thanks!
John
"Grant Richins [MS]" <gr*****@online.microsoft.com> wrote in message
news:OF**************@TK2MSFTNGP12.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... 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... 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
Grant,
I tried your suggestion, but alas it still responds the same way. What
exactly did you mean by "and then the other project needs to reference it"?
If you mean I need to set a reference in the Dll to the cleint's project, I
can't do that because that's the EXE and you can't set references to EXE's.
If you mean I need to set a reference in the EXE project to the Dll, I can't
do that either because the Dll is unknown (except that it supports a
particular interface) until runtime and is totally on it's own and MUST use
late binding..
It would seem to me that passing a function pointer into a late bound Dll
method should be straightforward and common occurance. Is there some reason
one should be prevented from doing this? If so, I suppose I could try
triggering a special custom event. Unfortunately, my fear is that it won't
work either because the unknown Dll would have to support triggering the
special custom event. How can you enforce that when only methods are allowed
to be part of the interface?
Any more ideas/suggestions?
TIA,
John
"John Bowman jo*********@thermo.com>" <<Remove this before reply> wrote in
message news:uq**************@TK2MSFTNGP09.phx.gbl... Grant,
Thanks for the info. & response.What I meant by Globally Declared was that
I have a .CS file that defines it and that file is shared in both projects
as you surmised. But I did not realize the runtime would treat them as 2 distinct delegate types.
I'll take another look at it in the morning and try your idea and let you know if it works.
Thanks!
John
"Grant Richins [MS]" <gr*****@online.microsoft.com> wrote in message news:OF**************@TK2MSFTNGP12.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... 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... > 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 > >
<"John Bowman" <<Remove this before reply> jo*********@thermo.com>>
wrote: I tried your suggestion, but alas it still responds the same way. What exactly did you mean by "and then the other project needs to reference it"? If you mean I need to set a reference in the Dll to the cleint's project, I can't do that because that's the EXE and you can't set references to EXE's.
See http://www.pobox.com/~skeet/csharp/plugin.html
It's not dealing with delegates specifically, but it should give you an
idea of the options.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon,
Thanks for the link and insight, if gives me an idea to at leat try. What I
still don't understand is since I'm passing the delegate as an argument why
doesn't the Dll code recieve what's passed in and ues it at runtime, rather
than creating a separate delegate inside dll? I must be missing something...
(: .
John
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... <"John Bowman" <<Remove this before reply> jo*********@thermo.com>> wrote: I tried your suggestion, but alas it still responds the same way. What exactly did you mean by "and then the other project needs to reference
it"? If you mean I need to set a reference in the Dll to the cleint's
project, I can't do that because that's the EXE and you can't set references to
EXE's. See http://www.pobox.com/~skeet/csharp/plugin.html
It's not dealing with delegates specifically, but it should give you an idea of the options.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
Jon,
I tried it out and it works great. Thanks for the link/help!
John
"John Bowman jo*********@thermo.com>" <<Remove this before reply> wrote in
message news:%2****************@TK2MSFTNGP10.phx.gbl... Jon,
Thanks for the link and insight, if gives me an idea to at leat try. What
I still don't understand is since I'm passing the delegate as an argument
why doesn't the Dll code recieve what's passed in and ues it at runtime,
rather than creating a separate delegate inside dll? I must be missing
something... (: .
John
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om... <"John Bowman" <<Remove this before reply> jo*********@thermo.com>> wrote: I tried your suggestion, but alas it still responds the same way. What exactly did you mean by "and then the other project needs to reference it"? If you mean I need to set a reference in the Dll to the cleint's project, I can't do that because that's the EXE and you can't set references to EXE's. See http://www.pobox.com/~skeet/csharp/plugin.html
It's not dealing with delegates specifically, but it should give you an idea of the options.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
<"John Bowman" <<Remove this before reply> jo*********@thermo.com>>
wrote: Thanks for the link and insight, if gives me an idea to at leat try. What I still don't understand is since I'm passing the delegate as an argument why doesn't the Dll code recieve what's passed in and ues it at runtime, rather than creating a separate delegate inside dll? I must be missing something... (: .
I don't believe it's creating a separate delegate - I believe it's not
able to reconcile what is passed to it as the type of delegate it is
expecting. Then again, I haven't looked at the code in detail.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon,
Your description sounds like what I'm seeing, so I think you're right.
Thanks again,
John
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... <"John Bowman" <<Remove this before reply> jo*********@thermo.com>> wrote: Thanks for the link and insight, if gives me an idea to at leat try.
What I still don't understand is since I'm passing the delegate as an argument
why doesn't the Dll code recieve what's passed in and ues it at runtime,
rather than creating a separate delegate inside dll? I must be missing
something... (: .
I don't believe it's creating a separate delegate - I believe it's not able to reconcile what is passed to it as the type of delegate it is expecting. Then again, I haven't looked at the code in detail.
-- Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Budi |
last post by:
Hi,
We try callback C# method from C++ DLL using delegate, using delegate
without argument works fine for me however the
CallBackFunctionWithParameter which call delegate with parameter crash...
|
by: kurtcobain1978 |
last post by:
--------------------------------------------------------------------------------
I need to do the exactly same thing in VB.NET.
Load a unmanaged C DLL dynamically and then call a function in...
|
by: Gerda |
last post by:
Hi!
I've implemented many times an asynchronous call of a method with a call
backfunction successfully.
But to implement this with VB.NET is not so successfully. I can implement
all events...
|
by: Wilfried Mestdagh |
last post by:
Hi,
I have an application using a DLL and callbacks. It generate random the
error "A callback was made on a garbage collected delegate".
I found some articles that the pointer to the delegate...
|
by: Edwin Gomez |
last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
|
by: Jon E. Scott |
last post by:
I've got a project where I need to create a C# project and a Delphi DLL, in
which the DLL has a callback function to send statuses back to the C#
application. It seems pretty straightforward in a...
|
by: sajin |
last post by:
Hi All..
We are using VB .Net 2005 for implementing an API. API needs to
generate events. For this client wants us to use Windows Callback
(delegate implementation). The intention of using...
|
by: jmDesktop |
last post by:
In a function that takes another function (function pointer) as a
argument, or the callback function, which is the one that "calls
back"? I'm having a hard time understanding the language.
Am I...
|
by: Jef Driesen |
last post by:
I have a C DLL that I want to use from a C# project. The C header file
contains these declarations:
typedef void (*callback_t) (const unsigned char *data, unsigned int
size, void *userdata);...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |