Reputation: 399
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.
Upvotes: 1
Views: 1164
Reputation: 612954
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.
Upvotes: 1
Reputation: 234705
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.
Upvotes: 0