Solved

DLL requires a char[]....

Posted on 2004-03-24
8
540 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
 

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
DevOps Toolchain Recommendations

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

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now