Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Add reference problem ?

Add reference problem ?

Scheduled Pinned Locked Moved C#
helpcsharpc++comquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kash
    wrote on last edited by
    #1

    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

    A 1 Reply Last reply
    0
    • K 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. Kash

      A Offline
      A Offline
      Alomgir Miah
      wrote on last edited by
      #2

      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

      K 1 Reply Last reply
      0
      • A 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

        K Offline
        K Offline
        Kash
        wrote on last edited by
        #3

        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; } } }

        D 1 Reply Last reply
        0
        • K Kash

          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; } } }

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          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

          K 1 Reply Last reply
          0
          • D Dave Kreskowiak

            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

            K Offline
            K Offline
            Kash
            wrote on last edited by
            #5

            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.

            D 1 Reply Last reply
            0
            • K Kash

              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.

              D Offline
              D Offline
              Dave Kreskowiak
              wrote on last edited by
              #6

              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

              K 1 Reply Last reply
              0
              • D Dave Kreskowiak

                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

                K Offline
                K Offline
                Kash
                wrote on last edited by
                #7

                subroutine aFunc(xi,xo) real , intent(in) :: xi real , intent(out) :: xo ! !DEC$ATTRIBUTES dllexport :: aFunc !DEC$ATTRIBUTES DEFAULT,STDCALL, DECORATE, ALIAS: 'aFunc' :: aFunc ! xo = xi*xi end subroutine aFunc

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups