Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29282 Discussions

facing problem in calling a cpp dll from fortran

pravsaugat
Beginner
1,000 Views
Hi Friends,
i am facing a difficuilt problem in callinga cpp dll from fortran. it shows that the file is corrupted or ivalid . while if i call the same dll from visual basic everything just works fine ..
my fortran code is .

PROGRAM fort
!INTEGER , INTENT(INOUT) :: x
!DEC$ ATTRIBUTES DLLIMPORT, ALIAS: 'SUMOF' :: SumOf

call SumOf(2, 3)
!WRITE(*,*) x
!fort = x
!return fort
end PROGRAM fort

and sumof is the routine in the cpp dll

it works very fine when calling from visual basic...
0 Kudos
8 Replies
Steven_L_Intel1
Employee
1,000 Views
What is the exact error message you get? What is the specification for sumof in the .cpp file?
0 Kudos
pravsaugat
Beginner
1,000 Views
// CalcFunc.cpp

#include "stdafx.h"
#include "CalcFunc.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

// SumOf() takes in two longs X and Y and returns their sum as a long
CALCFUNC_API long _stdcall SumOf(
IN const long X, // input param X (constant)
IN const long Y // input param Y (constant)
)
{
return (X + Y);
}

CALCFUNC_API long _stdcall Cube(
IN const long X
)
{return (X*X*X);
}


/////////////////////////////////////////////////
CalcFunc.h

#ifdef CALCFUNC_EXPORTS
#define CALCFUNC_API __declspec(dllexport)
#else
#define CALCFUNC_API __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif

// SumOf() takes in two longs X and Y and returns their sum as a long
CALCFUNC_API long _stdcall SumOf(
IN const long X, // input param X (constant)
IN const long Y // input param Y (constant)
);

CALCFUNC_API long _stdcall Cube(
IN const long X
);




#ifdef __cplusplus
}
#endif

//////////////////////////////////
stdafx.cpp

#include "stdafx.h"

///////////////////////////////
stdafx.h

#if !defined(AFX_STDAFX_H__B7A71F71_22B4_4FC5_A1B8_8F4CBEE50DFF__INCLUDED_)
#define AFX_STDAFX_H__B7A71F71_22B4_4FC5_A1B8_8F4CBEE50DFF__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


// Fgen Sie hier Ihre Header-Dateien ein
#define WIN32_LEAN_AND_MEAN // Selten benutzte Teile der Windows-Header nicht einbinden

#include

// ZU ERLEDIGEN: Verweisen Sie hier auf zustzliche Header-Dateien, die Ihr Programm bentigt

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ fgt zustzliche Deklarationen unmittelbar vor der vorherigen Zeile ein.

#endif // !defined(AFX_STDAFX_H__B7A71F71_22B4_4FC5_A1B8_8F4CBEE50DFF__INCLUDED_)

and i m getting error

Linking...
fort.obj : error LNK2001: unresolved external symbol _SUMOF@8
Debug/fort.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

while calling both functions from visual basic everything just works fine ..

plz help me out ..

thanx a lot
praveen
0 Kudos
Steven_L_Intel1
Employee
1,000 Views
Ok, this is simple name decoration and case. To your Fortran code, add:

!DEC$ ATTRIBUTES ALIAS:"SumOf" :: SumOf

SumOf accepts its arguments by value - do you have directives to control that in the Fortran code?
0 Kudos
pravsaugat
Beginner
1,000 Views
thank you very much for ur reply.. but i m still getting the problem
//////////////////////////////
my fortran console application code is

program test

!DEC$ ATTRIBUTES STDCALL, ALIAS:'Cube':: Cube, /X/
COMMON /X/ y, z
integer :: X, y, z
y = 3
!b = 4
print*,"before calling the dll"
CALL Cube(y)
!CALL forsum(a,b)
print*,"after calling the dll"
write(6,*) 'the cube of the value passed' ,z
END PROGRAM test

////////////////////////////////////
and my cpp dll code for it is ..
CalcFunc.cpp

#include "stdafx.h"
#include "CalcFunc.h"

BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

// SumOf() takes in two longs X and Y and returns their sum as a long

CALCFUNC_API long _stdcall Cube(
IN const long X
)
{return (X*X*X);
}


CalcFunc.h

#ifdef CALCFUNC_EXPORTS
#define CALCFUNC_API __declspec(dllexport)
#else
#define CALCFUNC_API __declspec(dllimport)
#endif

#ifdef __cplusplus
extern "C" {
#endif

CALCFUNC_API long _stdcall Cube(
IN const long X
);

#ifdef __cplusplus
}
#endif

and i get error like

fort.exe has encountered a problem and needs to close. We are sorry for the inconvenience.
it gets crashed .. and
before calling the dll
forrtl: severe (157): Program Exception - access violation
Image PC Routine Line Source
fort.exe 0040CB58 Unknown Unknown Unknown
fort.exe 004013B9 Unknown Unknown Unknown
fort.exe 0040122F Unknown Unknown Unknown
kernel32.dll 7C816D4F Unknown Unknown Unknown
Press any key to continue

///////////////////////////////////
and this is the code for fortrn dll and it works fine with fortran console application
with !DEC ATTRIBUTES DLLIMPORT::Cube

subroutine Cube(x)

! Expose subroutine Cube to users of this DLL
!
!DEC$ ATTRIBUTES DLLEXPORT::Cube, /X/
COMMON /X/ y, z
integer y, z
! Variables
write(6,*)'the value passed by frotran code is', y
z = y*y*y
write(6,*)'the cube of the values passed is', z
! Body of Cube
return
end subroutine Cube


plz help me .. i have trying this for last month ...
thanx in advance
praveen
0 Kudos
greldak
Beginner
1,000 Views
One thing I have noticed you call Cube passing it a parameter y, however you also pass this tothe subroutineusing a common block - isn't that illegal - I would certainly expect problems when trying to pass these values back to the calling program as you are trying to set one variable to potentially 2 values.
0 Kudos
Steven_L_Intel1
Employee
1,000 Views
On top of what Craig said, I see you have this:

!DEC$ ATTRIBUTES STDCALL, ALIAS:'Cube':: Cube, /X/

what you have done is tell the compiler that common X has the external name 'Cube'. I don't think you want that. Take /X/ off that directive.

I have not tried the code to see what else may be wrong.
0 Kudos
pravsaugat
Beginner
1,000 Views
Hi,
if i remove that directive i get another error..

D:CTestfortfort.f90
Linking...
fort.obj : error LNK2001: unresolved external symbol _CUBE@4
Debug/fort.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Creating browse info file...

fort.exe - 2 error(s), 0 warning(s)

thanx
praveen
0 Kudos
pravsaugat
Beginner
1,000 Views
Hi craig,
i didn't get you.. i tried to pass the parameters with a fortran dll in the same way.. and it worked wothout any problem...

but here i think the problem is with the linking ..
plz help me out .. i m quite new to these things..

thanx
praveen
0 Kudos
Reply