Solved

BSTR / LPCSTR question

Posted on 2001-08-21
10
1,515 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
Comment Utility
>>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
Comment Utility
How do I convert a BSTR to LPCSTR?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
use _bstr_t:

example:

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

0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
Comment Utility
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
Comment Utility
using char*()

example:

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

LPCSTR str = (char*)b;
   


0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
jkr, u r much quicker than me :)
0
 
LVL 1

Expert Comment

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

Expert Comment

by:jkr
Comment Utility
Well, anything else you need to know?
0
 

Author Comment

by:dbdoshi
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now