Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Enable to Call Fortran DLL many times using VB.net

Ahmed_A_2
初學者
4,584 檢視

Hi

 I have created Dll using fortran and I could able to call some subroutines from this Dll file using VB.net and passing the arrays I would like to pass between Fortran Dll and VB.net.But I'm facing a problem to call the subroutine more that one time, in other wards when I run the VB.net(interface) and Run amodel (Which will call the subroutine from Dll file), the simulation will be done without any problems. But if I opened a new model and try to run again I could not call this subroutine again from the Dll (it seems the Dll is busy in some thing, although the simulation is done), so I have to close all the interface to be able to run that model. I hope I could be able to describe my problem, What iam asking for if there is any setting I have to take in consideration while i'm creating the Dll file to over come this problem.

this is the fortran subroutine I'm using

Subroutine FortranDLL(A,IA)
!MS$ATTRIBUTES DLLEXPORT, ALIAS: 'FORTRANDLL' :: FortranDLL 
IMPLICIT REAL*8 (A-H,O-Z)
DIMENSION A(5000001),IA(5000001)
OPEN(unit=1000,file=
+'C:\Sams2000\Applications\ATTIF2000\Engine\AttifSavedResults.txt')
CALL ATIFLO00(A,IA)
CLOSE (1000)
Return
End Subroutine FortranDLL

Many thanks

Ahmed

 

0 積分
30 回應
Ahmed_A_2
初學者
1,628 檢視

Ok, I will do that, let me thank you one more time for your great help

Many thanks

Ahmed 

ZlamalJakub
新貢獻者 III
1,628 檢視

You can use something like this (I am writing it from memory) and it is C code.

    char *DllName="Fortran.dll";
    HMODULE FortranLib=LoadLibrary(DllName);
    typedef LPVOID(__cdecl *T_FortranFunction)(double *Array,int *ia);
    T_FortranFunction FortranFunction=(T_FortranFunction) GetProcAddress(FortranLib,"FORTRANFUNCTION"); // function name is exported uppercase
    FortranFunction(Array,ia);  //call to function

    BOOL retval=FreeLibrary(FortranLib);  //remove library from memory

   

If You want to use this approach You cannot link against Fortran.lib because linker creates initialisation of library instead You.



Steven_L_Intel1
1,628 檢視

Sure - if you can figure out how to do something like this from VB it will work, but that's quite complex in VB, if possible at all, and loses a lot of VB's friendliness in syntax.

Ahmed_A_2
初學者
1,628 檢視

Hi Steve

I have thought in some ways in VS to be able to load and unload the DLL file, and I think The only way to do that is using AppDomain in VS so I should be able to load and unload this AppDomain , I have done that but I'm facing a security problem while calling the subroutines from the Dll file , could you please help me to know if there problem I can manage from the dll or VS , I have attached the error I got while calling the Dll subroutine

Many thanks

Steven_L_Intel1
1,628 檢視

Ahmed, sorry, I have never seen that before and would not be able to diagnose based on a screenshot. My guess, though, is that you can't do this. I would encourage you to solve the problem another way. For example, you could have VB call a Fortran DLL that itself used LoadLibrary and GetProcAddress to load the "real" DLL and then call FreeLibrary before returning.

Ahmed_A_2
初學者
1,628 檢視

Hi Steve

Thank you so much for your reply, and I will be do appreciated if you just sent me an example to clarify the other way you suggested , I'm facing a lot of problems with the way I'm using right now.

Many thanks

Ahmed 

Steven_L_Intel1
1,628 檢視

I can't write you a complete example, but if you look at the provided sample DLL\LoadLibrary you'll have 90% of it.  Perhaps you could even write this part of the application in VB - I have no experience calling Windows API routines from VB. The idea is to give you control over loading and unloading the DLL where you need variables reset.

Ahmed_A_2
初學者
1,628 檢視

No problem at all

I will look at the provided examples and if there is any question I will let you know.

Many thanks

Ahmed

Ahmed_A_2
初學者
1,628 檢視

Hi Steve

I can not find such samples in my computer, Could you please help me to know where it is exist.

Many thanks

Ahmed

Steven_L_Intel1
1,628 檢視
Find the folder where the compiler is installed - for example, C:\Program Files (x86)\Intel\Composer XE 2013 Under that is a Samples\en_US\Fortran folder. Unzip DLL.zip to your desktop or some other writable place (NOT under Program Files). You will find the DynamicLoad sample there (sorry, I had the name wrong.) It shows you how to load a DLL, call a routine in it, and unload it.
回覆