Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在C#中实现回调以从具有多个参数的本机DLL方法检索返回代码_C#_.net_Dll_Callback - Fatal编程技术网

如何在C#中实现回调以从具有多个参数的本机DLL方法检索返回代码

如何在C#中实现回调以从具有多个参数的本机DLL方法检索返回代码,c#,.net,dll,callback,C#,.net,Dll,Callback,我需要从C#调用非托管DLL的本机方法。 DLL源代码当然不可用(由“第三方”提供) 本机DLL方法应接受两个参数并返回(本机)整数代码: MyDLLNativeMethod(字符串文件名,int返回代码) 本机DLL方法的第一个参数是字符串(文件名)&第二个参数必须是“引用”,以便在执行后收集返回代码。我对如何获取返回代码感到困惑。DLL调用工作正常,根据第一个参数生成预期的文件 我应该如何实现所需的回调以对检索到的不同返回码值执行操作 // ~~~~~~~~~~~~~~~~~~~~ // M

我需要从C#调用非托管DLL的本机方法。 DLL源代码当然不可用(由“第三方”提供)

本机DLL方法应接受两个参数并返回(本机)整数代码:

MyDLLNativeMethod(字符串文件名,int返回代码)

本机DLL方法的第一个参数是字符串(文件名)&第二个参数必须是“引用”,以便在执行后收集返回代码。我对如何获取返回代码感到困惑。DLL调用工作正常,根据第一个参数生成预期的文件

我应该如何实现所需的回调以对检索到的不同返回码值执行操作

// ~~~~~~~~~~~~~~~~~~~~
// Main entry point
// ~~~~~~~~~~~~~~~~~~~~
namespace MyProject
{
    class Program
    {
        static void Main(string[] args)
        {
            MyDLLCaller _dllcaller = new MyDLLCaller();
            _dllcaller.MyDLLNativeMethod("test_file.txt");
        }
    }
}

// ~~~~~~~~~~~~~~~~~~~~
// DLL calling class
// ~~~~~~~~~~~~~~~~~~~~
    namespace MyProject
    {
        class MyDLLCaller
        {
            // DLL Loading    
            [DllImport(@"C:\test\mydll.dll")]
            public static extern int MyDLLNativeMethod(string _filename);

            // Native DLL method pseudo code :
            // MyDLLNativeMethod(string filename, int returncode).
        }
    }

提前感谢

如果上述外部调用声明适用于您的库,您将从调用中获得返回代码

var retCode = _dllcaller.MyDLLNativeMethod("test_file.txt");

如果返回代码是第二个参数,我认为您应该使用
out
关键字:

int returncode = 0;

[DllImport(@"C:\test\mydll.dll")]
public static extern int MyDLLNativeMethod(string _filename, out int returncode);

if(returnCode != 0)
{
    // something went wrong
}

用一些你知道应该返回错误代码的文件试试。

使用上面的声明:
publicstaticextern int-MyDLLNativeMethod(string\u filename,out-int-returncode)
现在VisualStudio抱怨
“当仅使用“\u filename”参数调用此方法时,没有与所需的形式参数returncode相对应的参数:
\u dllcaller.MyDLLNativeMethod(“test\u file.txt”);