Home All Groups Group Topic Archive Search About

translating more stuff from C

Author
23 Aug 2006 6:03 PM
Lance
Hi all,

I'm not even really sure how to phrase this question, so please forgive me.

Given an entry in a C++ header file like such:

/////
// Callback for displaying error and warning messages.
typedef void (_stdcall *GM_MessageCallbackFunc)
(
const char* aMessageText
);
/////

and function in the C++ header file like such:

/////
GM_DLL_EXPORTED void __stdcall GM_SetMessageCallback
(
GM_MessageCallbackFunc aCallbackFunc
);
/////

how do I translate this to VB2005 and then actually use it?

Lance

Author
23 Aug 2006 7:57 PM
Mattias Sjögren
>how do I translate this to VB2005 and then actually use it?

Try this:

Delegate Sub GM_MessageCallbackFunc(aMessageText As String)

Declare Sub GM_SetMessageCallback Lib "your.dll" (aCallbackFunc As
GM_MessageCallbackFunc)


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
23 Aug 2006 8:46 PM
Lance
Mattias,

This will take a while me to properly implement, given that I know very little about
delegates.  According to the comments in the C++ header file, this is supposed to:

// Sets the function to call to display error and warning messages generated
// during operations. If a message callback is provided, a message
// dialog will not be shown, instead the callback function will be called
// with the error or warning message that would have been displayed.

Show quoteHide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:eaWTT6uxGHA.1300@TK2MSFTNGP05.phx.gbl...
> >how do I translate this to VB2005 and then actually use it?
>
> Try this:
>
> Delegate Sub GM_MessageCallbackFunc(aMessageText As String)
>
> Declare Sub GM_SetMessageCallback Lib "your.dll" (aCallbackFunc As
> GM_MessageCallbackFunc)
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
Author
23 Aug 2006 9:25 PM
Lance
Ok, I've placed

/////
Public Delegate Sub GM_MessageCallbackFunc(ByVal aMessageText As String)

Public Declare Sub GM_SetMessageCallback Lib "GlobalMapperInterface" _
( _
ByVal aCallbackFunc As GM_MessageCallbackFunc _
)
/////

into a module.  On a Form I've placed

/////
Private Sub HandleLoadError(ByVal sError As String)
Debug.Print(sError)
End Sub

Private Sub Load()
Dim MessageDelegate As GM_MessageCallbackFunc
MessageDelegate = AddressOf HandleLoadError
(...Attempt Data Loading...)
End Sub
/////

but my handling sub is never called.  It should be, because unmanaged DLL that I'm making
calls to displays it's Load Error dialog box - which is what the Sub above is supposed to
supress and let the client handle instead.   What have I done incorrectly?

Lance




Show quoteHide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:eaWTT6uxGHA.1300@TK2MSFTNGP05.phx.gbl...
> >how do I translate this to VB2005 and then actually use it?
>
> Try this:
>
> Delegate Sub GM_MessageCallbackFunc(aMessageText As String)
>
> Declare Sub GM_SetMessageCallback Lib "your.dll" (aCallbackFunc As
> GM_MessageCallbackFunc)
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
Author
23 Aug 2006 9:47 PM
Lance
Got it!  I added

/////
GM_SetMessageCallback(MessageDelegate)
/////

after

/////
Dim MessageDelegate As GM_MessageCallbackFunc
MessageDelegate = AddressOf HandleLoadError
/////

and it worked.

Thanks Mattias!
Lance


Show quoteHide quote
"Lance" <nu***@business.com> wrote in message
news:u8is$qvxGHA.3460@TK2MSFTNGP03.phx.gbl...
> Ok, I've placed
>
> /////
> Public Delegate Sub GM_MessageCallbackFunc(ByVal aMessageText As String)
>
> Public Declare Sub GM_SetMessageCallback Lib "GlobalMapperInterface" _
> ( _
> ByVal aCallbackFunc As GM_MessageCallbackFunc _
> )
> /////
>
> into a module.  On a Form I've placed
>
> /////
> Private Sub HandleLoadError(ByVal sError As String)
> Debug.Print(sError)
> End Sub
>
> Private Sub Load()
> Dim MessageDelegate As GM_MessageCallbackFunc
> MessageDelegate = AddressOf HandleLoadError
> (...Attempt Data Loading...)
> End Sub
> /////
>
> but my handling sub is never called.  It should be, because unmanaged DLL that I'm
> making calls to displays it's Load Error dialog box - which is what the Sub above is
> supposed to supress and let the client handle instead.   What have I done incorrectly?
>
> Lance
>
>
>
>
> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
> news:eaWTT6uxGHA.1300@TK2MSFTNGP05.phx.gbl...
>> >how do I translate this to VB2005 and then actually use it?
>>
>> Try this:
>>
>> Delegate Sub GM_MessageCallbackFunc(aMessageText As String)
>>
>> Declare Sub GM_SetMessageCallback Lib "your.dll" (aCallbackFunc As
>> GM_MessageCallbackFunc)
>>
>>
>> Mattias
>>
>> --
>> Mattias Sjögren [C# MVP]  mattias @ mvps.org
>> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
>> Please reply only to the newsgroup.
>
>
Author
24 Aug 2006 5:19 AM
Mattias Sjögren
Just make sure you keep a live reference to the delegate as long as
the callback is needed. If you let the delegate get garbage collected,
bad things will happen.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
24 Aug 2006 12:26 PM
Lance
If all the code requiring the reference to the delegate is in the same procedure as the
delegate itself then it shouldn't be collected too early, right?

Lance

Show quoteHide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:eig5n0zxGHA.3500@TK2MSFTNGP02.phx.gbl...
> Just make sure you keep a live reference to the delegate as long as
> the callback is needed. If you let the delegate get garbage collected,
> bad things will happen.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
Author
24 Aug 2006 8:12 PM
Mattias Sjögren
>If all the code requiring the reference to the delegate is in the same procedure as the
>delegate itself then it shouldn't be collected too early, right?

It could be. The garbage collector can be pretty aggressive and clean
up objects even before the method in which they are created returns.

Any time you're dealing with asynchronous callbacks where the actual
calls back to your code happen after the P/Invoke call (to
GM_SetMessageCallback in this case) returns, you have to be careful to
keep the delegate alive.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Author
24 Aug 2006 9:11 PM
Lance
Hm, ok....how would you suggest doing that?


Show quoteHide quote
"Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
news:OeMrTl7xGHA.1288@TK2MSFTNGP03.phx.gbl...
>
>>If all the code requiring the reference to the delegate is in the same procedure as the
>>delegate itself then it shouldn't be collected too early, right?
>
> It could be. The garbage collector can be pretty aggressive and clean
> up objects even before the method in which they are created returns.
>
> Any time you're dealing with asynchronous callbacks where the actual
> calls back to your code happen after the P/Invoke call (to
> GM_SetMessageCallback in this case) returns, you have to be careful to
> keep the delegate alive.
>
>
> Mattias
>
> --
> Mattias Sjögren [C# MVP]  mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.