Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

BSTR / LPCSTR question

Posted on 2001-08-21
10
Medium Priority
?
1,525 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
[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
  • 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:sedgwick
ID: 6410271
use _bstr_t:

example:

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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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:sedgwick
ID: 6410275
using char*()

example:

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

LPCSTR str = (char*)b;
   


0
 
LVL 42

Expert Comment

by:sedgwick
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

721 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