473,413 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Why does my dll not work for VBA calling?

1
I have programed a DLL, it compiles fine from C++ express edition 2008. But I can not call it from VBA.
The following is the code, hope anyone can help me.

This is testdll2_for_vba.h
Expand|Select|Wrap|Line Numbers
  1.  
  2. #ifndef DLL3_H
  3. #define DLL3_H
  4.  
  5. #ifdef DLL3_EXPORTS
  6.     #define DLL3_API __declspec(dllexport)
  7. #else
  8.     #pragma message("automatic link to DLL3.LIB")
  9. //    #pragma comment(lib, "DLL3.lib")
  10.     #define DLL3_API __declspec(dllimport)
  11. #endif
  12.  
  13.  
  14.  
  15. // This class is exported from the Test.dll
  16. class DLL3_API CTest 
  17. {
  18. public:
  19.     CTest(void);
  20.     // TODO: add your methods here.
  21. };
  22.  
  23. extern DLL3_API int nTest(const int& m);
  24.  
  25.  
  26. double DLL3_API __stdcall  testplus (const double& a, const double& b);
  27.  
  28.  
  29. #endif //DLL3_H
  30.  
  31.  

This is dllmain.cpp
Expand|Select|Wrap|Line Numbers
  1. // dllmain.cpp : Defines the entry point for the DLL application.
  2. #include "stdafx.h"
  3. #define TEST_EXPORTS                     // <===  ADD THIS LINE
  4. //#include "testdll2.h"
  5. #include "testdll2_for_vba.h"
  6. BOOL APIENTRY DllMain( HMODULE hModule,
  7.                        DWORD  ul_reason_for_call,
  8.                        LPVOID lpReserved
  9.                      )
  10. {
  11.     switch (ul_reason_for_call)
  12.     {
  13.     case DLL_PROCESS_ATTACH:
  14.     case DLL_THREAD_ATTACH:
  15.     case DLL_THREAD_DETACH:
  16.     case DLL_PROCESS_DETACH:
  17.         break;
  18.     }
  19.     return TRUE;
  20. }
  21.  
  22.  

This is my source code:
Expand|Select|Wrap|Line Numbers
  1. // testdll2.cpp : Defines the exported functions for the DLL application.
  2. //
  3.  
  4. #include "stdafx.h"
  5.  
  6. int nTest(const int& m){
  7.     return m;
  8. }
  9.  
  10. double testplus (const double& a, const double& b){
  11.     return a+b;
  12. }
  13.  
  14.  
This is my VBA code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Declare Function testplus Lib _
  3.     "C:\Documents and Settings\Owner.SpiritBirdV1\My Documents\Visual Studio 2008\Projects\testdll2\Debug\testdll2.dll" (ByRef a As Double, ByRef b As Double) As Double
  4.  
  5.  
  6. Sub test()
  7.  
  8. End Sub
  9.  
  10. Function bs_test(s As Double, k As Double, r As Double, sigma As Double, time As Double) As Double
  11. 'bs_test = option_price_call_black_scholes(s, k, r, sigma, time)
  12. bs_test = testplus(s, k)
  13. End Function
  14.  
  15.  
Thank you very much.

I read some articles about create DLL from VC++, that requires .def to alias the exported symbols. But I failed to find where to create .def in c++ express edition 2008. what should I do to fix the problem?

Thanks again
Sep 19 '07 #1
1 2163
weaknessforcats
9,208 Expert Mod 8TB
The problem you have is that C++ has function overloading.

Since the same function name with different arguments is acceptable, the C++ compiler decorates (mangles) the function name to a unique entitity that reflects the arguments and their type. Please don't ask how this is done.

The effect is that

void MyFunction(int);

becomes

?YAXX2GHQ (I made this up).

What's in the dll is ?YAXX2GHQ and VBA is looking for MyFunction and it's never found.

Use a .DEF file. It's just a text file. Here is an example:
Expand|Select|Wrap|Line Numbers
  1. ;BEGIN ADLL.DEF FILE
  2. ;This DEF file is required becuase the argument to GetProcAddress()
  3. ;for the function is a C-string and it will never be equal to the
  4. ;C++ mangled name for the function
  5. ;This DEF file maps the mangled name to a name that can be used with GetProcAddress()
  6. ;Note also: Change project settings in Visual Studio to send the LINK this def file.
  7. ;Visual Studio.NET: Project Properties/Linker/Input/Module Definition File/...Path to the def file\Adll.def
  8. LIBRARY ADll 
  9. EXPORTS 
  10. ;Exported Name    C++ Mangled Name
  11. AreaOfSquare   =  ?AreaOfSquare@@YGHHH@Z
  12. DisplayFromDll =  ?DisplayFromDll@@YGXXZ
  13. ;END DEF FILE 
  14.  
The lines beginning with semi-colons are comments.

Note the comment about adding the path to your DEF file to your VC project.

Let me know what happens.
Sep 22 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

28
by: Peter Olcott | last post by:
I want to double check my understanding about how the .NET framework works. From what I understand every call to the .NET framework is ultimately translated into one of more API calls, is this...
51
by: Tony Sinclair | last post by:
I'm just learning C#. I'm writing a program (using Visual C# 2005 on WinXP) to combine several files into one (HKSplit is a popular freeware program that does this, but it requires all input and...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
8
by: mdh | last post by:
May I ask this. Given the declaration: int myf( int, int); and a function pointer: (*fp)=int myf(int, int); where I am initializing fp to point at myf....or trying to..
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
3
by: xmail123 | last post by:
Why does this code work? I am new to C# and have been studying this piece of code. It loops through an Adjacency Matrix table to populate a tree view. I have two questions about why this code...
45
by: Bob Altman | last post by:
This code (Visual Studio 2005) should generate a compilation warning to the effect that I'm accessing a shared member through an instance variable (the "color" variable): Private Sub X(ByVal...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.