| Comments

thanks to those who joined us for my learn2asp.net session on interoperability with php and windows.  i apologize for the rescheduling (if you even knew about it) that had occurred, but glad you could make it to the session.  in the session i stated that i'd post the information and sample code i referred to on to my blog so here it is :-).

in the session i mentioned a few things, so i'll dump them to you here:

in the session (which is still available on-demand for you to watch -- and if you watch 3 webcasts i think you get some swag as well) we demonstrated some simple methods of integrating with .net applications using two primary methods: com interop and web services.  below are some of the samples i demonstrated as is the sample code for them is at the bottom of this post as an attached file download.

com interop
the com interop scenario allows us to directly interact (okay, well obviously not directly, but via com) with a .net class library that someone may have created.  a few best practices should be followed (and some required) for this to occur.  this isn't php-specific, but rather com interop specific.  first when building the .net class library, you have to make sure that you mark it ready for com interop.  in visual studio, you can view the project properties to do this and look at the "build" tab in c# and the "compile" tab in visual basic.

samples: build tab in c# (the output section is near the bottom of the info on this tab):

sshot-12

the second thing you have to do is make the class library "com visible."  in visual basic, you'll attribute the class like this:

<ComClass()> _
<Runtime.InteropServices.ComVisible(True)> _
Public Class RegClass

in c# you can again use the project properties window under the application tab, there is a setting for "assembly information":

sshot-14

which basically adds this to the AssemblyInfo.cs file in the project:

[assembly: ComVisible(true)]

the next thing you have to ensure is that the assembly is signed with a strong name.  the reason for this is that when the assembly (dll file) is going to be registered, it will need to have a reference to the codebase or alternatively will be placed in the global assembly cache (GAC) both of which require assemblies to be signed.  once you have those steps complete, you can register the .net assembly with com, using the regasm.exe tool that comes with the .net sdk.  to sign the assembly, visual studio makes this easy through the project properties options in the "signing" tab, allowing you to create a new strong name key file at that time as well:

sshot-13

once you have this you are ready to use the regasm.exe tool using the command seen here (note the highlighted portion):

sshot-16

i prefer to use the /codebase option as it allows me to not require it be in the GAC and gives me flexibility of where the assembly may physically reside.

after you have done this you can start calling the com object.  using the sample code (available at the end of this download) you would do something like this in php:

$com_app = new COM("PHPInteropCSharpClass.SimpleClass");
$result_var = $com_app->SayHello("Woodingo");
echo $result_var;

and when requesting the page it will call your com object.  i hope this helps.  for further explanation, you can view the webcast on-demand to see it all in action before you try it out for yourself.

soap interop
the second thing we demonstrated was using web services and soap.  i've included a sample file (phpSoap.php) in the zip file that uses the native PHP5 SOAP extension.  i won't go in detail here as that is well documented.  other options are to use the NuSOAP extension as well that provides similar functionality, but know that in PHP5 there is a native extension for creating a SoapClient object using a WSDL file.

i hope this helps, let me know if you have any questions.

file: PHPInterop

Please enjoy some of these other recent posts...

Comments