Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.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
System.AccessViolationException从C#调用到不安全的C++; 我编程C代码,导入类似C++的函数: //C++ code extern "C" _declspec(dllexport) int foo(double *var1, double *var2){ double dubs[10]; RandomFunction(dubs); *var1 = dubs[5]; *var2 = dubs[7]; return 0; }_C#_C++_C_Arrays_Struct - Fatal编程技术网

System.AccessViolationException从C#调用到不安全的C++; 我编程C代码,导入类似C++的函数: //C++ code extern "C" _declspec(dllexport) int foo(double *var1, double *var2){ double dubs[10]; RandomFunction(dubs); *var1 = dubs[5]; *var2 = dubs[7]; return 0; }

System.AccessViolationException从C#调用到不安全的C++; 我编程C代码,导入类似C++的函数: //C++ code extern "C" _declspec(dllexport) int foo(double *var1, double *var2){ double dubs[10]; RandomFunction(dubs); *var1 = dubs[5]; *var2 = dubs[7]; return 0; },c#,c++,c,arrays,struct,C#,C++,C,Arrays,Struct,要将此函数导入C#,我尝试了两种方法,两种方法都导致System.AccessViolationException。这意味着我正在尝试访问受保护的内存 //C# import code //method 1 [DllImport(example.dll,CallingConvention = CallingConvention.Cdecl)] public static unsafe extern int foo(ref double var1, ref double var2); //meth

要将此函数导入C#,我尝试了两种方法,两种方法都导致System.AccessViolationException。这意味着我正在尝试访问受保护的内存

//C# import code
//method 1
[DllImport(example.dll,CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern int foo(ref double var1, ref double var2);
//method 2
[DllImport(example.dll,CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern int foo(double *var1, double *var2);

//C# calling code
//method 1
public unsafe int call_foo(){
    ....Stuff
    double var1 = 0;
    double var2 = 0;
    foo(ref var1, ref var2);
    ....More Stuff
}
//method 2
public unsafe int call_foo(){
    ....Stuff
    double var1 = 0;
    double var2 = 0;
    double *v1 = &var1;
    double *v2 = &var2;
    foo(v1, v2);
    ....More Stuff
}

<强>直到现在,我一直认为这是我的C代码的问题,但是C++代码使用数组会导致问题吗?我在这里遗漏了一些非常明显的东西吗?

这不是您的C#p/调用或本机函数
foo
的签名的问题。除了C++的数组语法,还有什么是代码>随机函数< /> >,一切都很好。我刚刚用自己的小演示进行了测试:

C++:

我和你只有几点不同:

    < L> > C++数组使用标准语法<代码>双DUB[10 ] < /代码>声明。
  • 我的编译器需要_declspec语法中的两个下划线
  • 我正在Windows 7 SP 1上使用64位Visual Studio 2012编译所有内容

您没有正确声明函数。您必须在[DllImport]属性中使用CallingConvention.Cdecl,因为您在C声明中没有使用u stdcall。C++代码无法编译,你在标题中提到的“结构和数组”是很难看到的。你很难帮助你。我认为当你从C/C++库导出函数时,你应该使用.NET中的ItpTR来访问它们。它们是否安全是另一个问题。这完全取决于你的代码写得有多好。@armanali。intPtr有什么帮助?除了“因为它就在那里”之外,你还看到使用它的理由吗?(PS:intPtr让C#做指针运算。我看不到任何指针运算。)OOP我的坏,它仍然不工作。切换呼叫约定到CDDEL。仍然会出错。我忘记调用测试脚本中的函数。@ GNEY,这是我第一次看到有人正确使用C和C++标签。
void RandomFunction( double dubs[] );

extern "C" __declspec(dllexport) int foo ( double* var1, double* var2 ) 
{
    double dubs[10];

    RandomFunction( dubs );

    *var1 = dubs[5];
    *var2 = dubs[7];

    return 0;
}

void RandomFunction( double dubs[] ) {
    for ( int i = 0; i < 10; i++ ) {
        dubs[i] = i;
    }
}
[DllImport( "Native.dll", CallingConvention = CallingConvention.Cdecl )]
private static extern int foo( ref double var1, ref double var2 );

public static void Main( string[] args )
{
    FooTest();
}

private static void FooTest()
{
    double var1 = 0;
    double var2 = 0;

    foo( ref var1, ref var2 );

    Console.Out.WriteLine( "Var1: {0:0.0}; Var2: {1:0.0}", var1, var2 );
    // Prints "Var1: 5.0; Var2: 7.0"
}