[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Com object calling process and bstr processing

Posted on 1998-12-04
4
Medium Priority
?
262 Views
Last Modified: 2013-11-20
I have a comm object and a client program that I am working on. The com object has a method that has a BSTR* parameter and returns a BSTR* pointer.

Questions :

1. Suppose the name of the method is Reverse, what would the calling sequence be in the client program. The client program is going to pass a CString object and is expecting to store the value in a CString object when it is returned.

2. In the com object how would one go about looping through and getting and setting individual characters in the string ?

Thanks,
Andrew

P.S. More points to be awarded when I get the answer.
0
Comment
Question by:andrewmchorney
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 

Expert Comment

by:tsollas
ID: 1325787
I'm assuming your idl looks something like this:

HRESULT Reverse( [in,out] BSTR bstr );

Then your client call will look like this:

CString str( "abcdefg" );
BSTR bstr = str.AllocSysString();
pI->Reverse( bstr );
. // do something with bstr
::SysFreeString( bstr );

The server implementation should look like this:

HRESULT CServerClass::Reverse( BSTR bstr )
{
    WCHAR * pwszTmp = new WCHAR[ wcslen( bstr ) + 1 ];
    if ( pwszTmp )
    {
        wcscpy( pwszTmp, bstr );
        WCHAR * lpwsz = &pwszTmp[ wcslen( pwszTmp ) - 1 ];
        int ndx = 0;
        while ( lpwsz >= pwszTmp )
            bstr[ ndx++ ] = *lpwsz--;
    }
    if ( pwszTmp )
        delete pwszTmp;
}

The basic assumptions here are that a BSTR is the same as a LPWSTR (you'll notice the WinAPI says this is so, and the headers define it the same).  That means you can do string related operations, albeit with unicode functions.  Secondly, you only need to use a BSTR * if the server is allocating memory and returning it.  In the example you gave, the client is allocating the memory, so you don't need to pass a BSTR *.  Obviously, I could have used the _wcsrev() function to reverse the string, but the idea was to show how the string manipulation works.
0
 

Accepted Solution

by:
tsollas earned 40 total points
ID: 1325788
doh... consider my comment above as the answer.
0
 

Author Comment

by:andrewmchorney
ID: 1325789
I have gotten most of the work done before getting your message. My project has 2 interfaces with a function in each one. The first one has a short parameter and returns a short and compiles and works well. The second one has a BSTR * parameters and is to return a BSTR object. The call to the com function (ReverseString) complains about the parameter and I have not figured out the error. Can you take a look at what is wrong. There are more points to be awarded when this is solved.


Com function

BSTR StringFunctions::ReverseString(BSTR FAR* strInputString)
{
   // TODO: Add your dispatch handler code here
      
   CString strResult(*strInputString);

     // code to do reversal goes here and is not relevent to the question
    return strResult.AllocSysString();
}


Client Code :

void CTestReverseString::OnReverseStringCommand()
{
      // TODO: Add your control notification handler code here
            // TODO: Add your control notification handler code here
 
      IStringFunctions m_StringFunctions;

      BSTR strToBeReversed;
      BSTR strReversedString;

                 //
      // Get the variable values from the dialog
      //
      UpdateData(TRUE);

      //
      // Load the com object
      //
      if (!m_StringFunctions.CreateDispatch("mchorney2.StringFunctions"))
      {
            //
            // Com object not loaded
            //
            AfxMessageBox("mchorney2.StringFunctions not found");
          return;
      }

      strToBeReversed = m_strSourceString.AllocSysString();
      //
      // Reverse the string but does not compile
      //
                m_strReversedString = m_StringFunctions.ReverseString(
                                                             strToBeReversed);

      //
      // Release connection to com object
      //
      m_StringFunctions.ReleaseDispatch();

      //
      // Update the dialog
      //
      UpdateData(FALSE);
}


0
 

Author Comment

by:andrewmchorney
ID: 1325790
Tsollas

I just figured out the error and the program works well.

Andrew
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question