Tuesday, May 18, 2010

32 bit components on 64 bit coldfusion

If you try to initiate an 32 bit component on the 64 bit version of coldfusion (e.g. <cfobject action="Create" class="MSXML2.ServerXMLHTTP" name="objSrvHTTP">)
you will get the following error:

An exception occurred when instantiating a COM object.
The cause of this exception was that: java.lang.RuntimeException: Can not use native code: Initialisation failed.
Unfortunately there is no easy solution for this problem. The only way to solve this is to install a 32bit version of coldfusion on a different server or workstation (or run it parallel on the same server). I know of two possible solutions for this problem.

Option 1
Install a 32bit version of coldfusion on a different server or workstation (or run it parallel on the same server). You can run the component on a 32 bit server or workstation. In my case i created a web services that executed the 32bit component and returns the result to the caller.

<cffunction name="example" access="remote" returntype="struct" output="no">
<cfset pass_url="http://www.adobe.com/"">
<cfobject action="Create" name="objSrvHTTP" class="MSXML2.ServerXMLHTTP">
<cfset temp = objSrvHTTP.open("GET","#pass_url#")>
<cfset temp = objSrvHTTP.send("")>
<cfset websitedata=structnew()>
<cfset websitedata.responseBody = #toString(objSrvHTTP.responseBody)#>
<cfset websitedata.status = #left(objSrvHTTP.Status,3)#>
<cfreturn websitedata>

Option 2
Call the component in asp and return the data in a webservice or as html. The following example executes the MSXML2.ServerXMLHTTP component in aspx and writes the response to the page. This pages can be called from a coldfusion cfhttp tag. Please note i know very little of asp so this example could probebly be improved.

<%@ Page Language="VB" Debug="false" %>

dim page_url 
dim page_status  
dim page_responseBody
dim page_responseText
dim xmlhttp
dim mycharset
page_url =  Request.QueryString("weburl")   
xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")   
xmlhttp.open ("GET", page_url, false) 
xmlhttp.setRequestHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv: Gecko/20100914 Firefox/3.6.10 ( .NET CLR 3.5.30729)")
xmlhttp.send ("")

'  page_status =  Cint(xmlhttp.status)
page_responseBody =  xmlhttp.responseBody
page_responseText =  xmlhttp.responseText

mycharset = "" 
mycharset= xmlhttp.getResponseHeader("Content-Type") 
If(inStr(mycharset,"charset")=0) Then  
mycharset = "ISO-8859-1" 
Dim pattern As String = "(?i)>META(.*)charset=\s*([^\s|^""|^']*)"
If( Regex.IsMatch( page_responseText, pattern ))   Then
Response.CharSet = Regex.Match( page_responseText, pattern ).Groups(2).ToString() 
End If
End If  

Response.CharSet = mycharset 
response.StatusCode = page_status
' Response.Write(page_responseText)

xmlhttp = nothing


No comments: