CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 27 of 27
  1. #16
    Join Date
    Mar 2010
    Posts
    42

    Re: How to catch error happening AFTER the code / application

    Hi all, thanks for replying

    @itsmeandnobodyelse

    I tried using HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED); as well as HRESULT hr = CoInitializeEx(0, COINIT_APARTMENTTHREADED);.

    Unfortunately, both failed.

    @ADSOFT

    I tried removing that but it errors. Just initializing the PxeAuthClass using errors.

    Code:
    IPxeAuthClassPtr pxecls;
    hr = pxecls.CreateInstance(CLSID_PxeAuthClass);
    Might you have some suggestions how to 'silence' this error so that if I run the .exe program, it will not crash?

    Thank you!

  2. #17
    Join Date
    Jun 2004
    Posts
    1,352

    Re: How to catch error happening AFTER the code / application

    Quote Originally Posted by LeanA View Post
    Hi all, thanks for replying

    @itsmeandnobodyelse

    I tried using HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED); as well as HRESULT hr = CoInitializeEx(0, COINIT_APARTMENTTHREADED);.

    Unfortunately, both failed.

    @ADSOFT

    I tried removing that but it errors. Just initializing the PxeAuthClass using errors.

    Code:
    IPxeAuthClassPtr pxecls;
    hr = pxecls.CreateInstance(CLSID_PxeAuthClass);
    Might you have some suggestions how to 'silence' this error so that if I run the .exe program, it will not crash?

    Thank you!
    It's been a while since I've done COM stuff. Maybe even if you initialize a COM object you have to delete the object before the program ends. Others that are doing COM stuff might be able to jump in.

    If it were me I would do two things.

    1) Look for example on how to use IPxeAuthClassPtr. I did a search, there IS some stuff out there.

    2) Brush up on programing with COM objects. Once you know that you should be able to determine if the COM object is working properly. The problem with sample COM code is that they assume you know how to use COM components. There is alot of stuff out there on using COM objects (initializing, terminating, etc).
    Rate this post if it helped you.

  3. #18
    Join Date
    Mar 2010
    Posts
    42

    Re: How to catch error happening AFTER the code / application

    Hi again, thanks for replying

    Ok, I will do that also thank you.

    I just found out something weird.

    In my C++ code, if I compile and run the code using "CTRL + F5", the error does not occur! The .exe produced also works and does not error.

    However, in C#, compiling and running using "CTRL + F5" still errors.

    Can anyone enlighten as to why this happens? I think that CTRL + F5 means that "without debugger", but I am wondering why it does not work with C#.

    PS. Also, is it possible to call the get a variable from C++ code I did and just input it to a C# code so that the error does not display (because the error does not happen in C++)? Basically, the C++ code is the one to instantiate the COM Class I need and perform the necessary methods, while the C# code just gets something from the C++ code.

    Thank you!
    Last edited by LeanA; September 21st, 2010 at 06:23 AM.

  4. #19
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to catch error happening AFTER the code / application

    Well, typical COM-aware code goes like this:
    Code:
    // headers and similar stuff
    
    int _tmain(bla-bla)
    {
        HRESULT hrInit = ::CoInitialize(NULL);
        if (SUCCEEDED(hrInit))
        {
            // here you go with smart pointers to COM objects and objects creation
            . . . 
            // and usually no explicit Release required to those
        }
    
        if (SUCCEEDED(hrInit))
            ::CoUnintialize();
    
        return 0;
    }
    Please note, there's no smart pointers in _tmain root scope, there you only init/deinit COM.
    Best regards,
    Igor

  5. #20
    Join Date
    Mar 2010
    Posts
    42

    Re: How to catch error happening AFTER the code / application

    Hi Igor, thanks for replying;

    I would just like to clarify if you mean by "COM-aware" is managed code?

    So in C#, the CoInitialize and CoUinitialize is defined in a similar manner like the one you posted?

    Also, is it possible to disable the automatic calling of the above and just manually call CoInitialize and CoUnitialize in C#?

    Thank you!

  6. #21
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: How to catch error happening AFTER the code / application

    No, it's all about regular C++ code. And sorry, I do very little of C# programming and none of managed C++.
    Best regards,
    Igor

  7. #22
    Join Date
    Sep 2010
    Posts
    27

    Re: How to catch error happening AFTER the code / application

    Quote Originally Posted by Igor Vartanov View Post
    No, it's all about regular C++ code. And sorry, I do very little of C# programming and none of managed C++.
    I used to code in C# and had to drop it because C# doesn't offer challenging coding techniques and it uses only a subset of windows internals with a large number of functionalities, which is why things becomes easier with C#. The most beautiful integrated part it plays is to be with active pages
    Well I saw D, now see F# in my VS10 install disc

  8. #23
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to catch error happening AFTER the code / application

    Quote Originally Posted by HochiminhCity View Post
    I used to code in C# and had to drop it because C# doesn't offer challenging coding techniques and it uses only a subset of windows internals with a large number of functionalities, which is why things becomes easier with C#.
    So you prefer to code in an environment that's more challenging?

  9. #24
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to catch error happening AFTER the code / application

    Try this - use smart pointers and scope them so they go out of scope before ::CoUnitialized is called.

    Code:
    #include "stdafx.h"
    #include "ATLComTime.h"
    #import "TSCore.dll" named_guids
    #import "tsmediaapi.dll" named_guids
    using namespace TsMediaLib;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
          ::CoInitialize(NULL);
    
          IPxeAuthClassPtr pxecls;
          pxecls.CreateInstance(CLSID_PxeAuthClass);
         
    //      VARIANT varDate;
    //      VariantInit(&varDate);
       
          COleDateTime mytime(1996,1,1,0,0,0);
    //      varDate = 
    
          _variant_t vDate(mytime, VT_DATE);
       
          _bstr_t name( _T("ab40c4ab-7e74-4740-9a09-e999e876edaa") );
     
          _variant_t out;
          out = pxecls->CreateIdentity( name, name, name, &vDate, &vDate );
    
    //      VariantClear(&varDate);
    
    //pxecls.Release();
    
      } // end of scoping block
    
          ::CoUninitialize();
    
          cout << “END” << endl;
    
    }

  10. #25
    Join Date
    Mar 2010
    Posts
    42

    Re: How to catch error happening AFTER the code / application

    Hi All thanks for replying,

    @Igor

    Okay, thank you for your help.

    @Arjay

    I will try that and get back to you. Thank you for this!

    Also, can anyone enlighten me as to why it does not error / crash when I compile using CTRL + F5 instead of just F5?

    And since it does not error, is it possible to call the get a variable from C++ code I did and just input it to a C# code so that the error does not display (because the error does not happen in C++)?

    Something like the C++ code is the one to instantiate the COM Class I need and perform the necessary methods, while the C# code just gets something from the C++ code.

    Thanks!

  11. #26
    Join Date
    Mar 2010
    Posts
    42

    Re: How to catch error happening AFTER the code / application

    Quote Originally Posted by LeanA View Post
    Hi again, thanks for replying

    Ok, I will do that also thank you.

    I just found out something weird.

    In my C++ code, if I compile and run the code using "CTRL + F5", the error does not occur! The .exe produced also works and does not error.

    However, in C#, compiling and running using "CTRL + F5" still errors.

    Can anyone enlighten as to why this happens? I think that CTRL + F5 means that "without debugger", but I am wondering why it does not work with C#.

    PS. Also, is it possible to call the get a variable from C++ code I did and just input it to a C# code so that the error does not display (because the error does not happen in C++)? Basically, the C++ code is the one to instantiate the COM Class I need and perform the necessary methods, while the C# code just gets something from the C++ code.

    Thank you!
    Hi all,

    I just clarified this. If I use

    Code:
    CoInitialize(NULL);
    and press CTRL + F5, it still errors (0xC0000005).

    However! If I use

    Code:
    CoInitializeEx(0, COINIT_MULTITHREADED);
    and press CTRL + F5, it does not error!

    Just pressing F5 either way (CoInitialize and CoInitializeEx) will result in an error.

    I'm wondering why this is happening. Anyone has an idea?

    Thank you!

  12. #27
    Join Date
    Mar 2010
    Posts
    42

    Re: How to catch error happening AFTER the code / application

    Quote Originally Posted by Arjay View Post
    Try this - use smart pointers and scope them so they go out of scope before ::CoUnitialized is called.

    Code:
    #include "stdafx.h"
    #include "ATLComTime.h"
    #import "TSCore.dll" named_guids
    #import "tsmediaapi.dll" named_guids
    using namespace TsMediaLib;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
          ::CoInitialize(NULL);
    
          IPxeAuthClassPtr pxecls;
          pxecls.CreateInstance(CLSID_PxeAuthClass);
         
    //      VARIANT varDate;
    //      VariantInit(&varDate);
       
          COleDateTime mytime(1996,1,1,0,0,0);
    //      varDate = 
    
          _variant_t vDate(mytime, VT_DATE);
       
          _bstr_t name( _T("ab40c4ab-7e74-4740-9a09-e999e876edaa") );
     
          _variant_t out;
          out = pxecls->CreateIdentity( name, name, name, &vDate, &vDate );
    
    //      VariantClear(&varDate);
    
    //pxecls.Release();
    
      } // end of scoping block
    
          ::CoUninitialize();
    
          cout << “END” << endl;
    
    }
    Hi Arjay, thanks for this.

    I just tested it and it errors the same (0xC0000005). However, based from my post above, I tried changing the CoInitialize to CoInitializeEx(0, Multithreaded) and CTRL + F5. It does not error!

    Any idea why this happens?

    Also, I like your code; cleaner than mine but seems to perform the same thing.

    Thanks!

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured