Announcement

Collapse
No announcement yet.

C/C++ import libraries for PBDLL - Revisited

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • C/C++ import libraries for PBDLL - Revisited

    This is in response to Peter Stephensen's post below dated 11-22.
    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 down loaded 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:
    Code:
    #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++ only are 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:

    Code:
    #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.

    Code:
    // 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:

    Code:
    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 mindfull of one fact, C/C++ is case sensitive so function
    names must match exactly.

    Cheers,
    Cecil


    ------------------

  • #2
    Cecil,

    I had the feeling that there had to be a non-decorated 'C-way'
    to make the lib. Thanks for showing how.

    Regards
    Peter

    ------------------
    [email protected]
    www.dreammodel.dk

    Comment


    • #3
      here's how i got the above going in vc5.

      1. create the files myapp.bas, myapp.def, myapp.h, testlib.cpp from above
      2. in pb compile myapp.dll
      3. from the dos prompt, find and run vcvars32.bat
      4. from the dos prompt, lib /def:myapp.def
      5. in vc5, file open testlib.cpp
      6. comment out /* #include "stdafx.h" */ in testlib.cpp
      7. build compile testlib.cpp, let vc5 add new project workspace
      8. project add file myapp.lib
      9. build build testlib.exe
      10. copy myapp.dll to \debug\
      11. in \debug\ testlib

      some other info at: http://www.powerbasic.com/support/pb...ead.php?t=5457
      and http://www.powerbasic.com/support/pb...ead.php?t=2330


      Comment

      Working...
      X