<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[Approximatrix Forums — Help needed with VBA dll function call]]></title>
	<link rel="self" href="http://forums.approximatrix.com/extern.php?action=feed&amp;tid=903&amp;type=atom" />
	<updated>2023-07-02T19:56:02Z</updated>
	<generator>PunBB</generator>
	<id>http://forums.approximatrix.com/viewtopic.php?id=903</id>
		<entry>
			<title type="html"><![CDATA[Re: Help needed with VBA dll function call]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=4138#p4138" />
			<content type="html"><![CDATA[<p>Thanks!!! I made the recommended changes and all works correctly now. </p><p>Much appreciated.</p>]]></content>
			<author>
				<name><![CDATA[mdmfea]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=4136</uri>
			</author>
			<updated>2023-07-02T19:56:02Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=4138#p4138</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Help needed with VBA dll function call]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=4137#p4137" />
			<content type="html"><![CDATA[<p>There are a few problems in the code.&nbsp; First, despite using <em>intent(in)</em>, Fortran is still accepting the arguments by reference.&nbsp; You would actually need to use the attribute <em>value</em> instead that is only permissible when the function is <em>bind(c)</em>.&nbsp; However, I don&#039;t think you should change the Fortran.&nbsp; Instead, change the VBA to:</p><div class="codebox"><pre><code>Private Declare PtrSafe Function alpha Lib &quot;C:\PathToDLL\thexp.dll&quot; (MTCODE As Integer, TEMP As Double) As Single</code></pre></div><p>All I did was remove the <em>ByVal</em> argument modifiers.</p><p>The other problem that took me a few minutes to realize was that you&#039;ve declared the Fortran function to return single-precision (<em>c_float</em>), but your original declaration line was declaring it as <em>Double</em>.&nbsp; I changed it to <em>Single</em> in my declaration above, and I was able to get an answer.</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2023-07-02T19:45:53Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=4137#p4137</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Help needed with VBA dll function call]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=4136#p4136" />
			<content type="html"><![CDATA[<p>Thanks for the reply. </p><p>Yes, it is compiling as 64 bit. </p><p>When I open the dll file it is a long list of items but I am not sure what they all are. </p><p>I checked the .def file and its contents are shown below. </p><p>EXPORTS<br />&nbsp; &nbsp; thexp @1</p><p>I tried using alpha in the VBA declaration statement but still end up with #Value.</p>]]></content>
			<author>
				<name><![CDATA[mdmfea]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=4136</uri>
			</author>
			<updated>2023-07-02T14:26:17Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=4136#p4136</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: Help needed with VBA dll function call]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=4135#p4135" />
			<content type="html"><![CDATA[<p>I would check if the DLL is actually exporting the ALPHA function.&nbsp; You should be able to check by using File-&gt;Open&quot; in Simply Fortran and selecting the DLL itself.&nbsp; The tab that opens should provide a list of all symbols in the DLL.&nbsp; </p><p>My concern is that the function was actually exported as <strong>alpha</strong> in lowercase.&nbsp; Also, did you make sure that the Simply Fortran project is compiling as 64-bit?</p>]]></content>
			<author>
				<name><![CDATA[jeff]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=2</uri>
			</author>
			<updated>2023-07-02T14:13:00Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=4135#p4135</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Help needed with VBA dll function call]]></title>
			<link rel="alternate" href="http://forums.approximatrix.com/viewtopic.php?pid=4134#p4134" />
			<content type="html"><![CDATA[<p>Hello all.</p><p>I keep getting the Excel #Value error when I am trying to use a pretty simple function. The goal is to compile this older FORTRAN 77 code in a dll that can be used in VBA. I believe the issue is with the type definitions. </p><p>I am using a 64 bit version of Excel on a Windows 11 Pro 64 bit machine. </p><p>The DLL compiles without issue. </p><p>I followed the example provided on the forum that creates the three statistical functions and that all works correctly. I followed this example and changed the required input types to an INTEGER and a DOUBLE. </p><p>The full code from FOTRAN 77 is listed below inside the function. The MTCODE is an integer value and the TEMP is a double value. ALPHA is a double value that is returned from the function. Basically, pass an MTCODE and a TEMP and the function performs a simple calculation to obtain the ALPHA value. </p><p>c&nbsp; &nbsp; &nbsp;Computes the thermal expansion coefficient<br />c&nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; function ALPHA(MTCODE, TEMP) bind(c)<br />&nbsp; &nbsp; &nbsp; use ISO_C_BINDING<br />&nbsp; &nbsp; &nbsp; implicit none<br />&nbsp; &nbsp; &nbsp; <br />cGCC$ ATTRIBUTES DLLEXPORT :: ALPHA<br />&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; real(kind=c_float)::ALPHA<br />&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; integer(kind=c_int),intent(in)::MTCODE<br />&nbsp; &nbsp; &nbsp; real(kind=c_double),intent(in)::TEMP<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; IF (MTCODE .NE. 1) GO TO 100<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ALPHA=(-0.0000006609*(TEMP**2))+(0.0026933877*TEMP)+ <br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; &nbsp;5.88847007751<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GO TO 1000<br />100&nbsp; &nbsp;IF (MTCODE .NE. 2) GO TO 200<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ALPHA=(-0.0000005112*(TEMP**2))+(0.0021199360*TEMP)+ <br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; &nbsp;5.5841093063<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GO TO 1000<br />200&nbsp; &nbsp;IF (MTCODE .NE. 3) GO TO 300<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ALPHA=(-0.0000001792*(TEMP**2))+(0.0014422280*TEMP)+ <br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; &nbsp;9.0099201202<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GO TO 1000<br />300&nbsp; &nbsp;IF (MTCODE .NE. 4) GO TO 400<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ALPHA=(-0.0000005258*(TEMP**2))+(0.0020210845*TEMP)+ <br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; &nbsp;5.1010999680<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GO TO 1000<br />400&nbsp; &nbsp;IF (MTCODE .NE. 5) GO TO 500<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IF (TEMP .LT. 300) ALPHA=(0.0000005694*(TEMP**2))+<br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; (-0.0002694445*TEMP)+8.8211059570<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IF (TEMP .GE. 300 .AND. TEMP .LT. 875) ALPHA=(0.0000005046*<br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; (TEMP**2))+(-0.0002305726*TEMP)+8.8937530518<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IF (TEMP .GE. 875 .AND. TEMP .LT. 975) ALPHA=(0.0000008251*<br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; (TEMP**2))+(-0.0007913236*TEMP)+9.0507049561<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IF (TEMP .GE. 975) ALPHA=(0.0000003460*(TEMP**2))+<br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; (0.0001429432*TEMP)+8.6817054749<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GO TO 1000<br />500&nbsp; &nbsp;IF (MTCODE .NE. 6) GO TO 600<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IF (TEMP .LT. 400) ALPHA=(0.0000000003*(TEMP**2))+<br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; (0.0045000138*TEMP)+7.0000047684<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IF (TEMP .GE. 400) ALPHA=(0.0000002479*(TEMP**2))+<br />&nbsp; &nbsp; &nbsp;Z&nbsp; &nbsp; &nbsp; (0.0006652861*TEMP)+8.4942169189<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;GO TO 1000<br />600&nbsp; &nbsp;IF (TEMP .LT. 400) ALPHA=(-0.0000014229*(TEMP**2))+<br />&nbsp; &nbsp; &nbsp;Z&nbsp; (0.0023960555*TEMP)+6.9692468643<br />&nbsp; &nbsp; &nbsp; IF (TEMP .GE. 400 .AND. TEMP .LT. 800) ALPHA=(0.0010000509*TEMP)+<br />&nbsp; &nbsp; &nbsp;Z&nbsp; 7.2999811172<br />&nbsp; &nbsp; &nbsp; IF (TEMP .GE. 800) ALPHA=(-0.0000000833*(TEMP**2))+<br />&nbsp; &nbsp; &nbsp;Z&nbsp; (0.0014166683*TEMP)+7.0200033188<br />1000&nbsp; RETURN<br />&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; end function ALPHA</p><br /><p>Here is the calling statement in VBA. Please note that I removed the actual path to the dll file.</p><br /><p>Option Explicit</p><br /><p>Private Declare PtrSafe Function ALPHA Lib &quot;path to dll \thexp.dll&quot; (ByVal MTCODE As Integer, ByVal TEMP As Double) As Double<br />&nbsp; &nbsp; </p><p>Function wmALPHA(MTCODE, TEMP) As Double</p><br /><p>&nbsp; &nbsp; wmALPHA = ALPHA(MTCODE, TEMP)</p><br /><p>End Function</p><p>Any help would be greatly appreciated. Thanks!</p>]]></content>
			<author>
				<name><![CDATA[mdmfea]]></name>
				<uri>http://forums.approximatrix.com/profile.php?id=4136</uri>
			</author>
			<updated>2023-07-02T11:07:05Z</updated>
			<id>http://forums.approximatrix.com/viewtopic.php?pid=4134#p4134</id>
		</entry>
</feed>
