开发者

C# DLL Missing Public Functions

开发者 https://www.devze.com 2023-01-21 17:18 出处:网络
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 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 */ ) { /* ... */ }

   开发者_如何学Python     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.


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.


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?


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 =)


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.


I decided to write my DLLs in C++. However, here is a bunch of useful information I found for using managed code from unmanaged code:

  • Is it possible to use a DLL created using C# in an unmanaged VC++ application?
  • How do I call C++/CLI (.NET) DLLs from standard, unmanaged non-.NET applications?
  • Using managed code in an unmanaged application
  • Using Managed Components from Unmanaged Code
  • An Overview of Managed/Unmanaged Code Interoperability
  • Unmanaged Exports
  • Calling Managed .NET C# COM Objects from Unmanaged C++ Code
  • All-In-One Code Framework
  • CLR Hosting APIs
  • How To: Migrate to /clr
  • How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005
0

精彩评论

暂无评论...
验证码 换一张
取 消