Announcement

Collapse
No announcement yet.

Funktionen aus C# COM-DLL in C++ aufrufen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Funktionen aus C# COM-DLL in C++ aufrufen

    Hallo

    Ich habe folgende COM-DLL in C# geschrieben:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace CL
    {
        [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsDual)]
        public interface _Mathe
        {
            [DispId(1)]
            int add(int a, int b);
    
            [DispId(2)]
            int sub(int a, int b);
    
            [DispId(3)]
            int mult(int a, int b);
        }
    
        [ClassInterface(ClassInterfaceType.None)]
        [ProgId("CL.Mathe")]
        public class Mathe : _Mathe
        {
            public Mathe() { }
    
            public int add(int a, int b)
            {
                return (a + b);
            }
    
            public int sub(int a, int b)
            {
                return (a - b);
            }
    
            public int mult(int a, int b)
            {
                return (a * b);
            }
        }
    }
    Nun möchte ich die in der DLL enthaltenen Funktionen in C++ aufrufen, habe aber leider keinen blassen Schimmer wie das in C++ geht.

    Kann mir jemand helfen?

  • #2
    Siehe auch: http://entwickler-forum.de/showthread.php?t=52290

    mfG Gü
    "Any fool can write code that a computer can understand. Good programmers write code that humans can understand". - Martin Fowler

    Comment


    • #3
      In dem Beitrag finde ich leider keine Lösung für mein Problem. Sonst noch Vorschläge?

      Comment


      • #4
        Für alle die es interessiert:

        http://www.codeproject.com/KB/COM/nettocom.aspx

        Hab die Lösung gefunden.

        Tipp: Beispiel-Dateien runterladen und nachvollziehen!

        Comment


        • #5
          Originally posted by Bundel View Post
          Für alle die es interessiert:

          http://www.codeproject.com/KB/COM/nettocom.aspx

          Hab die Lösung gefunden.

          Tipp: Beispiel-Dateien runterladen und nachvollziehen!
          ich komm damit nicht klar

          bin schon den ganzen tag am verzweifelten versuchen, eine COM-DLL mit VS2008 Standard via C# zu erstellen um sie später im Excel einbinden zu können.

          Aber ich bekomm keine Einstiegspunkte definiert, also weder Excel mag sie finden noch kann ich sie mit Dependency Walker sehen und das ist auch bei dem Beispielprojekt von oben so ...

          kann mir grad mal einer auf die sprünge helfen, wie das funktionieren soll?

          also:
          ich hab eine klassenbibliothek angelegt, in den projekteigenschaften auf "Für Com Interop registrieren" aktiviert und folgenden Quelltext verbrochen:

          Code:
          using System;
          using System.Data.OleDb;
          using System.Data;
          using System.Runtime.InteropServices;
          
          namespace SolDataSys
          {
              [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
              public interface myCaller
              {
                  [DispId(1)]
                  string[] mySolar(string myModulId);
              }
              
              [ClassInterface(ClassInterfaceType.AutoDispatch)]
              [ProgId("SolDataSys.mySolData")]
              public class mySolData : myCaller
              {
                  public string[] mySolar(string myModulId)
                  {
                      string[] myArray = new string[5];
                      myCall(myModulId, out myArray);
                      return myArray;
                  }
          
                  private void myCall(string myModulId,out string[] myArray)
                  {
          	 //funktionierende SQL-DBabfrage
                  }
          
                  
                  #region myCaller Member
          
                  string[] myCaller.mySolar(string myModulId)
                  {
                      throw new NotImplementedException();
                  }
          
                  #endregion
              }
          erstellt und die dll ist auch registriert worden, aber sie tuts nicht.

          was mach ich falsch???

          Comment


          • #6
            @ f0r3nD1v@:

            schau dir mal das an - vielleicht hilft es:
            http://entwickler-forum.de/showthread.php?p=194548

            mfG Gü
            "Any fool can write code that a computer can understand. Good programmers write code that humans can understand". - Martin Fowler

            Comment


            • #7
              Originally posted by gfoidl View Post
              @ f0r3nD1v@:

              schau dir mal das an - vielleicht hilft es:
              steht bereits auf true, hatte ich in AssemblyInfo.cs schon eingetragen

              Comment


              • #8
                so, jetzt schein ich es verstanden zu haben, zumindest funktionierts in VBA

                Code:
                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Data;
                using System.Text;
                using System.Data.OleDb;
                using System.Runtime.InteropServices;
                
                namespace SolDataSysDll
                {
                    [Guid("d10f293c-036c-4690-8f8f-201309247998")]
                    [ComVisible(true)]
                
                    public interface ISolData
                    {
                        string getData(string myModulId);
                    }
                    
                    [Guid("e32b3f9f-e492-4cd2-b468-e8d5085df1a5")]
                    [ClassInterface(ClassInterfaceType.None)]
                    [ComVisible(true)]
                
                    public class SolData : ISolData
                    {
                
                        public SolData()
                        {
                
                        }
                
                        public string getData(string myModulId)
                        {
                            try
                            {
                                string myResult = "";
                                //Abfrage
                
                                if (myDataSet.Tables.Count == 1)
                                {
                                    for (int i = 0; i < 6; i++)
                                    {
                                        myResult = myResult + myDataSet.Tables[0].Rows[0].ItemArray[i].ToString() + ";";
                                    }
                                    return myResult;
                                }
                                else
                                    return "";
                            }
                            catch
                            {
                                return "";
                            }
                        }
                    }
                }
                im Excel dann den Verweis auf die DLL (stand erst nicht in der Liste, ging aber die tlb)

                und dann

                Code:
                Sub GetManagedFunction()
                    Dim ccw As SolData
                    Set ccw = New SolData
                    
                    Dim myString As String
                    Dim myArray() As String
                        
                    myString = ccw.GetData("*abfragewert*")
                    
                    myArray = Split(myString, ";")
                    
                    For i = 0 To UBound(myArray)
                        ActiveSheet.Cells(1, i + 1).Value = myArray(i)
                    Next
                    
                End Sub
                so kann ich wenigstens beruhigt feierabend machen

                Comment

                Working...
                X