DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    TLN1 Guest

    Calling unmanaged C DLL function with char args


    Ok,

    I have a problem with VB.NET calling an unmanaged VC 6.0 C DLL from a form
    that has char * args. The function prototype for the particular C function
    in the C DLL is:

    long _stdcall srv_binary_dxf_pntbat(char *inputname, char *dxfname, char
    *cmdfile)

    I have many other functions in this DLL that I call from my form with double
    and integer args with NO problems whatsoever. Its the char/string stuff that
    is giving me fits. In VB6 I use the following Declare:

    'For using my 'C' dll
    Public Declare Function srv_binary_dxf_pntbat Lib "srvm20.dll" (ByVal sBCGOfile
    _
    As String, ByVal sDXFfile As String, ByVal sCMDfile As String) As Long

    It works fine in VB6.

    I have tried passing the VB.NET args as byte arrays(ByRef and ByVal) with
    a terminating NULL char with no luck at all - I have tried marshaling the
    args with my declare in a module like this:

    Public Declare Ansi Function srv_binary_dxf_pntbat Lib "srvm20.dll" _
    (<MarshalAs(UnmanagedType.LPStr)> ByVal sBCGOfile As String, _
    <MarshalAs(UnmanagedType.LPStr)> ByVal sDXFfile As String, _
    <MarshalAs(UnmanagedType.LPStr)> ByVal sCMDfile As String) As Integer

    In my VB.NET form I call this function like so - all args are the correct
    type:

    lRx = srv_binary_dxf_pntbat(sBDA, sDXF, sCMD)

    No matter what I do, it errors with err.number=91 with an error message of
    "Object Reference Not set to an instance of an object"

    I am just learning .NET and it has been a shall we say a "challenge" for
    me. It is so different from VB6 its almost overwhelming - I am determined
    to get better at this coding but can one of you wizards help me over this
    hump?

    Thanks in advance,
    Terry


  2. #2
    Cali LaFollett Guest

    Re: Calling unmanaged C DLL function with char args

    Terry,

    > Public Declare Ansi Function srv_binary_dxf_pntbat Lib "srvm20.dll" _
    > (<MarshalAs(UnmanagedType.LPStr)> ByVal sBCGOfile As String, _
    > <MarshalAs(UnmanagedType.LPStr)> ByVal sDXFfile As String, _
    > <MarshalAs(UnmanagedType.LPStr)> ByVal sCMDfile As String) As Integer


    Try this:

    Public Declare Ansi Function _
    srv_binary_dxf_pntbat Lib "srvm20.dll" _
    ByVal sBCGOfile As StringBuilder, _
    ByVal sDXFfile As StringBuilder, _
    ByVal sCMDfile As StringBuilder) As Integer

    You will need to include and import for the following:

    Import System.Text

    You will also need to initialize initialize your StingBuilder objects to the
    correct information wether that is a buffer of " " or your data similar to
    this:


    'Buffer of 1000 spaces
    Dim sbBDA As New StringBuilder(1000)

    'Actual value
    Dim sbBDA As New StringBuilder("C:\somfile.txt")

    I can't guarantee it will work but I have found the StringBulider class to
    be much easier to work with when working with Strings and C API calls. Like
    you, I probably haven't stumbled across the "String" tricks yet. ;-)

    > I am just learning .NET and it has been a shall we say a "challenge" for
    > me. It is so different from VB6 its almost overwhelming - I am determined
    > to get better at this coding but can one of you wizards help me over this
    > hump?


    Hang in there. I get's much better!!! (and a heck of a lot easier too, even
    over VB6 IMHO)

    Cal



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links