How to pass a function address to a COM callback

G

Guest

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
G

Guest

How to declare the delagate?

Nicholas Paldino said:
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]
- (e-mail address removed)

Minfu Lu said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

Minfu,

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

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Minfu Lu said:
How to declare the delagate?

Nicholas Paldino said:
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]
- (e-mail address removed)

Minfu Lu said:
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
 
G

Guest

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 said:
Minfu,

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

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Minfu Lu said:
How to declare the delagate?

Nicholas Paldino said:
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]
- (e-mail address removed)

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
 
N

Nicholas Paldino [.NET/C# MVP]

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]
- (e-mail address removed)

Minfu Lu said:
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 said:
Minfu,

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

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Minfu Lu said:
How to declare the delagate?

:

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]
- (e-mail address removed)

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
 
W

Willy Denoyette [MVP]

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.


| 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]
| > - (e-mail address removed)
| >
| > | > > 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]
| > >> - (e-mail address removed)
| > >>
| > >> | > >> >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
| > >> >
| > >> >
| > >>
| > >>
| > >>
| >
| >
| >
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top