开发者

Creating a C++ DLL and then using it in C#

Ok I'm trying to make a C++ DLL that I can then call and reference in a c# App. I've already made a simple dll using the numberous guides out there, however when I try to reference it in the C# app I get the error

Unable to load DLL 'SDES.dll': The specified module could not be found.

The code for the program is as follows (bear with me I'm going to include all the files)

//These are the DLL Files.

#ifndef TestDLL_H
#define TestDLL_H

    extern "C"
    {
        // Returns a + b
        __declspec(dllexport) double Add(double a, double b);

        // Returns a - b
        __declspec(dllexport) double Subtract(double a, double b);

        // Returns a * b
        __declspec(dllexport) double Multiply(double a, double b);

        // Returns a / b
        // Throws DivideByZeroException if b is 0
        __declspec(dllexport) double Divide(double a, double b);
    }

#endif

//.cpp
#include "test.h"

#include <stdexcept>

using namespace std;

    extern double __cdecl Add(double a, double b)
    {
        return a + b;
    }

    extern double __cdecl Subtract(double a, double b)
    {
        return a - b;
    }

    extern double __cdecl Multiply(double a, double b)
    {
        return a * b;
    }

    extern double __cdecl Divide(double a, double b)
    {
        if (b == 0)
        {
            throw new invalid_argument("b cannot be zero!");
        }

        return a / b;
    }

//C# Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("SDES.dll")]
        public static extern double Add(double a, double b);
        static void Main(string[] args)
        {
            Add(1, 2); //Error here...
        }
    }
}

Anyone have any idea's what I may be missing i开发者_如何学Cn my program? Let me know if I missed some code or if you have any questions.


Download the Dependecy Walker and open your SDES.dll. Check if all the dependent DLLs can be loaded. If you see a missing dependence, put that dll in the target directory, too.

Are you using a 64-bit system? If yes, you should target your C# and C++ to the same architecture (both 32 or both 64 bit).

I just tested your functions and it worked out.

    [DllImport("Native_CPP.dll", CallingConvention=CallingConvention.Cdecl)]
    public static extern double Add(double a, double b); 

    static void Main(string[] args)
    {
        Console.WriteLine(Add(1.0, 3.0));

        Console.ReadLine();
    }

Output:

4

This is what I did with Visual Studio 2010:

  • create a new Solution

  • create a new C# project

  • create a new C++-Dll project (without MFC and other stuff)

  • copy paste your header and cpp-files

  • build the C++-Dll

  • copy the DLL to the Debug/Release (depends on what you are using) directory of your C# project (usually "Solution/CSharpProjectName/bin/Debug/" or "Solution/CSharpProjectName/bin/Release/", respectively)

  • add this P/Invoke signature to the C# file:

    [DllImport("Native_CPP.dll", CallingConvention=CallingConvention.Cdecl)] public static extern double Add(double a, double b);

    Note: I had to pass the parameter CallingConvention.Cdecl, otherwise I got an exception.

  • Run the C#-Project as shown above

P.S: I didn't have to set the architectures. It just worked. (I'm using a x64 machine, with a 64-bit OS.)


put SDES.dll to the folder where the C# .exe is placed.


Where is SimulateGameDLL defined? I dont see it in the C/C++ code.


it means when it tried to load the dll, it could not find it!

http://msdn.microsoft.com/en-us/library/ms682586(VS.85).aspx

most likely its not in the same directory as your C# app.

also, good time to mention

http://www.dependencywalker.com/

helps find DLL dependency problems.


I just compiled the code, and got no error. Here is my command lines.

> cl /LD test.cpp -o SDES.dll
> csc test.cs
> test.exe
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