我从asp.net应用程序调用dll(用vb6编写)。我正在调用dll的以下函数:
Public Function FamIdentify_BYTE(strName As String, strDatabaseTemplate As Variant, len_of_data As Long, strIdentifyTemplate As String, strIP As String) As Boolean
C#中的以下代码调用上述函数:
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;
}
}
但是当我在IIS express中运行应用程序时,我收到此错误:
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)
这是完整的错误列表:
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
如果我在IIS服务器上运行上述应用程序,重定向时间太长。我不明白为什么会这样。请帮我 。
答案 0 :(得分:2)
关于从MSDN Library传递参数机制的一个小问题:
在Visual Basic 6.0中,如果没有为a指定ByVal或ByRef procedure参数,传递机制默认为ByRef。这个 允许传入过程的变量在中修改 调用程序。
使用上面的解释,默认情况下,DLL库方法对所有输入变量使用pass-by-reference,这些变量与C#的默认值(而不是pass-by-value)具有不同的行为。因此,ref
关键字对于方法调用中的每个传递变量都是必需的,如下所示:
fingerMatched = famApp.FamIdentify_BYTE(ref name, ref fingerData, ref length_of_data, ref SampleModel, ref REMOTE_ADDR);
此时,它似乎工作正常但仍然有第二个问题,其中包含在传递输入参数期间从字节数组到object
(VB 6中的Variant
)的无效转换。您必须首先将该字节数组转换为object
:
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;
然后,上面的方法调用应改为:
fingerMatched = famApp.FamIdentify_BYTE(ref name, ref fingerObject, ref length_of_data, ref SampleModel, ref REMOTE_ADDR);
注意:您不能将带参考参数的方法调用到完全指定参数类型以外的类型,并且ref
参数必须包含任何可赋值(s) )。
相关参考资料:
Passing Objects By Reference or Value in C#
Why use the 'ref' keyword when passing an object?