473,411 Members | 2,184 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,411 software developers and data experts.

translating more stuff from C

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
Aug 23 '06 #1
8 954
>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.
Aug 23 '06 #2
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.

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:ea**************@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.

Aug 23 '06 #3
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" <ma********************@mvps.orgwrote in message
news:ea**************@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.

Aug 23 '06 #4
Got it! I added

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

after

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

and it worked.

Thanks Mattias!
Lance
"Lance" <nu***@business.comwrote in message
news:u8**************@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" <ma********************@mvps.orgwrote in message
news:ea**************@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.


Aug 23 '06 #5
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.
Aug 24 '06 #6
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

"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:ei**************@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.

Aug 24 '06 #7
>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.
Aug 24 '06 #8
Hm, ok....how would you suggest doing that?
"Mattias Sjögren" <ma********************@mvps.orgwrote in message
news:Oe**************@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.

Aug 24 '06 #9

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

Similar topics

7
by: Joshua Beall | last post by:
Hi All, Any thoughts on the easiest way to translate a MySQL timestamp (which looks like 20040422090941) to the datetime format (which looks like 2004-04-22 09:09:41). This is just to make it...
6
by: Davis Marques | last post by:
hi; I'm translating some PHP scripts to Python and have hit a roadblock with a for statement. If someone could explain to me how one should translate the multiple increment, evaluations, etc....
13
by: cjl | last post by:
Hey all: I'm working on a 'pure' python port of some existing software. Implementations of what I'm trying to accomplish are available (open source) in C++ and in Java. Which would be...
2
by: calin.hanchevici | last post by:
Hi all, I have a C++ library I call from python. The problem is I have c++ exceptions that i want to be translated to python. I want to be able to do stuff like: try: my_cpp_function() except...
12
by: Charles Law | last post by:
Hi guys A bit of curve ball here ... I have a document (Word) that contains a series of instructions in sections and subsections (and sub-subsections). There are 350 pages of them. I need to...
3
by: Jon Maz | last post by:
Hi All, Am getting frustrated trying to port the following (pretty simple) function to CSharp. The problem is that I'm lousy at Regular Expressions.... //from...
1
by: laredotornado | last post by:
Hello, My hosting company does not support the PHP mcrypt functions. Instead, they recomend using the command line, /usr/local/bin/mcrypt utility via PHP's exec method. Sadly, they do not provide...
23
by: gregf | last post by:
I have a paragraph of text pasted into a word document, it's in Polish, complete with polish characters. They show up just fine in word, but the program I use for web page programming, HomeSite,...
10
by: Dave | last post by:
Anyone familiar with PHP? I'm trying to make a translation. In PHP you can get the current object's name by going like this: get_class(item) == 'ClassName' I've tried type(item), but since I...
1
by: timn | last post by:
Translating Access SQL queries into SQL subqueries. -------------------------------------------------------------------------------- I have a query in Access that uses a subquery, I would like...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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...
0
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...

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.