                          ****************************
                            Notes from Kevin Diggins
                          ****************************

      I found the following article on the PowerBasic Web Forum.  It opened
      my eyes on how to build import libraries for DLL files created by the
    PowerBasic PB/DLL compiler. Using this information, I created my sample
     that allows BCX users to call the hideous looking but sometimes useful

                          PowerBasic InputBox function:

          Value$ = InputBox$( prompt$, title$, default$, xpos, ypos )


                        I've restructured my BCX call to:

          PB_InputBox( Title$, Prompt$, Default$, Xpos, Ypos, Value$ )


      You will need the PowerBasic Compiler ( $189.00 US ) to re-build this
      sample DLL.  You also need to manually create the EXPORT file (.EXP)
    for LCC-Win32 for these types of projects, which is not difficult at all.

       When using LCC-Win32, it's even easier than the following article.
        Read my BUILD.BAT file if you're interested how all this works.

   **************************************************************************
                  Original Posting from PowerBasic Web Forum
   **************************************************************************

  Topic:   C/C++ import libraries for PBDLL - Revisited
  Author:   Cecil Williams

  This describes creating an import library for C/C++ programmers
  to be able to link into and use PB DLLs. I did not realize
  why Peter was using the "mangled" aka "decorated" functions in
  the PB alias until I downloaded the source files. It is NOT
  necessary to go to that much trouble, in fact it's double work, in
  polling the linker to get the decorated value in C++.

  I am going to leave out the semantics of using MSDEV here and
  just list Peter's code modified. This would be a good example
  for the new PB offering to list as a reference in the next
  release.


  1. Create the PB DLL:

  #COMPILE DLL
  #INCLUDE "WIN32API.INC"

  FUNCTION LibMain(BYVAL hInstance   AS LONG, _
  BYVAL fwdReason   AS LONG, _
  BYVAL Reserved    AS LONG) EXPORT AS LONG
  SELECT CASE fwdReason
    CASE %DLL_PROCESS_ATTACH
    FUNCTION = 1
    EXIT FUNCTION
    CASE %DLL_PROCESS_DETACH
    EXIT FUNCTION
    CASE %DLL_THREAD_ATTACH
    EXIT FUNCTION
    CASE %DLL_THREAD_DETACH
    EXIT FUNCTION
  END SELECT
  END FUNCTION

  SUB TestFunc1 CDECL ALIAS "TestFunc1" (s AS ASCIIZ) EXPORT
  MSGBOX s
  END SUB

  FUNCTION TestFunc2 CDECL ALIAS "TestFunc2" (BYVAL x AS LONG) EXPORT AS LONG
  FUNCTION = 2*x
  END FUNCTION


  Notice the non-decorated alias in the above functions. Decorated
  functions in C++ are only for overloaded functions which do not
  work in PB.


  2. Next compile the above code to a DLL.

  3. Now, write the C header file for the PB functions:

  #ifndef MYAPP_H
  #define MYAPP_H
  #ifdef __cplusplus
  extern "C" {
  #endif
  void TestFunc1( char * );
  int  TestFunc2( int );
  #ifdef __cplusplus
  }
  #endif
  #endif //MYAPP_H

  Notice that we now have control over this header file if we are
  compiling in C++ context. The extern "C" adder now forces C
  linkage. The compiler uses standard C convention in decorating
  the functions with a leading underscore. Not worried about this.

  4. Create a console app in C++ to display our use of the PB DLL.

  // TestLib.cpp : Defines the entry point for the console application.

  #include "stdafx.h"
  #include <stdlib.h>
  #include <string.h>
  #include <stdio.h>
  #include "MyApp.h"


  int main(void)
  {
  char buffer[30];
  int y = 0;
  strcpy( buffer, "This is written in C++!" );

  TestFunc1( buffer );
  y = TestFunc2( 2 );
  _itoa( y, buffer, 10 );

  strcat( buffer, " is the product 2 X 2" );
  TestFunc1( buffer );
  return 0;
  }


  5. Create a definition file for the LIB utility program in VS6:

  LIBRARY      Myapp
  EXPORTS
  TestFunc1
  TestFunc2

  6. Call LIB from an MSDOS command prompt as follows:
     LIB /DEF:Myapp.def

  7. We now have the import lib Myapp.lib and export file Myapp.exp

  8. Compile and execute the above C++ file.

  And that's all there is to it. You can supply a PB DLL in usable
  form to C and C++ developers in the manner they expect.

  Also, be mindful of one fact, C/C++ is case sensitive so function
  names must match exactly.
