Reputation: 2220
I am calling a dll ( written in vb6 ) from asp.net application . I am calling the following function of dll :
Public Function FamIdentify_BYTE(strName As String, strDatabaseTemplate As Variant, len_of_data As Long, strIdentifyTemplate As String, strIP As String) As Boolean
The following code in C# calls the above function :
public Boolean VerifyFinger(String name , String SampleModel, String REMOTE_ADDR)
{
try
{
Connect();
OracleCommand command = connection.CreateCommand();
string sql = "SELECT FINGER_DATA,DATA_LENGTH,SERIAL_NO FROM FP_BIOMETRIC_DATA WHERE CUST_NO =" + name.Trim();
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
bool fingerMatched = false;
FamServer.FamApp famApp = new FamServer.FamApp();
while (reader.Read())
{
Object[] values = new object[3];
int numColumns = reader.GetValues(values);
byte[] fingerData = values[0] as byte[];
string result = System.Text.Encoding.UTF8.GetString(fingerData);
int length_of_data = Convert.ToInt32(values[1]);
int serial_no = Convert.ToInt32(values[2]);
// FamIdentify_BYTE(ref string, ref object, ref int, ref string, ref string)
fingerMatched = famApp.FamIdentify_BYTE(name, fingerData, length_of_data, SampleModel, REMOTE_ADDR);
// if (fingerMatched)
// break;
}
famApp.Termination();
// famApp = null;
GC.Collect();
Close();
return fingerMatched;
}
catch (Exception e)
{
HttpContext.Current.Response.Write("<br>" + e.GetBaseException() + "<br>");
return false;
}
}
But when I run the application in IIS express , I am getting this error :
The best overloaded method match for 'FamServer._FamApp.FamIdentify_BYTE(ref string, ref object, ref int, ref string, ref string)'
has some invalid arguments ASPNET(1)
This is the full error list :
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,37): error CS1502: The best overloaded method match for 'FamServer._FamApp.FamIdentify_BYTE(ref string, ref object, ref int, ref string, ref string)' has some invalid arguments
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,61): error CS1620: Argument '1' must be passed with the 'ref' keyword
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,67): error CS1503: Argument '2': cannot convert from 'byte[]' to 'ref object'
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,79): error CS1620: Argument '3' must be passed with the 'ref' keyword
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,95): error CS1620: Argument '4' must be passed with the 'ref' keyword
F:\ASP.NET Projects\ASPNET\App_Code\Global.asax.cs(180,108): error CS1620: Argument '5' must be passed with the 'ref' keyword
If I run the above the above application in IIS server , The redirecting time is too much long . I cant understand why this is happening . Please help me .
Upvotes: 2
Views: 1535
Reputation: 24957
A slight gotcha regarding passing parameters mechanism from MSDN Library:
In Visual Basic 6.0, if you do not specify ByVal or ByRef for a procedure parameter, the passing mechanism defaults to ByRef. This allows the variable passed into the procedure to be modified in the calling program.
Using explanation above, the DLL library method by default uses pass-by-reference for all input variables, which has different behavior with C#'s default (pass-by-value instead). Hence, ref
keyword is mandatory for every passed variables inside method call like this:
fingerMatched = famApp.FamIdentify_BYTE(ref name, ref fingerData, ref length_of_data, ref SampleModel, ref REMOTE_ADDR);
At this point, it seems working fine but still has second issue which contains invalid cast from a byte array to object
(Variant
in VB 6) during passing input parameters. You must convert that byte array to object
first:
byte[] fingerData = values[0] as byte[];
// Variant in VB 6 translated as Object into C#
// Therefore, a conversion to Object type is required
object fingerObject = fingerData;
Then, the method call above should be changed to this:
fingerMatched = famApp.FamIdentify_BYTE(ref name, ref fingerObject, ref length_of_data, ref SampleModel, ref REMOTE_ADDR);
NB: You can't call a method with reference parameters to types other than exactly specified parameter type, and the ref
parameter(s) must be contain any assignable value(s).
Related references:
Passing Objects By Reference or Value in C#
Why use the 'ref' keyword when passing an object?
Passing Parameters (MSDN Library)
Cannot convert from 'ref byte[]' to 'ref object'
Upvotes: 2