c++ from c#
-
Hi, I have an unmanaged C++ class, and a managed c++ wrapper for the class. I then access the managed c++ from c# client. The whole thing compiles fine, but when I run the c# client, it crashes as soon as it hits the line where the managed c++ object is initialized. Here is the code that I am working with:
// C++ CODE #include #include #using using namespace System; // from msdn class UnManagedClass { public: LPCWSTR GetPropertyA() { return 0; } void MethodB ( LPCWSTR ) {} }; public ref class AdapterNET { private: UnManagedClass * m_unman; public: // allocate unmanaged object AdapterNET() : m_unman( new UnManagedClass ) {} // deallocate unmanaged object ~AdapterNET() { delete m_unman; } protected: // deallocated unmanaged object in finalizer in case the constructor is never called !AdapterNET() { delete m_unman; } public: property String ^ get_PropertyA { String ^ get() { return gcnew String( m_unman->GetPropertyA() ); } } void MethodB( String ^ aString ) { pin_ptr str = PtrToStringChars( aString ); m_unman->MethodB( str ); } }; // C# CODE using System; using System.Collections.Generic; using System.Text; namespace TestingAdapterNET { class AdapterNETDriver { static void Main( string[] args ) { AdapterNET _adapterNET = new AdapterNET(); // <-- crashes here string propA = _adapterNET.get_PropertyA; _adapterNET.MethodB( propA ); } } }
Any ideas? Thanks,
----------------- Genaro
-
Hi, I have an unmanaged C++ class, and a managed c++ wrapper for the class. I then access the managed c++ from c# client. The whole thing compiles fine, but when I run the c# client, it crashes as soon as it hits the line where the managed c++ object is initialized. Here is the code that I am working with:
// C++ CODE #include #include #using using namespace System; // from msdn class UnManagedClass { public: LPCWSTR GetPropertyA() { return 0; } void MethodB ( LPCWSTR ) {} }; public ref class AdapterNET { private: UnManagedClass * m_unman; public: // allocate unmanaged object AdapterNET() : m_unman( new UnManagedClass ) {} // deallocate unmanaged object ~AdapterNET() { delete m_unman; } protected: // deallocated unmanaged object in finalizer in case the constructor is never called !AdapterNET() { delete m_unman; } public: property String ^ get_PropertyA { String ^ get() { return gcnew String( m_unman->GetPropertyA() ); } } void MethodB( String ^ aString ) { pin_ptr str = PtrToStringChars( aString ); m_unman->MethodB( str ); } }; // C# CODE using System; using System.Collections.Generic; using System.Text; namespace TestingAdapterNET { class AdapterNETDriver { static void Main( string[] args ) { AdapterNET _adapterNET = new AdapterNET(); // <-- crashes here string propA = _adapterNET.get_PropertyA; _adapterNET.MethodB( propA ); } } }
Any ideas? Thanks,
----------------- Genaro
Hi 1) what is "crashes here", what are the symptoms ? any error message, message box, ... ? 2) try putting your managed code inside a try-catch construct, and when you catch an exception entirely display it. (hint: the Exception.ToString() method gives it all). 3) BTW, how is your C# code to find adapterNET, arent you missing a using statement ? :)
Luc Pattyn
-
Hi 1) what is "crashes here", what are the symptoms ? any error message, message box, ... ? 2) try putting your managed code inside a try-catch construct, and when you catch an exception entirely display it. (hint: the Exception.ToString() method gives it all). 3) BTW, how is your C# code to find adapterNET, arent you missing a using statement ? :)
Luc Pattyn
Hi and thanks for the response. 1) Crashes here means that I get the following: No symbols are loaded for any call stack frame. The source code cannot be displayed. with exception: {"The specified module could not be found. (Exception from HRESULT: 0x8007007E)":null} 2) I added the try/catch, but it never hits it 3) I included a reference to the dll, and it seems to find the class (since intellisense is available). I have wrapped the c++ code with a namespace and am now '#using' it, just in case. Any more ideas? Thanks,
----------------- Genaro
-
Hi and thanks for the response. 1) Crashes here means that I get the following: No symbols are loaded for any call stack frame. The source code cannot be displayed. with exception: {"The specified module could not be found. (Exception from HRESULT: 0x8007007E)":null} 2) I added the try/catch, but it never hits it 3) I included a reference to the dll, and it seems to find the class (since intellisense is available). I have wrapped the c++ code with a namespace and am now '#using' it, just in case. Any more ideas? Thanks,
----------------- Genaro
Hi, HRESULT ending on 007E means good old ERROR_MOD_NOT_FOUND, so module not found, meaning a needed dll file is not found at run-time If I understand correctly you are using three modules: 1. your main program (C#), being an EXE file 2. your managed C++ adapter class in a DLL 3. your unmanaged C++ whatever, in another DLL unfortunately it is not clear which module is not found. I guess (and hope) you have 1. and 2. as two projects within a single solution in Visual Studio, so it should be perfectly possible to single-step as long as you do not access anything in 3. to make sure the managed C++ works, add another class to it, something that does not need 3. (e.g. a simple function that returns the sum of two integers) and try using that first; if that succeeds, continue with the adapter itself. :)
Luc Pattyn
-
Hi, I have an unmanaged C++ class, and a managed c++ wrapper for the class. I then access the managed c++ from c# client. The whole thing compiles fine, but when I run the c# client, it crashes as soon as it hits the line where the managed c++ object is initialized. Here is the code that I am working with:
// C++ CODE #include #include #using using namespace System; // from msdn class UnManagedClass { public: LPCWSTR GetPropertyA() { return 0; } void MethodB ( LPCWSTR ) {} }; public ref class AdapterNET { private: UnManagedClass * m_unman; public: // allocate unmanaged object AdapterNET() : m_unman( new UnManagedClass ) {} // deallocate unmanaged object ~AdapterNET() { delete m_unman; } protected: // deallocated unmanaged object in finalizer in case the constructor is never called !AdapterNET() { delete m_unman; } public: property String ^ get_PropertyA { String ^ get() { return gcnew String( m_unman->GetPropertyA() ); } } void MethodB( String ^ aString ) { pin_ptr str = PtrToStringChars( aString ); m_unman->MethodB( str ); } }; // C# CODE using System; using System.Collections.Generic; using System.Text; namespace TestingAdapterNET { class AdapterNETDriver { static void Main( string[] args ) { AdapterNET _adapterNET = new AdapterNET(); // <-- crashes here string propA = _adapterNET.get_PropertyA; _adapterNET.MethodB( propA ); } } }
Any ideas? Thanks,
----------------- Genaro
Best guess - you have a problem loading the library, because of WinSxS issues. Do you have more than one PC ? My notebook died recently, to the point that a vanilla dll and vanilla C# app would blow up in this circumstance. My own code runs fine on my desktop still. At the same time, are you sure that all the dlls your code relies on are in the directory of the exe ?
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
Hi, HRESULT ending on 007E means good old ERROR_MOD_NOT_FOUND, so module not found, meaning a needed dll file is not found at run-time If I understand correctly you are using three modules: 1. your main program (C#), being an EXE file 2. your managed C++ adapter class in a DLL 3. your unmanaged C++ whatever, in another DLL unfortunately it is not clear which module is not found. I guess (and hope) you have 1. and 2. as two projects within a single solution in Visual Studio, so it should be perfectly possible to single-step as long as you do not access anything in 3. to make sure the managed C++ works, add another class to it, something that does not need 3. (e.g. a simple function that returns the sum of two integers) and try using that first; if that succeeds, continue with the adapter itself. :)
Luc Pattyn
I actually have 2 and 3 combined... both managed and unmanaged c++ in the same dll. I added another managed class to the dll, which just returns the sum of two numbers and does not access any managed code, but I get the same error. Thanks,
----------------- Genaro
-
I actually have 2 and 3 combined... both managed and unmanaged c++ in the same dll. I added another managed class to the dll, which just returns the sum of two numbers and does not access any managed code, but I get the same error. Thanks,
----------------- Genaro
Gee, I was not even aware you could do that... But then, I dont use C++, I use either C#, or C# + unmanaged C with PInvoke. Maybe the presence of unmanaged code prevents you from calling the managed half of that mixed dll ?? :)
Luc Pattyn
-
I actually have 2 and 3 combined... both managed and unmanaged c++ in the same dll. I added another managed class to the dll, which just returns the sum of two numbers and does not access any managed code, but I get the same error. Thanks,
----------------- Genaro
Hi Picazo, Actually, come to think of it; I can not believe you can do that at all. Please feel free to e-mail me a zip with your solution and stuff, or at least such combined dll. I would like to look inside it ! Regards
Luc Pattyn
-
Hi Picazo, Actually, come to think of it; I can not believe you can do that at all. Please feel free to e-mail me a zip with your solution and stuff, or at least such combined dll. I would like to look inside it ! Regards
Luc Pattyn
Hey Luc, http://sqlite.phxsoftware.com/[^] These guys to the same, they have combined the original Sqlite3.dll (c only) with a managed C# wrapper.
Supports the Full and Compact .NET Framework as well as native C/C++
The library is 100% binary compatible with the original sqlite3.dll and has no linker dependencies on the .NET runtime for full unmanaged C/C++ development.And you can get the source ;)
-
Hey Luc, http://sqlite.phxsoftware.com/[^] These guys to the same, they have combined the original Sqlite3.dll (c only) with a managed C# wrapper.
Supports the Full and Compact .NET Framework as well as native C/C++
The library is 100% binary compatible with the original sqlite3.dll and has no linker dependencies on the .NET runtime for full unmanaged C/C++ development.And you can get the source ;)
Thanks a lot, Steve. I will look into that some day. Seems like picazo should have a look too into that source code... :)
Luc Pattyn