none
Caling a C++ function from C# RRS feed

  • Question

  • Hi,

    I have made a small sample application in C++ just to test out the DLL Export/Import stuff. The dll is built from Visual C++ project. Code:

    #include "stdafx.h"
    #include "DLLFunctionExposeTest.h"
    
    
    extern "C" DLLFUNCTIONEXPOSETEST_API int fnSumofTwoDigits(int a, int b)
    {
        return a + b;
    }

    C# code:

    using System.Runtime.InteropServices;
    namespace DLLImportTest
    {
        public partial class MainWindow : Window
        {
            int e = 3, f = 4;
            public MainWindow()
            {
                InitializeComponent();
                int g = fnSumofTwoDigits(e, f); //exception
            }
        [DllImport("DLLFunctionExposeTest.dll")]
        private static extern int fnSumofTwoDigits(int a, int b);
    }
    }

    When I try to run this application, I get an error saying - XamlParseException was unhandled in my C# WPF application

    The invocation of the constructor on type 'DLLImportTest.MainWindow' that matches the specified binding constraints threw an exception. On handling the exception, this is the prompt:

    The try catch block on the Initialize Component gives the following detail of the exception. "Unbale to find entrypoint named fnSumofTwoDigits in the DLL".

    The c++ functions that are here are not any class instance function but global. Also, prepended to these functions is extern "C", but the problem is still there. Any solutions?

    • Moved by Leo Liu - MSFT Friday, February 17, 2012 6:23 AM Moved for better support. (From:Visual C# General)
    Thursday, February 16, 2012 6:02 PM

Answers

  • Given the DumpBin listing the exported name is fnSumofTwoDigits.

    Are you sure that the (C++) binary you're building is in the same directory as the WPF application?  It sounds like the binary you're building isn't the one that is actually being run.  Did you run Dumpbin against the binary that was in the WPF output directory?

    The ILT is throwing me off though.  Are you compiling your C++ code as native x86?  If so are you also compiling your WPF as xx86?  If not then you're going to have a problem because I see you're running an x64 OS.  That means that if you are using Any CPU for your WPF app then you are running an x64 process.  Thus your C++ binary also needs to be x64.  Dumpbin seems to be indicating that maybe your native DLL is x64.

    Michael Taylor - 2/17/2012
    http://msmvps.com/blogs/p3net

    • Marked as answer by optimus_prime Monday, February 20, 2012 6:23 AM
    Friday, February 17, 2012 2:38 PM
    Moderator

All replies

  • You need to make sure that your macro (DLLFUNCTIONEXPOSETEST_API ) is properly exporting the function (ie: mapping to __declspec(dllexport) ).

    In addition, you likely need to import this using cdecl:

    [DllImport("DLLFunctionExposeTest.dll", CallingConvention = CallingConvention.Cdecl)]



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, February 16, 2012 6:13 PM
    Moderator
  • The entry point is wrong.  When exporting a "C" function the name always begins with an underscore.  The exported name (confirm by using Dependency Viewer or equivalent) is going to be _fnSumOfTwoDigits probably.  The only convention that doesn't mangle the name in some way is Pascal IIRC.

    Michael Taylor - 2/16/2012
    http://msmvps.com/blogs/p3net

    Thursday, February 16, 2012 6:38 PM
    Moderator
  • @Reed Copsey Jr:

    I am using the default template for Win32 DLL Project with Export symbol checked. So, that gives me a default template to work with.

    DLLFUNCTIONEXPOSETEST_API is actually implemented as:

    #ifdef DLLFUNCTIONEXPOSETEST_EXPORTS
    #define DLLFUNCTIONEXPOSETEST_API __declspec(dllexport)
    #else
    #define DLLFUNCTIONEXPOSETEST_API __declspec(dllimport)
    #endif

    I have added the CallingConvetion to the DLL attribute as well, but that still is giving the same error.

    Friday, February 17, 2012 4:46 AM
  • @CoolDadTx: Tried changing the function names beginning with _ (underscore) as well, but still giving the same error.

    Btw, this is the default template that I got in VS for DLL when selected a new project -> Visual C++ -> Win32 -> DLL (Export symbols option checked).

    Here is the dumpbin \Exports screenshot for the DLL

    Screenshot fo dumpbin
    Friday, February 17, 2012 4:49 AM
  • Given the DumpBin listing the exported name is fnSumofTwoDigits.

    Are you sure that the (C++) binary you're building is in the same directory as the WPF application?  It sounds like the binary you're building isn't the one that is actually being run.  Did you run Dumpbin against the binary that was in the WPF output directory?

    The ILT is throwing me off though.  Are you compiling your C++ code as native x86?  If so are you also compiling your WPF as xx86?  If not then you're going to have a problem because I see you're running an x64 OS.  That means that if you are using Any CPU for your WPF app then you are running an x64 process.  Thus your C++ binary also needs to be x64.  Dumpbin seems to be indicating that maybe your native DLL is x64.

    Michael Taylor - 2/17/2012
    http://msmvps.com/blogs/p3net

    • Marked as answer by optimus_prime Monday, February 20, 2012 6:23 AM
    Friday, February 17, 2012 2:38 PM
    Moderator
  • Doh! It was the wrong path. :-/
    Monday, February 20, 2012 6:24 AM