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. COM
  4. Sending string from COM to C# under Vista problem

Sending string from COM to C# under Vista problem

Scheduled Pinned Locked Moved COM
csharpc++helpvisual-studiocom
4 Posts 3 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.
  • S Offline
    S Offline
    Stevo Z
    wrote on last edited by
    #1

    Hi, I'm experiencing and issue regarding System.AccessViolationException : Attempted to read or write protected memory that occurs since Vista. On XP's or previous, everything run fine. To be more specific. I have a C++ ATL/COM object (created in VS 6) that is wrapped in C# .NET 2.0 . Problem occurs when handling Strings. I've created COM using standard tools in VS 6. this is the method definition from Interface IDL file :

    [id(9), helpstring("method GetString")] HRESULT GetString([out] LPSTR* word, [out, retval] int* count);

    Inferface .h Header file :

    STDMETHOD(GetString)(/*[out]*/ LPSTR* word, /*[out, retval]*/ int* count);

    Code Inferface.c file :

    STDMETHODIMP CLicenser::GetString(LPSTR* word, int *count)
    {
    // helper class is some class that provides information that we need...

     \*count = this->helperClass.GetCount();  // count works fine anytime.
    
     \*word = this->helperClass.GetWord();  // this causes Attemped to read or write protected memory...
     // \*word = "some text";  // !!! this works fine, if former line is commented out, this method throws
                                           // no Exception !!!.
    
     cout << "word:" << \*word << endl;  // word is displayed correctly in console window (Console is created by
                                        //C# application) everytime
    
     return S\_OK;
    

    }

    Method definition in C# wrapper looks like:

    int IClassNameObject.GetString(out string);

    Anytime this method is called, it finishes properly (also including that Console output word:[whatever word contains]), besides in Vista, it throws that System.AccessViolationException with name of that (GetString) method after it finishes. Method is Supposed to return a count and fill a supplied string. If u need I can create sample project / dll , I don't have one now....

    zilo

    N M 2 Replies Last reply
    0
    • S Stevo Z

      Hi, I'm experiencing and issue regarding System.AccessViolationException : Attempted to read or write protected memory that occurs since Vista. On XP's or previous, everything run fine. To be more specific. I have a C++ ATL/COM object (created in VS 6) that is wrapped in C# .NET 2.0 . Problem occurs when handling Strings. I've created COM using standard tools in VS 6. this is the method definition from Interface IDL file :

      [id(9), helpstring("method GetString")] HRESULT GetString([out] LPSTR* word, [out, retval] int* count);

      Inferface .h Header file :

      STDMETHOD(GetString)(/*[out]*/ LPSTR* word, /*[out, retval]*/ int* count);

      Code Inferface.c file :

      STDMETHODIMP CLicenser::GetString(LPSTR* word, int *count)
      {
      // helper class is some class that provides information that we need...

       \*count = this->helperClass.GetCount();  // count works fine anytime.
      
       \*word = this->helperClass.GetWord();  // this causes Attemped to read or write protected memory...
       // \*word = "some text";  // !!! this works fine, if former line is commented out, this method throws
                                             // no Exception !!!.
      
       cout << "word:" << \*word << endl;  // word is displayed correctly in console window (Console is created by
                                          //C# application) everytime
      
       return S\_OK;
      

      }

      Method definition in C# wrapper looks like:

      int IClassNameObject.GetString(out string);

      Anytime this method is called, it finishes properly (also including that Console output word:[whatever word contains]), besides in Vista, it throws that System.AccessViolationException with name of that (GetString) method after it finishes. Method is Supposed to return a count and fill a supplied string. If u need I can create sample project / dll , I don't have one now....

      zilo

      N Offline
      N Offline
      Nathan Holt at EMOM
      wrote on last edited by
      #2

      From your description, it looks like the problem is with the helperClass member. How does that work? Nathan

      1 Reply Last reply
      0
      • S Stevo Z

        Hi, I'm experiencing and issue regarding System.AccessViolationException : Attempted to read or write protected memory that occurs since Vista. On XP's or previous, everything run fine. To be more specific. I have a C++ ATL/COM object (created in VS 6) that is wrapped in C# .NET 2.0 . Problem occurs when handling Strings. I've created COM using standard tools in VS 6. this is the method definition from Interface IDL file :

        [id(9), helpstring("method GetString")] HRESULT GetString([out] LPSTR* word, [out, retval] int* count);

        Inferface .h Header file :

        STDMETHOD(GetString)(/*[out]*/ LPSTR* word, /*[out, retval]*/ int* count);

        Code Inferface.c file :

        STDMETHODIMP CLicenser::GetString(LPSTR* word, int *count)
        {
        // helper class is some class that provides information that we need...

         \*count = this->helperClass.GetCount();  // count works fine anytime.
        
         \*word = this->helperClass.GetWord();  // this causes Attemped to read or write protected memory...
         // \*word = "some text";  // !!! this works fine, if former line is commented out, this method throws
                                               // no Exception !!!.
        
         cout << "word:" << \*word << endl;  // word is displayed correctly in console window (Console is created by
                                            //C# application) everytime
        
         return S\_OK;
        

        }

        Method definition in C# wrapper looks like:

        int IClassNameObject.GetString(out string);

        Anytime this method is called, it finishes properly (also including that Console output word:[whatever word contains]), besides in Vista, it throws that System.AccessViolationException with name of that (GetString) method after it finishes. Method is Supposed to return a count and fill a supplied string. If u need I can create sample project / dll , I don't have one now....

        zilo

        M Offline
        M Offline
        Mike Dimmick
        wrote on last edited by
        #3

        If not used in any other projects I would strongly recommend that you stick to Automation-compatible types, so here you would use a BSTR rather than LPSTR. .NET understands Automation types better than raw types. If you can't do this, you need to ensure that the marshalling and memory model are followed correctly. You need to tell MIDL that the parameter is actually a null-terminated string (use the string attribute in addition to out) so that the data is marshalled correctly. Otherwise it assumes that the LPSTR points to a single char. For returning a string, you must return a new buffer allocated with the system allocator, CoTaskMemAlloc. I suspect what's happening is that .NET is trying to free the returned pointer by calling CoTaskMemFree and in Windows XP, you were getting away with it.


        DoEvents: Generating unexpected recursion since 1991

        S 1 Reply Last reply
        0
        • M Mike Dimmick

          If not used in any other projects I would strongly recommend that you stick to Automation-compatible types, so here you would use a BSTR rather than LPSTR. .NET understands Automation types better than raw types. If you can't do this, you need to ensure that the marshalling and memory model are followed correctly. You need to tell MIDL that the parameter is actually a null-terminated string (use the string attribute in addition to out) so that the data is marshalled correctly. Otherwise it assumes that the LPSTR points to a single char. For returning a string, you must return a new buffer allocated with the system allocator, CoTaskMemAlloc. I suspect what's happening is that .NET is trying to free the returned pointer by calling CoTaskMemFree and in Windows XP, you were getting away with it.


          DoEvents: Generating unexpected recursion since 1991

          S Offline
          S Offline
          Stevo Z
          wrote on last edited by
          #4

          Hi, I wasn't able to replace LPSTR with BSTR. My knowledge about C++ is limited and I did not find corresponding conversion. Anyway, I added string attribute and started using CoTaskMemAlloc on C++ side and so far it's working under Vista too, so thank you very much! :rose:

          zilo

          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