Constructor Failure

    Date: 10/05/07 (C Sharp)    Keywords: no keywords

    Hey guys. I know I've seen this before, but I can't, for the life of me, remember how to do it or where I saw it . . .

    I need to tell a constructor to fail. Basically, I'm wrapping some unmanaged calls to a system DLL. I need to tell the class constructor to gracefully catch the exception and return null if there was an error in the constructor. Anyone know / remember how to do this?


    public class ServiceInternalInstaller {
    	[DllImport( "advapi32.dll" )]
    	public static extern IntPtr OpenSCManager( string lpMachineName, string lpSCDB, int scParameter );
    
    	private IntPtr m_pSCM = IntPtr.Zero;
    
    	public ServiceInternalInstaller() {
    		const int SC_MANAGER_CREATE_SERVICE = 0x00000002;
    			try {
    			m_pSCM = OpenSCManager( null, null, SC_MANAGER_CREATE_SERVICE );
    			if( m_pSCM == 0 ) {
    				throw new Exception( GetLastErrorMessage() );
    			}
    		} catch( Exception e ) {
    			Console.Error.WriteLine( e.Message );
    		}
    	}
    }
    
    ...
    
    ServiceInternalInstaller oInstall = new ServiceInternalInstaller();
    if( oInstall == null ) return;
    


    I left a few things out for the sake of brevity. It's a little crude, but that should give you the idea.


    Thanks.

    Source: http://community.livejournal.com/csharp/86684.html

« debugger caching? || Text from multi line text... »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home