Help with calling external DLL....

G

Guest

Hey all,

Just starting to play with calling DLL's outside the .NET sandbox and
I'm hoping for some help with the following:

The function prototypes:

[DllImport("GM6S32.Dll")]
public static extern long GMW_DS_Query(
[MarshalAsUnmanagedType.LPStr)] string szSQL,
[MarshalAs(UnmanagedType.LPStr)] string szFilter,
[MarshalAs(UnmanagedType.LPStr)] string szFDlm,
[MarshalAs(UnmanagedType.LPStr)] string szRDlm );

[DllImport("GM6S32.Dll")]
public static extern long GMW_DS_Fetch(
long iHandle,
[MarshalAs(UnmanagedType.LPStr)] string szBuf,
int iBufSize,
int nGetRecs );


The code I'm calling them with:

long queryret;
long fetchret;
string qryretstring="";
queryret=GMAPISupport.GMW_DS_Query(qrystring,"","0","0");
fetchret= GMAPISupport.GMW_DS_Fetch(queryret,qryretstring,32767,1);
GMAPISupport.GMW_DS_Close( queryret );


The problem is with the GMW_DS_Fetch call, it is returning a null
reference exception for some reason. The params are:

GMW_DS_Fetch( <iHandle>, <szBuf>, <iBufSize>, <nGetRecs> )

Parameters:
<iHandle> The Datastream handle returned by GMW_DS_Query
<szBuf> Buffer to hold result packet
<iBufSize> Size of returned <szBuf>
<nGetRecs> Number of records to fetch.

So, the szBuf param is really an empty string that I'm passing ByRef so
the DLL has somewhere to dump its data. The general Idea is to run the
call twice, once to get the return data params (the size of the return
set, number of records, etc.,) again to get the data. Here, I'm just
using the max possible values and trying to return a single record to
keep it simple but, as I said earlier, I'm getting a null reference
exception.

Here's the manufacturer's code example in C++ (SQL Query shortened for
clarity):

long iHandle = 0;
long iOK = 0;

char *szSQL1 = "SELECT .... FROM....WHERE"
// send DataStream SQL Query
if((iHandle = GMW_DS_Query(szSQL1)) > 0 )
{
// allocate buffer for 200 contacts at 40 chars per/name
long iBufSize = 200*40 +20;
char *szBuf = new char[iBufSize];

// fetch first 200 records into buffer
iOK = GMW_DS_Fetch( iHandle, szBuf, iBufSize, 200 );

// do whatever with the data
ODS( szBuf );

// make sure to delete the buffer
delete [] szBuf; szBuf = NULL;

// close the query
iOK = GMW_DS_Close( iHandle ); iHandle = 0;
}

Any ideas?

-r
 
M

Mattias Sjögren

[DllImport("GM6S32.Dll")]
public static extern long GMW_DS_Fetch(
long iHandle,
[MarshalAs(UnmanagedType.LPStr)] string szBuf,
int iBufSize,
int nGetRecs );

You should use StringBuilder instead of string for output string
buffers.

iHandle should most likely be of type int instead of long.



Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top