Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the first character of the output where as I need full text in the output terminal.

Output I get:

Data Source - E
Data Source - M

What I have tried:

C++
#define NANODBC_USE_UNICODE 1
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <windows.h>  
#include <sqltypes.h>  
#include <sqlext.h>  
#include <iostream>
#include <string>
#include <stdio.h>
#include <iomanip> 

using namespace std;

#define BRWS_LEN 1000

int main() {
  

    SQLSMALLINT dsnNameLenReturned;

    SQLCHAR * driverDesc[256];
    SQLCHAR  dsnName[256];
    SQLRETURN retcode;
    SQLSMALLINT driverDescLenReturned;
    SQLUSMALLINT direction;


    // Allocate environment handle
    retcode = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
   

    // Set ODBC version
    retcode = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION,
        (void*)SQL_OV_ODBC3, 0);

    retcode = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
    
            
            // Get Data source names

            direction = SQL_FETCH_FIRST;

            SQLDataSources(henv,
                direction,
             (SQLWCHAR*)dsnName,
                sizeof(dsnName),
                &dsnNameLenReturned,
               (SQLWCHAR*)driverDesc,
                sizeof(driverDesc),
               &driverDescLenReturned);
            {

                direction = SQL_FETCH_NEXT;
               
                printf("Data Source - %s\n", dsnName);
                printf("Data Source - %s\n", driverDesc);


            }

        }

exit:
    printf("\nComplete.\n");

    if (henv != SQL_NULL_HENV) SQLFreeHandle(SQL_HANDLE_ENV, henv);
    system("pause");
    return 0;
}
Posted
Updated 24-Oct-22 21:57pm
v2

C++
SQLCHAR * driverDesc[256]; // both of these are character arrays (assuming you fixed driverDesc)
SQLCHAR  dsnName[256];

        SQLDataSources(henv,  // from the below casts I am guessing that this function returns wide characters.
            direction,
         (SQLWCHAR*)dsnName,
            sizeof(dsnName),
            &dsnNameLenReturned,
           (SQLWCHAR*)driverDesc,
            sizeof(driverDesc),
           &driverDescLenReturned);

So you now have two character arrays filled with Unicode characters. So when you try to print them as ASCII you get the results you see. You need to understand that using a cast does not change the original item, it just tells the compiler to ignore the item's declared type, and assume that you the programmer know what you are doing.

So change the definitions to
C++
SQLWCHAR driverDesc[256]; // Define with the correct type
SQLWCHAR  dsnName[256];

And do not use sizeof to get the number of wide characters. That returns the number of bytes, so you need to divide by 2, or use _countof instead.
 
Share this answer
 
v3
Comments
Abishek Samuel 25-Oct-22 5:13am    
Changed as you said but still its the same:(
Richard MacCutchan 25-Oct-22 6:30am    
Sorry, you will need to change your printf statements as follows:
printf("Data Source - %S\n", dsnName);    // use upper case S to indicate Unicode

printf("Data Source - %S\n", driverDesc);

As a general rule it is not a good idea to write ASCII based code when dealing with Unicode based libraries. You should convert your application to use all Unicode and thus avoid issues like this.
Abishek Samuel 25-Oct-22 8:07am    
Thank You so much....It worked!!!
I think the problem is the type you have declared this variable as :
C++
SQLCHAR * driverDesc[256];
That is an array of 256 pointers. The prototype says the function wants a single character pointer. Try using
C++
SQLCHAR driverDesc[256];
 
Share this answer
 
Comments
Abishek Samuel 25-Oct-22 0:59am    
I changed it and Tried .Still the same output is produced.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900