Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. c++ from c#

c++ from c#

Scheduled Pinned Locked Moved C#
csharpc++question
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    picazo
    wrote on last edited by
    #1

    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

    L C 2 Replies Last reply
    0
    • P picazo

      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

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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

      P 1 Reply Last reply
      0
      • L 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

        P Offline
        P Offline
        picazo
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • P picazo

          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

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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

          P 1 Reply Last reply
          0
          • P picazo

            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

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • L Luc Pattyn

              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

              P Offline
              P Offline
              picazo
              wrote on last edited by
              #6

              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

              L 2 Replies Last reply
              0
              • P picazo

                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

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                • P picazo

                  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

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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

                  S 1 Reply Last reply
                  0
                  • L 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

                    S Offline
                    S Offline
                    Steve Hansen
                    wrote on last edited by
                    #9

                    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 ;)

                    L 1 Reply Last reply
                    0
                    • S Steve Hansen

                      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 ;)

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups