| Hyderabad Jobs | Book Website | ![]() |
I Love Hyderabad | Hyderabad Colleges |
| Home | Business Emails | Hyderabad Classifieds | Contact Us | |
| 7 Wonders of Hyderabad | Web Hosting | Yellow Pages | Our Network | |
Advanced PowerBuilder
Creating C++ ClassesCreating a C++ class user object using PowerBuilder involves the following steps:
Let's start with a simple example of creating a C++ DLL that returns the factorial value of a given number. Click the User Object painter button and select New. Then select the C++ User Object icon and provide a DLL name (for example, pb_fact1.dll). This is the DLL that PowerBuilder will create once you save the user object.
The painter is no different from the standard user object painter, except that there is one more icon on the Painter Bar-to invoke the Watcom editor. Declare the function as follows by selecting Declare/UserObject Functions... from the menu.
When you click the
s CommandButton, control returns to the User Object painter.
We're going to write the actual logic in C++, so save the user
object as cuo_factorial and then click on the C++ Editor
button
Interface FilesWhen you create a C++ user object, PowerBuilder creates various templates which you can use to type in and compile your C++ code. It generates the following files for you. .hpp files .cpp files .hpp filesThe .hpp files are header files which contain functions and data member definitions. The extension is .h in C and .hpp in C++. To use functions declared in these files, you refer to them in .cpp files with the #include directive. PowerBuilder automatically generates this file by declaring the functions you declared in the User Object painter. The code is as follows:
.cpp FilesThese are the actual source code and are similar to .C extension files in C programs. In C++, the source file extension is .cpp. PowerBuilder generates three files for us: Lmain.cpp, which contains the LibMain and WEP functions that are required for the DLL. A file that contains the PowerBuilder interface. PowerBuilder doesn't make calls to the functions directly, but uses this file instead. This file name is typically prefixed with c. Don't change anything in this file. The code is as follows:
/* WATCOM Interface Generator Version 1.0 */ /* This file is generated by PowerBuilder. * Do not modify this file. * This file contains interface code called by PowerBuilder. */ #include <pbdll.h> #include "cuo_faCD.hpp" extern "C" {
long PB_EXPORT cuo_factorialuf_factorial(
cuo_factorial *this_hdl, long factorial_no );
cuo_factorial *PB_EXPORT cuo_factorial_CPP_CONSTRUCTOR();
void PB_EXPORT cuo_factorial_CPP_DESTRUCTOR( cuo_factorial *this_hdl );
}
long PB_EXPORT cuo_factorialuf_factorial(
cuo_factorial *this_hdl, long factorial_no )
{return( this_hdl->uf_factorial( factorial_no ) );}
cuo_factorial *PB_EXPORT cuo_factorial_CPP_CONSTRUCTOR()
{ return( new cuo_factorial );}
void PB_EXPORT cuo_factorial_CPP_DESTRUCTOR(
cuo_factorial *this_hdl ) { delete this_hdl;}
The third file, in our case cuo_facd.cpp, is where you write the actual logic for the functions declared in the User Object painter. The code generated by PowerBuilder looks like this: /* WATCOM Interface Generator Version 1.0 */ /* This file contains code generated by PowerBuilder. * Do not modify code delimited by comments of the form: * // $PB$ -- begin generated code for object <>. Do not modify this code * // $PB$ -- end generated code for object <>. * This file contains the bodies the functions for your user object. */ #include <pbdll.h> #include "cuo_faCD.hpp" // $PB$ -- begin generated code for object <cuo_factorial>.
// Do not modify this code
#if 1
long cuo_factorial::uf_factorial( long factorial_no ) {
// $PB$ -- end generated code for object <cuo_factorial>.
//==================================
/*
* PUT YOUR CODE HERE
*/
return( 0 ); } #endif // PowerBuilder code, do not remove Don't remove any lines generated by PowerBuilder other than replacing the 'Put your code here' comments with actual code. To edit this file, double-click on the file name. Add the following code: /* WATCOM Interface Generator Version 1.0 */
/* This file contains code generated by PowerBuilder.
* Do not modify code delimited by comments of the form:
* // $PB$ -- begin generated code for object <>. Do not modify this code
* // $PB$ -- end generated code for object <>.
* This file contains the bodies the functions for your user object.
*/
#include <pbdll.h>
#include "cuo_faCD.hpp"
// $PB$ -- begin generated code for object <cuo_factorial>. Do not modify this code
#if 1
long cuo_factorial::uf_factorial( long factorial_no ) {
// $PB$ -- end generated code for object <cuo_factorial>.
//==================================
long var1, var2;
if ( factorial_no > 10 || factorial_no < 0 )
return( -1 );
else
{
var1 = factorial_no;
for ( var2 = factorial_no - 1; var2 > 1; var2-- )
{
var1 *= var2;
}
return ( var1 );
}
}
#endif // PowerBuilder code, do not remove
Using Your User ObjectOnce you're done writing your functions, exit from the editor and use the Watcom compiler to link and compile the code into a DLL-select Actions/Make All from the menu. Then exit from the Watcom IDE. To use your user object in a window, you simply include its instance in the window and access the instance-declare a variable of the user object type and use the CREATE command to create an instance of the user object in the window. Paint a window like the one shown here and write the following code for the Calculate Factorial command button.
cuo_factorial lcuo_func1 Long lFactor lcuo_func1 = Create cuo_factorial // // typically you would test before calling the function // in this example we show that the DLL is returning an // error value as designed. // lFactor = lcuo_func1.uf_factorial( Long( sle_1.Text ) )
if ( lFactor > -1 ) then
MessageBox( "Factorial value of " + sle_1.Text, &
String( lFactor) )
else
MessageBox( "Invalid Value provided",
"Please Select A Value From 0 to 10." )
end if
Destroy lcuo_func1 You can now test the window out.
When you're done using your user object, use the DESTROY command to deallocate its memory. DESTROY lcuo_func1 As you can see the Enterprise edition of PowerBuilder eliminates the need for a separate C++ compiler to write DLLs for your application. PowerBuilder provides a one-stop center to write your PowerBuilder applications and external functions as a Windows DLL. When creating such a DLL, PowerBuilder automatically generates some of the necessary files for you thus saving time and effort. You can create and test your DLL from your PowerBuilder environment without having to switch to a separate C++ compiler.
|
| Copyright © 1996 - 2006 HamaraShehar.com Pvt. Ltd. All Rights Reserved.
Domain Registration, Website Design, Website Hosting by HamaraShehar.com |