Creating C DLLs
These are the steps to create a DLL that can be loaded and called from WinRunner.
1. Create a new Win32 Dynamic Link Library project, name it, and click
2. On Step 1 of 1, select “An empty DLL project,” and click
3. Click
4. Select File ® New from the VC++ IDE.
5. Select “C++ Source File,” name it, and click
6. Close the newly created C++ source file window.
7. In Windows Explorer, navigate to the project directory and locate the .cpp file you created.
8. Rename the .cpp file to a .c file
9. Back in the VC++ IDE, select the FileView tab and expand the tree under the Projects Files node.
10. Select the Source Files folder in the tree and select the .cpp file you created.
11. Press the Delete key; this will remove that file from the project.
12. Select Project ® Add To Project ® Files from the VC++ IDE menu.
13. Navigate to the project directory if you are not already there, and select the .c file that you renamed above.
14. Select the .c file and click
15. Double-click on the .c file to open it.
16. Create your functions in the following format:
#include “include1.h”
#include “include2.h”
.
.
.
#include “includen.h”
#define EXPORTED __declspec(dllexport)
…,
{
return
}
.
.
.
…,
{
return
}
17. Choose Build ®
18. Fix any errors and repeat step 17.
19. Once the DLL has compiled successfully, the DLL will be built in either a Debug directory or a Release directory under your project folder depending on your settings when you built the DLL.
20. To change this setting, select Build ® Set Active Configuration from the VC++ IDE menu, and select the Configuration you want from the dialog. Click
21. All the DLLs types that you are going to create are loaded and called in the same way in WinRunner. This process will be covered once in a later section.
Creating C++ DLLs
Here are the steps for creating a C++ DLL:
1. Create a new Win32 Dynamic Link Library project, name it, and click
2. On Step 1 of 1, select “An Empty DLL Project,” and click
3. Click
4. Select File ® New from the VC++ IDE.
5. Select C++ Source File, name it, and click
6. Double-click on the .cpp file to open it.
7. Create your functions in the following format:
#include “include1.h”
#include “include2.h”
.
.
.
#include “includen.h”
#define EXPORTED extern “C” __declspec(dllexport)
EXPORTED
…,
{
return
}
.
.
.
EXPORTED
…,
{
return
}
8. Choose Build ®
9. Fix any errors and repeat step 8.
10. Once the DLL has compiled successfully, the DLL will be built in either a Debug directory or a Release directory under your project folder depending on your settings when you built the DLL.
11. To change this setting, select Build ® Set Active Configuration from the VC++ IDE menu, and select the Configuration you want from the dialog. Click
12. All the DLLs types that you are going to create are loaded and called in the same way in WinRunner. This process will be covered once in a later section.
Creating MFC DLLs
1. Create a new MFC AppWizard(DLL) project, name it, and click
2. In the MFC AppWizard Step 1 of 1, accept the default settings and click
3. Click
4. Select the ClassView tab in the ProjectView and expand the classes tree. You will see a class that has the following name C
5. You should see the constructor function C
6. This should open the .cpp file for the project. At the very end of this file add the following definition:
#define EXPORTED extern "C" __declspec( dllexport )
7. Below you will add your functions in the following format:
#define EXPORTED extern “C” __declspec(dllexport)
EXPORTED
…,
{
return
}
.
.
.
EXPORTED
…,
{
return
}
8. You will see the functions appear under the Globals folder in the ClassView tab in the ProjectView.
9. Choose Build ®
10. Fix any errors and repeat step 9.
11. Once the DLL has compiled successfully, the DLL will be built in either a Debug directory or a Release directory under your project folder depending on your settings when you built the DLL.
12. To change this setting, select Build ® Set Active Configuration from the VC++ IDE menu, and select the Configuration you want from the dialog. Click
13. All the DLLs types that you are going to create are loaded and called in the same way in WinRunner. This process will be covered once in a later section.
Creating MFC Dialog DLLs
1. Create a new MFC AppWizard(DLL) project, name it, and click
2. In the MFC AppWizard Step 1 of 1, accept the default settings and click
3. Click
4. Select the ClassView tab in the ProjectView and expand the classes tree. You will see a class that has the following name C
5. You should see the constructor function C
6. This should open the .cpp file for the project. At the very end of this file add the following definition:
#define EXPORTED extern "C" __declspec( dllexport )
7. Switch to the ResourceView tab in the ProjectView.
8. Select Insert ® Resource from the VC++ IDE menu.
9. Select Dialog from the Insert Resource dialog and click
10. The Resource Editor will open, showing you the new dialog. Add the controls you want to the dialog, and set the properties of the controls you added.
11. Switch to the ClassView tab in the ProjectView and select View ® ClassWizard from the VC++ IDE menu, or double-click on the dialog you are creating.
12. The Class Wizard should appear with an “Adding a Class” dialog in front of it. Select “Create a New Class” and click
13. In the New Class dialog that comes up, give your new class a name and click
14. In the Class Wizard, change to the Member Variables tab and create new variables for the controls you want to pass information to and from. Do this by selecting the control, clicking
15. Switch to the Message Maps tab in the Class Wizard. Select the dialog class from the Object IDs list, then select the WM_PAINT message from the Messages List. Click
16. Add the following lines to the OnPaint function so it looks like the following:
void
{
CPaintDC dc(this); // device context for painting
this->BringWindowToTop();
UpdateData(FALSE);
// Do not call CDialog::OnPaint() for painting messages
}
17. Select IDOK from the Object IDs list, then select the BN_CLICKED message from the Messages
list. Click
18. Add the line UpdateData(TRUE); to the function, so it looks like this:
void
{
UpdateData(TRUE);
CDialog::OnOK();
}
19. When you are done with this, click
20. In the tree on the ClassView tab, double-click on the constructor function for the C
21. At the top of the file, along with the other includes, add an include statement to include the header file for your dialog class. It should be the same name as the name you gave the class in step 13 with a .h appended to it. If you are unsure of the name, you can look it up on the FileView tab under the Header Files folder.
22. At the very end of the file, after the #define you created in step 6, create a function that looks something like this:
EXPORTED int create_dialog(char* thestring)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
theDlg.
theDlg.DoModal();
strcpy(thestring,strVar1); //this will pass the value back to WinRunner.
return 0;
}
23. Choose Build ®
24. Fix any errors and repeat step 23.
25. Once the DLL has compiled successfully, the DLL will be built in either a Debug directory or a Release directory under your project folder depending on your settings when you built the DLL.
26. To change this setting, select Build ® Set Active Configuration from the VC++ IDE menu, then select the Configuration you want from the dialog. Click
27. All the DLLs types that you are going to create are loaded and called in the same way in WinRunner. This process will be covered once in a later section.
Loading and Calling the Above DLLs from WinRunner
Loading and calling DLLs from WinRunner is really very simple. There are only 3 steps.
1. Load the DLL using the command load_dll.
2. Declare the function in the DLL as an external function using the extern function.
3. Call the function as you would any other TSL function.
As simple as this is, there are some things you need to be aware of.
1. WinRunner has a limited number of variable types; basically, there is string, int, and long. Windows has many different types. Two common types, which may confuse you, are HWND and DWORD. Which WinRunner type do you choose for these? You should declare these as long.
2. If you are building a function in a DLL and you are testing it in WinRunner, make sure you unload the DLL in WinRunner using the unload_dll function before you try to recompile the DLL. If you leave the DLL loaded in WinRunner and try to recompile the DLL, you will receive an error message in VC++ that looks like this:
LINK : fatal error LNK1104: cannot open file “Debug/
Error executing link.exe
To resolve this error, step through the unload_dll line in WinRunner, then compile the DLL.
3. Before shipping a DLL make sure you compile it in Release mode. This will make the DLL much smaller and optimized.
No comments:
Post a Comment