Solved

DLL requires a char[]....

Posted on 2004-03-24
8
543 Views
Last Modified: 2012-08-13
OK, I have a dll written in VC++5.0 which as one of the parameter requires a char[] reference which is expected to have been already allocated by the calling function.  

the function declaration looks like this

Declare Auto Function cspGetBarcode Lib "csp32.DLL" (ByRef stBarData As Char(), ByVal lgBarcodeNumber As Long, ByVal maxLength As Long) As Int16

and the call like this:

Dim strCode As String = New String(" ", 10)
code = cspGetBarcode(strCode, i, codeLength)

obviously the reference is not being passed as I expected as I end up with a string exactly the same as it was before the call.
I know that the marshaller has something to do with this and have tried using a stringBuiler object.  I can't seem to get that to work either.

Any ideas ..?

this must be a fairly common problem.

Mahna Mahna.
0
Comment
Question by:AndyCapp922004
  • 5
  • 3
8 Comments
 
LVL 6

Expert Comment

by:KarunSK
ID: 10672509
The String data type is immutable in .NET; which means that you cannot change the value in the string once assigned. Try this:

Declare Auto Function cspGetBarcode Lib "csp32.DLL"
(<MarshalAs(UnmanagedType.LPStr)> ByRef stBarData As StringBuilder,
  ByVal lgBarcodeNumber As Long,
  ByVal maxLength As Long) As Int16
0
 

Author Comment

by:AndyCapp922004
ID: 10672698
Karun,

