Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

BSTR / LPCSTR question

Posted on 2001-08-21
10
Medium Priority
?
1,527 Views
Last Modified: 2008-02-26
hello,

  I am expermenting with writing COM components using ATL COM wizard. Now I have one user defined function in my ActiveX Server component.

STDMETHODIMP Cdeers::fnDeersdll(BSTR argv, BSTR *returned_record)

Now, I would like to use Strlen function call on "argv" parameter in my function, but it won't allow me. I need to convert it into a regular string so that I use associated string function calls. Similarly, I need to convert a "LPCSTR" type to "BSTR *", how do I do it? Thanks.
0
Comment
Question by:dbdoshi
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6410177
>>I need to convert a "LPCSTR" type to "BSTR *", how do I do it?

Use '_bstr_t':

#include <comdef.h>

LPCSTR _p ="abcdef";
_bstr_t _b ( _p);

BSTR bstr = _b.copy();
0
 

Author Comment

by:dbdoshi
ID: 6410250
How do I convert a BSTR to LPCSTR?
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 6410271
use _bstr_t:

example:

#include <comdef.h>
     LPCSTR lpcstr = "Jonny";
     _bstr_t b(lpcstr);
     

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 86

Accepted Solution

by:
jkr earned 400 total points
ID: 6410274
Well, your question was: "I need to convert a "LPCSTR" type to "BSTR *"

But, for the other way round, use

_bstr_t::operator char*( ) const;

(as LPCSTR _is_ a char*), e.g.

LPCSTR _p1 ="abcdef";
_bstr_t _b ( _p);

BSTR bstr = _b.copy();

LPCSTR _p2 = (char*) _b;

If you have a BSTR*, you'll have to

BSTR* pb;

// ...

_bstr_t _b2 ( *pb);

LPCSTR _p2 = (char*) _b2;






0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 6410275
using char*()

example:

LPCSTR lpcstr = "Jonny";
    _bstr_t b(lpcstr);

LPCSTR str = (char*)b;
   


0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 6410276
jkr, u r much quicker than me :)
0
 
LVL 1

Expert Comment

by:kuchnaheen
ID: 6410418
u ppl r so cool...i am just starting ATL/COM..
0
 
LVL 86

Expert Comment

by:jkr
ID: 6410589
Well, anything else you need to know?
0
 

Author Comment

by:dbdoshi
ID: 6410625
Just one more thing...(though its unrelated to my original question). As I had explained earlier, my user defined function in my ActiveX server object is:

STDMETHODIMP Cdeers::fnDeersdll(BSTR argv, BSTR *returned_record)

Here, the "argv" variable is my [in] variable, which I pass to my function. The function does some socket connections, and makes a string which I pass return back, called "[out, retval] returned record". Now the above function "fnDeersdll" returns a "HRESULT" type, as far as I understand.

Now I have this sample code in my function.

ret = send(client_socket, encrypt_inquiry, remainder, 0);
if(ret == SOCKET_ERROR)
     {
          closesocket(client_socket);
          WSACleanup();
          return -1;
     }

Finally, if all code executes properly, I say "return 0" as the end of the function.

Now, I need to test this "0" or "1" in my calling script (my ASP Page which is going to use this component), how do I go about doing that? I cannot tack the "0/1" to my returned_record variable. Is there any other "out" variable that I need to define?





0
 
LVL 86

Expert Comment

by:jkr
ID: 6410718
In ASP, you can use the 'Number' property of your 'Err' object - see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/comsrv2k/htm/cs_rp_comparammem_isay.asp

Quote:

"Standard COM Errors
Many, but not all, of the properties and methods of the Commerce Server objects use standard Component Object Model (COM) HRESULT return values to communicate whether the operation was successful or not.

In C++, these values are the actual return values from the methods themselves (including property get_ and put_ methods). The program must explicitly perform error checking by examining these return values, or use some variation of structured error handling.

In Microsoft Visual Basic (VB), and in Visual Basic Scripting Edition (VBS) as used in Active Server Pages (ASP), these values are placed in the Number property of the global Err object."
0

Featured Post

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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

885 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