【问题标题】:Not seeing my DLL function in test application在测试应用程序中看不到我的 DLL 函数
【发布时间】:2015-03-11 06:17:49
【问题描述】:

我正在测试一个简单的 DLL,我使用 C++/CLI 在 CLR 控制台应用程序中编写。 DLL 只有一个我正在尝试使用的功能。我正在引用 DLL 并在项目属性页中设置 Resolve #using Reference,但我看不到我编写的函数。我猜我可能在某处错过了访问修饰符,但我不确定。这是我的代码的细分:

DLL 代码头:

// LogDLL.h

#pragma once
#using <mscorlib.dll>
using namespace System;


namespace LogDLL {

    public ref class LogFuncs
    {
        // TODO: Add your methods for this class here.
        LogFuncs(){;};
        ~LogFuncs(){;};
        void log_to_file ( System::String ^file, bool overwrite, System::String ^text );
    };
}

DLL 代码来源:

#include "stdafx.h"
#include "LogDLL.h"
using namespace System::Globalization;

void LogDLL::LogFuncs::log_to_file ( System::String ^file, bool overwrite, System::String ^text )
{
    //Do Stuff
}

以及我正在使用的测试代码:

#include "stdafx.h"
#using <LogDLL.dll>
using namespace System;

int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");
    LogDLL::LogFuncs^ a;
    a::LogDLL::LogFuncs:: //<-- Intellisense doesn't show the function from the DLL
    return 0;
}

同样,我不确定我错过了什么。自从我使用 C++/CLI 以来已经有一段时间了,所以我很生疏。

更新:

我继续按照 Peter 的建议将类更改为结构。

修改后的 DLL 头代码:

// LogDLL.h

#pragma once
#using <mscorlib.dll>
using namespace System;


namespace LogDLL {

    public ref struct LogFuncs
    {
        // TODO: Add your methods for this class here.
        LogFuncs(){;};
        ~LogFuncs(){;};
        void log_to_file ( System::String ^file, bool overwrite, System::String ^text );
    };
}

我仍然不明白的是,为什么即使我将其指定为公共,该类仍会默认为私有。出现这种情况有什么根本原因吗?如果我使用非托管 C++ 会有什么不同吗??

【问题讨论】:

    标签: .net visual-c++ c++-cli


    【解决方案1】:

    类的默认访问是私有的。为您想要公开的成员添加“public:”或更改为默认访问公共的 ref 结构。

    关于 IntelliSense,我假设您使用的是 Visual Studio 2005 或 Visual Studio 2008; Visual Studio 2010 不支持 C++/CLI 代码的 IntelliSense(这是因为解析器已被 EDG 替换,并且他们没有为 2010 版本改进 C++/CLI 解析功能)。

    我怀疑 IntelliSense 会自动完成您正在使用的语法。你会想要 "a->" 代替(当然,在运行此代码之前 gcnew 它)。

    【讨论】:

    • 谢谢彼得,我把它改成了 ref struct 就成功了。
    • 对于您的后续问题,类本身的“公共”是指类型的可见性,而不是其成员的可见性。如果您只有“ref class LogFuncs”,则无法在其自己的程序集之外访问它。因此,要以您希望的方式在另一个程序集中访问成员,它需要是公共类型(忽略 InternalsVisibleToAttribute)并且成员本身需要是公共的。
    • 附加注释可能会消除混淆:C++/CLI 试图将尽可能多的 C++ 融合到 CLI(公共语言基础结构)的概念中。因此,在 C++/CLI 中有两种声明 CLI 类类型的方法,“ref class”和“ref struct”。前者的行为类似于 C++ 类:成员的默认私有访问。后者的行为类似于 C++ 结构:成员的默认公共访问。 “值类”和“值结构”也是如此,除了它们声明了 CLI 值类型。
    猜你喜欢
    • 2017-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    相关资源
    最近更新 更多