Re: Convert Char to LPCOLESTR using OLEDB.H

Peter Moss <[email protected]> Thu, 18 Apr 2002 08:40:14 -0400
Newsgroups gmane.comp.windows.devel.oledb.devel
Message-ID <[email protected]>
The thing here is to get your string into a WCHAR buffer. That is what the
OLE DB API requires. As Duncan wrote in his post, you have 2 choices:

1) Declare your string member variable as WCHAR. Then you use the wide
character equivalent of strcat which is wcscat.
2) Use MultiByteToWideChar to convert from an MBCS string (I assume you are
compiling with _MBCS defined) to a WCHAR.

What do you mean by
"when I convert it using WCHAR it doesn't display string in the debug window
" ?
How are you converting it? Can we see more of the code?

Also, I assume you initially NULL the char array m_cSQLCommand in your code
somewhere. The way the code is written, it would never work because there is
probably garbage in the array so strcat is just appending garbage to
garbage. strcpy (wcscpy) would work, however.

Also, assuming you are using VS 6.0, make sure in Tools->Options... Debug
tab that Display unicode Strings is checked.

BTW, I am pretty sure the use of the conversion macros Duncan referred to
(in atlconv.h) does not require MFC. They are in the ATL library, which is a
lightweight template library. ATL does not use MFC. All those conversion
macros do is declare variables on the stack and call some Win32 API
functions. You should not fear using them. They are very valuable.

  -----Original Message-----
  From: OLEDB_DEV [mailto:[email protected]]On Behalf Of Long,
Matthew Jr. (Inland)
  Sent: Thursday, April 18, 2002 1:02 AM
  To: [email protected]
  Subject: Re: [OLEDB_DEV] Convert Char to LPCOLESTR using OLEDB.H


  Well I'm not using MFC so A2COLE is out. This is just a small WIN32 app
that get messages from IBM Mqseries and Inserts them into a Oracle database.
  When I look at the value of the variable in the debug window I noticed it
has the address, and
  to the right of the address it says `string' when I do the following.....

  lpOleDb.m_szSQLCommand = OLESTR( "SELECT COUNT(*) FROM DEVDTA.F5549018
");

  But when I convert it using WCHAR it doesn't display string in the debug
window on the variable, therefore the SetCommandText function fails.
  Any ideas how to append to a string using OLESTR? I've tried passing it a
variable and that doesn't work.


  Thanks!

  Matt




    -----Original Message-----
    From: Long, Matthew Jr. (Inland)
    Sent: Wednesday, April 17, 2002 8:39 PM
    To: [email protected]
    Subject: FW: [OLEDB_DEV] Convert Char to LPCOLESTR using OLEDB.H



    -----Original Message-----
    From: Duncan Smith [mailto:[email protected]]
    Sent: Wednesday, April 17, 2002 6:19 PM
    To: [email protected]
    Subject: Re: [OLEDB_DEV] Convert Char to LPCOLESTR using OLEDB.H


    Matt,

    There are some useful string conversion macros defined in an ATL include
file, atlconv.h. If you are not already using ATL in your project, I think
you just need to #include <atlconv.h> to use the macros. Here is your sample
modified to use the A2COLE macro, which converts an LPCSTR to an LPCOLESTR:

    char m_szSQLCommand[6000];
    USES_CONVERSION;

    strcat(m_szSQLCommand," SELECT COUNT(*) FROM DEVDTA.F5549018  ");

    m_pCommandText->SetCommandText(DBGUID_DBSQL,  A2COLE(m_szSQLCommand) );

    Another alternative is to use the Win32 API MultiByteToWideChar.

    Yet another alternative is to use WCHAR in place of char and wcscat in
place of strcat, etc. Then no string conversion is required.

    Duncan Smith

      -----Original Message-----
      From: Long, Matthew Jr. (Inland) [mailto:[email protected]]
      Sent: Wednesday, April 17, 2002 7:00 PM
      To: [email protected]
      Subject: [OLEDB_DEV] Convert Char to LPCOLESTR using OLEDB.H


      I'm trying to use OLEDB.H for the first time, and I got everything
working the way I want except for the T-SQL Command that I build on the fly.
I have a char variable that I append to using strcat. I need to convert this
char buffer to LPCOLESTR, I just don't know how, at least to where the
SetCommandText method will recononize the text in the buffer.

      // My char buffer looks like this......

      char m_cSQLCommand[6000];
      strcat(m_cSQLCommand," SELECT COUNT(*) FROM DEVDTA.F5549018  ");


      // The following is the OleDB Interface I'm using to pass my T-SQL
buffer string.
      // This compiles with no errors but it doesn't work.

      m_pCommandText->SetCommandText(DBGUID_DBSQL, (LPCOLESTR)
m_szSQLCommand );


      // Note if I do it this way it works! But this isn't going to work for
me, because I have to build this string on the fly, and the statement can be
different each time I loop through.

      m_pCommandText->SetCommandText(DBGUID_DBSQL, OLESTR("SELECT COUNT(*)
FROM DEVDTA.F5549018 "));


      Any ideas would be appreciated.....

      VC 6.0 SP5 Win32

      Thanks!

      Matt