![]() |
is a 'field' but is used like a 'type'
Ich erstelle eine Class
Code:
Und bekomme einen Fehler wie im Thread Titel beschrieben.
[SuppressUnmanagedCodeSecurity]
public sealed class TagsLibraryDefs { IntPtr HTags = IntPtr.Zero; //TagsLibrary_Create [DllImport("TagsLib.dll", EntryPoint = "TagsLibrary_Create", CharSet = CharSet.Auto)] public static extern HTags TagsLibrary_Create(); } In C++ ist HTAGS als Public definiert da HTAGS von verschiedenen Externals verwendet werden.
Code:
Also wenn HTAGS ermittelt wurde mit TagsLibrary_Create werden in folgenden API's dieses Handle übergeben.
typedef void* HTAGS;
Beispiel:
Code:
Jemand eine Idee? was schief läuft.
typedef BOOL(TAGSLIBCALL *t_TagsLibrary_Free)(HTAGS Tags);
EDIT: Ich könnte mir das HTAGS vielleicht sparen wenn ich direkt IntPtr zurückgebe. Hmm.... gruss |
AW: is a 'field' but is used like a 'type'
Wie wär's wenn Du das in eine Methode packst?
|
AW: is a 'field' but is used like a 'type'
Zitat:
Bin noch am Lernen :) Wie sieht die aus? Edit OK.. Methode meinst du Public IntPtr Test(..) Function Public void Test(..) Procedure Nur wo einsetzen Ich schreibe ne DLL gruss |
AW: is a 'field' but is used like a 'type'
Code:
*1 deklariert eine private Variable 'HTags' vom Typ 'Integer' und weißt ihr einen Wert zu.
IntPtr HTags = IntPtr.Zero; // *1
public static extern HTags TagsLibrary_Create(); // *2 *2 deklariert eine statische Methode 'TagsLibrary_Create()' mit dem Rückgabetype... 'HTags'. Nun ist 'HTags' eine private Variable, wird aber dort verwendet, wo eigentlich ein Typ stehen soll. In Delphi wäre das z.B.
Delphi-Quellcode:
Das ergibt doch keinen Sinn.
Var
HTags : PInteger = Nil; Function TagsLibrary_Create() : HTags; Was Du willst (TypeDef) lässt sich in C# nicht machen. Du kannst am Anfang der Datei zwar 'using HTags = IntPtr;' schreiben, aber das gilt nur für die Datei. Wäre 'IntPtr' eine Klasse, könntest Du 'class HTags : IntPtr{}' verwenden, aber 'IntPtr' ist ein struct (Record) und damit geht inheritance nicht. Verwende einfach IntPtr statt HTags. |
AW: is a 'field' but is used like a 'type'
Zitat:
Zitat:
Methoden sind da auch überflüssig hab's getestet. Da kann ich mir auch einfach in der Anwendung
Code:
verwenden und gut ist.
Dim Tag as IntPtr
gruss |
AW: is a 'field' but is used like a 'type'
Eben. Genau das hast Du gemacht. Und deshalb kommt der Fehler. Und da Du fragst 'Jemand eine Idee? was schief läuft.' ist diese Frage zumindest beantwortet, ne war.
|
AW: is a 'field' but is used like a 'type'
Zitat:
Dein Vorschlag ist doch genau das was ich auch gemacht habe. Und du schreibst ja selbst Zitat:
Code:
typedef int(TAGSLIBCALL *t_TagsLibrary_TagCount)(HTAGS Tags, TTagType TagType);
Die (HTAGS Tags) verwenden HTAGS mit IntPtr ersetzen. Ist letztendlich nur eine Schreibweise ob da jetzt HTAGS gecastet wird oder nicht letztendlich ist es ein IntPtr. Na ja hab die Lib nicht geschrieben für die ich den Wrapper erstelle. Wie das in Delphi aussieht ist klar nur es lässt sich in C# anscheinend nicht verwenden. gruss |
AW: is a 'field' but is used like a 'type'
Weil du ein Feld genauso benannt hast wie einen Typ kommt dieser Fehler.
Mach das nicht und der Fehler geht weg. Die einfachste Lösung ist ja wohl dem Feld einen anderen Namen zu geben |
AW: is a 'field' but is used like a 'type'
Zitat:
@Dejan Vu Ach das sollte ein Zitat(Quote) sein mit dem du mir mitgeteilt hast was ich falsch gemacht habe? Habe das wohl falsch verstanden. Im Rahmen als Quote wäre es verständlicher gewesen hab jetzt angenommen das wäre ein Vorschlag von dir gewesen. Na gut .. gruss |
AW: is a 'field' but is used like a 'type'
Variablen in einer Klasse nennt man Feld (engl. Field)
Code:
[SuppressUnmanagedCodeSecurity]
// Ich bin eine Klasse public sealed class TagsLibraryDefs { // Ich bin ein Feld, heiße HTags und bin vom Typ IntPtr IntPtr HTags = IntPtr.Zero; //TagsLibrary_Create [DllImport("TagsLib.dll", EntryPoint = "TagsLibrary_Create", CharSet = CharSet.Auto)] // Ich bin eine Methode und liefere einen Wert vom Typ HTags zurück public static extern HTags TagsLibrary_Create(); } |
AW: is a 'field' but is used like a 'type'
Sorry aber was soll das jetzt. ?
Ich weiß was eine Klasse ein Feld und eine Methode ist. Meinen Fehler immer wieder zu posten bringt einfach nichts. Es ist mir bekannt das es falsch ist. Und wie schon gesagt höchstwahrscheinlich so wie in Delphi/C++ nicht umsetzbar. Wenn doch wie ist die Lösung. ? gruss |
AW: is a 'field' but is used like a 'type'
Zitat:
Zur Erinnerung (ist wohl schon zu lange her) Zitat:
|
AW: is a 'field' but is used like a 'type'
Die beste Lösung wäre wohl:
Zitat:
Zitat:
|
AW: is a 'field' but is used like a 'type'
Die beste Lösung wäre einfach, keine Synonyme für Typen zu verwenden. Das verwirrt doch nur.
|
AW: is a 'field' but is used like a 'type'
Zitat:
Ich wollte halt die API so auslegen wie es in C++/Delphi ist, geht aber wohl nicht. Ist halt nicht überall Konform abhängig von der Developer Sprache halt. Ich habe es nun mit IntPtr gemacht. Zitat:
gruss |
AW: is a 'field' but is used like a 'type'
C-Sprachen sind case-sensitive, d.h. HTAGS != HTags
|
AW: is a 'field' but is used like a 'type'
Zitat:
Hat aber nichts mit meinem Problem zu tun. gruss |
AW: is a 'field' but is used like a 'type'
Was Sir Rufo meinte ist, dass du eine Variable (bzw. ein Feld) in der Klasse vom Typ IntPtr mit dem Namen HTags erstellt.
Die Funktion sollte aber den Typ HTAGS zurückgeben. Deshalb wohl auch die Anmerkung von Sailor. Zitat:
Code:
Man beachte das groß geschriebene HTAGS. Aber schlag mich bitte nicht, wenn das nicht so funktioniert wie gewünscht. Bin auch kein C#/C++ Profi. Habs mir nur mal für ein kleineres Projekt angesehen.
public static extern HTAGS TagsLibrary_Create();
Dein HTAGS Typ solltest du dir dann so einbauen können.
Code:
typedef IntPtr HTAGS
Stimmt, in C# gibt es ja gar kein Typdef :shock: Vielleicht hilft es ja trotzdem weiter. :roll: |
AW: is a 'field' but is used like a 'type'
Zitat:
Sie muss identisch mit dem Definierten Name sein. (Groß und Kleinschreibung beachtend) Wenn als
Code:
definiert muss sie auch in folge so benannt werden.
IntPtr HtAgS = IntPtr.Zero;
Aber das merkt der Compiler da muss man sich selbst nicht drum kümmern. Zitat:
Code:
Genau so hab ich es gemacht.
public static extern HTAGS TagsLibrary_Create();
Code:
HTags = HTags
IntPtr HTags = IntPtr.Zero;
public static extern HTags TagsLibrary_Create(); HTAGS = HTAGS HTAGS = HTags geht nicht.. Was ich aber schon sagte (Ist mir bekannt) gruss |
AW: is a 'field' but is used like a 'type'
Letztendlich habe ich die Lösung dann doch selbst noch erarbeitet (nicht aus dem Internet)
Code:
Jetzt geht das!
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct HTAGS { private IntPtr _htags; public IntPtr HTags { get { return _htags; } set { _htags = value; } } }
Code:
und das! (Darum ging es mir.. Die API gleich zu halten mit den Aufruf Konventionen.
//TagsLibrary_Create
[DllImport("TagsLib.dll", EntryPoint = "TagsLibrary_Create", CharSet = CharSet.Auto)] public static extern HTAGS TagsLibrary_Create();
Code:
in der Anwendung wird dann einmalig HTAGS definiert.
//TagsLibrary_Free
[return: MarshalAs(UnmanagedType.Bool)] [DllImport("TagsLib.dll", EntryPoint = "TagsLibrary_Free", CharSet = CharSet.Auto)] public static extern bool TagsLibrary_Free(HTAGS Tags);
Code:
Dim hTags As HTAGS
Code:
In Delphi ist der Aufruf wie folgt!
hTags = TagsLib.TagsLibrary_Create()
If TagsLib.TagsLibrary_Free(hTags) Then MessageBox.Show("Is Free") End If
Delphi-Quellcode:
t_TagsLibrary_Free = function (Tags: HTags): LongBool; {$IFDEF MSWINDOWS}stdcall{$ELSE}cdecl{$ENDIF};
Und genau diese Art des Aufrufs wollte ich beibehalten Tags: HTags Deshalb schreibe ich die Lib (Wrapper in C#) damit Developer mit VB_NET sich mit der API nicht rumquälen müssen. gruss |
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:39 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz