473,574 Members | 2,686 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Hardwa re
Dim FileName As String

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

Private Sub cmdSendData_Cli ck()
Dim lAddress As Long
lAddress = AddressOf UpdateProgressB ar
Call objHardware.Sen dData(FileName, lAddress, ProgressValue)
End Sub
Public Sun UpdateProgressB ar()
ProgressBar1.Va lue = ProgressValue
End Sub

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

Many thanks,
Minfu
Jan 11 '06 #1
6 2397
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.co m

"Minfu Lu" <Minfu Lu@discussions. microsoft.com> wrote in message
news:98******** *************** ***********@mic rosoft.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.Hardwa re
Dim FileName As String

Private Form_Load()
Set objHardware = New MyComLib.Hardwa re
FileName = "C:\Test.bi n"
End Sub

Private Sub cmdSendData_Cli ck()
Dim lAddress As Long
lAddress = AddressOf UpdateProgressB ar
Call objHardware.Sen dData(FileName, lAddress, ProgressValue)
End Sub
Public Sun UpdateProgressB ar()
ProgressBar1.Va lue = ProgressValue
End Sub

Actually objHardware, ProgressValue and UpdateProgressB ar 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.co m

"Minfu Lu" <Minfu Lu@discussions. microsoft.com> wrote in message
news:98******** *************** ***********@mic rosoft.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.Hardwa re
Dim FileName As String

Private Form_Load()
Set objHardware = New MyComLib.Hardwa re
FileName = "C:\Test.bi n"
End Sub

Private Sub cmdSendData_Cli ck()
Dim lAddress As Long
lAddress = AddressOf UpdateProgressB ar
Call objHardware.Sen dData(FileName, lAddress, ProgressValue)
End Sub
Public Sun UpdateProgressB ar()
ProgressBar1.Va lue = ProgressValue
End Sub

Actually objHardware, ProgressValue and UpdateProgressB ar 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.co m

"Minfu Lu" <Mi*****@discus sions.microsoft .com> wrote in message
news:32******** *************** ***********@mic rosoft.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.co m

"Minfu Lu" <Minfu Lu@discussions. microsoft.com> wrote in message
news:98******** *************** ***********@mic rosoft.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.Hardwa re
> Dim FileName As String
>
> Private Form_Load()
> Set objHardware = New MyComLib.Hardwa re
> FileName = "C:\Test.bi n"
> End Sub
>
> Private Sub cmdSendData_Cli ck()
> Dim lAddress As Long
> lAddress = AddressOf UpdateProgressB ar
> Call objHardware.Sen dData(FileName, lAddress, ProgressValue)
> End Sub
> Public Sun UpdateProgressB ar()
> ProgressBar1.Va lue = ProgressValue
> End Sub
>
> Actually objHardware, ProgressValue and UpdateProgressB ar 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.co m

"Minfu Lu" <Mi*****@discus sions.microsoft .com> wrote in message
news:32******** *************** ***********@mic rosoft.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.co m

"Minfu Lu" <Minfu Lu@discussions. microsoft.com> wrote in message
news:98******** *************** ***********@mic rosoft.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.Hardwa re
> Dim FileName As String
>
> Private Form_Load()
> Set objHardware = New MyComLib.Hardwa re
> FileName = "C:\Test.bi n"
> End Sub
>
> Private Sub cmdSendData_Cli ck()
> Dim lAddress As Long
> lAddress = AddressOf UpdateProgressB ar
> Call objHardware.Sen dData(FileName, lAddress, ProgressValue)
> End Sub
> Public Sun UpdateProgressB ar()
> ProgressBar1.Va lue = ProgressValue
> End Sub
>
> Actually objHardware, ProgressValue and UpdateProgressB ar 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.co m

"Minfu Lu" <Mi*****@discus sions.microsoft .com> wrote in message
news:AD******** *************** ***********@mic rosoft.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.co m

"Minfu Lu" <Mi*****@discus sions.microsoft .com> wrote in message
news:32******** *************** ***********@mic rosoft.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.co m
>>
>> "Minfu Lu" <Minfu Lu@discussions. microsoft.com> wrote in message
>> news:98******** *************** ***********@mic rosoft.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.Hardwa re
>> > Dim FileName As String
>> >
>> > Private Form_Load()
>> > Set objHardware = New MyComLib.Hardwa re
>> > FileName = "C:\Test.bi n"
>> > End Sub
>> >
>> > Private Sub cmdSendData_Cli ck()
>> > Dim lAddress As Long
>> > lAddress = AddressOf UpdateProgressB ar
>> > Call objHardware.Sen dData(FileName, lAddress, ProgressValue)
>> > End Sub
>> > Public Sun UpdateProgressB ar()
>> > ProgressBar1.Va lue = ProgressValue
>> > End Sub
>> >
>> > Actually objHardware, ProgressValue and UpdateProgressB ar 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.intero p.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*****@discus sions.microsoft .com> wrote in message
news:AD******** *************** ***********@mic rosoft.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.co m
| >
| > "Minfu Lu" <Mi*****@discus sions.microsoft .com> wrote in message
| > news:32******** *************** ***********@mic rosoft.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.co m
| > >>
| > >> "Minfu Lu" <Minfu Lu@discussions. microsoft.com> wrote in message
| > >> news:98******** *************** ***********@mic rosoft.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.Hardwa re
| > >> > Dim FileName As String
| > >> >
| > >> > Private Form_Load()
| > >> > Set objHardware = New MyComLib.Hardwa re
| > >> > FileName = "C:\Test.bi n"
| > >> > End Sub
| > >> >
| > >> > Private Sub cmdSendData_Cli ck()
| > >> > Dim lAddress As Long
| > >> > lAddress = AddressOf UpdateProgressB ar
| > >> > Call objHardware.Sen dData(FileName, lAddress, ProgressValue)
| > >> > End Sub
| > >> > Public Sun UpdateProgressB ar()
| > >> > ProgressBar1.Va lue = ProgressValue
| > >> > End Sub
| > >> >
| > >> > Actually objHardware, ProgressValue and UpdateProgressB ar 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
6166
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 calling-function for further handling. If I've confused you, here's the code snippet (it's within a simple Win32-Dll-project): char* buffer; //...
16
14103
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 byteArray and afterwards see the changes in C#. (if you wanna know more details: the goal is to write the content of an existing char-array in c++...
7
8474
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. However, as soon as I try to add any "char *" parameters, I start getting exceptions in my C# app. So far I've tried (among others): public...
1
8279
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: AsyncCallback callback = new AsyncCallback(this.NotifyIt); FtpClient ftpC = new FtpClient(FtpServer, FtpUserName, FtpPassword); private void...
6
7737
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 triggered. Does a delegate is the correct object to use to do that? Thanks!
0
1079
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 void* pointer to "private data" (which gets passed back to the callback function). What I want to do is pass an instance of a managed class as the...
6
7660
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 a .NET application and thus am looking into writing a managed C++ wrapper for in vs2005. Furthermore, this library has many callback hooks which...
2
2627
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 function... I have mentioned my doubts in code comments. My code is... void myfun_callback(void(*fp)(char*)) //is this function is a call back? {
9
3536
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 3rd party unmanaged DLL will pass this reference into standard Win32 unmanaged static callback function in my code. Inside this unmanaged callback...
0
7813
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7736
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8106
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6461
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5631
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5305
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3755
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1350
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.