[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 ?
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 ?

Poser une question


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 :
Problem still be there ... :)
Sébastien FERRAND a écrit :
[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 :
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)
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 :