我正在从 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 服务器中运行上述应用程序,重定向时间太长。我不明白为什么会发生这种情况。请帮我 。
最佳答案
关于传递参数机制的一个小问题 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.
根据上面的解释,DLL 库方法默认对所有输入变量使用按引用传递,这与 C# 的默认行为不同(改为按值传递)。因此,对于方法调用中传递的每个变量,ref
关键字是必需的,如下所示:
fingerMatched = famApp.FamIdentify_BYTE(ref name, ref fingerData, ref length_of_data, ref SampleModel, ref REMOTE_ADDR);
此时,它似乎工作正常,但仍然存在第二个问题,其中包含在传递输入参数期间从字节数组到对象
(VB 6中的Variant
)的无效转换。您必须首先将该字节数组转换为对象
:
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
参数必须包含任何可分配的参数值。
相关引用:
Passing Objects By Reference or Value in C#
Why use the 'ref' keyword when passing an object?
关于c# - 最佳重载方法匹配...有一些无效参数 [byref/byval],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42883198/