简体   繁体   中英

Use C CLR::Class Library in C#

I am trying to get some C++ code working under C#. I have been trying to follow tutorials online all over the place without any luck compiling or executing the code.

I've written a basic C++ class that compiles to a .dll, and a C# console application. - I've changed the platform toolset to v100 on the C++ dll - I've added a reference to the dll in my C# application

Note I am trying to use a C++ class, not C++ static functions... on to the code!

// CTrans.h
#pragma once

#include "windows.h"

using namespace System;

namespace CTrans {

    public ref class CTransClass
    {
    public:
    System::String^ helloWorld();
    };
}

// CTrans.cpp
// This is the main DLL file.
#include "stdafx.h"
#include "CTrans.h"

String^ CTrans::CTransClass::helloWorld()
{
    return gcnew System::String("Hello World!");
}

// Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CTWrapper
{
    class Program
    {
        static void Main(string[] args)
        {
            unsafe
            {
                CTrans.CTransClass trans = new CTrans.CTransClass();
                String tmp = trans.helloWorld();
            }
        }
    }
}

Error: http://sumarlidason.com/Capture.PNG

edit: removed niceties, add screenshot of error.

Did you try adding a "using CTrans;" to the top of the Program.cs using block?

Check to make sure the assemblies are compiled with the same architecture (x86/x64), and that there are no other related warnings in the Error List.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM