Solved

BSTR / LPCSTR question

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create string splitting user defined function in C++ 23 337
Building cUrl in Windows v7.43.0 4 39
Fully specialized class template function 21 146
I could not build boost code, 10 107
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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

749 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