Solved

c++ BSTR to ATL object

Posted on 2001-06-13
13
535 Views
Last Modified: 2013-11-25
I'm trying to send BSTRs to an ATL component from C++.
The BSTR's appear to be created ok from the client, but when watching the incoming BSTRS in the component the debugger reports 'Invalid string format'.

method set up as follows...

in idl file...

[id(3), helpstring("method VerifyUser")] HRESULT VerifyUser([in] BSTR UserID, [in] BSTR Password, [out] VARIANT_BOOL *bCorrect);

in cpp file...

STDMETHODIMP CHMAccounts::VerifyUser(BSTR UserID, BSTR Password, VARIANT_BOOL *bCorrect)

and called from the client...

  BSTR x = SysAllocString(wUser); // wchar_t
  BSTR y = SysAllocString(wPass);
  hr = pHMAI->VerifyUser(x, y, &ok);
  SysFreeString(x);
  SysFreeString(y);


Can someone tell me what I'm doing wrong please.


0
Comment
Question by:mark50
[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
  • 5
  • 5
  • 2
  • +1
13 Comments
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6185483
have you set debugger/environment to show unicode-strings?
In MSVC it is in menu options->Debug->show Unicode-strings?
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6185528
Try to use CString class to convert char and wchar:

CString xx(wUser);
CString yy(wPass);
BSTR x = xx.SysAllocString();
BSTR y = yy.SysAllocString();
0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6185536
why he should use mfc for this?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6185555
This is only one solution.
0
 
LVL 3

Expert Comment

by:elcapitan
ID: 6185572
Another way to see unicode strings:
In the watch window, add ',su' after the variable name. For example, when you dubug your component, write in the watch window: UserID,su in order to see the unicode string in UserID.

--EC--
0
 

Author Comment

by:mark50
ID: 6185592
Setting the 'show unicode strings' shows that the bstr is passed ok.

the program aborts when trying to execute an SQL statement.
strange thing is that the component works from VB...

the full method code is.........

STDMETHODIMP CHMAccounts::VerifyUser(BSTR UserID, BSTR Password, VARIANT_BOOL *bCorrect)
{
  FieldPtr  fp;
  _variant_t   Value, Pword(Password);
  _bstr_t cmdText;
 

  *bCorrect = -1;
 
  if(!bconnected)
    return E_FAIL;
 
  // Create command object
  if(!SUCCEEDED(m_command.CreateInstance(__uuidof(Command))))
    return E_FAIL;    
 
  // Set command parameters.
  m_command->ActiveConnection = m_connection;
  cmdText = L"Select Password from Accounts where UserID = '";
  cmdText += UserID;
  cmdText += L"'";
 
  if(!SUCCEEDED(m_command->put_CommandText( cmdText )))
    return E_FAIL;
 
  if(!SUCCEEDED(m_command->put_CommandType( adCmdText )))
    return E_FAIL;
 
  // create recordset object
  if(!SUCCEEDED(m_recordset.CreateInstance(__uuidof(Recordset))))
    return E_FAIL;
 
  // Execute ******* CAUSES EXCEPTION *******
  m_recordset = m_command->Execute( NULL, NULL, 0 );
  if(m_recordset->adoEOF)
  {
    *bCorrect = false;
    return S_OK;
  }
 
  m_recordset->MoveFirst();
  //Get the value
  fp = m_recordset->Fields->GetItem("Password");
  Value = fp->Value;

  if(Value != Pword)
    *bCorrect = false;
 
  return S_OK;
}

can you see why ?
0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6185629
isn't this another question? however, how do you defined the passed VARIANT_BOOL, the 'ok'-variable?

hr = pHMAI->VerifyUser(x, y, &ok);

The error must be in the definition of this var, in VB this seems to be correct.
0
 

Author Comment

by:mark50
ID: 6185708
I suppose it is a different question though the cause of the original problem is the same.

VARIANT_BOOL ok = false;

It doesn't matter how the ok variable is defined because this has nothing to do with the sql statement being executed.

the value of cmdtext is a valid SQL statement....

{"Select Password from Accounts where UserID = 'User1'" (1)}

this is taken straight from the watch window.


0
 
LVL 2

Expert Comment

by:missionImpossible
ID: 6185758
have you debugged where it aborts? believe me, the problem is at the client.

can you look into the idl-file. is the last parameter in your method an [out] or an [in, out] parameter?

0
 

Author Comment

by:mark50
ID: 6185872
I suppose it is a different question though the cause of the original problem is the same.

VARIANT_BOOL ok = false;

It doesn't matter how the ok variable is defined because this has nothing to do with the sql statement being executed.

the value of cmdtext is a valid SQL statement....

{"Select Password from Accounts where UserID = 'User1'" (1)}

this is taken straight from the watch window.


0
 

Author Comment

by:mark50
ID: 6185877
[id(3), helpstring("method VerifyUser")] HRESULT VerifyUser([in] BSTR UserID, [in] BSTR Password, [out] VARIANT_BOOL *bCorrect);
0
 
LVL 2

Accepted Solution

by:
missionImpossible earned 30 total points
ID: 6185945
sorry, but I was too blind to see your comment at the execute-statement.

the execute returns an _RecordsetPtr. How did you declared your recordset-member m_recordset?

here's a code snipped how I call the execute:

_RecordsetPtr m_spRecordset;

VARIANT varAffected;
varAffected.vt = VT_I2;
m_spRecordset = m_spCommand->Execute(&varAffected,NULL,0);

and second thing: Is your connection-Object in open state?
0
 

Author Comment

by:mark50
ID: 6186004
My connection object is not in an open state.

Consequently I feel like a complete tit !

here's your points.

thanks
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

728 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