calling DLL from VB

calling DLL from VB

imagem de milan2

I'm calling a DLL from Visual Basic. It works now well (due to your valuable help). I'm passing all my variables in arrays, each array for a certain variable type.

The code in VB is written so that in each loop, arrays are refilled with new values and program calls DLL. DLL then runs number of simulation iterations (within a single call) and sends results back to VB in arrays. New arrays are refilled and so on until all simulation scenarios are executed.

Is there a way I could see which iteration is currently running from the VB user interface? I would like to write this information into the text box so the user will have a better control. That means that within the call, there should be some interactive control between VB and DLL.

Can you, please, give me any help?

Thank you very much.

8 posts / 0 new
Último post
Para obter mais informações sobre otimizações de compiladores, consulte Aviso sobre otimizações.
imagem de Jugoslav Dujic

I'm pretty sure it appeared earlier (maybe in the old Forum but Search facility seems to be defunct there) but I can't find it either. There are several ways, the most simple being using a VB callback routine passed through fortran's arg-list (as EXTERNAL). The routine should be called, say, at the end of iteration loop.

Jugoslav www.xeffort.com
imagem de milan2

I tried to make a simple example, but probably the declaration in VB is wrong.

This is the fortran code:
SUBROUTINE callingback(valuein, cbsub)
!DEC$ ATTRIBUTES DLLEXPORT :: callingback
!DEC$ ATTRIBUTES ALIAS : 'callingback' :: callingback

INTERFACE
SUBROUTINE cbsub(c1)
INTEGER c1
END
END INTERFACE

REAL valuein
REAL array1(1000, 1000)
REAL array2(1000, 1000)
INTEGER c1

c1 = 0
CALL RANDOM_NUMBER(array1)
c1 = 1
CALL cbsub(c1)
CALL RANDOM_NUMBER(array1)
array2 = array1 * valuein
c1 = 2
CALL cbsub(c1)
END SUBROUTINE callingback

And here is the declaration in VB module:
Public Declare Sub callingback Lib "c:My DocumentsCallBackDebugCallBack.dll" (valuein As Single)

Here is how I call it from form1
Private Sub Command1_Click()
valuein = 5
Call callingback(valuein)
End Sub

And here is the callback subroutine that I place in another module:
Public Sub cbsub(c1 As Long)
Form1.Text1.Text = c1
End Sub

I tried to follow the callback VF example, but unfortunatelly there is no description of VB code.

Thank you very much for your advice. Milan2

imagem de Jugoslav Dujic

That code will crash and burn since you have mismatched declarations of callingback -- the second argument is missing in VB. You should pass address of cbsub there. However, sorry, I don't know how to do it -- I'm not quite familiar with VB. Marco?

Jugoslav www.xeffort.com
imagem de Community Admin

Milan,
maybe MS KB Q171729 (have problem with url) will be of some help.

A.

imagem de Jugoslav Dujic

Q181578 seems to be The Right Thing.

Jugoslav www.xeffort.com
imagem de milan2

Thank you for your help. I made a search in the old forum and on the internet. The only step by step example I found was the Q181578 that you proposed. I tried to follow it with my example, but I was not successful.

I started to think about some modifications in my Fortran code with the option to call each iteration using another loop in visual basic, but I would still rather use callbacks that are (I think) much better for my program.

I give my new code here following the same example as above. Please, be so kind and let me know if you have any ideas what might be the problem.

Here is the declaration in general declaration section in Form1 in VB:

Private Declare Sub callingback Lib "c:My DocumentsCallBackDebugCallBack.dll" (valuein As Single)

This is the code in Click event of the Command Button (placed on Form1):
Private Sub Command1_Click()
valuein = 5
Call callingback(valuein, AddressOf callback)
End Sub

Finally, here is the callback subroutine in VB module:
Sub callback(c1 As Long)
Form1.Test1.Text = c1
End Sub

The fortran code:
SUBROUTINE callingback(valuein, callback)

!DEC$ ATTRIBUTES DLLEXPORT :: callingback
!DEC$ ATTRIBUTES ALIAS : 'callingback' :: callingback

INTERFACE
SUBROUTINE callback(c1)
INTEGER c1
END
END INTERFACE

REAL valuein
REAL array1(1000, 1000)
REAL array2(1000, 1000)
INTEGER c1

c1 = 0
CALL RANDOM_NUMBER(array1)
c1 = 1
CALL callback(c1)
CALL RANDOM_NUMBER(array1)
array2 = array1 * valuein
c1 = 2
CALL callback(c1)
END SUBROUTINE callingback

Thank you very much,

Milan

imagem de Jugoslav Dujic

Now it looks quite OK. As I said, I'm not familiar enough with VB; however, I'm suspicious about statement

Form1.Test1.Text = c1


My guess is that it doesn't update the form immediately. Is there perhaps a method Form1.Test1.Update() or something like that?

Could you put a breakpoint inside callback and verify that it's indeed called?

Jugoslav www.xeffort.com

Faça login para deixar um comentário.