Solved

BSTR / LPCSTR question

Posted on 2001-08-21
10
1,521 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
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 100 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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 how to clear a vector as well as how to detect empty vectors in C++.

752 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