473,889 Members | 1,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing function pointer in VB.Net

Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr? > )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))
....

How can I get something like that going in VB.Net?
Nov 21 '05 #1
10 15199
Use delegates. Delegates are the equivalent of function pointers in .NET.

Regards,

Ron

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr? > )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))
...

How can I get something like that going in VB.Net?

Nov 21 '05 #2
"Ron Bremmers" <r.********@hom e.nl> schrieb:
Use delegates. Delegates are the equivalent of function pointers in .NET.


Documentation:

Visual Basic Language Concepts -- Events and Delegates
<URL:http://msdn.microsoft. com/library/en-us/vbcn7/html/vaconEventsDele gatesInheritanc e.asp>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3
I tried using delegates but I don't know how. I tried something like this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in .NET.

Regards,

Ron

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr? > )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))
...

How can I get something like that going in VB.Net?


Nov 21 '05 #4
So.. any help here? Anyone else got delegates working as a parameter for a
function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in .NET.

Regards,

Ron

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr? > )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))
...

How can I get something like that going in VB.Net?


Nov 21 '05 #5
So.. any help here? Anyone else got delegates working as a parameter for a
function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in .NET.

Regards,

Ron

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
Coming from a C/C++ background, how would I pass a function pointer to a
function? I want to write a function that handles certain thread spawning.
Here's what I'm trying to invision:

function( thesub as <functionptr? > )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))
...

How can I get something like that going in VB.Net?


Nov 21 '05 #6
ThreadStart wants the name of the actual method that's being executed
because it's actually creating a delegate as well, and I don't think you
can have a delegate of a delegate. Here's what I came up with that
might meet your needs:

Private Sub ThisTest()
'Do Something
End Sub

Private Function StartAThread(By Val thisSub As Threading.Threa dStart)
Dim t as New Threading.Threa d(thisSub)
End Function

Private Sub MyTest()
Dim ts as New Threading.Threa dStart(AddressO f ThisTest)
SomeName(ts)
End Sub

'ThisTest' is the routine you are wanting to run on a separate thread.
'StartAThread' is the routine that actually creates the new thread an
excutes the passed method to it.
'MyTest' is the routine that is running a finds a method that needs to
be spawned on a separate thread.

All you have to do is create the ThreadStart object and pass that to the
'StartAThread' function and everything should work.

Hopefully this helps,
Brian Swanson

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:Ch****@dis cussions.micros oft.com:
So.. any help here? Anyone else got delegates working as a parameter for a

function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like
this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it
did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in
.NET.

Regards,

Ron

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
> Coming from a C/C++ background, how would I pass a function pointer
> to a
> function? I want to write a function that handles certain thread
> spawning.
> Here's what I'm trying to invision:
>
> function( thesub as <functionptr? > )
> dim t as new system.threadin g.thread( _
> new system.threadin g.threadstart( Addressof thesub ))
> ...
>
> How can I get something like that going in VB.Net?


Nov 21 '05 #7
ThreadStart wants the name of the actual method that's being executed
because it's actually creating a delegate as well, and I don't think you
can have a delegate of a delegate. Here's what I came up with that
might meet your needs:

Private Sub ThisTest()
'Do Something
End Sub

Private Function StartAThread(By Val thisSub As Threading.Threa dStart)
Dim t as New Threading.Threa d(thisSub)
End Function

Private Sub MyTest()
Dim ts as New Threading.Threa dStart(AddressO f ThisTest)
SomeName(ts)
End Sub

'ThisTest' is the routine you are wanting to run on a separate thread.
'StartAThread' is the routine that actually creates the new thread an
excutes the passed method to it.
'MyTest' is the routine that is running a finds a method that needs to
be spawned on a separate thread.

All you have to do is create the ThreadStart object and pass that to the
'StartAThread' function and everything should work.

Hopefully this helps,
Brian Swanson

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:Ch****@dis cussions.micros oft.com:
So.. any help here? Anyone else got delegates working as a parameter for a

function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like
this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it
did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
Use delegates. Delegates are the equivalent of function pointers in
.NET.

Regards,

Ron

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:87******** *************** ***********@mic rosoft.com...
> Coming from a C/C++ background, how would I pass a function pointer
> to a
> function? I want to write a function that handles certain thread
> spawning.
> Here's what I'm trying to invision:
>
> function( thesub as <functionptr? > )
> dim t as new system.threadin g.thread( _
> new system.threadin g.threadstart( Addressof thesub ))
> ...
>
> How can I get something like that going in VB.Net?


Nov 21 '05 #8
ChrisB.
Think of "AddressOf" as New for Delegates, you only need to use it when you
want to create a new delegate, not when you already have an existing
Delegate.

Define your function as taking a ThreadStart parameter, when you call your
function use AddressOf, when you create the Thread itself you do not need
AddressOf as you already have a Delegate.

Something like:

Imports System.Threadin g

Private Sub Worker()
' do work here...
End Sub

Private Sub MoreWork()
' do more work here...
End Sub

