Share this page 

Create and use a C DLL (created with VC++)Tag(s): WinAPI/Registry

  1. With VC++ v6 , create a new Win32 Dynamic DLL project and name it : howto
  2. Specify that it is a DLL that exports some symbols
  3. Edit the howto.cpp file.
    Add the following function :
    HOWTO_API char * __stdcall sayhello(void)
        return "hello";

  4. Edit the howto.h file.
    Add the following line :
    HOWTO_API CHAR * __STDCALL sayhello(VOID); 

  5. 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.
  6. From the Build menu, set the active configuration to Win 32 release.
    Build the project.
  7. Open the file located in the Release directory.
    Find the sayhello entry and note the "decorated name".
    In this example, it's ?sayhello@@YGPADXZ
  8. 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
    sayhello =?sayhello@@YGPADXZ 
    note: this will add /def:".\howto.def" to the Project options (you can see these options in Project settings).
  9. Save everything and build the DLL
  10. Place the howto.dll (located in the Release directory) in the same directory as your PBL.
  11. To use the DLL from PB, add the Local external function declaration :
    function string sayhello() library "howto.dll" 
  12. In Powerscript
    messagebox("", string(sayhello()))  

  13. 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 :
  14. Add the following function to to the howto.cpp
    HOWTO_API DWORD __stdcall bitwiseAnd( DWORD arg1, DWORD arg2)
       return ( arg1 & arg2 );
  15. Rebuild and determine from the 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
    sayhello =?sayhello@@YGPADXZ
    bitwiseAnd = ?bitwiseAnd@@YGKKK@Z 
  16. Rebuild every thing with the new howto.def.
  17. From Powerscript, you can use your new function with :
    [local external function declaration]
    FUNCTION ulong bitwiseAnd(ulong a, ulong b) LIBRARY "howto.dll" 
    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" 

string ls_hello

ls_hello = space(20)
MessageBox("", ls_hello)