Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How Can I get driver list in mfc c++? I need MySql diver list;

What I have tried:

I found GetDriverList, but I don't know how I can use it
Posted
Updated 8-Feb-22 13:58pm
v2
Comments
PIEBALDconsult 18-Jan-22 11:21am    
Via ODBC?
Member 14594285 18-Jan-22 12:04pm    
yes driver ODBC
Member 14594285 18-Jan-22 12:07pm    
I saw in internet that I can use

SQLGetInstalledDriversW ( LPWSTR lpszBuf,
WORD cbBufMax,
WORD * pcbBufOut
)

but I don't understand how use this function

This is a fairly typical MFC function to return information about a module or component.

The 1st argument "LPWSTR lpszBuf" is a string to which the function will return the requested information (the driver list).
The 2nd argument "WORD cbBufMax" is the length in characters of the first argument. It's used by the function to ensure it doesn't overflow the first argument when it's called. You must ensure the string you supply as the first argument is at least as long as the value you pass here.
The 3rd argument "WORD *pcbBufOut" is a pointer to a WORD variable. The function returns the number of characters it actually wrote to the buffer - including null terminators.

The function returns a boolean BOOL. True if the call is successful, False otherwise. If it returns False, the 1st and 3rd arguments will have no meaningful values. And you'll need to determine the cause of the error by calling another function (SQLInstallerError I think) - an internet search will show what and how to call. It is similar in nature to this function.

Once a successful call has been made you'll need to parse the result returned in the first argument. Since it is possible for more than one SQL driver to be installed on your computer this result may consist of 1 or more driver strings in your supplied character buffer. Each driver string will be separated in your buffer by a null terminator and the final driver will have an additional null terminator added.
For example: "Driver 1\0Driver 2\0Driver 3\0\0"
The bolded \0 characters denote the null terminators between each driver string. You'll have to parse each driver string out one by one and examine it to determine if it's the driver you need.

C++
WCHAR driverBuffer[1000];
WORD  returnedCount;
BOOL isOK = SQLGetInstalledDriversW(driverBuffer, 1000, &returnedCount);
if (isOK)
{
    // Parse away
}
else
{
    // Determine what is wrong
}


The function you listed in your question (SQLGetInstalledDriversW) is the Multi-Byte Character Set (eg. unicode) version of the function (That's what the W signifies). I believe the character set is set by the MFC project configuration. If you're not configured for MBCS this function will give you some trouble when you compile (function not found, WCHAR undefined, etc.). If you are using MBCS you're fine. If not, use the version without the "W"(SQLGetInstalledDrivers). Then substitute char or TCHAR for WCHAR. If you use TCHAR then I believe the non-W function will work regardless as to whether or not you configure for MBCS.
 
Share this answer
 
In addition to FreedMalloc's solution, which works, here are some additional hints:
If no database support was selected when creating the project, the following includes could help.
C++
#include <windows.h>
#include <tchar.h>
#include <odbcinst.h>

#pragma comment(lib, "user32.lib")
#pragma comment(lib, "odbccp32.lib")
#pragma comment(lib, "legacy_stdio_definitions.lib")

C++
TCHAR buf[1000];
WORD  count=0;
BOOL isOK = SQLGetInstalledDrivers(buf, sizeof(buf)/sizeof(TCHAR), &count);

I get as result:
L"Driver da Microsoft para arquivos texto (*.txt; *.csv)"

No idea why the text is in Portuguese.
But "Microsoft driver for text files" sounds right.
 
Share this answer
 
v3

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