Why is Pro API returning a -9978 error from a VB.NET integration?

Document type: Tech doc  
Associated product(s): Product name: Pro Web Version: 1.0 - 3.x Implementation: APIOS: Windows
Views: 303  |  Created: 2 years agoLast updated: 2 years ago
Countries: United Kingdom United Kingdom 

Summary

The document illustrates how to resolve the –9978 ‘No match’ error when integrating the UK Pro API using VB.NET.

Solution

During an integration of QAS Pro UK API, the error –9978 ‘ No match’ may be returned when calling QAPro_Search. This is because in a .NET environment in order to pass and retrieve strings from an API function, the usage of a StringBuilder buffer is required. Therefore, it is necessary to pass a Stringbuilder buffer as the argument instead of a string.

A fixed-length character buffer must be passed into unmanaged code to be manipulated. Simply passing a string does not work in this case because the caller cannot modify the contents of the passed buffer. Even if the string is passed by reference, there is no way to initialize the buffer to a given size. Strings are thus immutable, whereas the contents of StringBuilder buffers can be changed by the callee and copied back to the callee.

A StringBuilder object can be de-referenced and modified by the callee, provided it does not exceed the capacity of the StringBuilder. It can also be initialized to a fixed length. For example, if you initialize a StringBuilder buffer to a capacity of N, the marshaler provides a buffer of size (N+1) characters. The +1 accounts for the fact that the unmanaged string has a null terminator while StringBuilder does not.

The following is example code, where an integrator was using an ASP.NET web form and VB.Net code behind it. Please note that this is sample code and not a released QAS product. As such, it has not been subjected to our usual testing procedures and therefore while we will do our utmost to support its use QAS cannot be held responsible for damage, direct or otherwise, its use brings about.

Public Class QAWebForm

Inherits System.Web.UI.Page

' Define the calls to the Quick Address Pro DLL

<DllImport("qapuieb.dll", CharSet:=CharSet.Unicode)> _

Private Shared Function QAPro_Open(<MarshalAs(UnmanagedType.LPStr)> ByVal vs1 As String, _

ByVal vs2 As String) As Integer

End Function

<DllImport("QAPUIEB.DLL", CharSet:=CharSet.Auto)> _

Public Shared Function QAPro_Search(<MarshalAs(UnmanagedType.LPStr)> ByVal vs1 As String) As Integer

End Function

<DllImport("QAPUIEB.DLL", CharSet:=CharSet.Ansi)> _

Private Shared Function QAPro_ListItem(ByVal vl1 As Integer, _

ByVal rs2 As StringBuilder, _

ByVal vi3 As Integer, _

ByVal vi4 As Integer) As Integer

End Function

<DllImport("QAPUIEB.DLL", CharSet:=CharSet.Ansi)> _

Public Shared Sub QAVersionInfo(ByVal rs1 As StringBuilder, _

ByVal vi2 As Integer)

End Sub

Sub ButtonSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSearch.Click

Dim iOpenReturn As Integer

Dim iSearchReturn As Integer

Dim iNoOfItemsFound As Integer

Dim iListItemReturn As Integer

Dim searchString As String

' Create a StringBuilder object, as strings can't be modified by a DLL

Dim sbBuffer As New StringBuilder(200)

' Open Quick Address (iOpenReturn: Success=0, -ve =error)

iOpenReturn = QAPro_Open("qapro.ini", "")

'Get the Quick Address version detail

Call QAVersionInfo(sbBuffer, sbBuffer.Capacity)

Context.Response.Write(" Version:" & sbBuffer.ToString)

' Search for the postcode in the stringBuilder

searchString = "sw40ql"

iSearchReturn = QAPro_Search(searchString)

Context.Response.Write("<BR>lSearchReturn:" & iSearchReturn)

If iSearchReturn = 0 Then

iListItemReturn = QAPro_ListItem(0, sbBuffer, 0, sbBuffer.Capacity)

Context.Response.Write("<BR>QAPro_ListItem: " & sbBuffer.ToString)

End If

End Sub

End Class

Was this document helpful?

What can we do to improve this information?