Calling VB 6.0 Function from C# .Net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sakthikumarb
    New Member
    • Sep 2008
    • 19

    Calling VB 6.0 Function from C# .Net

    Hello All,
    I am having vb 6.0 ActiveX dll contains one method
    Code:
    Public Function GetHandleCount() As String
    Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
    Set objServicesCimv2 = GetObject("winmgmts:\\" _
        & "." & "\root\cimv2")
    Set objRefreshableItem = objRefresher.AddEnum(objServicesCimv2, "Win32_PerfFormattedData_PerfProc_Process")
    objRefresher.Refresh
    strf = Format(Now, "dd-MMM-yyyy HH:nn:ss") & "." & Right(Format(Timer, "#0.000"), 3)
      For I = 1 To 2
     objRefresher.Refresh
        For Each Process In objRefreshableItem.ObjectSet
          
            If Process.Name = "_Total" Then
              strf = strf & "  " & Process.HandleCount
          End If
        Next
    Next
     GetHandleCount = strf
    End Function
    In C#.Net console application i added this vb 6.0 dll and crated the object to that and then try to access GetHandleCount( ) method.
    Code:
     Project1.VBCls obj = new Project1.VBCls();
     Console.WriteLine(obj.GetHandleCount());
    Please find my next post for question...
  • sakthikumarb
    New Member
    • Sep 2008
    • 19

    #2
    Post Cont...


    When I run the C#.NET application getting the exception
    An unhandled exception of type 'System.Runtime .InteropService s.COMException' occurred in HandleCountSamp le.exe
    Additional information: Exception from HRESULT: 0x80041003.

    what is the solution of this error.
    I observed that This line is giving exception
    Set objRefreshableI tem = objRefresher.Ad dEnum(objServic esCimv2, "Win32_PerfForm attedData_PerfP roc_Process").

    Please give me any solution.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Well, I don't know why you are using an old VB DLL, when WMI is fully supported in .NET, but don't you need to use DLLImport when using an unmanaged resource like your VB DLL?

      Comment

      • sakthikumarb
        New Member
        • Sep 2008
        • 19

        #4
        Originally posted by Plater
        Well, I don't know why you are using an old VB DLL, when WMI is fully supported in .NET, but don't you need to use DLLImport when using an unmanaged resource like your VB DLL?
        Thanks for your reply.

        I think there is no Refresh method in C#.Net like SWbemRefresher. Refresh . Do you know any equivalent method for that in C#.Net to refresh the Objects.
        to make use of Refresh method I am calling vb 6.0 method in C#.Net.
        I Tried with DllImport also, It is giving the error "entry point not found" in vb 6.0 dll . If you have any solution for this, please provide that steps.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          When you use the DLL with DLLImport, you have to specify which public functions/objects you want to use I think.

          And what do you mean by "refresh"? Just running the query again?

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by sakthikumarb
            Thanks for your reply.

            I think there is no Refresh method in C#.Net like SWbemRefresher. Refresh . Do you know any equivalent method for that in C#.Net to refresh the Objects.
            to make use of Refresh method I am calling vb 6.0 method in C#.Net.
            I Tried with DllImport also, It is giving the error "entry point not found" in vb 6.0 dll . If you have any solution for this, please provide that steps.

            What does the "Refresh" method do?

            It sounds like you are going to have to Marshal your VB 6 code so that .NET knows how to use it properly.

            It would really simplify your life if you could just write a method in .NET that "Refreshes" your objects instead of using unmanaged code to do this.

            -Frinny

            Comment

            • sakthikumarb
              New Member
              • Sep 2008
              • 19

              #7
              Originally posted by Frinavale
              What does the "Refresh" method do?

              It sounds like you are going to have to Marshal your VB 6 code so that .NET knows how to use it properly.

              It would really simplify your life if you could just write a method in .NET that "Refreshes" your objects instead of using unmanaged code to do this.

              -Frinny
              Please check this link which talks about the "Refresh" method. It Updates information. http://msdn.microsoft. com/en-us/library/aa392091(VS.85) .aspx

              When I use the DLL with DLLImport, how and where to specify the public functions/objects that I want to use.

              Comment

              • zubair1
                New Member
                • Sep 2008
                • 79

                #8
                Originally posted by sakthikumarb
                When I use the DLL with DLLImport, how and where to specify the public functions/objects that I want to use.
                Code:
                [DllImport("MyDLL.dll", CharSet = CharSet.Unicode)] 
                public bool MyFuncEx(string exName) 
                { 
                }

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  The refresher sounds just like you are receiving an event from WMI. That can be done as well in .NET

                  Comment

                  • andyuk0000000000
                    New Member
                    • Oct 2008
                    • 5

                    #10
                    You should just be able to add a project reference to the com dll, Doing this will invoke the type library importer and create a callable wrapper for you COM library. The you can just use it. You dont need to get into writing you own interop signatures when you only using basic types.

                    Comment

                    Working...