Calling external functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim Greenwood

    Calling external functions

    I am at my wits end here. I have a simple project with one global function
    returning a long value.

    __declspec(dlle xport) long myfunc()
    {
    }

    I've built the dll, added it as a reference to my C# project. Now how in
    the world do I reference "myfunc" ????
    It totally eludes me.

    Many thanks for any help here.


  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Calling external functions

    Tim,

    You should have received an error when you added it to your project. If
    you have a function that is exported by a DLL, you need to call it through
    the P/Invoke layer. If the dll is somewhere in the path where LoadLibrary
    will find it, you can do this:

    [DllImport("<dll name here>.dll")]
    static extern int myfunc();

    And then call the dll function in managed code.

    Hope this helps.


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
    news:%23faLcxE$ FHA.3872@TK2MSF TNGP12.phx.gbl. ..[color=blue]
    >I am at my wits end here. I have a simple project with one global function
    >returning a long value.
    >
    > __declspec(dlle xport) long myfunc()
    > {
    > }
    >
    > I've built the dll, added it as a reference to my C# project. Now how in
    > the world do I reference "myfunc" ????
    > It totally eludes me.
    >
    > Many thanks for any help here.
    >[/color]


    Comment

    • Tim Greenwood

      #3
      Re: Calling external functions

      I did that exactly and it says "Unable to find an entry point named gettime
      in DLL ctime.dll"


      "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
      message news:uzUlXzE$FH A.4088@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Tim,
      >
      > You should have received an error when you added it to your project.
      > If you have a function that is exported by a DLL, you need to call it
      > through the P/Invoke layer. If the dll is somewhere in the path where
      > LoadLibrary will find it, you can do this:
      >
      > [DllImport("<dll name here>.dll")]
      > static extern int myfunc();
      >
      > And then call the dll function in managed code.
      >
      > Hope this helps.
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      > "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
      > news:%23faLcxE$ FHA.3872@TK2MSF TNGP12.phx.gbl. ..[color=green]
      >>I am at my wits end here. I have a simple project with one global
      >>function returning a long value.
      >>
      >> __declspec(dlle xport) long myfunc()
      >> {
      >> }
      >>
      >> I've built the dll, added it as a reference to my C# project. Now how in
      >> the world do I reference "myfunc" ????
      >> It totally eludes me.
      >>
      >> Many thanks for any help here.
      >>[/color]
      >
      >[/color]


      Comment

      • Tim Greenwood

        #4
        ARRGGGHH Re: Calling external functions

        this should be so simple....here is the code in my simple "C" dll

        #define DllExport __declspec(dlle xport)

        #include <time.h>

        //DllExport

        long gettime(void);

        long gettime()

        {

        time_t tt = time(NULL);

        return (long)tt;

        }

        --------------------------------------------------------------------------
        here is the import in my C# app

        [DllImport("ctim e.dll", CallingConventi on=CallingConve ntion.Cdecl)]

        public static extern long gettime();



        -------------------------------------------

        I've tried several different CallingConventi ons and nothing same thing
        "Unable to find entry point named gettime in dll ctime.dll








        "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote in
        message news:uzUlXzE$FH A.4088@TK2MSFTN GP09.phx.gbl...[color=blue]
        > Tim,
        >
        > You should have received an error when you added it to your project.
        > If you have a function that is exported by a DLL, you need to call it
        > through the P/Invoke layer. If the dll is somewhere in the path where
        > LoadLibrary will find it, you can do this:
        >
        > [DllImport("<dll name here>.dll")]
        > static extern int myfunc();
        >
        > And then call the dll function in managed code.
        >
        > Hope this helps.
        >
        >
        > --
        > - Nicholas Paldino [.NET/C# MVP]
        > - mvp@spam.guard. caspershouse.co m
        >
        > "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
        > news:%23faLcxE$ FHA.3872@TK2MSF TNGP12.phx.gbl. ..[color=green]
        >>I am at my wits end here. I have a simple project with one global
        >>function returning a long value.
        >>
        >> __declspec(dlle xport) long myfunc()
        >> {
        >> }
        >>
        >> I've built the dll, added it as a reference to my C# project. Now how in
        >> the world do I reference "myfunc" ????
        >> It totally eludes me.
        >>
        >> Many thanks for any help here.
        >>[/color]
        >
        >[/color]


        Comment

        • Tim Greenwood

          #5
          Re: ARRGGGHH Re: Calling external functions

          The line
          //DllExport

          was not always commented ....


          "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
          news:uqTGmQF$FH A.2036@TK2MSFTN GP14.phx.gbl...[color=blue]
          > this should be so simple....here is the code in my simple "C" dll
          >
          > #define DllExport __declspec(dlle xport)
          >
          > #include <time.h>
          >
          > //DllExport
          >
          > long gettime(void);
          >
          > long gettime()
          >
          > {
          >
          > time_t tt = time(NULL);
          >
          > return (long)tt;
          >
          > }
          >
          > --------------------------------------------------------------------------
          > here is the import in my C# app
          >
          > [DllImport("ctim e.dll", CallingConventi on=CallingConve ntion.Cdecl)]
          >
          > public static extern long gettime();
          >
          >
          >
          > -------------------------------------------
          >
          > I've tried several different CallingConventi ons and nothing same thing
          > "Unable to find entry point named gettime in dll ctime.dll
          >
          >
          >
          >
          >
          >
          >
          >
          > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
          > in message news:uzUlXzE$FH A.4088@TK2MSFTN GP09.phx.gbl...[color=green]
          >> Tim,
          >>
          >> You should have received an error when you added it to your project.
          >> If you have a function that is exported by a DLL, you need to call it
          >> through the P/Invoke layer. If the dll is somewhere in the path where
          >> LoadLibrary will find it, you can do this:
          >>
          >> [DllImport("<dll name here>.dll")]
          >> static extern int myfunc();
          >>
          >> And then call the dll function in managed code.
          >>
          >> Hope this helps.
          >>
          >>
          >> --
          >> - Nicholas Paldino [.NET/C# MVP]
          >> - mvp@spam.guard. caspershouse.co m
          >>
          >> "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
          >> news:%23faLcxE$ FHA.3872@TK2MSF TNGP12.phx.gbl. ..[color=darkred]
          >>>I am at my wits end here. I have a simple project with one global
          >>>function returning a long value.
          >>>
          >>> __declspec(dlle xport) long myfunc()
          >>> {
          >>> }
          >>>
          >>> I've built the dll, added it as a reference to my C# project. Now how
          >>> in the world do I reference "myfunc" ????
          >>> It totally eludes me.
          >>>
          >>> Many thanks for any help here.
          >>>[/color]
          >>
          >>[/color]
          >
          >[/color]


          Comment

          • Tim Greenwood

            #6
            RESOLVED Calling external functions

            AAAHHHHH changed the name of my code file from .cpp to .c removed the
            function prototype and now it finds it....go figure




            "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
            news:%23TVnqSF$ FHA.160@TK2MSFT NGP12.phx.gbl.. .[color=blue]
            > The line
            > //DllExport
            >
            > was not always commented ....
            >
            >
            > "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
            > news:uqTGmQF$FH A.2036@TK2MSFTN GP14.phx.gbl...[color=green]
            >> this should be so simple....here is the code in my simple "C" dll
            >>
            >> #define DllExport __declspec(dlle xport)
            >>
            >> #include <time.h>
            >>
            >> //DllExport
            >>
            >> long gettime(void);
            >>
            >> long gettime()
            >>
            >> {
            >>
            >> time_t tt = time(NULL);
            >>
            >> return (long)tt;
            >>
            >> }
            >>
            >> --------------------------------------------------------------------------
            >> here is the import in my C# app
            >>
            >> [DllImport("ctim e.dll", CallingConventi on=CallingConve ntion.Cdecl)]
            >>
            >> public static extern long gettime();
            >>
            >>
            >>
            >> -------------------------------------------
            >>
            >> I've tried several different CallingConventi ons and nothing same thing
            >> "Unable to find entry point named gettime in dll ctime.dll
            >>
            >>
            >>
            >>
            >>
            >>
            >>
            >>
            >> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
            >> in message news:uzUlXzE$FH A.4088@TK2MSFTN GP09.phx.gbl...[color=darkred]
            >>> Tim,
            >>>
            >>> You should have received an error when you added it to your project.
            >>> If you have a function that is exported by a DLL, you need to call it
            >>> through the P/Invoke layer. If the dll is somewhere in the path where
            >>> LoadLibrary will find it, you can do this:
            >>>
            >>> [DllImport("<dll name here>.dll")]
            >>> static extern int myfunc();
            >>>
            >>> And then call the dll function in managed code.
            >>>
            >>> Hope this helps.
            >>>
            >>>
            >>> --
            >>> - Nicholas Paldino [.NET/C# MVP]
            >>> - mvp@spam.guard. caspershouse.co m
            >>>
            >>> "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
            >>> news:%23faLcxE$ FHA.3872@TK2MSF TNGP12.phx.gbl. ..
            >>>>I am at my wits end here. I have a simple project with one global
            >>>>function returning a long value.
            >>>>
            >>>> __declspec(dlle xport) long myfunc()
            >>>> {
            >>>> }
            >>>>
            >>>> I've built the dll, added it as a reference to my C# project. Now how
            >>>> in the world do I reference "myfunc" ????
            >>>> It totally eludes me.
            >>>>
            >>>> Many thanks for any help here.
            >>>>
            >>>
            >>>[/color]
            >>
            >>[/color]
            >
            >[/color]


            Comment

            • Willy Denoyette [MVP]

              #7
              Re: ARRGGGHH Re: Calling external functions

              In C++ you have to export your functions with "C linkage", that is, you need
              to wrap your functions or prototypes in a C linkage block.

              extern "C"
              {
              __declspec(dlle xport) long gettime();
              ...
              }

              long gettime()
              {
              ...
              }


              Note also that your function declaration in C# has the wrong return type, a
              long in C/C++ is 32 bit a long in C# is 64 bit.

              [C#]
              public static extern int gettime();


              Willy.

              "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
              news:uqTGmQF$FH A.2036@TK2MSFTN GP14.phx.gbl...[color=blue]
              > this should be so simple....here is the code in my simple "C" dll
              >
              > #define DllExport __declspec(dlle xport)
              >
              > #include <time.h>
              >
              > //DllExport
              >
              > long gettime(void);
              >
              > long gettime()
              >
              > {
              >
              > time_t tt = time(NULL);
              >
              > return (long)tt;
              >
              > }
              >
              > --------------------------------------------------------------------------
              > here is the import in my C# app
              >
              > [DllImport("ctim e.dll", CallingConventi on=CallingConve ntion.Cdecl)]
              >
              > public static extern long gettime();
              >
              >
              >
              > -------------------------------------------
              >
              > I've tried several different CallingConventi ons and nothing same thing
              > "Unable to find entry point named gettime in dll ctime.dll
              >
              >
              >
              >
              >
              >
              >
              >
              > "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard .caspershouse.c om> wrote
              > in message news:uzUlXzE$FH A.4088@TK2MSFTN GP09.phx.gbl...[color=green]
              >> Tim,
              >>
              >> You should have received an error when you added it to your project.
              >> If you have a function that is exported by a DLL, you need to call it
              >> through the P/Invoke layer. If the dll is somewhere in the path where
              >> LoadLibrary will find it, you can do this:
              >>
              >> [DllImport("<dll name here>.dll")]
              >> static extern int myfunc();
              >>
              >> And then call the dll function in managed code.
              >>
              >> Hope this helps.
              >>
              >>
              >> --
              >> - Nicholas Paldino [.NET/C# MVP]
              >> - mvp@spam.guard. caspershouse.co m
              >>
              >> "Tim Greenwood" <tim_greenwoo d A-T yahoo D-O-T com> wrote in message
              >> news:%23faLcxE$ FHA.3872@TK2MSF TNGP12.phx.gbl. ..[color=darkred]
              >>>I am at my wits end here. I have a simple project with one global
              >>>function returning a long value.
              >>>
              >>> __declspec(dlle xport) long myfunc()
              >>> {
              >>> }
              >>>
              >>> I've built the dll, added it as a reference to my C# project. Now how
              >>> in the world do I reference "myfunc" ????
              >>> It totally eludes me.
              >>>
              >>> Many thanks for any help here.
              >>>[/color]
              >>
              >>[/color]
              >
              >[/color]


              Comment

              Working...