用vs2008新建立了一个Solution2的解决方案,新建完这后,添加一个windows窗体应用程序(c++的)Test1
这后在添加一个名字为LR500的类库(也是c++的
在默认生成的文件LR500.h中的加入如下代码:
C/C++ code
// LR500.h
#pragma once
#ifndef LR500
#define LR500
using namespace System;
namespace LR500 {
    public ref class Class1
    {
        // TODO: 在此处添加此类的方法。
    public:
        Class1();
        Class1(int index);
    private:
        int _index;
    };
}
#endif
在LR500.cpp文件里加入如下代码:
C/C++ code
// 这是主 DLL 文件。
#include "stdafx.h"
#include "LR500.h"
LR500::Class1::Class1()
{
    this->_index = 0;
}
LR500::Class1::Class1(int index)
{
    this->_index = index;
}
之后我在Test1项目里添加对LR500类库的引用,在Form1的Load事件里加入如下代码:
C/C++ code
    private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
                 LR500::Class1 cls1(2);
             }
编译程序通不过,但是我把LR500.h中的
C/C++ code
[color=#FF0000]#ifndef LR500
#define LR500
...
#endif[/color]
去掉,就能运行成功,有谁知道为什么吗?建的是C++/CLI这个微软的C++变种。
用的人不多