473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2382
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ekim | last post by:
my question is as follows: I've got a DLL in which I have a method GetBuffer (this one is extern, exported, is called from outside this program) which shall pass a char-buffer to the...
16
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
7
by: SB | last post by:
What is the proper way to pass a character array (char *) from a "C" dll to a C# method (delegate) in my app? Getting the dll (which simulates a third party dll) to call my delegate works fine. ...
1
by: Lenn | last post by:
Hi, I am using .BeginInvoke to make an asynchronous call to a function, and passing in a AsyncCallback, so client gets notification when method completes, relevant code is provided below: ...
6
by: Marty | last post by:
Hi, I would like to have my class to execute eventually an outside function. I was thinking to pass to my class constructor a delegate object that would be later used in my class to be...
0
by: Stephen Walch | last post by:
I am writing a managed C++ app that calls a third party "C" library. One of the functions I need to call is a standard callback routine: I supply a "C" style callback function and a optional...
6
by: smmk25 | last post by:
Before I state the problem, I just want to let the readers know, I am knew to C++\CLI and interop so please forgive any newbie questions. I have a huge C library which I want to be able to use in...
2
by: Pradeep | last post by:
Hi all, Can any one explain me what is callback function.... I have written some code after reading some tutorials from internet... But I am not sure is it a right way to write a call back...
9
by: =?Utf-8?B?RWR3YXJkUw==?= | last post by:
I would greatly appreciate some help on passing managed object into unmanaged code. I need to pass a reference (address of) of a managed class into unmanaged code (written by a thrid party). The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.