Autor Beitrag
AeroX
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Do 03.04.08 10:32 
hallo,
ich habe mal probiert eine DLL einzubinden, aber das geht net (..kann nicht gemarshalt werden)

hier der code:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace UseDLLs
{
    class Program
    {
        [DllImport("Search.dll")]
        public static extern string[] SearchForFile(string _dir, string _file, int _ArrayLength);

        static void Main(string[] args)
        {
            Console.WriteLine(SearchForFile(Directory.GetCurrentDirectory(), "Search.dll"1)[0]);
        }
    }
}


Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt


Zuletzt bearbeitet von AeroX am Do 03.04.08 11:20, insgesamt 1-mal bearbeitet
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 03.04.08 10:55 
Dein Thread-Titel ist ein bisschen sparsam, erweitere ihn bitte um ein paar mehr Informationen. Danke! :-)

Zu Deinem Problem: Wie sieht denn die Signatur der Funktionen in der search.dll aus?

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
AeroX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Do 03.04.08 11:24 
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace Search
{
    public class Class1
    {
        string[] FileArray;
        int i;

        public string[] SearchForFile(string _dir, string _file, int _ArrayLength)
        {
            i = 0;
            FileArray = new string[_ArrayLength];
            OpenDir(_dir, _file);
            return FileArray;
        }

        private void OpenDir(string _dir, string _file)
        {
            try
            {
                foreach (string file in Directory.GetFiles(_dir))
                {
                    if (file.Equals(_file))
                    {
                        FileArray[i] = file;
                        i++;
                    }
                }
                foreach (string dir2 in Directory.GetDirectories(_dir))
                {
                    OpenDir(dir2,_file);
                }
            }
            catch { }

        }
    }
}


Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 03.04.08 11:37 
DLLImport wird nur für DLLs benutzt, die nicht managed sind. In Deinem Fall kannst Du viel einfacher vorgehen:

Füge Deiner Anwendung eine Referenz auf die DLL-Datei hinzu (wie Du es mit den DLLs des Frameworks auch machst) und binde den Namespace "Search" ein. Dann kannst Du den Inhalt der DLL so nutzen, als wäre er in Deiner Anwendung.

Bitte benutze demnächst cs- statt code-Tags. Danke!



//edit: Noch ein paar Anmerkungen zu Deiner Klasse:

1. "Class1" ist kein guter Name für Deine Klasse
2. Benutze für das FileArray eine List<String> anstatt eines Arrays. Dann musst Du auch nicht so gruselige Code-Konstrukte benutzen, um abzufangen, wenn Du über Array-Grenzen hinaus Dateien einfügen willst.
3. Mache FileArray als lokale Variable und übergebe es als Parameter an OpenDir.
4. Parameter mit "_" anfangen zu lassen ist schlechter Stil
5. Die Namen der Felder einer Klasse sollten mit einem kleinen Buchstaben anfangen.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
AeroX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Do 03.04.08 13:00 
Vielen Dank für die umfangreiche Hilfe!

noch ne kleine Frage:

wenn ich das so mache, wird dann die Datei beim Kompilieren mit eingeschlossen, oder beim starten des Programmes?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 03.04.08 14:15 
Die DLL wird nicht einkompiliertt, Du musst sie mit ausliefern und beim Starten des Programmes wird danach gesucht (oder bei erster Benutzung? :gruebel: Bin gerade unsicher, wird für Dich aber wahrscheinlich nicht so wichtig sein)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
AeroX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Do 03.04.08 14:39 
alles klar,

und wie mache ich eine unmanaged-DLL?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Do 03.04.08 14:42 
Mit einer Sprache, die nativen Code ausspuckt. Also z.B. Delphi, dafür müsstest Du noch nichtmal die Community wechseln, denn wir haben in der Entwickler-Ecke neben dem C#-Forum ja auch das Delphi-Forum ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
AeroX Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 68



BeitragVerfasst: Do 03.04.08 17:55 
und wie kann ich dann z.B. mit einer if-anweisung eine Datei einlesen?
also

if (...) { Import-dll }