471,354 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

How to pass a function address to a COM callback

I have a problem dealing with passing a function address to a COM callback. I
use this COM function for communicating to a hardware. My original project
was written in VB. I have converted it to C#. One of the problem is passing
a function address to a COM function as a parameter with another progress
value. My callback function is very simple using the progress value to update
my progressbar. Because this COM function usually takes a long time (sending
a large mount of data to a hardware for example). In my old VB project, it
works fine. But when I convert it to C#, I do not know how to do it. Is there
anybody can help me to solve this problem?

The following is the VB code:

‘Declaration
Dim ProgressValue As Long
Dim objHardware As MyComLib.Hardware
Dim FileName As String

Private Form_Load()
Set objHardware = New MyComLib.Hardware
FileName = “C:\Test.bin”
End Sub

Private Sub cmdSendData_Click()
Dim lAddress As Long
lAddress = AddressOf UpdateProgressBar
Call objHardware.SendData(FileName, lAddress, ProgressValue)
End Sub
Public Sun UpdateProgressBar()
ProgressBar1.Value = ProgressValue
End Sub

Actually objHardware, ProgressValue and UpdateProgressBar are defined in
Module as public variables.

Many thanks,
Minfu
Jan 11 '06 #1
6 2261
Minfu,

You should be able to declare the parameter of type MethodInvoker (which
is a delegate with the same signature). Then, it will pass the address of
the delegate to the COM object.

The only thing you have to worry about here is storing the object and
making sure it does not get garbage collected before your function is done
executing (if it executes asynchronously, if it doesn't, then it doesn't
matter). You need to store the object that has this method somewhere so
that it doesn't get collected.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Minfu Lu" <Minfu Lu@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have a problem dealing with passing a function address to a COM callback.
I
use this COM function for communicating to a hardware. My original project
was written in VB. I have converted it to C#. One of the problem is
passing
a function address to a COM function as a parameter with another progress
value. My callback function is very simple using the progress value to
update
my progressbar. Because this COM function usually takes a long time
(sending
a large mount of data to a hardware for example). In my old VB project, it
works fine. But when I convert it to C#, I do not know how to do it. Is
there
anybody can help me to solve this problem?

The following is the VB code:

'Declaration
Dim ProgressValue As Long
Dim objHardware As MyComLib.Hardware
Dim FileName As String

Private Form_Load()
Set objHardware = New MyComLib.Hardware
FileName = "C:\Test.bin"
End Sub

Private Sub cmdSendData_Click()
Dim lAddress As Long
lAddress = AddressOf UpdateProgressBar
Call objHardware.SendData(FileName, lAddress, ProgressValue)
End Sub
Public Sun UpdateProgressBar()
ProgressBar1.Value = ProgressValue
End Sub

Actually objHardware, ProgressValue and UpdateProgressBar are defined in
Module as public variables.

Many thanks,
Minfu

Jan 11 '06 #2
How to declare the delagate?

"Nicholas Paldino [.NET/C# MVP]" wrote:
Minfu,

You should be able to declare the parameter of type MethodInvoker (which
is a delegate with the same signature). Then, it will pass the address of
the delegate to the COM object.

The only thing you have to worry about here is storing the object and
making sure it does not get garbage collected before your function is done
executing (if it executes asynchronously, if it doesn't, then it doesn't
matter). You need to store the object that has this method somewhere so
that it doesn't get collected.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Minfu Lu" <Minfu Lu@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
I have a problem dealing with passing a function address to a COM callback.
I
use this COM function for communicating to a hardware. My original project
was written in VB. I have converted it to C#. One of the problem is
passing
a function address to a COM function as a parameter with another progress
value. My callback function is very simple using the progress value to
update
my progressbar. Because this COM function usually takes a long time
(sending
a large mount of data to a hardware for example). In my old VB project, it
works fine. But when I convert it to C#, I do not know how to do it. Is
there
anybody can help me to solve this problem?

The following is the VB code:

'Declaration
Dim ProgressValue As Long
Dim objHardware As MyComLib.Hardware
Dim FileName As String

Private Form_Load()
Set objHardware = New MyComLib.Hardware
FileName = "C:\Test.bin"
End Sub

