-2

After a long reading time I didn't get the dll working...
I tried so much different ways but no way worked..

I did the following things: (IDE: VS2013Ultimate)

  1. I added a clean c++ project. There I added 1 header file [header.h]:

    #pragma once
    
    class myClass{
    public:
        myClass(double varx, double vary);
        double sumxy();
    private:
        double x;
        double y;
    };
    
  2. I added a body.cpp file:

    #pragma once
    #include "header.h"
    
    myClass::myClass(double varx, double vary){
        x = varx;
        y = vary;
    }
    
    double myClass::sumxy(){
        return x + y;
    }
    

    That's all the code I would need. I only want a working example code.

  3. I added one class [main.cpp]:

    #include "header.h"  
    #include "body.cpp"  
    
    extern "C" __declspec(dllexport) double sumxy(double var_x, double var_y){
        myClass MC(var_x, var_y);
    
        return MC.sumxy();
    }
    
  4. After this I compiled this dll and I got it without any compile errors. `I copied it to the debug folder of the c# Console Application

  5. C# Console Application:

    using System.Runtime.InteropServices;
    //all using directories
    
    namespace Klassen_Tester {
        class Program {
            [DllImport("CppClassDll.dll")]
            public static extern double sumxy(double var_x, double var_y);
    
            static void Main(string[] args) {
                Console.WriteLine(sumxy(3, 5).ToString());
    
                Console.ReadLine();
            } 
        }
    }
    

Please help me. Don't know what to do.. And sorry for my bad english.

Edit: There is an error: System.DllNotFoundException in Klassen_Tester.exe. DLL "CppClassDll.dll" could not be found. HRESULT: 0x8007007E) could not be load.

Leorus
  • 1
  • 2
  • 3
    http://stackoverflow.com/help/formatting – 001 Aug 20 '15 at 19:41
  • First of all, **never** use absolute paths in a `DllImport` attribute. Put `CppClassDll.dll` in the same directory than the C# console exe, and use `[DllImport("CppClassDll.dll")]`. Same thing about `#include`. The isue is that `\U` for instance in a string gets transleted to `U` (the backspace is the escaping character and is ignored in front of a character that does not need escaping) – Lucas Trzesniewski Aug 20 '15 at 19:44
  • Yeah i did this before... But i didnt work also.. so i searched other examples.. this is the last one i found.. – Leorus Aug 20 '15 at 19:45
  • I did it all in one Project (c++ side) then i compiled it and copied into the debug path of c# project and then i changed the path of dll to the only string and then i started the C# project again and it says: "System.DllNotFoundException" Couldnt load dll – Leorus Aug 20 '15 at 19:49
  • Is a DllMain required for C# interop? – 001 Aug 20 '15 at 19:52
  • It could be that it is required.. I dont know, thats why i am asking.. Its the first dll i am writing.. – Leorus Aug 20 '15 at 19:55
  • Is your C++ library [manifested](http://stackoverflow.com/questions/10774250/dllnotfoundexception-with-hresult-0x8007007e-when-loading-64-bit-dll#comment14010912_10774451) @Leorus? – GSerg Aug 20 '15 at 20:00
  • How could i check it? @GSerg – Leorus Aug 20 '15 at 20:04
  • If you click that link and read the next comment, it asks just that, and then the next comment answers it. – GSerg Aug 20 '15 at 20:04
  • Generate Manifest "Yes(/MANIFEST)" <- do you mean this? – Leorus Aug 20 '15 at 20:06
  • Yes, I mean that. So if it's not the manifest, then use ProcMon to see exactly what is not found, [as suggested](http://stackoverflow.com/a/10774451/11683). – GSerg Aug 20 '15 at 20:08
  • I installed procmon but how is it to handle? – Leorus Aug 20 '15 at 20:12
  • Start it, provide a condition that matches your program's process, run your program, find relevant errors in the huge list procmon will generate (most likely will be related to some flavour of CreateFile). – GSerg Aug 20 '15 at 20:21
  • Could you help me please via teamviewer >. – Leorus Aug 20 '15 at 20:25

1 Answers1

0

You might consider using an interop between C++ and C# so you don't have to mess with pinvoke. I'm not saying pinvoke is bad but interops are nice.

You are going to want to write a CLR wrapper to act as an intermediary between the managed and unmanaged code. It looks like you have the C# and C++ mostly written so I'll describe the CLR portion.

  1. Create a CLR project in Visual Studio
  2. Add your native C++ project as a reference
  3. Write your wrapper
  4. Add reference to CLR project from C# project
  5. Call CLR wrapper

Create a class with your C++ signatures likes this:

namespace YourWrapper
{

    public ref class WrappedFunction
    {

        public:

            // Adjust according to your c++ class and function
            double sumxy(double a, double b) {
                return myClass::sumxy(a, b);
            }       
    };

}

For a full example, see my project https://github.com/corytodd/interop-example:

cory.todd
  • 516
  • 5
  • 14