views:

64

answers:

5

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.

+2  A: 

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.

Tamás Szelei
This is a bit over my head at the moment. An example would be very helpful. Thanks.
Jim Fell
There you go :)
Tamás Szelei
Due to complexity, I decided to write my DLLs in C++.
Jim Fell
A: 

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?

JaredPar
A: 

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

Flo.
A: 

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.

rerun