2

I am using C++ to create a simple DLL that I can use from VBA code. However, while it works on my development computer, when attempting to access the DLL on different computers, VBA states the DLL File was not found, despite hardcoding the path in.

My DLL looks like this (created with Visual C++ 2010 Express as a Win32 dll project):

DEF file:

LIBRARY "squareNumber"
EXPORTS
squareNumber

Function.cpp:

double __stdcall squareNumber(double & x)
{
    return x*x;
}

The VBA code looks like this:

Public Declare Function squareNumber Lib "C:\MySimpleDLL.dll" (ByRef number As Double) As Double

Sub test()
    MsgBox squareNumber(2)
End Sub

I am very new to C++ DLLs, did I code the dll wrong, or is it VBA's problem?

Using RegSvr32 also yields "The module [dllpath] failed to load. Make sure the binary is stored at the specified path or debug it to check for problems with the binary or dependent .DLL files."

James
  • 2,445
  • 2
  • 25
  • 35
  • 2
    Project + Properties, C/C++, Code generation. Change Runtime Library to /MT. This avoid trouble when you forget to install the CRT on the target machine or use the Debug build instead of the Release build. – Hans Passant Dec 22 '11 at 17:08

3 Answers3

6

It sounds like your DLL has dependancies on other DLLs that exist on your development machine but not on the target machine. When the OS loads your DLL but then can't find the dependent DLLs, it reports the same "cannot find file" error, making you think it cannot find your DLL when it really did. If that is the case, then you need to either distribute those extra DLLs or remove the dependencies on them.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
1

In order to obtain info about other dependencies that your dll require you can try Dependency Walker free software.

myszon
  • 11
  • 1
1

I just spent days tracking down and solving this problem.

In my case, the problem was that my DLL was being dynamically linked with the Multi-threaded runtime library DLL.

The fix was to load my project in VS, edit the project's Properties, and under "Configuration Properties>C/C++>Code Generation" change "Runtime Library" from "Multi-threaded DLL (/MD)" to "Multi-threaded (/MT)".

Problem solved, for me anyway. Hope this helps someone else.

jkhoffman
  • 199
  • 2
  • 11