e_livesay
asked on
How do I get Excel 2013 on Windows 7 to access C++ DLL functions that are successfully accessed by Excel 2010 on Windows XP.
I have written a C++ DLL using Microsoft Visual Studio 2010. The DLL contains a function that takes as parameters 2 doubles and returns their product as a double. Using Excel 2010 on Windows XP I am able to get access to this function using VBA but using Excel 2013 on Windows 7 I encounter an error. I have added the PtrSafe keyword in the declaration of the function in Excel 2013 but this has not resolved the problem.
Here is the C++ routine in the DLL that is being tested:
double __stdcall SimpleMultiply(double x, double y)
{
double result = x * y;
return result;
}
Here is the *.def file in the C++ project which controls the names of the exported routines:
LIBRARY "Calculate"
EXPORTS
CalculateEXL = Calculate
TestSettingDoublesEXL = InitializeDoubles
SimplyMultiplyTestEXL = SimpleMultiply
It should be noted that all three of the routines (CalculateEXL, TestSettingDoublesEXL and SimplyMultiplyTestEXL) are listed when I run dumpbin /exports myDLL.dll from a Command Prompt.
Here is what my VBA code looks like in Excel 2010 on Windows XP:
Option Explicit
Private Declare Function SimplyMultiplyTestEXL Lib "C:\myProject\Debug\myDLL. dll" (ByVal x As Double, ByVal y As Double) As Double
Sub testTheDLL()
Dim a As Double
Dim b As Double
Dim c As Double
a = 6.1
b = 2.5
c = SimplyMultiplyTestEXL(a, b)
End Sub
Here is what my VBA code looks like in Excel 2013 on Windows 7 (same as above with the PtrSafe keyword added):
Option Explicit
Private Declare PtrSafe Function SimplyMultiplyTestEXL Lib "C:\myProject\Debug\myDLL. dll" (ByVal x As Double, ByVal y As Double) As Double
Sub testTheDLL()
Dim a As Double
Dim b As Double
Dim c As Double
a = 6.1
b = 2.5
c = SimplyMultiplyTestEXL(a, b)
End Sub
When I run the code in Excel 2010 in the VBA debugger (Windows XP), c = 15.25, as expected. However, when I run the code in Excel 2013 in the VBA debugger (Windows 7) I get the error message:
Run-time error '48':
Error in loading DLL
Any thoughts on how to fix this? I've been looking around to see if this is a 32-bit vs 64-bit issue but various things on the web imply that all I need to do is add the PtrSafe keyword to resolve this issue (described succinctly here: http://stackoverflow.com/questions/5506912/how-should-i-make-my-vba-code-compatible-with-64-bit-windows).
Here is the C++ routine in the DLL that is being tested:
double __stdcall SimpleMultiply(double x, double y)
{
double result = x * y;
return result;
}
Here is the *.def file in the C++ project which controls the names of the exported routines:
LIBRARY "Calculate"
EXPORTS
CalculateEXL = Calculate
TestSettingDoublesEXL = InitializeDoubles
SimplyMultiplyTestEXL = SimpleMultiply
It should be noted that all three of the routines (CalculateEXL, TestSettingDoublesEXL and SimplyMultiplyTestEXL) are listed when I run dumpbin /exports myDLL.dll from a Command Prompt.
Here is what my VBA code looks like in Excel 2010 on Windows XP:
Option Explicit
Private Declare Function SimplyMultiplyTestEXL Lib "C:\myProject\Debug\myDLL.
Sub testTheDLL()
Dim a As Double
Dim b As Double
Dim c As Double
a = 6.1
b = 2.5
c = SimplyMultiplyTestEXL(a, b)
End Sub
Here is what my VBA code looks like in Excel 2013 on Windows 7 (same as above with the PtrSafe keyword added):
Option Explicit
Private Declare PtrSafe Function SimplyMultiplyTestEXL Lib "C:\myProject\Debug\myDLL.
Sub testTheDLL()
Dim a As Double
Dim b As Double
Dim c As Double
a = 6.1
b = 2.5
c = SimplyMultiplyTestEXL(a, b)
End Sub
When I run the code in Excel 2010 in the VBA debugger (Windows XP), c = 15.25, as expected. However, when I run the code in Excel 2013 in the VBA debugger (Windows 7) I get the error message:
Run-time error '48':
Error in loading DLL
Any thoughts on how to fix this? I've been looking around to see if this is a 32-bit vs 64-bit issue but various things on the web imply that all I need to do is add the PtrSafe keyword to resolve this issue (described succinctly here: http://stackoverflow.com/questions/5506912/how-should-i-make-my-vba-code-compatible-with-64-bit-windows).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
http://msdn.microsoft.com/en-us/library/9yb4317s.aspx