on a previous post, I requested about conversion from C++ to 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:
[error] I cannot use printhello because the compiler say that it is not supported by the language
[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"