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

Y

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.
 
A

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)?
 
Y

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 said:
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. said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top