Home All Groups Group Topic Archive Search About

using unmanaged dlls from managed dll wrappers.

Author
1 Feb 2006 8:35 PM
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.

Author
1 Feb 2006 11:51 PM
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.

--
Show quote
"drawing in aspnet" <csharpu***@hotmail.com> wrote in message
news:1138826132.728910.313210@g14g2000cwa.googlegroups.com...
> 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.
>
Author
2 Feb 2006 4:02 AM
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.
Lloyd Dupont wrote:
Show quote
> 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.
>
> --
> "drawing in aspnet" <csharpu***@hotmail.com> wrote in message
> news:1138826132.728910.313210@g14g2000cwa.googlegroups.com...
> > 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.
> >
Author
2 Feb 2006 5:45 AM
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>
Show quote
"drawing in aspnet" <csharpu***@hotmail.com> wrote in message
news:1138852970.420253.246970@f14g2000cwb.googlegroups.com...
> 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.
> Lloyd Dupont wrote:
>> 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.
>>
>> --
>> "drawing in aspnet" <csharpu***@hotmail.com> wrote in message
>> news:1138826132.728910.313210@g14g2000cwa.googlegroups.com...
>> > 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.
>> >
>
Author
2 Feb 2006 7:03 PM
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.
Lloyd Dupont wrote:
Show quote
> 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>
> "drawing in aspnet" <csharpu***@hotmail.com> wrote in message
> news:1138852970.420253.246970@f14g2000cwb.googlegroups.com...
> > 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.
> > Lloyd Dupont wrote:
> >> 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.
> >>
> >> --
> >> "drawing in aspnet" <csharpu***@hotmail.com> wrote in message
> >> news:1138826132.728910.313210@g14g2000cwa.googlegroups.com...
> >> > 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.
> >> >
> >
Author
3 Feb 2006 3:49 AM
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

Show quote
> 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.
> Lloyd Dupont wrote:
>> 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>
>> "drawing in aspnet" <csharpu***@hotmail.com> wrote in message
>> news:1138852970.420253.246970@f14g2000cwb.googlegroups.com...
>> > 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.
>> > Lloyd Dupont wrote:
>> >> 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.
>> >>
>> >> --
>> >> "drawing in aspnet" <csharpu***@hotmail.com> wrote in message
>> >> news:1138826132.728910.313210@g14g2000cwa.googlegroups.com...
>> >> > 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.
>> >> >
>> >
>

AddThis Social Bookmark Button