Solved

DLL requires a char[]....

Posted on 2004-03-24
8
537 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Expert Comment

by:KarunSK
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

763 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

12 Experts available now in Live!

Get 1:1 Help Now