C# DLL 缺少公共函数

发布于 2024-09-29 04:01:16 字数 567 浏览 1 评论 0原文

我正在尝试使用 C# .NET Framework 2.0 编写 DLL。一切都编译正常,但是当我尝试从应用程序访问 DLL 时,尝试获取过程地址时失败。因此,我在 Dependency Walker 中操作了 DLL,但我的所有公共函数都丢失了!

到目前为止,我的 DLL 相当简单:

namespace MyDll_Namespace
{
    public class MyDllClass
    {
        public static int Func1( /* params */ ) { /* ... */ }

        public static int Func2( /* params */ ) { /* ... */ }

        public static int Func3( /* params */ ) { /* ... */ }

        public static int Func4( /* params */ ) { /* ... */ }
    }
}

没有太多其他内容,只有一些在类内部以及类外部的命名空间中定义的常量和委托。任何想法或建议将不胜感激。谢谢。

I am attempting to write a DLL using the C# .NET Framework 2.0. Everything compiles okay, but when I try to access the DLL from my application, it fails when attempting to get the procedure address. So, I oped the DLL in Dependency Walker, and all of my public functions are missing!

My DLL, so far, is fairly straightforward:

namespace MyDll_Namespace
{
    public class MyDllClass
    {
        public static int Func1( /* params */ ) { /* ... */ }

        public static int Func2( /* params */ ) { /* ... */ }

        public static int Func3( /* params */ ) { /* ... */ }

        public static int Func4( /* params */ ) { /* ... */ }
    }
}

There's not much else, just a few constants and delegates defined inside the class, as well as outside the class in the namespace. Any thoughts or suggestions would be much appreciated. Thanks.

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

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

发布评论

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

评论(5

灼痛 2024-10-06 04:01:16

依赖关系遍历器适用于 win32 DLL(这是本机代码),而不是 .NET 程序集(这是托管代码)。它不会在任意类中找到方法(即使它们是静态的)。如果您需要从本机代码调用托管代码,有多种方法可以实现,但事实并非如此漂亮

如果您想从托管代码中使用 dll,那就容易多了。查看 System.Assembly激活器

一个例子:

var assembly = Assembly.LoadFile(@"\path\to\your.dll");
var mydllclass_type = assembly.GetType("MyDllClass");
var instance = Activator.CreateInstance(mydllclass_type);

实例将是一个对象。要调用这些方法,您需要使用反射,因为接口在编译时是未知的。

如果您正在创建一个插件系统,最好的方法是为所有插件提供一个通用接口或抽象基础,并让您的程序引用它。那些实施插件的第三方也将参考本合同。在本例中,最后一行发生了一些变化:

var instance = (IMyDllClass)Activator.CreateInstance(mydllclass_type);

现在您可以像在常规构造的对象中一样使用这些方法。

Dependency walker is for win32 DLLs (which is native code), not .NET assemblies (which is managed code). It won't find methods in an arbitrary class (even if they are static). If you need to call managed code from native code, there are ways of doing that, but it's not pretty.

If you want to use your dll from managed code, it's a lot easier. Check out System.Assembly and Activator.

An example of this:

var assembly = Assembly.LoadFile(@"\path\to\your.dll");
var mydllclass_type = assembly.GetType("MyDllClass");
var instance = Activator.CreateInstance(mydllclass_type);

The instance will be an object. To call the methods, you need to use reflection, because the interface is not known at compile-time.

If you are creating a plugin system, the best way is to have a common interface or abstract base for all plugins, and have that referenced by your program. Those third parties who implement a plugin, will also reference this contract. In this case, the last line changes a bit:

var instance = (IMyDllClass)Activator.CreateInstance(mydllclass_type);

Now you can use the methods like in a regularly constructed object.

日记撕了你也走了 2024-10-06 04:01:16

这里的部分问题是依赖遍历器是本机应用程序的工具。它不理解托管代码,因此不会显示您定义的任何托管类型+方法。

我对你问题中的这一行感到困惑

当我尝试从应用程序访问 DLL 时,尝试获取过程地址失败

这听起来有点像我在本机应用程序(而不是托管应用程序)中看到的错误。您是否尝试从本机应用程序访问 C# 代码?如果是这样,这只能通过 COM 魔法来完成,而不能通过直接调用来完成。您能更详细地解释一下这里发生了什么吗?

Part of the problem here is that dependency walker is a tool for native applications. It doesn't understand managed code and hence won't display any of the managed types + methods that you've defined.

I'm confused by this line in your question

when I try to access the DLL from my application, it fails when attempting to get the procedure address

This sounds a bit like an error I would see in a native application, not a managed one. Are you trying to access the C# code from a native application? If so this can only be done via COM magic and not via direct calling. Can you explain in more detail what is going on here?

苍风燃霜 2024-10-06 04:01:16

尝试使用 .net Reflector 来准确查看构建的 DLL 内部的内容(以确保一切都按照预期方式进行)。
另外,在引用 DLL 之前,请确保在构建 DLL 时处于发布模式...我不知道它是否会改变任何内容,但值得一试 =)

Try .net Reflector to see exactly what's inside your built DLL (in order to make sure everything is the way it is supposed to).
Also make sure you're in release mode while building your DLL before referencing it... I don't know if it's going to change anything, but it worths the try =)

旧夏天 2024-10-06 04:01:16

您需要添加对 dll 的引用,运行时将自动允许您调用您的函数。这是一个关于编写 .net 类库的小教程

You need to add a reference to your dll and the runtime will automatically allow you to call your function. Here is a little tutorial on writing a .net class library.

往事风中埋 2024-10-06 04:01:16

我决定用 C++ 编写 DLL。但是,这里是我发现的一堆关于从非托管代码使用托管代码的有用信息:

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