accessing unmanaged DLL in C#

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • delphiconsultingguy@yahoo.com

    accessing unmanaged DLL in C#

    Hello,

    I'm trying to convert a VB.Net app into C#. The VB app calls functions
    in an unmanaged DLL. I'm able to call most of the functions in the DLL
    from C# with the exception of the one below:

    in VB.Net, the unmanaged function is declared through an interop as:

    Public Overridable Function LocGetName(ByVa l pLoc As Integer, ByVal
    pLocNum As Integer, ByVal pType As LOCOLELib.tLocN ameType, ByVal
    pFullName As Boolean, ByRef pName As String, ByVal pMaxSize As Integer)
    As String

    and I've declared it in C# like:

    [DllImport("Loca tion.dll")]
    static extern string LocGetName(int location, int locationNumber,
    locationNameTyp e locationNameTyp e, bool fullName, ref string name, int
    maxSize);

    when I run the following statement in VB.Net, it works fine:

    LocOle.LocGetNa me(llLocations, llLocIndex,
    LOCOLELib.tLocN ameType.eLocNam eState, True, lsStateName.Val ue,
    LOCOLELib.tLocO leConstants.eLO C_NAME_STATE_LE N)

    but when I run the following statement in C#, I get 'Object reference
    not set to an instance of an object':

    LocGetName(loca tions, i, locationNameTyp e.state, true, ref stateString,
    MAX_STATE_LENGT H);

    Every parameter sent is an integer with the exception of the 'true',
    and the statestring. I've tried initializing the stateString to some
    text, I've tried changing the parameter type to 'out', and pass by
    value, to no avail. I've even tried declaring my stateString with a
    marshaling attribute:

    [MarshalAs(Unman agedType.ByValT Str, SizeConst=MAX_S TATE_LENGTH)]
    string stateString = "";

    and that didn't work. I also tried declaring stateString as a
    StringBuilder object (changing the function parameter types
    accordingly) and that didn't work. Is there anything else I'm missing?
    thanks,
    Sean

  • Q. John Chen

    #2
    Re: accessing unmanaged DLL in C#

    Try
    StringBuilder stateString,

    Comment

    • delphiconsultingguy@yahoo.com

      #3
      Re: accessing unmanaged DLL in C#

      > Try[color=blue]
      > StringBuilder stateString,[/color]

      I tried that, it still didn't work

      Comment

      • Mattias Sjögren

        #4
        Re: accessing unmanaged DLL in C#

        [color=blue]
        >in VB.Net, the unmanaged function is declared through an interop as:
        >
        >Public Overridable Function LocGetName(ByVa l pLoc As Integer, ByVal
        >pLocNum As Integer, ByVal pType As LOCOLELib.tLocN ameType, ByVal
        >pFullName As Boolean, ByRef pName As String, ByVal pMaxSize As Integer)
        >As String[/color]

        That can't be a VB.NET external function declaration. For that you
        either the Declare statement, or have a Shared method decorated with
        the DllImport attribute, and Overridable can't be used on shared
        methods. How about posting the real declaration?




        Mattias

        --
        Mattias Sjögren [MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • delphiconsultingguy@yahoo.com

          #5
          Re: accessing unmanaged DLL in C#

          > >Public Overridable Function LocGetName(ByVa l pLoc As Integer, ByVal[color=blue][color=green]
          > >pLocNum As Integer, ByVal pType As LOCOLELib.tLocN ameType, ByVal
          > >pFullName As Boolean, ByRef pName As String, ByVal pMaxSize As[/color][/color]
          Integer)[color=blue][color=green]
          > >As String[/color]
          >
          > That can't be a VB.NET external function declaration. For that you
          > either the Declare statement, or have a Shared method decorated with
          > the DllImport attribute, and Overridable can't be used on shared
          > methods. How about posting the real declaration?[/color]

          The program was originally in old VB, and when I loaded it into Visual
          Studio .NET, it was morphed into a .NET version that utilized a newly
          created Interop dll. The contents of this Interop dll can be viewed in
          the object browser, and by navigating its tree I was able to find the
          LocGetName function and it was described at the bottom of the window
          exactly how I wrote it above.

          Sean

          Comment

          • Mattias Sjögren

            #6
            Re: accessing unmanaged DLL in C#

            [color=blue]
            >The program was originally in old VB, and when I loaded it into Visual
            >Studio .NET, it was morphed into a .NET version that utilized a newly
            >created Interop dll.[/color]

            I'm not that familiar with the VB upgrade wizard, but I though it only
            created interop assemblies for COM components you reference, not
            Declare statements. Is the library you're calling a COM library or
            does it export static entrypoints?



            Mattias

            --
            Mattias Sjögren [MVP] mattias @ mvps.org
            http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
            Please reply only to the newsgroup.

            Comment

            • delphiconsultingguy@yahoo.com

              #7
              Re: accessing unmanaged DLL in C#

              > I'm not that familiar with the VB upgrade wizard, but I though it
              only[color=blue]
              > created interop assemblies for COM components you reference, not
              > Declare statements. Is the library you're calling a COM library or
              > does it export static entrypoints?[/color]

              It is an interop assembly that is generated; there are no declare
              statements. I pulled that function's definition from the object
              browser but it's not in code anywhere.

              Comment

              • Mattias Sjögren

                #8
                Re: accessing unmanaged DLL in C#

                [color=blue]
                >It is an interop assembly that is generated; there are no declare
                >statements. I pulled that function's definition from the object
                >browser but it's not in code anywhere.[/color]

                OK, then you should't write any DllImport methods at all. Just
                reference the interop assembly generated for the VB project.



                Mattias

                --
                Mattias Sjögren [MVP] mattias @ mvps.org
                http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
                Please reply only to the newsgroup.

                Comment

                Working...