C Interoperability on Windows -- or __cdecl, __stdcall, __fastcall

alexzenk zenk2004@mail.ru
Thu Aug 9 20:54:00 GMT 2007



Tobias Burnus wrote:
> 
> Hi all,
> 
> on the GNU-Fortran@googlegroups list (gfortran user list) there was the
> question how to access the Windows' ABI function from Fortran.
> 
> Using the ISO C Bindings helps in principle, but Windows has another
> speciality: It uses the __stdcall calling conventions whereas gfortran
> uses the __cdecl, calling convention. Cf. 
> http://www.codeproject.com/cpp/calling_conventions_demystified.asp
> 
> The question is now: How to tell the compiler to generate a call to a  C
> function using this special calling convention?
> 
> 
> Lahey seemingly has a "DLL_IMPORT FunctionName" statement, one could
> also enhance BIND(c [, name=...]) by a
> "callingconvention="[cdecl,stdcall,fastcall]", or ?
> 
> What do you think?
> 
> Tobias,
> who does not have much Windows programming experience.
> 
> 
This is working example: How to call Win32 API function from gfortran.
I think this you will help.


---------------File myfirst1.f95------------------------------------
SUBROUTINE FortranSub()
integer i, j
 CHARACTER(LEN = 15):: Text
 CHARACTER(LEN = 15) :: Caption
i = 0
j = 0           
Text = "Hello All"//CHAR(0)
 Caption = "gfortran"//CHAR(0) 
 CALL MBox(i, j, Caption, Text)
END  SUBROUTINE FortranSub
--------------File csub.c----------------------------------------------
#include <windows.h>
#include <stdio.h>
void fortransub_();
void mbox_(int *hWnd, long int *uTape, char *lpCaption, char *lpText);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
{
  fortransub_();
  return 0;
}
void mbox_(int *hWnd, long int *uTape, char *lpCaption, char *lpText)
{
  MessageBox(*hWnd, lpText, lpCaption, *uTape);
}
_____________________________________________________________________________
------------ .bat file--------------------------------------------------
path=h:\gfortran\bin
set C_INCLUDE_PATH = h:\gfortran\include
set LIBRARY_PATH=H:\gfortran\lib 
 gfortran -c  %1.f95
 gcc -I h:\gfortran\include  -c  csub.c
rem                                            gcc %1.o csub.o -o 
H:/gfortran/Output/%1.exe -mwindows
 gcc %1.o csub.o -o  H:/gfortran/Output/%1.exe
echo off
if errorlevel 1 goto osh
echo TERMINATED WITH CODE 0
goto con
:osh
echo TERMINATED WITH CODE 1
:con
 Alex


-- 
View this message in context: http://www.nabble.com/C-Interoperability-on-Windows----or-__cdecl%2C-__stdcall%2C-__fastcall-tf4232210.html#a12080528
Sent from the gcc - fortran mailing list archive at Nabble.com.



More information about the Fortran mailing list