» »

[C#] unmanaged->managed code

[C#] unmanaged->managed code

Zzzzzzz ::

a mi lahko kdo pomaga pri postavljanju (DllImport) tele kode v C#?
samemu mi ne uspeva glih najbolj - malce prekomplicirane stvari so notr... se zajebavam že cel popoldne, pa mi nikakor ne uspe...

typedef struct exHTMLObject_s {
    int id;
    int size;
    unsigned char *data;
} exHTMLObject;

int EXFreeOutputBuffer(
   unsigned char* outputBuffer, 
   exHTMLObjects* referencedObjects, 
   int referencedObjectCount);

int EXBufferRTF2WEB(
   unsigned char* inputBuffer,
   int inputBufferSize,
   unsigned char** outputBuffer,
   int* outputBufferSize,
   exHTMLObject** referencedObjects,
   int* referencedObjectCount,
   int options,
   char* bgcolor,
   char* title,
   int dpi);
  • spremenil: Zzzzzzz ()

NoUse4AName ::

edit: aja slabo pogledal kaj si sploh pastal sem. Vidim da moreš iz managed v unmanaged pošiljat bufferje. To ni priporočljivo počet z dll importom. Veliko boljša bo rešitev če napišeš wrapper v managed c++, kjer se da mešat managed in unmanaged koda.

Zgodovina sprememb…

Zzzzzzz ::

you've missed the point (al pa sm jst nerazločno napisal).

torej, potrebujem pomoč pri izdelavi DllImporta za te funkcije (in strukturo), ki so definirane v enem DLL-ju, ki je napisan v ANSI-C.
tukaj sem le prekopiral C-jeve definicije; tole moram spravit v C# tipe, ampak se mi malce zatika pri bolj "kompliciranih" stvareh (npr. struktura, unsigned char*...)

NoUse4AName ::

Kar se tiče pointerjev se uporabi IntPtr, če imaš kazalce na polja pa uporabi char[], int[] itd. Kaj je pač bolj primerno. Je v msdnju mislim da precej o tem napisano.
Enkrat sem se mučil z nekaj ravno takšnim. Ampak mi projekta nikoli ni uspelo vspostavit v delujoče stanje, če je prav ne vem.
cjevski original
Seznam funkcij:

//[DllImport("libspeex.dll")]
//public static extern IntPtr speex_encoder_init( ref SpeexMode mode );
[DllImport("libspeex.dll")]
public static extern void speex_bits_init( ref SpeexBits bits );
[DllImport("libspeex.dll")]
public static extern int speex_encoder_ctl( IntPtr state, int request, ref int ptr );
[DllImport("libspeex.dll")]
public static extern void speex_bits_reset( ref SpeexBits bits );
[DllImport("libspeex.dll")]
public static extern int speex_encode( IntPtr state, [MarshalAs(UnmanagedType.LPArray)] float[] inn, ref SpeexBits bits );
[DllImport("libspeex.dll")]
public static extern int speex_bits_write( ref SpeexBits bits, [MarshalAs(UnmanagedType.LPArray)] char[] bytes, int max_len );
[DllImport("libspeex.dll")]
public static extern void speex_bits_destroy( ref SpeexBits bits );
[DllImport("libspeex.dll")]
public static extern void speex_encoder_destroy( IntPtr state );
//[DllImport("libspeex.dll")]//void *speex_decoder_init(SpeexMode *mode);
//public static extern IntPtr speex_decoder_init( ref SpeexMode mode );
[DllImport("libspeex.dll")]//int speex_decoder_ctl(void *state, int request, void *ptr);
public static extern int speex_decoder_ctl( IntPtr state, int request, ref int ptr );
[DllImport("libspeex.dll")]//void speex_bits_read_from(SpeexBits *bits, char *bytes, int len);
public static extern void speex_bits_read_from( ref SpeexBits bits, [MarshalAs(UnmanagedType.LPArray)] char[] bytes, int len );
[DllImport("libspeex.dll")]//int speex_decode(void *state, SpeexBits *bits, float *out);
public static extern int speex_decode( IntPtr state, ref SpeexBits bits, [MarshalAs(UnmanagedType.LPArray)] float[] outt );
[DllImport("libspeex.dll")]//void speex_decoder_destroy(void *state);
public static extern void speex_decoder_destroy( IntPtr state );
[DllImport("libspeex.dll")]
public static extern IntPtr speex_encoder_nb_init();
[DllImport("libspeex.dll")]
public static extern IntPtr speex_encoder_wb_init();
[DllImport("libspeex.dll")]
public static extern IntPtr speex_encoder_uwb_init();
[DllImport("libspeex.dll")]
public static extern IntPtr speex_decoder_nb_init();
[DllImport("libspeex.dll")]
public static extern IntPtr speex_decoder_wb_init();
[DllImport("libspeex.dll")]
public static extern IntPtr speex_decoder_uwb_init();

Zzzzzzz ::

ta preprosti tipi mi kar gredo...
mučijo me tile:
    unsigned char* data
    exHTMLObjects* referencedObjects,
    unsigned char** outputBuffer
    exHTMLObject** referencedObjects

Zgodovina sprememb…

  • spremenil: Zzzzzzz ()

NoUse4AName ::

to so sama polja.

Zzzzzzz ::

hm... managed c++... težka bo...
c++ ne obvladam (zanam nekaj c-ja, pa še to bolj ta simpl stvari)
tako da bom poskušal ugotoviti kaj in kako v C# (čeprav po celopopoldanskem googlanju in msdn-janju nimam kaj preveč pojma...)
najbolj me mučijo tisti tipi, ki so definirani z dvema zvezdicama; mislim, da je v c-ju to pointer na array, tku da bi morda delovalo tole:
[MarshalAs(UnmanagedType.LPArray)] ref exHTMLObject[] outputBuffer
in pa
unsigned char*
(char* je v C# lahko String - kaj pa unsigned char*? tudi?)

Zgodovina sprememb…

  • spremenil: Zzzzzzz ()

NoUse4AName ::

sej ima c# tudi unsigned, torej unsigned char[]. Z dvema zvezdicama pa je matrika torej, exHTMLObject[][]

Zzzzzzz ::

tile parametri:
   unsigned char** outputBuffer
   int* outputBufferSize
   exHTMLObject** referencedObjects
   int* referencedObjectCount
so IZHODNI parametri funkcije

Zzzzzzz ::

C# nima unsigned char....

NoUse4AName ::

sem preveril zdaj, za unisgned char se uporabi Byte, za int* se uporabi int[], za exHTMLObject** pa exHTMLObject[][]. Verjetno bo treba dat tudi ref pred vsakim parametrom.

Zzzzzzz ::

evo, mi je uspelo...
[StructLayout(LayoutKind.Sequential)]
public struct exHTMLObject {
            public int id;
            public int size;
            public byte[] data;
}

public enum exOptions {
            EXO_RESULTS=	0x0001,
            EXO_INLINECSS=	0x0002,
            EXO_WMF2GIF=	0x0004,
            EXO_XML=	0x0008,
            EXO_HTML=	0x0010,
            EXO_MEMORY=	0x0020,
            EXO_NOHEADER=	0x0040
}
       
[DllImport("irun.dll", CharSet = CharSet.Ansi)]
public static extern int EXRTF2WEB(string source,
                                          string destination,
                                          exOptions options,
                                          string bgcolor,
                                          string title,
                                          int dpi);

[DllImport("irun.dll", CharSet = CharSet.Ansi)]
public static extern int EXFreeOutputBuffer(ref StringBuilder outputBuffer,
                                          ref exHTMLObject[] referencedObjects,
                                          int referencedObjectCount);

[DllImport("irun.dll", CharSet = CharSet.Ansi)]
public static extern int EXBufferRTF2WEB(byte[] inputBuffer,
                                          int inputBufferSize,
                                          ref StringBuilder outputBuffer,
                                          ref int outputBufferSize,
                                          ref exHTMLObject[] referencedObjects,
                                          ref int referencedObjectCount,
                                          exOptions options,
                                          string bgcolor,
                                          string title,
                                          int dpi);


Vredno ogleda ...

TemaSporočilaOglediZadnje sporočilo
TemaSporočilaOglediZadnje sporočilo
»

Keylogger

Oddelek: Programska oprema
352624 (1351) Blisk
»

Funkcija GetAsyncKeyState

Oddelek: Programiranje
61621 (1324) Looooooka
»

kako narediti program ki bere drugi program

Oddelek: Programiranje
465730 (4043) imagodei
»

Dll napisan v C++ importan v C# aplikacijo

Oddelek: Programiranje
113853 (3640) cikopero
»

uporaba C++ unmanaged kode v C#.NET projektu

Oddelek: Programiranje
111895 (1618) krho

Več podobnih tem