C#调用C++编写的dll库

我用vs2008建的C++ dll类库,名字test_interopVC,和C#的CeshiVC项目

一:C++项目dll类库:

1.test_interopVC.h,主要简单的测试,所以就在一个类里直接定义了一个常量a=9;

#pragma once
using namespace System;
using namespace System::Runtime::InteropServices;
#include<stdlib.h>
#include<memory.h>
namespace test_interopVC
{

    public ref class VcClass
	{
	public:
		 static const int a=9;
	};
}

2.test_interopVC.cpp

// test_interopVC.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"

#include"test_interopVC.h" //这行要加上,要不会出错的。。

二:C#中program中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;


using test_interopVC;
namespace CeshiVC
{
    class Program
    {
        [DllImport("kernel32.dll")]
        public static extern bool SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);

        static void Main(string[] args)
        {
            //VcClass b = new VcClass();

            System.Console.WriteLine(VcClass.a.ToString());
          
        }
    }
}

三。配置:当缺少这一步,也是不能正常访问的

   1。要解决方案中第一级中debug中的dllcopy到C#项目中bin/debu中去。

   2.请按照下图红色矩形框内进行修改。并保存运行。

   

  3。运行结果。

        在dos命令窗口输出C++类库中的常量a=9;

如果大家有问题的话可以给我留言,或联系我QQ:987514881,共同学习交流

原文地址:https://www.cnblogs.com/nanyangzp/p/3428422.html