在C#中使用带有Ref指针参数的non.NET Dll [英] Using a non.NET Dll with a Ref pointer-parameter in C#

查看:85
本文介绍了在C#中使用带有Ref指针参数的non.NET Dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我的C#程序有问题,我想征询您的意见
过去,我使用以下代码编写了传统的DLL(TryingDLL)
....

Hi everybody
I have a problem with my C# program and I''d like to get your advice
In the past, I wrote a traditional DLL (TryingDLL) with following code
....

EXPORT void Test(double* &ptrDbl,long n)
{
	ptrDbl=new double[n];
	for(long i=0;i<n;i++)
	{
		ptrDbl[i]=i+100;
	}
}

...
然后,我编译为"TryingDLL.dll".现在,我想在我的C#程序的该库中使用测试功能
...

...
Then I compiled to "TryingDLL.dll". Now I want to use the function Test in that library in my C# program
...

namespace CSApp
{
    class Program
    {
        [DllImport("E:\\Temp\\Programming\\C++\\TryingDll\\App\\Output\\TryingDll.dll")] 
        private static extern unsafe void Test(out double * p,long n);
        
        static unsafe void Main(string[] args)
        {
            double* p;
            Test(out p,3);
            //Console.WriteLine(p[2])        
            Console.ReadLine();
        }
    }


...
但是此代码不起作用(构建时没有错误,但程序无法运行并显示错误消息).
谁能帮助我解决问题?
(我还尝试将库与测试的C ++代码一起使用,效果很好)
谢谢.


...
But this code didn''t work (there''s no error when I built it, but the program can not run with an error message).
Can anyone help me to solve the problem?
(I also tried using the library with a test C++ code and it worked very well)
Thanks.

推荐答案

您可以将double *参数作为ref double传递,从而消除了不安全的代码.另外,为什么您没有关键字?
You can pass the double* parameter as ref double thus eliminating the unsafe code. Also, why do you have out keyword?


这篇关于在C#中使用带有Ref指针参数的non.NET Dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