Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
	}
}
...
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();
        }
    }

...
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.
Posted
Updated 28-Aug-10 8:41am
v4

1 solution

You can pass the double* parameter as ref double thus eliminating the unsafe code. Also, why do you have out keyword?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900