在测试应用程序中没有看到我的 DLL 函数

发布于 2024-10-19 04:43:08 字数 1727 浏览 1 评论 0原文

我正在测试一个简单的 DLL,我在 CLR 控制台应用程序中使用 C++/CLI 编写。该 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 了,所以我已经很生疏了。

更新:

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

修改的 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++,会有什么不同吗?

I'm testing a simple DLL, I wrote using C++/CLI, in a CLR console application. The DLL has only one function I'm trying to use. I'm referencing the DLL and set the Resolve #using Reference in the project property page, but I cannot see the function I wrote. I'm guessing I may have missed an access modifier somewhere, but I'm not sure. Here is a breakdown of my code:

DLL Code Header:

// 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 Code Source:

#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
}

And the test code I'm using:

#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;
}

Again, I'm not sure what I'm missing. It's been awhile since I've worked with C++/CLI so I'm pretty rusty.

UPDATE:

I went ahead and changed the class to a struct per Peter's advice.

Modified DLL Header Code:

// 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 );
    };
}

What I still don't understand is why the class would still default to private even though I specify it as public. Is there some fundamental reason why this is the case? Would it be any different if I was using unmanaged C++??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日情怀 2024-10-26 04:43:08

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

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

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

The default access for class is private. Either add "public:" for the members you want to be public or change to a ref struct, which has default access of public.

With respect to IntelliSense, I'm assuming you're using Visual Studio 2005 or Visual Studio 2008; Visual Studio 2010 does not support IntelliSense for C++/CLI code (this is because the parser was replaced with EDG and they didn't retrofit the C++/CLI parsing capabilities into it for 2010's release).

I doubt IntelliSense would auto-complete with the syntax you're using anyway. You'd want "a->" instead (of course, gcnew it before running this code).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文