Private Sub cmdSendData_Click()
Dim lAddress As Long
lAddress = AddressOf UpdateProgressBar
Call objHardware.SendData(FileName, lAddress, ProgressValue)
End Sub
Public Sun UpdateProgressBar()
ProgressBar1.Value = ProgressValue
End Sub

Actually objHardware, ProgressValue and UpdateProgressBar are defined in
Module as public variables.

Many thanks,
Minfu


Jan 11 '06 #3
Minfu,

Most likely, you will have to create the COM interface declaration
yourself.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Minfu Lu" <Mi*****@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
How to declare the delagate?

"Nicholas Paldino [.NET/C# MVP]" wrote:
Minfu,

You should be able to declare the parameter of type MethodInvoker
(which
is a delegate with the same signature). Then, it will pass the address
of
the delegate to the COM object.

The only thing you have to worry about here is storing the object and
making sure it does not get garbage collected before your function is
done
executing (if it executes asynchronously, if it doesn't, then it doesn't
matter). You need to store the object that has this method somewhere so
that it doesn't get collected.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Minfu Lu" <Minfu Lu@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
>I have a problem dealing with passing a function address to a COM
>callback.
>I
> use this COM function for communicating to a hardware. My original
> project
> was written in VB. I have converted it to C#. One of the problem is
> passing
> a function address to a COM function as a parameter with another
> progress
> value. My callback function is very simple using the progress value to
> update
> my progressbar. Because this COM function usually takes a long time
> (sending
> a large mount of data to a hardware for example). In my old VB project,
> it
> works fine. But when I convert it to C#, I do not know how to do it. Is
> there
> anybody can help me to solve this problem?
>
> The following is the VB code:
>
> 'Declaration
> Dim ProgressValue As Long
> Dim objHardware As MyComLib.Hardware
> Dim FileName As String
>
> Private Form_Load()
> Set objHardware = New MyComLib.Hardware
> FileName = "C:\Test.bin"
> End Sub
>
> Private Sub cmdSendData_Click()
> Dim lAddress As Long
> lAddress = AddressOf UpdateProgressBar
> Call objHardware.SendData(FileName, lAddress, ProgressValue)
> End Sub
> Public Sun UpdateProgressBar()
> ProgressBar1.Value = ProgressValue
> End Sub
>
> Actually objHardware, ProgressValue and UpdateProgressBar are defined
> in
> Module as public variables.
>
> Many thanks,
>
>
> Minfu
>
>


Jan 11 '06 #4
Nicholas,

I do not have the original COM source code. This COM function interface is
fixed (the second parameter must be an int32 address of a callback function).
How can I do it?

Thanks,
Minfu

"Nicholas Paldino [.NET/C# MVP]" wrote:
Minfu,

Most likely, you will have to create the COM interface declaration
yourself.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Minfu Lu" <Mi*****@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
How to declare the delagate?

"Nicholas Paldino [.NET/C# MVP]" wrote:
Minfu,

You should be able to declare the parameter of type MethodInvoker
(which
is a delegate with the same signature). Then, it will pass the address
of
the delegate to the COM object.

The only thing you have to worry about here is storing the object and
making sure it does not get garbage collected before your function is
done
executing (if it executes asynchronously, if it doesn't, then it doesn't
matter). You need to store the object that has this method somewhere so
that it doesn't get collected.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Minfu Lu" <Minfu Lu@discussions.microsoft.com> wrote in message
news:98**********************************@microsof t.com...
>I have a problem dealing with passing a function address to a COM
>callback.
>I
> use this COM function for communicating to a hardware. My original
> project
> was written in VB. I have converted it to C#. One of the problem is
> passing
> a function address to a COM function as a parameter with another
> progress
> value. My callback function is very simple using the progress value to
> update
> my progressbar. Because this COM function usually takes a long time
> (sending
> a large mount of data to a hardware for example). In my old VB project,
> it
> works fine. But when I convert it to C#, I do not know how to do it. Is
> there
> anybody can help me to solve this problem?
>
> The following is the VB code:
>
> 'Declaration
> Dim ProgressValue As Long
> Dim objHardware As MyComLib.Hardware
> Dim FileName As String
>
> Private Form_Load()
> Set objHardware = New MyComLib.Hardware
> FileName = "C:\Test.bin"
> End Sub
>
> Private Sub cmdSendData_Click()
> Dim lAddress As Long
> lAddress = AddressOf UpdateProgressBar
> Call objHardware.SendData(FileName, lAddress, ProgressValue)
> End Sub
> Public Sun UpdateProgressBar()
> ProgressBar1.Value = ProgressValue
> End Sub
>
> Actually objHardware, ProgressValue and UpdateProgressBar are defined
> in
> Module as public variables.
>
> Many thanks,
>
>
> Minfu
>
>


