- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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...
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...
Link Copied
8 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What is the exact error message you get? What is the specification for sumof in the .cpp file?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
// 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
#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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
!DEC$ ATTRIBUTES ALIAS:"SumOf" :: SumOf
SumOf accepts its arguments by value - do you have directives to control that in the Fortran code?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
//////////////////////////////
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
!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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page