<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic I usually call variables in in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938529#M89421</link>
    <description>&lt;P&gt;I usually call variables&amp;nbsp;in Fortran dll by reference;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Public Declare Function ComputeMult Lib "c:\excel_test.dll" ( &lt;STRONG&gt;Byref &lt;/STRONG&gt;A1&amp;nbsp;As Single, &lt;STRONG&gt;Byref &lt;/STRONG&gt;A2&amp;nbsp;&amp;nbsp;As Single)&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 07 Mar 2014 01:32:45 GMT</pubDate>
    <dc:creator>Hitoshi</dc:creator>
    <dc:date>2014-03-07T01:32:45Z</dc:date>
    <item>
      <title>Excel Function from Fortran .dll</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938527#M89419</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I found several threads addressing this topic, but still need some help getting my Fortran .dll file to work.&amp;nbsp; I am using Windows 7, Excel 2010 and&amp;nbsp; Visual Fortran Composer XE 2013 SP1.&amp;nbsp; I dummed down the code from a 2002 post on this forum (titled Visual Fortran dll call from Excel/VBA)&lt;/P&gt;

&lt;P&gt;My FORTRAN code is:&lt;/P&gt;

&lt;P&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;FUNCTION&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; COMPUTEMULT ( ARG1, ARG2 )&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;

&lt;P&gt;&lt;B&gt;&lt;FONT color="#006400" face="Consolas" size="2"&gt;&lt;FONT color="#006400" face="Consolas" size="2"&gt;&lt;FONT color="#006400" face="Consolas" size="2"&gt;!DEC$ ATTRIBUTES DLLEXPORT,ALIAS:'ComputeMult' :: COMPUTEMULT&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;REAL&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt;*4 ARG1, ARG2, COMPUTEMULT&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;

&lt;P&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt;COMPUTEMULT = ARG1 * ARG2 &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;

&lt;P&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;&lt;FONT color="#0000ff" face="Consolas" size="2"&gt;END FUNCTION&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Consolas" size="2"&gt;&lt;FONT face="Consolas" size="2"&gt; COMPUTEMULT&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;

&lt;P&gt;My VB code is:&lt;/P&gt;

&lt;P&gt;Public Declare Function ComputeMult Lib "c:\excel_test.dll" (A1 As Single, A2 As Single) As Single&lt;/P&gt;

&lt;P&gt;Sub junk()&lt;/P&gt;

&lt;P&gt;X = 4&lt;BR /&gt;
	Y = 5&lt;BR /&gt;
	MsgBox "X= " &amp;amp; X&lt;BR /&gt;
	MsgBox "Y= " &amp;amp; Y&lt;BR /&gt;
	Z = ComputeMult(X, Y)&lt;/P&gt;

&lt;P&gt;MsgBox "X*Y= " &amp;amp; Z&lt;/P&gt;

&lt;P&gt;End Sub&lt;/P&gt;

&lt;P&gt;When i run the VB proceedure I get teh error "Bad .dll calling convention"&lt;/P&gt;

&lt;P&gt;I also ran Dependency Walker on the excel_test.dll and recieved errors:&lt;/P&gt;

&lt;P&gt;&lt;B&gt;&lt;FONT color="#ff0000" size="1"&gt;&lt;FONT color="#ff0000" size="1"&gt;Error: At least one module has an unresolved import due to a missing export function in an implicitly dependent module.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;&lt;B&gt;&lt;FONT color="#ff0000" size="1"&gt;&lt;FONT color="#ff0000" size="1"&gt;Error: Modules with different CPU types were found.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/B&gt;&lt;/P&gt;

&lt;P&gt;There is a link to KERNEL32.dll, despite setting the FORTRAN&amp;gt;library&amp;gt;runtime &amp;nbsp;option to multithread.&lt;/P&gt;

&lt;P&gt;Any help is appricated.&lt;/P&gt;

&lt;P&gt;Andre&lt;/P&gt;</description>
      <pubDate>Wed, 05 Mar 2014 23:29:21 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938527#M89419</guid>
      <dc:creator>Andre_P_</dc:creator>
      <dc:date>2014-03-05T23:29:21Z</dc:date>
    </item>
    <item>
      <title>You need to set the calling</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938528#M89420</link>
      <description>&lt;P&gt;You need to set the calling convention of the Fortran procedure to stdcall.&amp;nbsp; Add&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;
!DEC$ ATTRIBUTES STDCALL :: COMPUTEMULT&lt;/PRE&gt;

&lt;P&gt;to the body of the Fortran procedure.&lt;/P&gt;

&lt;P&gt;Dependency walker gets confused when resolving DLL's for a 32 bit DLL on a 64 bit system.&amp;nbsp; The error messages you quote are typical of that situation.&lt;/P&gt;

