Reports on answers to my --- Question: How do you call a Visual C++ (DLL) function from Visual Basic
Quote:
>> you have to add a .def file to your C++-Project and write the following lines:
EXPORTS
fnMyDll
and your C++-declaration should look like
extern "C" int WINAPI fnMyDll(void)
{
return 770;
Quote:
}
That method works!
-----------------------------------------------------------------------
' EXPORTS
fnMyDll '
All the articles in my MSDN CD help (Ver 6.0) under the title "Export from a DLL " say that if you use "__declspec( dllexport )" (which was the original method that I tried), then you don't need to use a DEF file (with regards to exporting). For some reason out of all the things I did to get the DLL working, going against the documentation was not something that I tried, even though I had suspected that maybe that was the problem. Wasted hours!
-----------------------------------------------------------------------
* To the guy who suggested as follows:
Quote:
>> Read this article in the MSDN....Q106553
The article is an old one. It suggests declaring DLL functions as "__export CALLBACK " and provides an enormous DEF file. It did not try it.
-----------------------------------------------------------------------
Quote:
>> You need to use the StdCall parameter passing method.
That method did not work for me, for some reason.
-----------------------------------------------------------------------
* CONCLUSION
Now that I see that "__declspec( dllexport )" does in fact work if you supply a def file, then I will stick to that since it is basically the solution autogenerated by the MSVC DLL wizard. (If I had wanted the MFC version for DLLs then it would have autogenned also the DEF file and I would never have run into the problem).
Thanks everybody.
Avraham.
Need help with calling from within (Excel) VB a DLL function created within MSVC.
Even the simplest MSDN 'textbook' example does not seem to work, as follows:
I used the MSVC Development environment "Win32 Dynamic-Link Library" wizard (from File | New | Projects Tab) to create a sample DLL with sample variables and functions. I called it the Dll project MyDLL. The full declaration of the sample function that I want to call from within VB is:
MYDLL_API int fnMyDll(void)
{
return 770;
}
where MYDLL_API is declared in the .h file as:
#define MYDLL_API __declspec(dllexport)
I want to call the int fnMyDll(void) function from within an Excel Visual Basic program.
To find out how to "declare" a "link" to a DLL from a Visual Basic program, I copied the style of the MSDN (CD ROM Version 6) help page entitled "Using a DLL Procedure in Your Application", and wrote a simple VB test bed as follows:
Private Declare Function fnMyDll Lib "D:\MyDll\Debug\MyDll.Dll" () As Long
'Excel Test Bed For Dll
Sub TestBed()
Dim FnRetVal As Long
FnRetVal = fnMyDll()
End Sub
When stepping through the above VB code piece, it seems to recognize the dll MyDll and the function fnMyDll( ), however FnRetVal does not receive the return value 770.
I also tried running the code piece within regular VB (not VBA for any Office app) but this time not only does it not work but I get the following error msg:
'Run-time error '453':
Can't find DLL entry point fnMyDll in D:\MyDll\Debug\MyDll.Dll( )'
Clicking on Help causes the follwoing MSDN page to appear:
Specified DLL function not found (Error 453)
The dynamic-link library (DLL) in a user library reference was found, but the DLL function specified wasn't found within the DLL. This error has the following causes and solutions:
You specified an invalid ordinal in the function declaration.
Check for the proper ordinal or call the function by name.
You gave the right DLL name, but it isn't the version that contains the specified function.
You may have the correct version on your machine, but if the directory containing the wrong version precedes the directory containing the correct one in your path, the wrong DLL is accessed. Check your machine for different versions. If you have an early version, contact the supplier for a later version.
If you are working on a 32-bit Microsoft Windows platform, both the DLL name and alias (if used) must be correct.
Make sure the DLL name and alias are correct.
Some 32-bit DLLs contain functions with slightly different versions to accommodate both Unicode and ANSI strings. An "A" at the end of the function name specifies the ANSI version. A "W" at the end of the function name specifies the Unicode version.
If the function takes string-type arguments, try appending an "A" to the function name.
For additional information, select the item in question and press F1.
None of the points mentioned seem to be the problem in my case.
Anybody out there who knows enough (VC and) VB programming to be able to tell me what's wrong?
Avraham.
Shevach
Need help with calling from within (Excel) VB a DLL function created within MSVC.
Even the simplest MSDN 'textbook' example does not seem to work, as follows:
I used the MSVC Development environment "Win32 Dynamic-Link Library" wizard (from File | New | Projects Tab) to create a sample DLL with sample variables and functions. I called it the Dll project MyDLL. The full declaration of the sample function that I want to call from within VB is:
MYDLL_API int fnMyDll(void)
{
return 770;
}
where MYDLL_API is declared in the .h file as:
#define MYDLL_API __declspec(dllexport)
I want to call the int fnMyDll(void) function from within an Excel Visual Basic program.
To find out how to "declare" a "link" to a DLL from a Visual Basic program, I copied the style of the MSDN (CD ROM Version 6) help page entitled "Using a DLL Procedure in Your Application", and wrote a simple VB test bed as follows:
Private Declare Function fnMyDll Lib "D:\MyDll\Debug\MyDll.Dll" () As Long
'Excel Test Bed For Dll
Sub TestBed()
Dim FnRetVal As Long
FnRetVal = fnMyDll()
End Sub
When stepping through the above VB code piece, it seems to recognize the dll MyDll and the function fnMyDll( ), however FnRetVal does not receive the return value 770.
I also tried running the code piece within regular VB (not VBA for any Office app) but this time not only does it not work but I get the following error msg:
'Run-time error '453':
Can't find DLL entry point fnMyDll in D:\MyDll\Debug\MyDll.Dll( )'
Clicking on Help causes the follwoing MSDN page to appear:
Specified DLL function not found (Error 453)
The dynamic-link library (DLL) in a user library reference was found, but the DLL function specified wasn't found within the DLL. This error has the following causes and solutions:
You specified an invalid ordinal in the function declaration.
Check for the proper ordinal or call the function by name.
You gave the right DLL name, but it isn't the version that contains the specified function.
You may have the correct version on your machine, but if the directory containing the wrong version precedes the directory containing the correct one in your path, the wrong DLL is accessed. Check your machine for different versions. If you have an early version, contact the supplier for a later version.
If you are working on a 32-bit Microsoft Windows platform, both the DLL name and alias (if used) must be correct.
Make sure the DLL name and alias are correct.
Some 32-bit DLLs contain functions with slightly different versions to accommodate both Unicode and ANSI strings. An "A" at the end of the function name specifies the ANSI version. A "W" at the end of the function name specifies the Unicode version.
If the function takes string-type arguments, try appending an "A" to the function name.
For additional information, select the item in question and press F1.
None of the points mentioned seem to be the problem in my case.
Anybody out there who knows enough (VC and) VB programming to be able to tell me what's wrong?
Avraham.