Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / December 2006

Tip: Looking for answers? Try searching our database.

problem with calling C++/CLI wrapper to C++ code from C# applicati

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
phys - 29 Dec 2006 00:46 GMT
I need to write a C# application that uses unmanaged C++ code. I created a
C++/CLI wrapper to C++ code and encountered the following problem. Any time I
try to instantiate a wrapper in C# application the program crashes with an
error
"An unhandled exception of type 'System.IO.FileNotFoundException' occurred
in mscorlib.dll, Additional information: The specified module could not be
found. (Exception from HRESULT: 0x8007007E)". C# application has a reference
only to the wrapper and the wrapper has a reference to unmanaged dll. Similar
application written in C++/CLI gives exactly the same error if it has a
reference only to the wrapper namespace. In case of C++/CLI application,
VS2005 allows to add a reference to unmanaged dll and then the application
works OK.
I have seen an example of managed C++ wrapper that is virtual identical to
the code below and is claimed to work with C#
(http://www.ondotnet.com/lpt/a/4731)
Cannot figure out what is the problem with my code.
Here is the code

//CppLib.h
#ifndef _CPPLIB_EXPORT_IMPORT
    #ifdef _CPPLIB_EXPORT
       #define _CPPLIB_EXPORT_IMPORT  __declspec(dllexport)
   #else
       #define _CPPLIB_EXPORT_IMPORT  __declspec(dllimport)
   #endif
#endif

class _CPPLIB_EXPORT_IMPORT CCppLib
{
   double _a;
   double _b;
   double _c;

   public:
       CCppLib(double a, double b, double c);
       ~CCppLib(){};

   bool IsValid();

};
//CppLib.cpp

CCppLib::CCppLib(double a, double b, double c)
{
   _a = a;
   _b = b;
   _c = c;
}

bool CCppLib::IsValid()
{
   return (_a + _b > _c && _a + _c > _b && _b + _c > _a );
}

//CLRWrapper.h

#pragma once
#include "..\\CppLib\\CppLib.h"

using namespace System;

namespace CLRWrapper
{

    public ref class Triangle
    {
        CCppLib* _CppLib;

   public:

       Triangle()
       {
           _CppLib = new CCppLib();
       }

       Triangle(double a, double b, double c)
       {
           _CppLib = new CCppLib(a, b, c);
       }

       ~Triangle()
       {
         this->!Triangle();
       }

       !Triangle()
       {
           if (_CppLib != NULL)
           delete _CppLib;
           _CppLib = 0;
       }

       CCppLib* GetCppLib() { return _CppLib;}
       
       bool IsValid()
       {
           if (_CppLib != NULL)
               return _CppLib->IsValid();
           return false;
       }

    };
}

// CLRTest.cpp  C++/CLI test application
using namespace System;
using namespace CLRWrapper;

int main(array<System::String ^> ^args)
{
    Triangle^ tr = gcnew Triangle(1, 2, 3);
   Console::WriteLine(tr->IsValid());

   return 0;
}

//Program.cs C# test application

using System;
using System.Collections.Generic;
using System.Text;
using CLRWrapper;

namespace TestTriangle
{
   class Program
   {
       static void Main(string[] args)
       {
           Triangle tr = new Triangle(0.1, 0.2, 0.3);
           Console.WriteLine(tr.IsValid());
       }
   }
}



Signature

phys

wpcmame - 29 Dec 2006 06:32 GMT
> "An unhandled exception of type 'System.IO.FileNotFoundException' occurred
> in mscorlib.dll, Additional information: The specified module could not be
> found. (Exception from HRESULT: 0x8007007E)".

This error could have something to do with the manifest in debugging mode.
Try to generate and embed the manifest in the assembly (linker/manifest
propertypage)

I found this out by using the procmon utility to see what file the loader
was looking for (msvcr80d.dll).

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.