?
Solved

c++ BSTR to ATL object

Posted on 2001-06-13
13
Medium Priority
?
539 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 120 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

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

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

765 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