using unmanaged dlls from managed dll wrappers.

  • Thread starter drawing in aspnet
  • Start date
D

drawing in aspnet

Hi,

I hope someone can direct me to the correct newsgroup if this one is
not correct.

I have a unmanaged C++ dll that I need to use from a C# client. Rather
than use dllImport directly, I created a managed C++ dll (in the same
solution). In this managed C++ dll, I added a reference (under
references) to the unmanaged dll (it's listed under the project tab).
I also had to #include the header file for the unmanaged dll in my
managed code.

Then I created a C# client (.exe.) in my solution to test this idea.
In the C# client I simply added a reference to the managed C++ dll.
All worked great and I was happy.

Now two weeks later, I would like to use this stuff from a real
project. From a C# class library (which is used by my C# exe), I call
into my C++ managed dll. I of course had to add a reference to the C++
managed dll. Everything compiled. However, when I run, I get the
following runtime error:

An unhandled exception of type 'System.IO.FileNotFoundException'
occurred in CSharpLibrary.dll.

Additional Information: File or assembly name ManagedConverterWrapper,
or one of its dependencies, was not found

ManagedConverterWrapper is the managed C++ wrapper I wrote.

I tried adding a reference in my C# class library to the unmanaged C++
dll using the 'Add Reference' tab. It did not work (got error saying
could not add non DotNet dll) . Curiously, I was able to add a
reference to the unmanaged dll to my managed C++ dll. Was that because
it was within same solution or does C++ behave differently with respect
to adding references.

I also tried copying the unmanaged dll everywhere I could think of
(bin/debug of C# class library and C# exe). No luck!

My questions are:

1. When one uses a managed C++ dll that in turn makes use of unmanaged
dlls, what else must one do to get things working? I.e. Compiler
settings, additional references, file copying, etc.

2. Why did everything work fine when all the project where within one
solution?

Thanks in advance for any pointers, advice or references.

Dave.
 
L

Lloyd Dupont

Hi!

This is just a DLL mumbob-jumbo!!

I was surprised when you said you a reference to the unmanaged DLL to your
managed C++ wrapper.
But in fact you probably add a PROJECT reference.

Anyway you're right, the problem is that the .NET runtime can't find your
unmanaged DLL because it's not in the directory.
To solve that you should either:
1. Use a custom build action (project properties) which will copy the dll
after successfull compilation
2. Add the unmanaged DLL project in your solution and reference it.
 
D

drawing in aspnet

Lloyd,

Thanks for the response. I tried copying the unmanaged dll to the
correct directory (manually), and that did not work. I will try the
"Add the unmanaged DLL project in your solution and reference it"
option tomorrow. However, this is not an ideal solution. For
instance, I might want to distribute the unmanaged dll (and the managed
C++ wrapper), without source code.

Dave.
 
L

Lloyd Dupont

you misunderstood me.
copy the unmanaged DLL in some directory next to your solution.
right click on your project => properties => build event
in the appropriate text box write: "copy /y $(SolutionDir)../bin/myDll.dll
$(TargetDir)"

--
Regards,
Lloyd Dupont

NovaMind development team
NovaMind Software
Mind Mapping Software
<www.nova-mind.com>
 
D

drawing in aspnet

Lloyd,

I'm afraid I still cannot get it to work by copying the dll. And quite
possibly I am still misunderstanding you. There are two solutions. In
solution1 I have the unmanaged dll project, a managed C++ dll that
wraps the unmanaged calls, and a C# winforms project to test it all
out. In the C# project I add a reference (by project) to the managed
C++ project. In the managed C++ project I add a reference to (by
project) to the unmanaged C++ project. This all works.

Now in another solution (in a completely different location on the
disk), I attempt to use this managed/unmanaged pair. I add a reference
to the managed dll in my C# project. It compiles fine. I then get the
runtime error I showed earlier. I have copied the unmanaged dll to
this 2nd solution directory (and a directory next to it called "Test",
and to all the debug, bin, and project directories). Is this what you
meant? It does not work. I should say that I have done similar things
with managed (not unmanaged) dlls where I was able to get things
working. I.e. I had dllA that referenced dllB. In that case, simply
adding a reference to both dlls did the job. Note that you can't add a
reference to the unmanaged dll in a project outside the original
solution. It gives an error.

I am using Visual Studio 2003. Thanks for all the help.

Regards,

Dave.
 
L

Lloyd Dupont

Hi... drawing!

It's not what I mean.

1st, just ouf of curiosity (and to be sure), forget about VisualStudio for a
while, manuall copy the unmanaged DLL in the same directory as your managed
application and its DLL (usuauall a dir called Debug or Release) and try to
run the application.
It should work, if not it's the pronlem.
Now delete the DLL.


2nd: what about the part I was telling you to edit the build event?
I have the feeling you have not created any build event!
You should create a build event!
Build events are found with Project Properties => Build event
Then in either post or pre build event text box, type the following build
event DOS command:
"copy /y $(SolutionDir)../bin/myDll.dll $(TargetDir)"

And try again.
Does it work better with build event?
You see, the solution is the build event!
I hope you get the message about the build event by now ;-)

Anyway, be sure it's the build event which solve the your problem by
manually deleting the unamanged DLL from the Debug/Release directory first!
The build event should copy it there again.

That said it just solve you debugging problem. When you will create a Setup
you should explicitely add the DLL in your file list (as VS.NET won't pick
it up for you).


Regards,
Lloyd Dupont
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top