Jan 11 '06 #5
Minfu,

You will have to define the interface that you are trying to access in
code. When defining that particular method in code, you will have to
declare that parameter as type MethodInvoker. Once you do that, COM interop
will marshal the parameter as a function pointer.

Then, you cast your instance of the COM object to the interface
definition that you created, and it should work.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Minfu Lu" <Mi*****@discussions.microsoft.com> wrote in message
news:AD**********************************@microsof t.com...
Nicholas,

I do not have the original COM source code. This COM function interface is
fixed (the second parameter must be an int32 address of a callback
function).
How can I do it?

Thanks,
Minfu

"Nicholas Paldino [.NET/C# MVP]" wrote:
Minfu,

Most likely, you will have to create the COM interface declaration
yourself.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Minfu Lu" <Mi*****@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
> How to declare the delagate?
>
> "Nicholas Paldino [.NET/C# MVP]" wrote:
>
>> Minfu,
>>
>> You should be able to declare the parameter of type MethodInvoker
>> (which
>> is a delegate with the same signature). Then, it will pass the
>> address
>> of
>> the delegate to the COM object.
>>
>> The only thing you have to worry about here is storing the object
>> and
>> making sure it does not get garbage collected before your function is
>> done
>> executing (if it executes asynchronously, if it doesn't, then it
>> doesn't
>> matter). You need to store the object that has this method somewhere
>> so
>> that it doesn't get collected.
>>
>> Hope this helps.
>>
>>
>> --
>> - Nicholas Paldino [.NET/C# MVP]
>> - mv*@spam.guard.caspershouse.com
>>
>> "Minfu Lu" <Minfu Lu@discussions.microsoft.com> wrote in message
>> news:98**********************************@microsof t.com...
>> >I have a problem dealing with passing a function address to a COM
>> >callback.
>> >I
>> > use this COM function for communicating to a hardware. My original
>> > project
>> > was written in VB. I have converted it to C#. One of the problem is
>> > passing
>> > a function address to a COM function as a parameter with another
>> > progress
>> > value. My callback function is very simple using the progress value
>> > to
>> > update
>> > my progressbar. Because this COM function usually takes a long time
>> > (sending
>> > a large mount of data to a hardware for example). In my old VB
>> > project,
>> > it
>> > works fine. But when I convert it to C#, I do not know how to do it.
>> > Is
>> > there
>> > anybody can help me to solve this problem?
>> >
>> > The following is the VB code:
>> >
>> > 'Declaration
>> > Dim ProgressValue As Long
>> > Dim objHardware As MyComLib.Hardware
>> > Dim FileName As String
>> >
>> > Private Form_Load()
>> > Set objHardware = New MyComLib.Hardware
>> > FileName = "C:\Test.bin"
>> > End Sub
>> >
>> > Private Sub cmdSendData_Click()
>> > Dim lAddress As Long
>> > lAddress = AddressOf UpdateProgressBar
>> > Call objHardware.SendData(FileName, lAddress, ProgressValue)
>> > End Sub
>> > Public Sun UpdateProgressBar()
>> > ProgressBar1.Value = ProgressValue
>> > End Sub
>> >
>> > Actually objHardware, ProgressValue and UpdateProgressBar are
>> > defined
>> > in
>> > Module as public variables.
>> >
>> > Many thanks,
>> >
>> >
>> > Minfu
>> >
>> >
>>
>>
>>


Jan 11 '06 #6
You do have a typelib or the DLL has an embedded typelib (else it would be
real hard to use it from VB6), right.
Take that typelib (.tlb) (or the DLL if you don't have a typelib) as
typelibname and run
tlbimp.exe typelibname /out:xxxx.interop.dll

- Run ildasm on the generated interop assembly and post the SendData method
declaration, I'm just curious to see how the second arg. is declared.

Willy.
"Minfu Lu" <Mi*****@discussions.microsoft.com> wrote in message
news:AD**********************************@microsof t.com...
| Nicholas,
|
| I do not have the original COM source code. This COM function interface is
| fixed (the second parameter must be an int32 address of a callback
function).
| How can I do it?
|
| Thanks,
|
|
| Minfu
|
|
|
| "Nicholas Paldino [.NET/C# MVP]" wrote:
|
| > Minfu,
| >
| > Most likely, you will have to create the COM interface declaration
| > yourself.
| >
| > --
| > - Nicholas Paldino [.NET/C# MVP]
| > - mv*@spam.guard.caspershouse.com
| >
| > "Minfu Lu" <Mi*****@discussions.microsoft.com> wrote in message
| > news:32**********************************@microsof t.com...
| > > How to declare the delagate?
| > >
| > > "Nicholas Paldino [.NET/C# MVP]" wrote:
| > >
| > >> Minfu,
| > >>
| > >> You should be able to declare the parameter of type MethodInvoker
| > >> (which
| > >> is a delegate with the same signature). Then, it will pass the
address
| > >> of
| > >> the delegate to the COM object.
| > >>
| > >> The only thing you have to worry about here is storing the object
and
| > >> making sure it does not get garbage collected before your function is
| > >> done
| > >> executing (if it executes asynchronously, if it doesn't, then it
doesn't
| > >> matter). You need to store the object that has this method somewhere
so
| > >> that it doesn't get collected.
| > >>
| > >> Hope this helps.
| > >>
| > >>
| > >> --
| > >> - Nicholas Paldino [.NET/C# MVP]
| > >> - mv*@spam.guard.caspershouse.com
| > >>
| > >> "Minfu Lu" <Minfu Lu@discussions.microsoft.com> wrote in message
| > >> news:98**********************************@microsof t.com...
| > >> >I have a problem dealing with passing a function address to a COM
| > >> >callback.
| > >> >I
| > >> > use this COM function for communicating to a hardware. My original
| > >> > project
| > >> > was written in VB. I have converted it to C#. One of the problem is
| > >> > passing
| > >> > a function address to a COM function as a parameter with another
| > >> > progress
| > >> > value. My callback function is very simple using the progress value
to
| > >> > update
| > >> > my progressbar. Because this COM function usually takes a long time
| > >> > (sending
| > >> > a large mount of data to a hardware for example). In my old VB
project,
| > >> > it
| > >> > works fine. But when I convert it to C#, I do not know how to do
it. Is
| > >> > there
| > >> > anybody can help me to solve this problem?
| > >> >
| > >> > The following is the VB code:
| > >> >
| > >> > 'Declaration
| > >> > Dim ProgressValue As Long
| > >> > Dim objHardware As MyComLib.Hardware
| > >> > Dim FileName As String
| > >> >
| > >> > Private Form_Load()
| > >> > Set objHardware = New MyComLib.Hardware
| > >> > FileName = "C:\Test.bin"
| > >> > End Sub
| > >> >
| > >> > Private Sub cmdSendData_Click()
| > >> > Dim lAddress As Long
| > >> > lAddress = AddressOf UpdateProgressBar
| > >> > Call objHardware.SendData(FileName, lAddress, ProgressValue)
| > >> > End Sub
| > >> > Public Sun UpdateProgressBar()
| > >> > ProgressBar1.Value = ProgressValue
| > >> > End Sub
| > >> >
| > >> > Actually objHardware, ProgressValue and UpdateProgressBar are
defined
| > >> > in
| > >> > Module as public variables.
| > >> >
| > >> > Many thanks,
| > >> >
| > >> >
| > >> > Minfu
| > >> >
| > >> >
| > >>
| > >>
| > >>
| >
| >
| >
Jan 11 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Ekim | last post: by
7 posts views Thread by SB | last post: by
reply views Thread by Stephen Walch | last post: by
2 posts views Thread by Pradeep | last post: by
9 posts views Thread by =?Utf-8?B?RWR3YXJkUw==?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.