GNT sans publicité, site mobile, fonctionnalitées exclusives...

[C]Dll in csharp

Le
ZaRMaS
Hi,

I have a dll coding in C langage and I want to use it in Csharp. This
is my declaration in csharp :
[DllImport("myDll.dll")]
public static extern unsafe string
myFunction([MarshalAs(UnmanagedType.LPArray)] byte[] myData, int width,
int height);

I call in csharp like this :
try{
string test = myFunction(tempArray, 320, 240); and
tempArray is correctly filled.
}
catch {
Console.Writeline("Error in fonction DLL");
}

My C function seems like that:
char *myFunction(char *myData, int Arg_width, int Arg_height)
{
char *myResult;

//malloc of myResult
myResult = malloc(strlen(myData) * sizeof (char));

printf("I fill myResult.");

*(myData) = '\0';

printf("I return my data");
return (myData);
}



And it print :
I fill myResult.
I return my data.
Error in fonction DLL

Can anyome explain why Csharp get an error ?
Lire les 6 réponses

Vidéos High-Tech et Jeu Vidéo
Téléchargements
Vos réponses Page 1 / 2
Gagnez chaque mois un abonnement Premium avec GNT : Inscrivez-vous !
Trier par : date / pertinence
Sébastien FERRAND
Le #12237871
Warning...

In .net int is 32bits... but in C, only 16bits

try to use Int16.

[DllImport("myDll.dll")]
public static extern unsafe string
myFunction([MarshalAs(UnmanagedType.LPArray)] byte[] myData, Int16
width, Int16 height);

Cordialement,
Sébastien FERRAND
Consultant Indépendant
Microsoft Visual C# MVP
http://blogs.developpeur.org/sebmafate

a écrit :
Hi,

I have a dll coding in C langage and I want to use it in Csharp. This
is my declaration in csharp :
[DllImport("myDll.dll")]
public static extern unsafe string
myFunction([MarshalAs(UnmanagedType.LPArray)] byte[] myData, int width,
int height);

I call in csharp like this :
try{
string test = myFunction(tempArray, 320, 240); and
tempArray is correctly filled.
}
catch {
Console.Writeline("Error in fonction DLL");
}

My C function seems like that:
char *myFunction(char *myData, int Arg_width, int Arg_height)
{
char *myResult;

//malloc of myResult
myResult = malloc(strlen(myData) * sizeof (char));

printf("I fill myResult.n");

*(myData) = '';

printf("I return my datan");
return (myData);
}



And it print :
I fill myResult.
I return my data.
Error in fonction DLL

Can anyome explain why Csharp get an error ?



ZaRMaS
Le #12237861
Thanks for this precision

Problem still be there ... :)


Sébastien FERRAND a écrit :

Warning...

In .net int is 32bits... but in C, only 16bits

try to use Int16.

[DllImport("myDll.dll")]
public static extern unsafe string
myFunction([MarshalAs(UnmanagedType.LPArray)] byte[] myData, Int16
width, Int16 height);

Cordialement,
Sébastien FERRAND
Consultant Indépendant
Microsoft Visual C# MVP
http://blogs.developpeur.org/sebmafate

a écrit :
> Hi,
>
> I have a dll coding in C langage and I want to use it in Csharp. This
> is my declaration in csharp :
> [DllImport("myDll.dll")]
> public static extern unsafe string
> myFunction([MarshalAs(UnmanagedType.LPArray)] byte[] myData, int width,
> int height);
>
> I call in csharp like this :
> try{
> string test = myFunction(tempArray, 320, 240); and
> tempArray is correctly filled.
> }
> catch {
> Console.Writeline("Error in fonction DLL");
> }
>
> My C function seems like that:
> char *myFunction(char *myData, int Arg_width, int Arg_height)
> {
> char *myResult;
>
> //malloc of myResult
> myResult = malloc(strlen(myData) * sizeof (char));
>
> printf("I fill myResult.n");
>
> *(myData) = '';
>
> printf("I return my datan");
> return (myData);
> }
>
>
>
> And it print :
> I fill myResult.
> I return my data.
> Error in fonction DLL
>
> Can anyome explain why Csharp get an error ?
>


Sébastien FERRAND
Le #12237851
try like this :

[DllImport("myDll.dll")]
public static extern unsafe char
myFunction(ref char[] myData, Int16 width, Int16 height);

and
string test = myFunction(ref tempArray, 320, 240);

Cordialement,
Sébastien FERRAND
Consultant Indépendant
Microsoft Visual C# MVP
http://blogs.developpeur.org/sebmafate

a écrit :
Thanks for this precision

Problem still be there ... :)


Sébastien FERRAND a écrit :

Warning...

In .net int is 32bits... but in C, only 16bits

try to use Int16.

[DllImport("myDll.dll")]
public static extern unsafe string
myFunction([MarshalAs(UnmanagedType.LPArray)] byte[] myData, Int16
width, Int16 height);

Cordialement,
Sébastien FERRAND
Consultant Indépendant
Microsoft Visual C# MVP
http://blogs.developpeur.org/sebmafate

a écrit :
Hi,

I have a dll coding in C langage and I want to use it in Csharp. This
is my declaration in csharp :
[DllImport("myDll.dll")]
public static extern unsafe string
myFunction([MarshalAs(UnmanagedType.LPArray)] byte[] myData, int width,
int height);

I call in csharp like this :
try{
string test = myFunction(tempArray, 320, 240); and
tempArray is correctly filled.
}
catch {
Console.Writeline("Error in fonction DLL");
}

My C function seems like that:
char *myFunction(char *myData, int Arg_width, int Arg_height)
{
char *myResult;

//malloc of myResult
myResult = malloc(strlen(myData) * sizeof (char));

printf("I fill myResult.n");

*(myData) = '';

printf("I return my datan");
return (myData);
}



And it print :
I fill myResult.
I return my data.
Error in fonction DLL

Can anyome explain why Csharp get an error ?








Michael Bonfils
Le #12237841
Sébastien FERRAND wrote:
Warning...

In .net int is 32bits... but in C, only 16bits

try to use Int16.




Ca me semble vraiment curieux, le int sous Win32 fait 32bit, non ?

Pour moi, le problème se situe sans doute là :
*(myData) = '';

ou alors, le framework remarque myResult n'est pas libéré ?
(mais là, c'est une pure supposition)
Sébastien FERRAND
Le #12237831
non non... c'est le long qui fait 32bits en win32...

je n'arrive plus à retrouver la page qui résume ca sur MSDN :/


Cordialement,
Sébastien FERRAND
Consultant Indépendant
Microsoft Visual C# MVP
http://blogs.developpeur.org/sebmafate

Michael Bonfils a écrit :
Sébastien FERRAND wrote:
Warning...

In .net int is 32bits... but in C, only 16bits

try to use Int16.




Ca me semble vraiment curieux, le int sous Win32 fait 32bit, non ?

Pour moi, le problème se situe sans doute là :
*(myData) = '';

ou alors, le framework remarque myResult n'est pas libéré ?
(mais là, c'est une pure supposition)


Publicité
Suivre les réponses
Poster une réponse
Anonyme