Add reference problem ?
-
I have successfully called a Fortran dll from unmanaged C++ and now I'm trying this in C#. This is my attempt:
class MyClass { [DllImport(@"square.dll", EntryPoint="aFunc")] public static extern void aFunc(out int aNum); public static int Main() { int aNum=20; aFunc(out aNum); Console.WriteLine(aNum); return 0; } }
The dll merely squares the number and sends it back out. The error message is System.DllNotFoundException. I think the reason is that I'm not able to add reference. It keeps saying that it's not a valid .COM or .dll ! Suggestions greatly welcomed. Kash -
I have successfully called a Fortran dll from unmanaged C++ and now I'm trying this in C#. This is my attempt:
class MyClass { [DllImport(@"square.dll", EntryPoint="aFunc")] public static extern void aFunc(out int aNum); public static int Main() { int aNum=20; aFunc(out aNum); Console.WriteLine(aNum); return 0; } }
The dll merely squares the number and sends it back out. The error message is System.DllNotFoundException. I think the reason is that I'm not able to add reference. It keeps saying that it's not a valid .COM or .dll ! Suggestions greatly welcomed. KashYou need to add the DLL path to the PATH environment variable or you can just copy the DLL in the same folder as your managed assemblies. Live Life King Size Alomgir Miah
-
You need to add the DLL path to the PATH environment variable or you can just copy the DLL in the same folder as your managed assemblies. Live Life King Size Alomgir Miah
I managed to get it to work by putting the .lib and .dll file in bin\Release folder but now I have some new problems. This works to an extent. The number goes in OK, and does the calc inside the dll correctly but the new number is not sent back. I tried both "out" and "ref" declarations.
class MyClass { [DllImport(@"square.dll", EntryPoint="aFunc")] public static extern void aFunc(float aNum); public static int Main() { float aNum=20.0F; aFunc(aNum); Console.WriteLine(aNum); return 0; } } }
-
I managed to get it to work by putting the .lib and .dll file in bin\Release folder but now I have some new problems. This works to an extent. The number goes in OK, and does the calc inside the dll correctly but the new number is not sent back. I tried both "out" and "ref" declarations.
class MyClass { [DllImport(@"square.dll", EntryPoint="aFunc")] public static extern void aFunc(float aNum); public static int Main() { float aNum=20.0F; aFunc(aNum); Console.WriteLine(aNum); return 0; } } }
Kash wrote: public static extern void aFunc(float aNum); Of course not. Since you've told C# that the return type for the funtion is void, it's not expecting any return value. What this code is supposed to look like depends heavily on what the function declaration in your C++ code looks like. Without knowing that... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
Kash wrote: public static extern void aFunc(float aNum); Of course not. Since you've told C# that the return type for the funtion is void, it's not expecting any return value. What this code is supposed to look like depends heavily on what the function declaration in your C++ code looks like. Without knowing that... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
The actual dll is written in Fortran and I performaed a test in C++ using a void function declaration and it worked. What I'm trying to do is do the same in C#. I'm very new to C#, sorry if this is basic. I've tried putting the code in an unsafe context but still not returning xo althought both xi and xo (xi**2) in the dll are correct.
class MyClass { [DllImport(@"square.dll", EntryPoint="aFunc")] unsafe public static extern void aFunc(float xi, out float * xo); public static int Main() { float xi=20.0F; unsafe { float * xo; aFunc(xi,out xo); Console.WriteLine(*xo); } return 0; } } }
thanks in advance. -
The actual dll is written in Fortran and I performaed a test in C++ using a void function declaration and it worked. What I'm trying to do is do the same in C#. I'm very new to C#, sorry if this is basic. I've tried putting the code in an unsafe context but still not returning xo althought both xi and xo (xi**2) in the dll are correct.
class MyClass { [DllImport(@"square.dll", EntryPoint="aFunc")] unsafe public static extern void aFunc(float xi, out float * xo); public static int Main() { float xi=20.0F; unsafe { float * xo; aFunc(xi,out xo); Console.WriteLine(*xo); } return 0; } } }
thanks in advance.OK, now the function definition changed. You're no longer passing one parameter, but two. One more time -> WITHOUT SEEING THE ORIGINAL FUNCTION DEFINITION, IT'S IMPOSSIBLE TO TELL YOU WHAT YOU'RE DOING WRONG! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
OK, now the function definition changed. You're no longer passing one parameter, but two. One more time -> WITHOUT SEEING THE ORIGINAL FUNCTION DEFINITION, IT'S IMPOSSIBLE TO TELL YOU WHAT YOU'RE DOING WRONG! RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome