1

We compiled a C++ program as DLL and want to use it from VB6. The program has subroutines like

int __stdcall setup(int exposure_time, double shutter, double gain, int numImages) {
....
}

int __stdcall test() {
  return 8;
}

And the Def file is defined as:

LIBRARY
EXPORTS
   setup=setup
   test=test

And we are declaring them in VB6 like this:

Public Declare Function setup Lib "C:\MyDll.dll" () As Long

Public Declare Function test Lib "C:\MyDll.dll" () As Long

And trying to access then in a form:

Private Sub Form_Load()

     Debug.Print (test())

End Sub

But we are getting "File not found" in VB, when the execution hits to very first function call! The MyDll.dll program is in the declared location and it is not to be registered. What is missing to declare?

Hello Bathsheba,

I followed your suggestions but the VB program still could not find the dll.

Declarations in VB:

 Public Declare Function setup Lib "C:\Math\FlyCapture2\bin\PGLCTrigger.dll" ( _
     ByVal exposure_time As Long, _
     ByVal shutter As Double, _
     ByVal gain As Double, _
     ByVal numImages As Long) As Long

 Public Declare Function test Lib "C:\Math\FlyCapture2\bin\PGLCTrigger.dll" () As Long

Def File:

 LIBRARY
 EXPORTS
    setup=@1
    test=@2

C++ program:

 __declspec(dllexport) int __stdcall setup(int exposure_time, double shutter, double gain,  int numImages) {
 ....
}

 __declspec(dllexport) int __stdcall test() {
    return 8;
}

And the VB calling program:

 Private Sub Form_Load()

      setup 12, 24#, 1#, 10
      test

 End Sub

As soon as the execution hits the setup line in the program above, the "dll not found" error comes.

I defined the following in a .def file as suggested by Compile a DLL in C/C++, then call it from another program:

 //DLL Export-Import definitions
#ifdef BUILD_DLL
#define EXPORT __declspec(dllexport)
#else
#define EXPORT __declspec(dllimport)
#endif

So that I can reference the functions in the DLL as

EXPORT int __stdcall setup(int exposure_time, double shutter, double gain, int numImages)

But VS2010 produces an error message for the import.

So I am stuck. Any further help will be appreciated a lot. Thank you.

Community
  • 1
  • 1
user1046647
  • 369
  • 1
  • 6
  • 18

2 Answers2

1

Others have told you that you must declare the parameters for the function. If the DLL will not load, and you are sure it is there then it is likely missing a dependency. Debug this with Dependency Walker. Load up the executable and run it in profile mode from the Profile menu. This will log loader events and you'll see exactly the cause for failure.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • If I run PCTriggerDll.Dll under Dependency walker I get a bunch of API-MS-Win... errors, If I run Vb program executable, then I get MSVBVM60.dll not found error. But even if I place this file into the directory the executable is running it still cannot find the dll. So you might be right, possibly a dependent file is missing. But which one. – user1046647 Oct 02 '13 at 18:50
  • Don't worry about the static analysis errors with the DLL. It's runtime that counts. And it looks like you are missing some VB6 runtime files. – David Heffernan Oct 02 '13 at 18:52
  • I put some files in the executable of the VB calling program is running. It runs fine! But the program cannot find the DLL in the VB development environment! You are possible right. I need to hunt the necessary files. Thank you again. – user1046647 Oct 02 '13 at 19:10
  • David, by taking my attention to the right problem, you helped me to solve it. I found the dependent program and now it is working. Thank you very much. I will certainly vote and accept your answer. Sincerely, – user1046647 Oct 02 '13 at 19:28
  • Please also make sure you review your previous questions. Thanks! – David Heffernan Oct 02 '13 at 19:29
0

You need to tell VB6 about the function arguments for setup:

Public Declare Function setup Lib "C:\MyDll.dll" ( _
    ByVal exposure_time As Long, _
    ByVal shutter As Double, _
    ByVal gain As Double, _
    ByVal numImages A Long) As long

And your .def file, I think, is incorrect. I use

EXPORTS
   setup @1
   test @2

Where 1 and 2 are arbitrary but distinct positive integers called ordinals. A couple of remarks:

A Long in VB is an int in C++.

You can use __declspec(dllexport) and extern "C" {/*your function here*/} in place of the .def file.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Hello Bathsheba, Thank you for your help. I followed your suggestions but I am still getting DLL not found error. See the text in my question above for further code. Any more ideas? – user1046647 Oct 02 '13 at 18:24
  • Yes, do as David Heffernan says. And try putting MSVBVM60.dll in the same folder as PGLCTrigger.dll (or whatever you're calling it); just as a temporary measure until you figure out what's going on. – Bathsheba Oct 02 '13 at 19:21