Jump to content
Excelsior Forums
Sign in to follow this  
fneudert

Trying to get null terminated list which isgiven in String

Recommended Posts

Hello together,

I want to get the list of installed ODBC Drivers from my system. Therefore I use the function SQLGetInstalledDrivers which is defined as

BOOL SQLGetInstalledDrivers(
? ? ?LPSTR? ? ?lpszBuf,
? ? ?WORD? ? ?cbBufMax,
? ? ?WORD *? ? ?pcbBufOut);

Each driver description is terminated with a null byte, and the entire list is terminated with a null byte. (That is, two null bytes mark the end of the list.) If the allocated buffer is not large enough to hold the entire list, the list is truncated without error. An error is returned if a null pointer is passed in as lpszBuf. ( from Microsoft API Description)

In my Javaclass I have tried many possibilities but ending up getting only one entry. Here is my code:

? ? public static void main( String[] args ) {
? ? ? ? 
? ? ? ? int? ? ? ?bufferSize = 1000;
? ? ? ? xFunction sqlGetInstalledDrivers;
? ? ? ? 
? ? ? ? try {
? ? ? ? ? ? sqlGetInstalledDrivers = new xFunction( "odbccp32", "int SQLGetInstalledDrivers( CSTRING, int, int* )" );
? ? ? ? } catch ( xFunctionException e ) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? 
? ? ? ? // Holen der Liste der ODBC Treiber
? ? ? ? try {
? ? ? ? ? ? Argument arg1 = new Argument( "", Argument.CSTRING, 1000 );
? ? ? ? ? ? Argument arg2 = new Argument( bufferSize );
? ? ? ? ? ? Pointer? ptr3 = arg2.createPointer();
? ? ? ? ? ? 
? ? ? ? ? ? Object? ?oRc? ?= sqlGetInstalledDrivers.invoke( arg1, arg2, ptr3 );

? ? ? ? ? ? System.out.println( "arg1: " + arg1.getValue() );
? ? ? ? ? ? System.out.println( "arg2: " + arg2.getValue() );
? ? ? ? ? ? System.out.println( "ptr3: " + ((Integer)ptr3.deref()).intValue() );

? ? ? ? } catch ( xFunctionException e ) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? return;
? ? }

The result is only the following:

arg1: SQL Server
arg2: 795
ptr3: 795

I don't know, if SQL Server is the first or the last, but the result is 795 bytes long which is possible because I have a lot of drivers installed.

The question is, how can I access the other entries in the String at arg1?

I have tried to use Pointer and so on, but with no success.

Any help is appreciated.

Thank you in advance.

Frank

Share this post


Link to post
Share on other sites

I have found the solution myself. It is necessary to make a pointer from the argument in question, cast to char**, deref this pointer again and then create an array. Maybe not the smartest solution but it works for me. See the following code:

? ? public static void main( String[] args ) {
? ? ? ? 
? ? ? ? int? ? ? ?bufferSize = 1000;
? ? ? ? xFunction sqlGetInstalledDrivers;
? ? ? ? ?
? ? ? ? try {
? ? ? ? ? ? sqlGetInstalledDrivers = new xFunction( "odbccp32", "int SQLGetInstalledDrivers( CSTRING, int, int* )" );
? ? ? ? } catch ( xFunctionException e ) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? 
? ? ? ? // Holen der Liste der ODBC Treiber
? ? ? ? try {
? ? ? ? ? ? Argument arg1 = new Argument( "", Argument.CSTRING, 1000 );
? ? ? ? ? ? Argument arg2 = new Argument( bufferSize );
? ? ? ? ? ? Pointer? ptr3 = arg2.createPointer();
? ? ? ? ? ? 
? ? ? ? ? ? Object? ?oRc? ?= sqlGetInstalledDrivers.invoke( arg1, arg2, ptr3 );

? ? ? ? ? ? System.out.println( "arg1: " + arg1.getValue() );
? ? ? ? ? ? System.out.println( "arg2: " + arg2.getValue() );
? ? ? ? ? ? System.out.println( "ptr3: " + ((Integer)ptr3.deref()).intValue() );
? ? ? ? ? ? 
? ? ? ? ? ? Pointer p = arg1.createPointer().cast( "char**" );
? ? ? ? ? ? p = (Pointer)p.deref();
? ? ? ? ? ? char[] ca = (char[])p.createArray( ((Integer)ptr3.deref()).intValue() );

? ? ? ? ? ? StringBuffer sb = new StringBuffer(); 
? ? ? ? ? ? for ( int i = 0; i < ca.length; i++ ) {
? ? ? ? ? ? ? ? if? ( ca[i] != 0 ) {
? ? ? ? ? ? ? ? ? ? sb.append( ca[i] );
? ? ? ? ? ? ? ? ? ? continue;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if? ( sb.length() > 0 ) {
? ? ? ? ? ? ? ? ? ? System.out.println( sb );
? ? ? ? ? ? ? ? ? ? sb = new StringBuffer();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? } catch ( xFunctionException e ) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? return;
? ? }

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×