[파워빌더] Create and use a C DLL (created with VC++)
With VC++ v6 , create a new Win32 Dynamic DLL project and name it : howto
Specify that it is a DLL that exports some symbols
Edit the howto.cpp file.
Add the following function :HOWTO_API char * __stdcall sayhello(void)
{
return "hello";
}
Edit the howto.h file.
Add the following line :HOWTO_API CHAR * __STDCALL sayhello(VOID);
In the Project settings for Win32 release.
Make sure that you are not using the MFC in General Tab.
In the Link tab, category General, check the option Generate MAP file.
Click the OK button.
From the Build menu, set the active configuration to Win 32 release.
Build the project.
Open the file howto.map located in the Release directory.
Find the sayhello entry and note the "decorated name".
In this example, it's ?sayhello@@YGPADXZ
Click on Source files (in the treeview) and from Menu choose New.
Select Text file, check Add to the project and give howto.def as the name.Enter the following lines in the new howto.def file :
; DEF file for the Howto DLL
EXPORTS
sayhello =?sayhello@@YGPADXZ
note: this will add /def:".\howto.def" to the Project options (you can see these options in Project settings).
Save everything and build the DLL
Place the howto.dll (located in the Release directory) in the same directory as your PBL.
To use the DLL from PB, add the Local external function declaration :function string sayhello() library "howto.dll"
In Powerscriptmessagebox("", string(sayhello()))
A more useful example is to create a bitwiseAnd function since this not supported in Powerscript unless you use complicated string manipulations which are very time consuming.
Add the following declaration in the howto.h file :HOWTO_API DWORD __STDCALL bitwiseAnd( DWORD , DWORD );
Add the following function to to the howto.cppHOWTO_API DWORD __stdcall bitwiseAnd( DWORD arg1, DWORD arg2)
{
return ( arg1 & arg2 );
}
Rebuild and determine from the howto.map file the decorated name for the bitwiseAnd function. On my system, it's ?bitwiseAnd@@YGKKK@Z. So now the howto.def file should look like :; DEF file for the Howto DLL
EXPORTS
sayhello =?sayhello@@YGPADXZ
bitwiseAnd = ?bitwiseAnd@@YGKKK@Z
Rebuild every thing with the new howto.def.
From Powerscript, you can use your new function with :[local external function declaration]
FUNCTION ulong bitwiseAnd(ulong a, ulong b) LIBRARY "howto.dll"
[powerscript]
ulong a = 3
ulong b = 2
ulong c
c = bitwiseAnd( a , b )
messagebox("", string(c)) // should be 2
Where returning a string to PB from a DLL, you should always allocate some spaces from PB. In the following example, the sayhello() will return "hello" in the sayit variable passed by reference. Before the actual call, the PB variable is initialized with enough spaces.
[local external function declaration]
SUBROUTINE sayhello(ref string sayit) LIBRARY "howto.dll"
[Powerscript]
string ls_hello
ls_hello = space(20)
sayhello(ls_hello)
MessageBox("", ls_hello)
Specify that it is a DLL that exports some symbols
Edit the howto.cpp file.
Add the following function :HOWTO_API char * __stdcall sayhello(void)
{
return "hello";
}
Edit the howto.h file.
Add the following line :HOWTO_API CHAR * __STDCALL sayhello(VOID);
In the Project settings for Win32 release.
Make sure that you are not using the MFC in General Tab.
In the Link tab, category General, check the option Generate MAP file.
Click the OK button.
From the Build menu, set the active configuration to Win 32 release.
Build the project.
Open the file howto.map located in the Release directory.
Find the sayhello entry and note the "decorated name".
In this example, it's ?sayhello@@YGPADXZ
Click on Source files (in the treeview) and from Menu choose New.
Select Text file, check Add to the project and give howto.def as the name.Enter the following lines in the new howto.def file :
; DEF file for the Howto DLL
EXPORTS
sayhello =?sayhello@@YGPADXZ
note: this will add /def:".\howto.def" to the Project options (you can see these options in Project settings).
Save everything and build the DLL
Place the howto.dll (located in the Release directory) in the same directory as your PBL.
To use the DLL from PB, add the Local external function declaration :function string sayhello() library "howto.dll"
In Powerscriptmessagebox("", string(sayhello()))
A more useful example is to create a bitwiseAnd function since this not supported in Powerscript unless you use complicated string manipulations which are very time consuming.
Add the following declaration in the howto.h file :HOWTO_API DWORD __STDCALL bitwiseAnd( DWORD , DWORD );
Add the following function to to the howto.cppHOWTO_API DWORD __stdcall bitwiseAnd( DWORD arg1, DWORD arg2)
{
return ( arg1 & arg2 );
}
Rebuild and determine from the howto.map file the decorated name for the bitwiseAnd function. On my system, it's ?bitwiseAnd@@YGKKK@Z. So now the howto.def file should look like :; DEF file for the Howto DLL
EXPORTS
sayhello =?sayhello@@YGPADXZ
bitwiseAnd = ?bitwiseAnd@@YGKKK@Z
Rebuild every thing with the new howto.def.
From Powerscript, you can use your new function with :[local external function declaration]
FUNCTION ulong bitwiseAnd(ulong a, ulong b) LIBRARY "howto.dll"
[powerscript]
ulong a = 3
ulong b = 2
ulong c
c = bitwiseAnd( a , b )
messagebox("", string(c)) // should be 2
Where returning a string to PB from a DLL, you should always allocate some spaces from PB. In the following example, the sayhello() will return "hello" in the sayit variable passed by reference. Before the actual call, the PB variable is initialized with enough spaces.
[local external function declaration]
SUBROUTINE sayhello(ref string sayit) LIBRARY "howto.dll"
[Powerscript]
string ls_hello
ls_hello = space(20)
sayhello(ls_hello)
MessageBox("", ls_hello)