calling from c# into a dll

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Al the programmer

    calling from c# into a dll

    I have a dll that consists of unmanaged C++ routines. I can call a function
    in the dll from a C++ executable, but not from a C# executable. I am calling
    an independent function, not a class. C# gives me a runtime exception saying
    the entry point to the dll cannot be found.

    I declare the function in C# with the following lines:
    [DllImport("simp leDLL.dll", CallingConventi on=CallingConve ntion.Cdecl)]
    public static extern int fnsimpleDLL(int i);

    I then call the function with the line:
    fnsimpleDLL(1);

    Any ideas?
  • Willy Denoyette [MVP]

    #2
    Re: calling from c# into a dll

    Make sure the exported C function is not mangled (has C linkage), make also
    sure the calling convention is cdecl (CallingConvent ion.Cdecl)]).

    Willy.

    "Al the programmer" <Altheprogramme r@discussions.m icrosoft.com> wrote in
    message news:1466DA50-01F4-4B7A-9324-C81AD4A5417D@mi crosoft.com...[color=blue]
    >I have a dll that consists of unmanaged C++ routines. I can call a
    >function
    > in the dll from a C++ executable, but not from a C# executable. I am
    > calling
    > an independent function, not a class. C# gives me a runtime exception
    > saying
    > the entry point to the dll cannot be found.
    >
    > I declare the function in C# with the following lines:
    > [DllImport("simp leDLL.dll", CallingConventi on=CallingConve ntion.Cdecl)]
    > public static extern int fnsimpleDLL(int i);
    >
    > I then call the function with the line:
    > fnsimpleDLL(1);
    >
    > Any ideas?[/color]


    Comment

    • Al the programmer

      #3
      Re: calling from c# into a dll

      Thanks for the info. I remember the mangling issue from when I had to call C
      code from C++, but the C# docs are silent on the issue.

      What do I do when I want to export a C++ class in a dll to a C# program?

      Al


      "Willy Denoyette [MVP]" wrote:
      [color=blue]
      > Make sure the exported C function is not mangled (has C linkage), make also
      > sure the calling convention is cdecl (CallingConvent ion.Cdecl)]).
      >
      > Willy.
      >
      > "Al the programmer" <Altheprogramme r@discussions.m icrosoft.com> wrote in
      > message news:1466DA50-01F4-4B7A-9324-C81AD4A5417D@mi crosoft.com...[color=green]
      > >I have a dll that consists of unmanaged C++ routines. I can call a
      > >function
      > > in the dll from a C++ executable, but not from a C# executable. I am
      > > calling
      > > an independent function, not a class. C# gives me a runtime exception
      > > saying
      > > the entry point to the dll cannot be found.
      > >
      > > I declare the function in C# with the following lines:
      > > [DllImport("simp leDLL.dll", CallingConventi on=CallingConve ntion.Cdecl)]
      > > public static extern int fnsimpleDLL(int i);
      > >
      > > I then call the function with the line:
      > > fnsimpleDLL(1);
      > >
      > > Any ideas?[/color]
      >
      >
      >[/color]

      Comment

      • Willy Denoyette [MVP]

        #4
        Re: calling from c# into a dll


        "Al the programmer" <Altheprogramme r@discussions.m icrosoft.com> wrote in
        message news:017E109C-8812-44AB-B950-E1391EED9DBD@mi crosoft.com...[color=blue]
        > Thanks for the info. I remember the mangling issue from when I had to
        > call C
        > code from C++, but the C# docs are silent on the issue.
        >
        > What do I do when I want to export a C++ class in a dll to a C# program?
        >
        > Al
        >
        >[/color]

        Check this....


        Willy.


        Comment

        • Al the programmer

          #5
          Re: calling from c# into a dll

          Thanks for the info. The docs don't talk about importing a class, only
          functions, but I know where to start looking in the future. For my current
          project I need to access C functions, which I can do.

          Al

          "Willy Denoyette [MVP]" wrote:
          [color=blue]
          >
          > "Al the programmer" <Altheprogramme r@discussions.m icrosoft.com> wrote in
          > message news:017E109C-8812-44AB-B950-E1391EED9DBD@mi crosoft.com...[color=green]
          > > Thanks for the info. I remember the mangling issue from when I had to
          > > call C
          > > code from C++, but the C# docs are silent on the issue.
          > >
          > > What do I do when I want to export a C++ class in a dll to a C# program?
          > >
          > > Al
          > >
          > >[/color]
          >
          > Check this....
          > http://msdn2.microsoft.com/library/e59b22c5.aspx
          >
          > Willy.
          >
          >
          >[/color]

          Comment

          • Willy Denoyette [MVP]

            #6
            Re: calling from c# into a dll


            "Al the programmer" <Altheprogramme r@discussions.m icrosoft.com> wrote in
            message news:66BAFFCC-886E-435D-BC81-BA018CFE528F@mi crosoft.com...[color=blue]
            > Thanks for the info. The docs don't talk about importing a class, only
            > functions, but I know where to start looking in the future. For my
            > current
            > project I need to access C functions, which I can do.
            >
            > Al
            >[/color]

            It doesn't talk about importing a class because it isn't possible to access
            unmanaged classes cross-language.
            If you need this, your only option is to create a wrapper class using
            managed C++ (or better the upcoming C++/CLI)., this wrapper simply delegates
            the accesses from managed code into unmanaged C++.

            Willy.


            Comment

            Working...