OVH Cloud OVH Cloud

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

2 réponses
Avatar
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.

2 réponses

Avatar
Alex Feinman [MVP]
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." wrote in message
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.



Avatar
Yannick S.
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]" a écrit dans le message
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." wrote in message
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.