&lt;P&gt;KERNEL32 is a basic system DLL.&amp;nbsp; If you DLL interacts with the system in any way, it needs to (perhaps indirectly) reference this DLL.&lt;/P&gt;</description>
      <pubDate>Thu, 06 Mar 2014 00:12:52 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938528#M89420</guid>
      <dc:creator>IanH</dc:creator>
      <dc:date>2014-03-06T00:12:52Z</dc:date>
    </item>
    <item>
      <title>I usually call variables in</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938529#M89421</link>
      <description>&lt;P&gt;I usually call variables&amp;nbsp;in Fortran dll by reference;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;Public Declare Function ComputeMult Lib "c:\excel_test.dll" ( &lt;STRONG&gt;Byref &lt;/STRONG&gt;A1&amp;nbsp;As Single, &lt;STRONG&gt;Byref &lt;/STRONG&gt;A2&amp;nbsp;&amp;nbsp;As Single)&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Mar 2014 01:32:45 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938529#M89421</guid>
      <dc:creator>Hitoshi</dc:creator>
      <dc:date>2014-03-07T01:32:45Z</dc:date>
    </item>
    <item>
      <title>Hitoshi,</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938530#M89422</link>
      <description>&lt;DIV class="field-item even"&gt;
	&lt;P&gt;Hitoshi,&lt;/P&gt;

	&lt;P&gt;I think in VBA, ByRef is assumed as default, and is not required.&amp;nbsp; In VB, however, it is (just to make life more interesting).&lt;/P&gt;

	&lt;P&gt;To allow for the different versions of Excel, I need to now use declarations like this one.&amp;nbsp; The VBA7 section is for Excel 2010 - it needs the addition of the PtrSafe keyword.&lt;/P&gt;

	&lt;P&gt;#If VBA7 Then&lt;BR /&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Declare PtrSafe Sub WaterDensity_F Lib "AWAProps.dll" _&lt;BR /&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (TempC As Double, Value As Double, ByVal Units As String)&lt;BR /&gt;
		#Else&lt;BR /&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp; Private Declare Sub WaterDensity_F Lib "AWAProps.dll" _&lt;BR /&gt;
		&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; (TempC As Double, Value As Double, ByVal Units As String)&lt;BR /&gt;
		#End If&lt;/P&gt;

	&lt;P&gt;on the Fortran Side, I have:&lt;/P&gt;

	&lt;P&gt;Subroutine WaterDensity_F(TempC, Value, Units)&lt;/P&gt;

	&lt;P&gt;&lt;B&gt;!DEC$ ATTRIBUTES DLLEXPORT, STDCALL, ALIAS:'WaterDensity_F' :: WaterDensity_F&lt;/B&gt;&lt;/P&gt;

	&lt;P&gt;&lt;B&gt;!DEC$ ATTRIBUTES REFERENCE :: Units&lt;/B&gt;&lt;/P&gt;

	&lt;P&gt;&amp;nbsp;Hope this is helpful.&amp;nbsp; (note the apparent inconsistency in keywords&amp;nbsp;for passing back strings - on the VB side ByVal vs using the Reference attribute on the Fortran side)&lt;/P&gt;

	&lt;P&gt;David&lt;/P&gt;
&lt;/DIV&gt;</description>
      <pubDate>Fri, 07 Mar 2014 02:54:09 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938530#M89422</guid>
      <dc:creator>DavidWhite</dc:creator>
      <dc:date>2014-03-07T02:54:09Z</dc:date>
    </item>
    <item>
      <title>Thanks to everyone for the</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938531#M89423</link>
      <description>&lt;P&gt;Thanks to everyone for the help.&lt;/P&gt;

&lt;P&gt;I added STDCALL per IanH's advice.&amp;nbsp; The VBA code ran, but returned a 0 for any input.&amp;nbsp; I added REFERENCE as shown below and the code works properly.&lt;/P&gt;

&lt;PRE class="brush:fortran;"&gt;
1&amp;nbsp; !DEC$ ATTRIBUTES STDCALL, REFERENCE :: COMPUTEMULT&lt;/PRE&gt;

&lt;P&gt;Also, removing the REFERENCE command&amp;nbsp;from the FORTRAN code and adding Byref to the VBA caused&amp;nbsp;Excel to&amp;nbsp;return a 0 for any input.&lt;/P&gt;

&lt;P&gt;Refering to the FORTRAN compiler documentation&amp;nbsp; - REFERENCE specifies a dummy argument's memory location is to be passed instead of the argument's value.&lt;/P&gt;

&lt;P&gt;That said, when should REFERENCE be used and omited?&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;

&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Mar 2014 15:56:13 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Excel-Function-from-Fortran-dll/m-p/938531#M89423</guid>
      <dc:creator>Andre_P_</dc:creator>
      <dc:date>2014-03-07T15:56:13Z</dc:date>
    </item>
  </channel>
</rss>