That was the same road I was going down... though the example i had was buiding the Declaration a little  different (I couldn't get it to work until trying yours).

To no avail. with any of the unmanaged types supported by stringBuilder...

I am trying those with String type instead ... no luck yet though.
I'm not even getting an error, just empty strings on the other end.
Everything else from the dll is good, quantity of values, status values etc, etc.

0
 
LVL 6

Expert Comment

by:KarunSK
ID: 10672812
Did you try out LPTStr and LPWStr versions? (They stand for Ansi and Unicode encoding of the strings resply). This will take care of encoding differences, if any should exist.

Or, you can pass an array of chars as input and later do a ToString when it comes back.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:AndyCapp922004
ID: 10677565
yep.. tried those still get zip in return.  Tried an array of characters also.  

I dug through the cpp file included with the dll  (convenient).... here is the actual function.

/*******************************************************************
 *              CSP Data Get                                       *
 *******************************************************************/

/*
 ** long DLL_IMPORT_EXPORT cspGetBarcode(char szBarData[], long nBarcodeNumber, long nMaxLength)
 *
 *  FILENAME: C:\Symbol\Csp32Proj\Csp32\Src\Csp32.cpp
 *
 *  PARAMETERS:     szBarData[] is a character array the user has allocated
 *                  to hold the barcode data. The string will be null terminated.
 *                  nBarcodeNumber indicates which barcode stored in
 *                  the szCspBcString[] will be returned.
 *                  nMaxLength is the length of the allocated space including the
 *                  null terminator. If nMaxLength is set to DETERMINE_SIZE, the
 *                  function will return the length of the barcode without
 *                  copying any data.
 *
 *  DESCRIPTION:    This function copies the barcode data from szCspBcString[]
 *                  to szBarData[] for up to nMaxLength characters. If the
 *                  barcode is longer it will be truncated.
 *
 *  RETURNS:        > 0     length of the barcode including the NULL terminator
 *                  BAD_PARAM if the requested barcode does not exist
 */

NoMangle long DLL_IMPORT_EXPORT cspGetBarcode(char szBarData[], long nBarcodeNumber, long nMaxLength)
{
    long i;
    long nBarFound;
    long nBarLength;
   
    // make sure the requested barcode is valid...
    if (nBarcodeNumber < 0)
        return( BAD_PARAM );

    if (nBarcodeNumber >= nCspStoredBarcodes)
        return( BAD_PARAM );

    // OK, go find the barcode...
    i = 0;
    nBarFound = 0;
    while (nBarFound < nBarcodeNumber)
        {
        // Look for the null terminator separating the strings...
        while (szCspBarData[i++] != 0);
       
        // bump count to the next barcode...
        nBarFound++;
        }  

    // OK, we've got the barcode, so process it...
    nBarLength = strlen(&szCspBarData[i]) + 1;

    // did the user only request the string's length?
    if (nMaxLength > DETERMINE_SIZE)
        {
        // get the maximum number of characters to copy...
        nMaxLength = min(nBarLength,nMaxLength);

        // copy the barcode...
        memcpy(szBarData, &szCspBarData[i], nMaxLength);
       
        // and null terminate the string...
        szBarData[nMaxLength-1] = 0;
        }

    return(nBarLength);
}

This only tells me that the stringbuilder i was originally using should work.  Perhaps you'll see it  though?
0
 
LVL 6

Expert Comment

by:KarunSK
ID: 10679763
Let me check this out and get back to you. Meanwhile, just an observation, I noticed that the function header comment says szCspBcString[] whereas the function uses szCspBarData[]. I hope this is not a bug.
0
 
LVL 6

Accepted Solution

by:
KarunSK earned 125 total points
ID: 10680485
OK. Here we go. I got this interface working (though the logic inside was different, but you should not have any problem either):

Original declaration:

NoMangle long DLL_IMPORT_EXPORT cspGetBarcode(char szBarData[], long nBarcodeNumber, long nMaxLength)

Imported stmt in .NET:

    <DllImport("csp32.DLL", CharSet:=CharSet.Ansi, EntryPoint:="cspGetBarcode")> _
    Public Shared Function cspGetBarcode(<MarshalAs(UnmanagedType.LPStr)> ByVal stBarData As StringBuilder, _
                                         ByVal BarcodeNumber As Int32, _
                                         ByVal MaxLength As Int32) As Int32

Thing to remember: C/C++ long != .NET Long; former is 4 bytes, latter is 8. So Int32 is the corresponding type for "long".
You can get this code to work even without the CharSet and MarshalAs attributes, but using them always forces your code to use Ansi rather than Unicode.

In my earlier post I mentioned the StringBuilder as ByRef, sorry for that. It should be ByVal since it is an object itself.

Now to access it:

        Dim sbBarCode As New StringBuilder(<length>)
        'Populate sbBarCode if required.
        Dim BarLength As Int32 = cspGetBarcode(sbBarCode, <BarCodeNumber>, <MaxLength>)

Note that instead of StringBuilder, you can also use a Byte array:

    <DllImport("csp32.DLL", CharSet:=CharSet.Ansi, EntryPoint:="cspGetBarcode")> _
    Public Shared Function cspGetBarcode(<MarshalAs(UnmanagedType.LPStr)> ByVal stBarData As Byte(), _
                                         ByVal BarcodeNumber As Int32, _
                                         ByVal MaxLength As Int32) As Int32

And then:

        Dim sbBarCode As Byte = New Byte(<length>) {}

Sorry it took me some time to figure this out, but my C++ is all rusty (I could literally hear it creaking in my head :-)). Thanks to you, I got to dig this up.

Karun.
0
 

Author Comment

by:AndyCapp922004
ID: 10681115
Thank you Karun,

I had to change all of the return types from the original .bas file to get anything to work at all.  At the time I couldn't understand why int16 was working and not int32.  As I it turns out it the same .bas (vb5 or 6) file set up all the necessary enumerations as VB Long instead of int32(not sure why I could not at receive as VB long though).

I have changed all instances of Long to Int32 and things are working perfectly.

I never considered the fact that the numeric parameters were the problem.  It makes crystal clear sense now though, that the string was never being copied over because of this.  Live and learn. Thanks again Karun, this will was my first attempt at an unmanaged dll import from VB.NET (actually first .dll intreraction altogether) and it is afterall a success!!!

Andy.
0
 
LVL 6

Expert Comment

by:KarunSK
ID: 10681469
Here is a very good link that will help you further in using data types for Interoperability:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp09192002.asp
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question