views:

46

answers:

3

I'm finding plenty of examples through Google of how to call an API function within an unmanaged DLL from .NET code, but what about instantiating an object from that unmanaged code?

I'm currently tinkering with an old legacy application at work (and I'm yet unconvinced that we're going to be able to do anything with this, but tinker and research I must) that the company wants to migrate to .NET. It appears that this was attempted before, and I'm looking through that now. The most common compile errors are references to old DLLs that the project doesn't understand.

An example of a line of code in this would be:

Dim cAccounts As HSVMETADATALib.HsvAccounts

There's a broken reference to the library in the project, which I believe is the result of some wizard-driven attempt to just port some pre-.NET project to .NET. I have the DLL that it's looking for, but adding a reference to it returns the error:

A reference to 'HFMConstants.dll' could nto be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.

Now, I know nothing about this DLL. And I have very little experience in pre-.NET Windows programming. So I've been looking around online for information about using an old DLL in this code, and it all focuses on calling API functions within the DLL. Looking through this code, however, it's much less about API functions and more about objects. So how would I go about instantiating an instance of one of these objects?

(Be aware that this is one of those situations where we don't even know if we have the right source code, there's no documentation, nothing about the ownership and maintenance of this application was built to last. But this one hurdle of being able to instantiate objects from the external DLLs, which are from some unknown and potentially unreachable at this point third party source, will cut down on most of the compiler errors and help us get into a little more meat of the code, possibly even getting a stripped-down version at least compilable.)

It's vague, but it's worth asking :)

+1  A: 

You will need to try to find out more information about this DLL. For example if it is a COM object you could generate a strongly typed managed wrapper that will allow you to consume it using tlbimp.exe. If it is a standard Win32 library you will need to find out whether it exposes some functions, what are their names and arguments in order to PInvoke them. You could use dumpbin.exe to see a list of exported function names.

Darin Dimitrov
I'm guessing it's not COM. ActiveX perhaps? I'm not really familiar with the difference, if any. tlbimp created a DLL, but it doesn't appear to be usable. PInvoke is just for invoking methods, isn't it? I'm not finding anything about instantiating objects with it.
David
If it's a COM object `tlbimp` should generate a managed assembly containing the exposed types.
Darin Dimitrov
+2  A: 

I suggest you try opening the DLL with Dependency Walker. This should give you more information about the DLL's internals and API.

See Also:

Referencing a non-CLR DLL
http://illuminatedcomputing.com/blog/?p=44

Robert Harvey
I like this Dependency Walker, nice little tool. Naturally, this one sample DLL has a large tree of dependencies (some of which aren't found, which I imagine will be another fun roadblock). I'm not sure what information from within here will be particularly useful for this effort and what I can safely ignore just yet, though.
David
A: 

You could comment out all the code that doesn't compile, anything dependent on files you don't have.

If you have the .dll and all it's dependent .dlls, you should be able to add it to your project references. If not, without external documentation, it's pretty hard to figure out what that compiled code was doing.

Beth