Defined type mismatches with an argument

Hi, I found a super-basic text ODBC driver (http://www.prowaretech.com/Computer/Windows/Odbc) which won't compile for me.


Line 19 calling SQLConnect requires 7 arguments, and one of them is causing an error.

Am I correct in saying that parameter 2 (see error below) should be a pointer, not a literal?

Error C2664 'SQLRETURN SQLConnectW(SQLHDBC,SQLWCHAR *,SQLSMALLINT,SQLWCHAR *,SQLSMALLINT,SQLWCHAR *,SQLSMALLINT)': cannot convert argument 2 from 'const wchar_t [6]' to 'SQLWCHAR *'


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <windows.h>
#include <sql.h>
#include <sqlext.h>
#include <tchar.h>

#define MAX_DATA			100

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
	RETCODE rc;			 /* Return code for ODBC functions	*/
	HENV henv;				/* Environment handle				*/
	HDBC hdbc;				/* Connection handle				 */
	HSTMT hstmt;			/* Statement handle				*/
	char szData[MAX_DATA];	/* Variable to hold data retrieved */
	SDWORD cbData;			/* Output length of data			 */

	SQLAllocEnv(&henv);
	SQLAllocConnect(henv, &hdbc);
	SQLConnect(hdbc, _T("hello"), SQL_NTS, NULL, 0, NULL, 0); // "hello" refers to the DSN
	SQLAllocStmt(hdbc, &hstmt);								 // or Data Source Name

	SQLExecDirect(hstmt, _T("SELECT * FROM hello.txt"), SQL_NTS);

	rc = SQLFetch(hstmt);
	while (rc == SQL_SUCCESS) {
		SQLGetData(hstmt, 1, SQL_C_CHAR, szData, sizeof(szData), &cbData);
		MessageBoxA(NULL, szData, "ODBC", MB_OK);
		rc = SQLFetch(hstmt);
	}
	SQLFreeStmt(hstmt, SQL_DROP);
	SQLDisconnect(hdbc);
	SQLFreeConnect(hdbc);
	SQLFreeEnv(henv);
	return(TRUE);
}


Is this a valid way to fix? And if not, why not?

1
2
3
4
	char xVal[] = {"hello"};
	char* ptrX = xVal;

	SQLConnect(hdbc, reinterpret_cast<SQLWCHAR*>(ptrX), SQL_NTS, NULL, 0, NULL, 0); // "hello" refers to the DSN 


Thanks,

Jack
What's wrong with
SQLConnect(hdbc, "hello", SQL_NTS, NULL, 0, NULL, 0);


The _T() macro is what you use for passing strings into Win32 API functions, so it does the right thing when switching between ANSI and UNICODE mode (which map to char and wchar_t for the respective character types).

You wouldn't normally use _T() for strings being passed to third party libraries such as SQL. At least not until you'd read the documentation to find out.

> MessageBoxA(NULL, szData, "ODBC", MB_OK);
Now this one should be
MessageBox(NULL, szData, _T("ODBC"), MB_OK);

If I use the literal as suggested, I get:

Error (active) E0167 argument of type "const char *" is incompatible with parameter of type "SQLWCHAR *"

Error C2664 'SQLRETURN SQLConnectW(SQLHDBC,SQLWCHAR *,SQLSMALLINT,SQLWCHAR *,SQLSMALLINT,SQLWCHAR *,SQLSMALLINT)': cannot convert argument 2 from 'const char [6]' to 'SQLWCHAR *'

Message Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


Is it possible to:
1
2
SQLWCHAR x[] = "hello";
SQLConnect( hdbc, x, ...
Tried that without success, but this works:
1
2
SQLWCHAR x[] = { L"hello" };
SQLConnect(hdbc, x, SQL_NTS, NULL, 0, NULL, 0);


Thanks for your help. If you know of a guide that I could use to help me understand what C++ is doing with all these typdefs, and what effect they have, please let me know!

Jack
SQLWCHAR is not defined by C++. It is probably defined by the third-party SQL library that you use.
Many of the other types/macros are defined by Microsoft Windows.

The prefix L is C++. It makes the compiler handle the source code for the string literal as wide chars, rather than narrow.
https://en.cppreference.com/w/cpp/language/string_literal
Topic archived. No new replies allowed.