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. How to call unmanaged C++ DLL in C#.NET

How to call unmanaged C++ DLL in C#.NET

Scheduled Pinned Locked Moved C#
csharpc++designdebugginghelp
5 Posts 5 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.
  • P Offline
    P Offline
    Pranav Thakur
    wrote on last edited by
    #1

    Hi, I have coded few methods in C++ language. These methods are to be used in C#.NET . I guess I need to convert the .CPP file(C++ code) into DLL and then import the methods in C#. I made a Win32 project and converted the exe(C++ code) into unmanaged C++ DLL. Then I tried to call its method in C#.NET as : using System.Runtime.InteropServices; public partial class CDLL : System.Web.UI.Page { [DllImport("C:\\Project1\\TestProject\\Debug\\TestProject.dll", EntryPoint = "testMethod", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern void testMethod(); protected void Page_Load(object sender, EventArgs e) { testMethod(); } } After running the above code i am getting error like : Unable to find an entry point named 'testMethod' in DLL 'C:\Project1\TestProject\Debug\TestProject.dll'. Can any one suggest how to run the above code successfully. Thanks.

    C L N 3 Replies Last reply
    0
    • P Pranav Thakur

      Hi, I have coded few methods in C++ language. These methods are to be used in C#.NET . I guess I need to convert the .CPP file(C++ code) into DLL and then import the methods in C#. I made a Win32 project and converted the exe(C++ code) into unmanaged C++ DLL. Then I tried to call its method in C#.NET as : using System.Runtime.InteropServices; public partial class CDLL : System.Web.UI.Page { [DllImport("C:\\Project1\\TestProject\\Debug\\TestProject.dll", EntryPoint = "testMethod", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern void testMethod(); protected void Page_Load(object sender, EventArgs e) { testMethod(); } } After running the above code i am getting error like : Unable to find an entry point named 'testMethod' in DLL 'C:\Project1\TestProject\Debug\TestProject.dll'. Can any one suggest how to run the above code successfully. Thanks.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Pranav Thakur wrote:

      After running the above code i am getting error like : Unable to find an entry point named 'testMethod' in DLL 'C:\Project1\TestProject\Debug\TestProject.dll'. Can any one suggest how to run the above code successfully.

      Write a dll that exports testMethod, that's what the error is telling you. I would also not use full paths, just let the dll be in the same dir as the exe.

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      1 Reply Last reply
      0
      • P Pranav Thakur

        Hi, I have coded few methods in C++ language. These methods are to be used in C#.NET . I guess I need to convert the .CPP file(C++ code) into DLL and then import the methods in C#. I made a Win32 project and converted the exe(C++ code) into unmanaged C++ DLL. Then I tried to call its method in C#.NET as : using System.Runtime.InteropServices; public partial class CDLL : System.Web.UI.Page { [DllImport("C:\\Project1\\TestProject\\Debug\\TestProject.dll", EntryPoint = "testMethod", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern void testMethod(); protected void Page_Load(object sender, EventArgs e) { testMethod(); } } After running the above code i am getting error like : Unable to find an entry point named 'testMethod' in DLL 'C:\Project1\TestProject\Debug\TestProject.dll'. Can any one suggest how to run the above code successfully. Thanks.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        See your DLL first with this tool : http://www.nirsoft.net/utils/dll_export_viewer.html[^] Probably the entry point of that function is not only "testMethod", but it contains more char in it. For example : @.!testMethod..,,"" or something like that

        1 Reply Last reply
        0
        • P Pranav Thakur

          Hi, I have coded few methods in C++ language. These methods are to be used in C#.NET . I guess I need to convert the .CPP file(C++ code) into DLL and then import the methods in C#. I made a Win32 project and converted the exe(C++ code) into unmanaged C++ DLL. Then I tried to call its method in C#.NET as : using System.Runtime.InteropServices; public partial class CDLL : System.Web.UI.Page { [DllImport("C:\\Project1\\TestProject\\Debug\\TestProject.dll", EntryPoint = "testMethod", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)] public static extern void testMethod(); protected void Page_Load(object sender, EventArgs e) { testMethod(); } } After running the above code i am getting error like : Unable to find an entry point named 'testMethod' in DLL 'C:\Project1\TestProject\Debug\TestProject.dll'. Can any one suggest how to run the above code successfully. Thanks.

          N Offline
          N Offline
          Nicholas Butler
          wrote on last edited by
          #4

          C++ mangles names. Declare your exported C++ functions like this:

          extern "C"
          {
          __declspec(dllexport) int Function( ... );
          }

          Nick

          ---------------------------------- Be excellent to each other :)

          S 1 Reply Last reply
          0
          • N Nicholas Butler

            C++ mangles names. Declare your exported C++ functions like this:

            extern "C"
            {
            __declspec(dllexport) int Function( ... );
            }

            Nick

            ---------------------------------- Be excellent to each other :)

            S Offline
            S Offline
            suzalinda
            wrote on last edited by
            #5

            I have a dll file. The dll file has functions like that "typedef short apiStatus; apiStatus __declspec(dllexport) __stdcall DrfCommOpen (HANDLE * hCom, char *com_port);" how i can call and use this function in my c#.net application? thanks in advance.. regards..

            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