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

[CF]Invoke a dll function with a struct paremeters wich contains char

Le
Yannick S.
Hi all,

I want to re-use what I have written in C/C++ in a C# projet (Compact
framework).

In the C/C++ project :

I have a dll : myDll.dll
I have a struct : myStruct
I have a function : myFunction

typedef struct myStruct
{
char* a;
char* b;
} myStruct;

extern "C" MYDLL_DLL_API int myFunction(myStruct *pMyStruct){

pMyStrunct.a = new char[36];
return 1;
}

How do I do to make it work in my C# programm ?

I have try several way but the only I found is to use unsafe code:
[StructLayout(Layout.Sequential)]
public unsafe class MyStruct
{
char *a;
char *b
}

[DllImport("MyDll.dll")]
public static extern int myFunc([In,Out] ref MyStruct);


but when I call myFunc in a C# prog :
MyStruct ms = new MyStruct();
myFunc(ref ms);

after that ms value null.

How is it possible ? How can I make to call a dll function that take a
structure in parameters and that structure has a string field. It is for the
compact framework, so the MarshalAs does not exit, and the Charset param in
the Structlayout does not exist.

thx for your help.
Lire les 2 réponses

Vidéos High-Tech et Jeu Vidéo
Téléchargements
Vos réponses
Gagnez chaque mois un abonnement Premium avec GNT : Inscrivez-vous !
Trier par : date / pertinence
Alex Feinman [MVP]
Le #12516721
It's a class, not a struct. Try removing "ref". Also instead are you sure
your unmanaged code uses ANSI (vs Unicode)?

--
Alex Feinman
---
Visit http://www.opennetcf.org
"Yannick S." news:
Hi all,

I want to re-use what I have written in C/C++ in a C# projet (Compact
framework).

In the C/C++ project :

I have a dll : myDll.dll
I have a struct : myStruct
I have a function : myFunction

typedef struct myStruct
{
char* a;
char* b;
} myStruct;

extern "C" MYDLL_DLL_API int myFunction(myStruct *pMyStruct){

pMyStrunct.a = new char[36];
return 1;
}

How do I do to make it work in my C# programm ?

I have try several way but the only I found is to use unsafe code:
[StructLayout(Layout.Sequential)]
public unsafe class MyStruct
{
char *a;
char *b
}

[DllImport("MyDll.dll")]
public static extern int myFunc([In,Out] ref MyStruct);


but when I call myFunc in a C# prog :
MyStruct ms = new MyStruct();
myFunc(ref ms);

after that ms value null.

How is it possible ? How can I make to call a dll function that take a
structure in parameters and that structure has a string field. It is for
the compact framework, so the MarshalAs does not exit, and the Charset
param in the Structlayout does not exist.

thx for your help.



Yannick S.
Le #12516681
you are right, that was the ref.
I am sure I use ANSI.

The real function of myFunc is to get a array of myStruct generated in the
dll. myStruct contains several field like variable size char.

PS: Alex I use you CFLauncher and it is very usefull ;)

"Alex Feinman [MVP]" de news:
It's a class, not a struct. Try removing "ref". Also instead are you sure
your unmanaged code uses ANSI (vs Unicode)?

--
Alex Feinman
---
Visit http://www.opennetcf.org
"Yannick S." news:
Hi all,

I want to re-use what I have written in C/C++ in a C# projet (Compact
framework).

In the C/C++ project :

I have a dll : myDll.dll
I have a struct : myStruct
I have a function : myFunction

typedef struct myStruct
{
char* a;
char* b;
} myStruct;

extern "C" MYDLL_DLL_API int myFunction(myStruct *pMyStruct){

pMyStrunct.a = new char[36];
return 1;
}

How do I do to make it work in my C# programm ?

I have try several way but the only I found is to use unsafe code:
[StructLayout(Layout.Sequential)]
public unsafe class MyStruct
{
char *a;
char *b
}

[DllImport("MyDll.dll")]
public static extern int myFunc([In,Out] ref MyStruct);


but when I call myFunc in a C# prog :
MyStruct ms = new MyStruct();
myFunc(ref ms);

after that ms value null.

How is it possible ? How can I make to call a dll function that take a
structure in parameters and that structure has a string field. It is for
the compact framework, so the MarshalAs does not exit, and the Charset
param in the Structlayout does not exist.

thx for your help.







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