OVH Cloud OVH Cloud

[C]Dll in csharp

6 réponses
Avatar
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.\n");

*(myData) = '\0';

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



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

Can anyome explain why Csharp get an error ?

6 réponses

Avatar
Sébastien FERRAND
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 ?



Avatar
ZaRMaS
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 ?
>


Avatar
Sébastien FERRAND
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 ?








Avatar
Michael Bonfils
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)
Avatar
Sébastien FERRAND
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)


Avatar
Simon Mourier [SoftFluent]
En fait, la taille de int dépend de la machine/OS. Sur un OS 16 bits, la
taille de int c'est 16bits, sur un OS 32bits, c'est 32, etc... ce qui pose
toujours des problèmes de portage (surtout quand on passe de 16 à 32 à 64
!). C'est bien pour cela que le .NET framework enlève ces ambiguïtés
(System.Int32, System.Int16, etc...). Notez que le "int" C# n'est qu'un
alias sur System.Int32.

Pour le cas ici, c'est donc probablement 32 (sauf si on est sur Win64!).

Mais le problème ne vient pas de là, il vient de la conception de la
fonction qui est incorrecte au niveau de la gestion et des échanges en
mémoire.

Si on a accès au code C/C++, il vaut mieux concevoir la fonction en faisant
allouer la mémoire à .NET, et en rajoutant un argument spécifiant la taille
du tampon alloué, ce qui évite les problèmes. La plupart des API Win32 sont
conçues comme cela, par exemple, on pourrait avoir une fonction de ce genre:

WINAPI VOID myFunction(PBYTE pInData, DWORD dwInDataSize, PBYTE pOutData,
DWORD dwOutDataSize, DWORD dwArgWidth, DWORD dwArgHeight)
{
...
if (dwOutDataSize > sizeof(int)) // par exemple
{
*(pOutData) = '';
}
...
}

Simon.
www.softfluent.com


"Sébastien FERRAND" <"listes-seb["@]vbmaf.net> a écrit dans le message de
news:
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)