网上看了半天,找到的资料要么太老,要么不对,干脆自己调试一遍。下面把体会说一说。
调试环境:visual studio 2010 .
时间:2012.12.20
一、C#写的类库:
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
}
// C#程序配置,一定是类库

二、C++ 程序
共有三个程序文件
(1) 主程序
// test1.cpp : 定义控制台应用程序的入口点。
//
///
//
//
//
#include "stdafx.h"
#include "yotopcompany.h"
#using"..\ClassLibrary1\bin\Debug\ClassLibrary1.dll"
using namespaceClassLibrary1;
int _tmain(int argc, _TCHAR* argv[])
{
}
(2) c++ 中自己编写的一个类
// yotopCompany.h
#pragma once
ref class YotopCompany
{
public:
};
(3) c++编写的类的CPP文件
//yotopcompany.cpp
#include "StdAfx.h"
#include "YotopCompany.h"
YotopCompany::YotopCompany(void)
{
}
YotopCompany::YotopCompany(char*_name ,char* _address,char*_phoneNumber)
{
}
(4) c++ 程序配置

三、如果还有疑问,请参考 MSDN 文章:
// How to call a managed DLL from native Visual C++ code in VisualStudio.NET or in Visual Studio 2005
// 如何在 Visual Studio.NET 或 Visual Studio 2005 中的本机 Visual C++代码中调用托管的 DLL
//
四、总结:
1)用C#写任何的类库
2)C++ 中要引用此类库
3)创建C#对象时要用gcnew ;
4) C++ 编译设置一定设置为:支持 公共语言运行时支持(/clr)
4) 自身的C++类要用 ref class 定义。