[RESOLVED] C++/CLI stack sematics - receiving a ^
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: [RESOLVED] C++/CLI stack sematics - receiving a ^

  1. #1
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    [RESOLVED] C++/CLI stack sematics - receiving a ^

    Is there any way to receive a handle created by another member function and still use the C++/CLI stack semantics (i.e. automatic calling of Dispose) ?



    I have a simple member function:

    Code:
    delegate void DataRecipient
        (
        SqlDataReader^    inSqlDataReader
        );
    
    //...
    
    void Database::ExecuteQuery
        (						
        String^           inSqlQuery,
        DataRecipient^    inDataRecipient
        )
    {
        // Create connection:
        SqlConnection connection(ConnectionString);
        SqlCommand    command(inSqlQuery, %connection);
       
        connection.Open();
    
        // Execute query:
        SqlDataReader^ reader;
        try
        {
            reader = command.ExecuteReader();
    
            // Give the query results to the data recipient.
            inDataRecipient(reader);
        }
        finally
        {
            delete reader;
        }
    }
    This compiles & works fine.



    However, I would like use the stack semantics syntax with the SqlDataReader :

    Code:
    delegate void DataRecipient
        (
        SqlDataReader^    inSqlDataReader
        );
    
    //...
    
    void Database::ExecuteQuery
        (						
        String^           inSqlQuery,
        DataRecipient^    inDataRecipient
        )
    {
        // Create connection:
        SqlConnection connection(ConnectionString);
        SqlCommand    command(inSqlQuery, %connection);
       
        connection.Open();
    
        // Execute query:
        SqlDataReader reader( command.ExecuteReader() );
    
        // Give the query results to the data recipient (a delegate).
        inDataRecipient(%reader);
    }
    At the line where 'reader' is created, the compiler complains:
    Code:
    error C3673: 'System::Data::SqlClient::SqlDataReader' : class does not have a copy-constructor
    Is it not possible to use that syntax in this way?

    -------------------------------------------------------------

    EDIT:

    Yes! http://www.codeguru.com/forum/showth...48#post1582471
    Last edited by Zaccheus; June 27th, 2007 at 03:45 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: C++/CLI stack sematics - receiving a C++/CLI handle

    You need copy-constructor (which is not by default provided by the compiler for reference types) to employ stack semantics.

    http://msdn2.microsoft.com/en-US/lib...91(VS.80).aspx

    Since SqlDataReader doesn't seem to have one, you can't use stack semantics with it.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: C++/CLI stack sematics - receiving a ^

    I was hoping there would be a way around that.


    C++/CLI stack semantics are nothing more than an abstraction of the try/finally code I'm using at the moment.
    The need for a 'copy constructor' seems completely illogical to me.

    Anyway, thanks for your reply.
    Last edited by Zaccheus; January 23rd, 2007 at 11:00 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  4. #4
    Join Date
    Feb 2007
    Posts
    1

    Re: C++/CLI stack sematics - receiving a ^

    I think msclr::auto_gcroot can get you pretty close. You'd have to change a couple of lines:

    Code:
    #include <msclr\auto_gcroot.h>
    ...
        msclr::auto_gcroot<SqlDataReader ^> reader(command.ExecuteReader());
    
        // Give the query results to the data recipient (a delegate).
        inDataRecipient(reader.get());
    Would that be sufficient?

  5. #5
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: C++/CLI stack sematics - receiving a ^

    Thanks, that looks great.


    One concern:

    MSDN says that auto_gcroot is for mixing native and managed code in the same assembly. I compile all my C++/CLI with /clr:safe which means no native code is allowed. I'll try it out and see what happens.

    My hobby projects:
    www.rclsoftware.org.uk

  6. #6
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: C++/CLI stack sematics - receiving a ^

    I've just checked <msclr\auto_gcroot.h>, auto_gcroot is a native class, which means I cannot use it in my pure managed .net code.


    Thanks anyway.
    My hobby projects:
    www.rclsoftware.org.uk

  7. #7
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Smile Disposing a received .net pointer in C++/CLI.



    I've managed to come up with a 'pure .net' solution:
    Disposing a received .net pointer in C++/CLI.

    My hobby projects:
    www.rclsoftware.org.uk

  8. #8
    Join Date
    Jan 2009
    Posts
    1

    Lightbulb Re: [RESOLVED] C++/CLI stack sematics - receiving a ^

    For anyone who's looking at this thread, I think auto_handle should do the trick that the original poster was looking for.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center