1

on a previous post, I requested about conversion from C++ to C#:

Calling C from C#

Than I searched a lot and found a lot of similar tutorial and I implement the following code.

In my first C++ project, it is the place that I will eventually implement all my usb methode but for now I got the following code:

in my header file (hpp) , I got the following code:

#pragma once
#define DLLEXP   __declspec( dllexport )
#include <string>
namespace test{
DLLEXP std::string helloworld(void);
};

The implementation in the cpp is the following:

#include "test.hpp"

std::string test::helloworld(void){
return "Hello WORLD from DLL";
 }

So it is just a simple hello world to test if the code is correctly import into the c#.

Than I made what is called a "wrapper" from a library v C++ project, I don;t know all the detail about that (from my previous post I am an hardware guy) but I get the following code in my header:

// wrapper.h

  #pragma once
  #include "Stdafx.h"

  using namespace System;

namespace wrapper {

public ref class wrapperclass
{
    public:
        wrapperclass(){ husb = 0; };
        std::string printhello(void);
    private:
        int husb;
};
}

I include my test.hpp in the stdafx.h

#pragma once
#include "test.hpp"

Finaly, this is my implementation into the Cpp file:

#include "stdafx.h"

#include "wrapper.h"

using namespace test;

namespace wrapper{
std::string wrapperclass::printhello(void){
    std::string message = test::helloworld();
    return message;
};
};

Everything compile just fine and I am able to import the wrapper dll into a c# project. Here my code that I got in C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using wrapper;
namespace testfinal
{
class Program
{
    static void Main(string[] args)
    {
        wrapperclass wc = new wrapperclass();
        wc.printhello();
    }
}
}

Than I get 2 thing from the compiler:

  1. [error] I cannot use printhello because the compiler say that it is not supported by the language

  2. [warning] "There was a mismatch between the processor architecture of the project being built "MSIL" abd the processor architecture of the reference "Wrapper" ( imported DLL). This mismatch may cause runtime failures. Please consider changing the targeted processor architecture of you project through the configuration manager so as to align the processor architectures between your project and references or a take a dependency on referenes with a processor architecture that matches the targeted processor architecture of your project.

So my question, Why can`t i call the printhello method from the wrapper class? I am pretty sure it is linked to the warning message but I am not what his meaning is. Is it reliate to language architecture

[EDIT]

I resolve the architecture problem with the following post: How do I fix the Visual Studio compile error, "mismatch between processor architecture"?

[EDIT-#2]

I achieve to advance a little more. Now my wrapper header is

#pragma once
#include "Stdafx.h"
#include "msclr\marshal_cppstd.h"

 using namespace System;
 using namespace msclr::interop;


namespace wrapper {

public ref class wrapperclass
{
    public:
        wrapperclass(){ husb = 0; };
        String^ printhello(void);
    private:
        int husb;
};
}

and my cpp is :

#include "stdafx.h"

#include "wrapper.h"

 using namespace test;

 namespace wrapper{
String^ wrapperclass::printhello(void){
    String^ message = marshal_as<String^>( test::helloworld());
    return message;
};
};

The c# code is compiling now, but when I try to run the code, Visual studio tell me

"An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll"

Community
  • 1
  • 1
MathieuL
  • 155
  • 7
  • 4
    You can't return a C++ `std::string`, instead you need to marshal the data to .Net types (`System::String^`). – crashmstr Aug 05 '15 at 15:51
  • @crashmstr do you have a tutorial on that? – MathieuL Aug 05 '15 at 15:52
  • Or probably use C string as middle ground. – Cem Kalyoncu Aug 05 '15 at 15:55
  • Maybe this will help: https://msdn.microsoft.com/en-us/library/aa288468%28v=vs.71%29.aspx?f=255&MSPPError=-2147217396 – 1.618 Aug 05 '15 at 16:00
  • CLR managed memory is in its own heap. You will need to manually marshal bits back and forth between the heaps if you want to interop. This is not cheap. – whoisj Aug 05 '15 at 16:07
  • 1
    @MathieuL [Overview of Marshaling in C++](https://msdn.microsoft.com/en-us/library/bb384865.aspx) and [marshal_as](https://msdn.microsoft.com/en-us/library/bb384859.aspx) as well as [convert from std::string to String^](http://stackoverflow.com/questions/13718188/convert-from-stdstring-to-string). I'm sure you can also find other related questions here. – crashmstr Aug 05 '15 at 16:25
  • The "file not found" error could be because the C++ dll is not in the probing path of the C# application. First test should be to copy all the binaries and assemblies into same folder and try run it from there. Following on that, there are a few .Net runtime hooks that you can add handlers to that can assist in finding what file is missing. – Niall Aug 05 '15 at 19:10
  • 1
    Re: your latest update - .Net is *horrible* about this as it obscures any useful message, but it probably can't find your C++ dll. Make sure you copy it to the output folder! – crashmstr Aug 05 '15 at 19:11
  • You really need just one DLL. The mixed mode C++/CLI assembly. – David Heffernan Aug 05 '15 at 19:55
  • @crashmstr I got my wrapper dll into the debug file of my final test project – MathieuL Aug 06 '15 at 15:30
  • @DavidHeffernan Yeah, I reference my wrapper dll only but Visual Studio seem to not be able to find the file – MathieuL Aug 06 '15 at 15:30
  • @yms Well I build the native and wrapper dll but in the c# project I only reference to the wrapper dll – MathieuL Aug 06 '15 at 15:31
  • @yms I build my cpp file into win32 and the C# is build into x86. Can it cause any problem – MathieuL Aug 06 '15 at 15:57

0 Answers0