Using .Net Assemblies from PowerBuilder

advertisement
Bruce Armstrong
TeamSybase
bruce.armstrong@teamsybase.com
Techniques

PowerBuilder 11 and beyond only
 Referencing .Net assemblies directly within
Conditional Code blocks in PowerScript

Any version of PowerBuilder
 .Net Interop via COM Callable Wrappers
Referencing .Net Assemblies

Currently only non-visual assemblies
 Visual assemblies is a release milestone for
PowerBuilder 12

Conditional Code blocks
 AutoScript is not supported (errors are
reported at compile time)
 Syntax is neither PowerScript or C# but a
mix of them (PowerScript#)
.Net Interop
Runtime Callable Wrapper
 COM Callable Wrapper

Runtime Callable Wrapper
• Allows a .Net application to use a Win32
COM object
COM Object
Runtime Callable
Wrapper
.Net Application
http://msdn.microsoft.com/en-us/library/8bwh56xe.aspx
COM Callable Wrapper
• Allows a Win32 application to use a .Net
object via COM
.Net Object
COM Callable
Wrapper
Win32
Application
http://msdn.microsoft.com/en-us/library/f07c8z1c.aspx
COM Callable Wrapper

Assembly has to be COM-Visible
 Assembly can be marked such at compile time
 FxCop can be used to determine if marked
 ILSASM/ILASM can be used (in some cases) to
change the marking
COM Callable Wrapper

Marking the Assembly COM-Visible in Visual Studio
Using FxCop

Examining the COM-Visible attribute using Microsoft’s FxCop
http://msdn.microsoft.com/en-us/library/bb429476(VS.80).aspx
ILDASM

ILASM (IL Assembler) is part of the .Net
runtime
 Typical location:
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
 In the System Path

ILDASM (IL Disassembler) is part of the
.Net SDK
 Typical Location: C:\Program
Files\Microsoft.NET\SDK\v2.0\Bin
 May not be in the System Path
Using ILDASM

Examining the COM-Visible attribute in the disassembled IL
REGASM
.Net Assembly Registration Utility
 Used to create COM registry entries for
.Net component
 Syntax: RegAsm AssemblyName
[Options]

 Options:



/unregister
/regfile[:FileName]
/codebase
REGASM Output

With regfile option but without codebase
option
REGASM Output

With regfile option and codebase option
Registry Free COM
Not .Net specific, can be used for any
COM components
 Supported on Windows version XP and
later (e.g., 2003, Vista, 2008)


http://msdn.microsoft.com/enus/library/ms973913.aspx
How Registry Free COM
Works
Client App
Client
App.Manifest
Dependant
Assembly.Manifest
Dependant
Assembly (COM
Component)
GenMan32
Useful utility for generating manifest files
for Registry Free COM
 Syntax: GenMan32 AssemblyName
[Options]

 Options:



/add
/out:filename
http://blogs.msdn.com/junfeng/archive/2007/04/14/genman32-a-tool-togenerate-sxs-manifest-for-managed-assembly-for-registration-free-com-netinterop.aspx
GenMan32 Output
CCW Limitations
Types, methods, properties, fields and
events must all be public
 Methods must not be static
 Types must have a public default
constructor
 Overloaded methods should be avoided
 Late binding clients can’t retrieve error
information from .Net exceptions
 PowerBuilder Specific: Events

Avoid Overloaded Methods
.Net Class
• foo ( int a )
• foo ( string a )
• foo ( long a )
COM
Wrapper
• foo (int a )
• foo_2 ( string a
)
• foo_3 ( long a )
Why error information gets lost
CCW
1. COM Call
PowerBuilder
Client
IDispatch
4.SetErrorInfo
2. .Net Call
.Net
Assembly
5. GetErrorInfo
6. Return from call
Exception
3. Exception Thrown
What we can do about it
Exception
Publisher
CCW
IDispatch
Exception
Managed
Exception
Logger
Managed
Exception
Queue
Event Log
Debug
Window
Further Reading
Microsoft’s Exception Handling
Application Block
http://msdn.microsoft.com/enus/library/cc511522.aspx

Get Seamless .NET Exception Logging
From COM Clients Without Modifying
Your Code
http://msdn.microsoft.com/enus/magazine/cc188710.aspx

Interop Forms Toolkit 2.0

Not just forms, but controls as well
 http://msdn.microsoft.com/en-
us/vbasic/bb419144.aspx
Can be used in the Express versions of
Visual Studio with some limitations
 C# implementation available on
CodeProject

 http://www.codeproject.com/KB/dotnet/VB6_
-_C__Interop_Form.aspx
Supporting Events

Create a Interface of type
ComInterfaceType.InterfaceIsIDispatch with
the defined events
 Provide a GUID for the interface
 Assign a unique DispID for each event



Add a ComSourceInterfaces attribute to the
control
Define the events within the control
Call those events from within the user control
http://www.15seconds.com/issue/060309.htm
Event Declaration Interface
Implementing Events
Events exposed in PowerBuilder
Questions
Download