Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

Excel add in development in C++ Aplications in finance - Dalton S.

Dalton S. Excel add in development in C++ Aplications in finance - Wiley publishing , 2005. - 425 p.
ISBN 0-470-02469-0
Download (direct link): exceladdindevelopmentincand2005.pdf
Previous << 1 .. 38 39 40 41 42 43 < 44 > 45 46 47 48 49 50 .. 168 >> Next

Configuration: | Act мэ (Detug) Elatfbrm: |Active(Win32)
*J
Configuration Manager...
Ci Configuration Prcpertie General y Debuggrg ^ C/C++
□ Linker
LJ Browse Information _J Build Events £3 Custom Build Step £3 Web Deployment
|s Aft inn
1 Command С :\program Files VHicrosoft OfFice\pfnce'£XCEL.EXE| -
Command Arguments
Working Directory
Attach No
Symbol Palh
В Debuggers
Debugger Type Auto
B Remote Settings
Connection Local
Remote Machine
Remote Command
HTTP URL

Command
The debug command when usng the Local Ccrmecton.
Cancel I Apply I Help
As you can see, these are the settings for the debug configuration. The full path and filename for Excel has been entered as the debug executable. Now, if you select Debug/Start, or press {F5}, VC.NET will run Excel. If your project needs rebuilding because of changes you’ve made to source code, VC.NET will ask you if you want to rebuild first.
92
Excel Add-in Development in C/C++
So far all we’ve done is created a DLL project, written and exported a function and set up the debugger to run Excel. Now we need to create something that accesses the function. Later chapters describe how to use Excel’s add-in manager and Paste Function wizard, but for now we’ll just create a simple spreadsheet which calls our function from a VB module.
To follow the steps in the next section, you need to run Excel from VC.NET by debugging the DLL. (Select Build/Start Debug.. ./Go or press {F5}.) This enables you to experiment by setting breakpoints in the DLL code.
You can also specify a spreadsheet that Excel is to load whenever you start a debug session. This example shows the name and location of a test spreadsheet called Get-TimeTest.xls entered into the Command Arguments field. (Excel interprets a command line argument as an autoload spreadsheet.)
Next time Debug/Start is selected, or {F5} is pressed, VC.NET will run Excel and load this test spreadsheet automatically. This is a great time-saver and helps anyone who might take over this project to see how the DLL was supposed to work.
4.11 ACCESSING DLL FUNCTIONS FROM VB
VB provides a way of making DLL exports available in a VB module using the Declare statement. (See section 3.6 Using VBA as an interface to external DLL add-ins on page 48 for a detailed description.) In the case of the example in our add-in the declaration in our VB module would be:
Declare Function get_system_time_C Lib "GetTime.dll" (ByVal trigger As Long) As Double
(Note the use of the line continuation character ‘_’.)
Creating a 32-bit Windows (Win32) DLL Using VC 6.0 or VS. NET
93
As described in Chapter 3 Using VBA on page 41, if you open a new VB module in GetTimeTest.xls and add the following code to it, you will have added two user-defined functions to Excel, Get_C_System_Time() and Get_VB_Time().
Declare Function get_system_time_C Lib "GetTime.dll" _ (ByVal trigger As Long) As Double
Function Get_C_System_Time(trigger As Double) As Double
Get_C_System_Time = get_system_time_C(0)
End Function
Function Get_VB_Time(trigger As Double) As Double Get_VB_Time = Now End Function
(Note that the full path of the DLL is, in general, required in the VB Declare statements.) Back in Excel, the following simple spreadsheet has been created:
I £4 Microsoft Excel - GetTimeTest.xls -JQJ.XI
B] File Edit Ylew insert Format loots ßata ffindow Help -|g|x|
D а? И Ш Eà «n • г» • 1 £ U £111 ioo% -fin * " J10 - “
А13 -| > I С run-time function
A I В С D :
1 GetTime.dll Test Spreadsheet 1
2
3
4 Excel NOW function 15:07:00 93
5 VB Now function 15:07:00.00
6 C run-time function 15:07:00.00
7
8
9
IC : ■ >ІГ
I к < ► Mfysheetl/ M
Cell Formula
B4 =NOW()
B5 =Get_VB_Time(B4)
B6 =Get_C_System_Time(B4)
Here, cell B4 will recalculate whenever you force a recalculation by pressing {F9}, or when Excel would normally recalculate, say, if some other cell’s value changes. (The
94
Excel Add-in Development in C/C++
Now() function is volatile and is re-evaluated whenever Excel recalculates despite not depending on anything on the sheet.) The fact that B4 is a precedent for B5 and B6 triggers Excel to then re-evaluate these cells too. (See section 2.11.2 Triggering functions to be called by Excel - The Trigger Argument on page 26.)
Pressing {F9} will therefore force all three cells to recalculate and you will see that the C run-time functions and the VB Now function are in synch. You should also see that the NOW() function is also in synch but goes one better by showing 100 ths of a second increments. (This is discussed more in Chapter 9 where the relative execution speeds of VB and C/C++ are timed and compared.)
4.12 ACCESSING DLL FUNCTIONS FROM EXCEL
In order to access DLL functions directly from Excel, as either worksheet functions or commands, without the need for a VBA wrapper to the functions, you need to provide an interface - a set of functions - that Excel looks for when using the Add-in Manager to load the DLL. This is covered in detail in Chapter 5 Turning DLLs into XLLs: The Add-in Manager Interface as well as subsequent sections. The interface functions are intended to be used to provide Excel with information it needs about the DLL functions you are exporting so that it can integrate them - a process know as registration, covered in detail in section 8.5 Registering and un-registering DLL (XLL) functions on page 182.
Previous << 1 .. 38 39 40 41 42 43 < 44 > 45 46 47 48 49 50 .. 168 >> Next