Private Sub SubmitWork(ByVa l worker As ThreadStart)
Dim theThread As New Thread(worker)
theThread.Start ()
End Sub

Public Sub Main()
SubmitWork(Addr essOf Worker)
SubmitWork(Addr essOf MoreWork)
End Sub

Notice that when we call SubmitWork we use AddressOf to create a new
Delegate. Then when we create a new Thread within SubmitWork, we simply pass
that Delegate directly to the constructor.

Hope this helps
Jay

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:D2******** *************** ***********@mic rosoft.com...
So.. any help here? Anyone else got delegates working as a parameter for a
function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like
this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it
did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
> Use delegates. Delegates are the equivalent of function pointers in
> .NET.
>
>
>
> Regards,
>
>
>
> Ron
>
>
>
> "ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
> news:87******** *************** ***********@mic rosoft.com...
> > Coming from a C/C++ background, how would I pass a function pointer
> > to a
> > function? I want to write a function that handles certain thread
> > spawning.
> > Here's what I'm trying to invision:
> >
> > function( thesub as <functionptr? > )
> > dim t as new system.threadin g.thread( _
> > new system.threadin g.threadstart( Addressof thesub ))
> > ...
> >
> > How can I get something like that going in VB.Net?
>
>
>

Nov 21 '05 #9
ChrisB.
Think of "AddressOf" as New for Delegates, you only need to use it when you
want to create a new delegate, not when you already have an existing
Delegate.

Define your function as taking a ThreadStart parameter, when you call your
function use AddressOf, when you create the Thread itself you do not need
AddressOf as you already have a Delegate.

Something like:

Imports System.Threadin g

Private Sub Worker()
' do work here...
End Sub

Private Sub MoreWork()
' do more work here...
End Sub

Private Sub SubmitWork(ByVa l worker As ThreadStart)
Dim theThread As New Thread(worker)
theThread.Start ()
End Sub

Public Sub Main()
SubmitWork(Addr essOf Worker)
SubmitWork(Addr essOf MoreWork)
End Sub

Notice that when we call SubmitWork we use AddressOf to create a new
Delegate. Then when we create a new Thread within SubmitWork, we simply pass
that Delegate directly to the constructor.

Hope this helps
Jay

"ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
news:D2******** *************** ***********@mic rosoft.com...
So.. any help here? Anyone else got delegates working as a parameter for a
function to be used with an AddressOf operator?

"ChrisB" wrote:
I tried using delegates but I don't know how. I tried something like
this:

delegate function fpMyfunc()
(I also tried delegate sub fpMyfunc())

Then:
function( thesub as fpMyfunc )
dim t as new system.threadin g.thread( _
new system.threadin g.threadstart( Addressof thesub ))

But this did not work. VB.Net complained about the "thesub" after the
Addressof.. saying that I do not need ()'s after the sub name, as if it
did
not recognize "thesub" as being a subroutine.
"Ron Bremmers" wrote:
> Use delegates. Delegates are the equivalent of function pointers in
> .NET.
>
>
>
> Regards,
>
>
>
> Ron
>
>
>
> "ChrisB" <Ch****@discuss ions.microsoft. com> wrote in message
> news:87******** *************** ***********@mic rosoft.com...
> > Coming from a C/C++ background, how would I pass a function pointer
> > to a
> > function? I want to write a function that handles certain thread
> > spawning.
> > Here's what I'm trying to invision:
> >
> > function( thesub as <functionptr? > )
> > dim t as new system.threadin g.thread( _
> > new system.threadin g.threadstart( Addressof thesub ))
> > ...
> >
> > How can I get something like that going in VB.Net?
>
>
>

Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
3617
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
1
342
by: kevin | last post by:
Does any one know how to pass a function pointer as a function parameter from VB .NET to a C dll? Currently I'm passing it this way Public Delegate Sub DSCUserInterruptFunction() Public Declare Function dscUserInt Lib "dscud.dll" (ByVal board As Short,
0
2024
by: Haxan | last post by:
Hi, I have an unmanaged application that converts a function pointer to a delegate and then pass this as a parameter(delegate) to a managed function which then invokes it. Currently Im able to jump to this unmanaged function, but the values of the parameters inside this function Im seeing are not correct(they have some garbage values). //unmanaged class (C++ application)
4
2065
by: peter | last post by:
I can't get this code to link properly. I got two error during link: error LNK2028: unresolved token (0A00000C) "extern "C" int __stdcall EnumFontFamiliesExW(struct HDC__ *,struct tagLOGFONTW *,int (__stdcall*)(struct tagLOGFONTW const *,struct tagTEXTMETRICW const *,unsigned long,long),long,unsigned long)" (?EnumFontFamiliesExW@@$$J220YGHPAUHDC__@@PAUtagLOGFONTW@@P6GHPBU2@PBUtagTEXTMETRICW@@KJ@ZJK@Z) referenced in function "int...
0
9962
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9810
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10791
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9609
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 project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7993
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7150
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6029
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4251
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3256
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.