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:
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:
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.
5. Create a definition file for the LIB utility program in VS6:
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
------------------
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
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
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; }
Code:
LIBRARY Myapp EXPORTS TestFunc1 TestFunc2
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

------------------
Comment