Developing for the Compact framework

D

Denis C

Hi,

I have a C library and a VB .NET app using the C library
that I want to use on Windows CE 4.2. I have installed
EVC++ 4.0 and VS .NET 2003. Unfortunately they both
deploy to different emulators. Is it possible to set the
target emulator? Or could I install some other compact
framework environment and compile both programs for that
environment?

Denis C

Here also is the question unanswered from my previous
post concerning C structs containing arrays:

Sorry I forgot to put my name on the last one. I'm
wondering if I'm on the right track for dealing with char
arrays from C in VB.

Here's what I'm going to try:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

Public Structure GetLongRTS
Public sval As StringPtr
Public lval As Integer
End Structure

Public Structure StringPtr
Private szString As IntPtr

Public Sub New(ByVal s As Integer)
Me.szString = Memory.AllocHLocal(2 * (1 + s))
End Sub

Public Overrides Function ToString() As String
Return Marshal.PtrToStringUni(Me.szString)
End Function

Public Sub Free()
Memory.FreeHLocal(Me.szString)
End Sub
End Structure

Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal()
Dim obj As DBAccessFuncDecl.GetLongRTS
obj.sval = New DBAccessFuncDecl.StringPtr(30)
obj = CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),
DBAccessFuncDecl.GetLongRTS)


Is that the idea??

And then we also have some 2d arrays, char[20][20].
Can this approach work?

Thanks

Denis

..
 
P

Paul G. Tobey [eMVP]

Don't use the emulator. Use the target device. You'll spend a lot of time
screwing around, only to find that you have to test on the real device,
anyway.

I think that your mapping of structure from C to VB.NET is incorrect. I
think that Chris suggested this previously, but you should look at the
OpenNetCF library source for some tips on doing this type of thing.
Specifically, as I recall, the time zone stuff does this sort of thing. It
creates a class which represents the C data type. Rather than storing the
fields, though, it stores an array of bytes to represent the C structure
memory layout. When you access the 'fields' from outside an instance of the
class, the class converts the value you are sending in to an array of bytes
and copies the bytes to the right offset in the byte array. The reverse is
done when you read a 'field' of the structure externally. When you need to
call a C API and pass the structure, you do something like this:

CFunction( csobject.ToByteArray() );

where the ToByteArray() method simply returns the structure's internal byte
array. The CFunction can even alter the structure contents and you'll be
able to see them in the managed code after the call.

For your case, you may want to build some accessors for array elements, but
the basic scheme should work just fine.

Paul T.

Denis C said:
Hi,

I have a C library and a VB .NET app using the C library
that I want to use on Windows CE 4.2. I have installed
EVC++ 4.0 and VS .NET 2003. Unfortunately they both
deploy to different emulators. Is it possible to set the
target emulator? Or could I install some other compact
framework environment and compile both programs for that
environment?

Denis C

Here also is the question unanswered from my previous
post concerning C structs containing arrays:

Sorry I forgot to put my name on the last one. I'm
wondering if I'm on the right track for dealing with char
arrays from C in VB.

Here's what I'm going to try:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

Public Structure GetLongRTS
Public sval As StringPtr
Public lval As Integer
End Structure

Public Structure StringPtr
Private szString As IntPtr

Public Sub New(ByVal s As Integer)
Me.szString = Memory.AllocHLocal(2 * (1 + s))
End Sub

Public Overrides Function ToString() As String
Return Marshal.PtrToStringUni(Me.szString)
End Function

Public Sub Free()
Memory.FreeHLocal(Me.szString)
End Sub
End Structure

Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal()
Dim obj As DBAccessFuncDecl.GetLongRTS
obj.sval = New DBAccessFuncDecl.StringPtr(30)
obj = CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),
DBAccessFuncDecl.GetLongRTS)


Is that the idea??

And then we also have some 2d arrays, char[20][20].
Can this approach work?

Thanks

Denis

.
 
C

Chris Tacke, eMVP

I go one step further and create an explicit or implicit operator to get the
byte array, as I personally don't like calling a ToByteArray() method, but
that's a style thing. The WinAPI has a lot of these classes. Look at any
one of these classes: WAVEHDR, WAVEINCAPS, SYSTEMTIME, TZREG,
TIME_ZONE_INFORMATION, FileInformation, BITMAPINFOHEADER, BITMAPFILEHEADER


--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Paul G. Tobey said:
Don't use the emulator. Use the target device. You'll spend a lot of time
screwing around, only to find that you have to test on the real device,
anyway.

I think that your mapping of structure from C to VB.NET is incorrect. I
think that Chris suggested this previously, but you should look at the
OpenNetCF library source for some tips on doing this type of thing.
Specifically, as I recall, the time zone stuff does this sort of thing. It
creates a class which represents the C data type. Rather than storing the
fields, though, it stores an array of bytes to represent the C structure
memory layout. When you access the 'fields' from outside an instance of the
class, the class converts the value you are sending in to an array of bytes
and copies the bytes to the right offset in the byte array. The reverse is
done when you read a 'field' of the structure externally. When you need to
call a C API and pass the structure, you do something like this:

CFunction( csobject.ToByteArray() );

where the ToByteArray() method simply returns the structure's internal byte
array. The CFunction can even alter the structure contents and you'll be
able to see them in the managed code after the call.

For your case, you may want to build some accessors for array elements, but
the basic scheme should work just fine.

Paul T.

Denis C said:
Hi,

I have a C library and a VB .NET app using the C library
that I want to use on Windows CE 4.2. I have installed
EVC++ 4.0 and VS .NET 2003. Unfortunately they both
deploy to different emulators. Is it possible to set the
target emulator? Or could I install some other compact
framework environment and compile both programs for that
environment?

Denis C

Here also is the question unanswered from my previous
post concerning C structs containing arrays:

Sorry I forgot to put my name on the last one. I'm
wondering if I'm on the right track for dealing with char
arrays from C in VB.

Here's what I'm going to try:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

Public Structure GetLongRTS
Public sval As StringPtr
Public lval As Integer
End Structure

Public Structure StringPtr
Private szString As IntPtr

Public Sub New(ByVal s As Integer)
Me.szString = Memory.AllocHLocal(2 * (1 + s))
End Sub

Public Overrides Function ToString() As String
Return Marshal.PtrToStringUni(Me.szString)
End Function

Public Sub Free()
Memory.FreeHLocal(Me.szString)
End Sub
End Structure

Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal()
Dim obj As DBAccessFuncDecl.GetLongRTS
obj.sval = New DBAccessFuncDecl.StringPtr(30)
obj = CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),
DBAccessFuncDecl.GetLongRTS)


Is that the idea??

And then we also have some 2d arrays, char[20][20].
Can this approach work?

Thanks

Denis

.
 
D

Denis C

Okay. So if I can get the device from the company, if I
connect it evc and vs .net 2003 will compile for the
correct target? With the emulators I'm still getting a
MethodNotFound type exception and I am assuming this is
because the library isn't compiled correctly.

So, just to be sure I'm understanding you correctly, I'm
looking at creating a class for each C type I need to
handle? i.e. char id[30] would have its own class.

Or do you mean a class for each struct that contains
array types?

The structs without array types inside are posing no
problems. We hava declared "equivilent structures in VB
and can access the data inside them without problems.
This leads me to believe you are suggesting the first
option. If so, I thought that was what I was doing in
the attached example. Instead of a class I created a
struct called StringPtr that was allocated the space
necessary to hold the 30 charater array it would be
exected to hold when the C struct was assigned to it.

Am I making any sense?

Denis
-----Original Message-----
Don't use the emulator. Use the target device. You'll spend a lot of time
screwing around, only to find that you have to test on the real device,
anyway.

I think that your mapping of structure from C to VB.NET is incorrect. I
think that Chris suggested this previously, but you should look at the
OpenNetCF library source for some tips on doing this type of thing.
Specifically, as I recall, the time zone stuff does this sort of thing. It
creates a class which represents the C data type. Rather than storing the
fields, though, it stores an array of bytes to represent the C structure
memory layout. When you access the 'fields' from outside an instance of the
class, the class converts the value you are sending in to an array of bytes
and copies the bytes to the right offset in the byte array. The reverse is
done when you read a 'field' of the structure externally. When you need to
call a C API and pass the structure, you do something like this:

CFunction( csobject.ToByteArray() );

where the ToByteArray() method simply returns the structure's internal byte
array. The CFunction can even alter the structure contents and you'll be
able to see them in the managed code after the call.

For your case, you may want to build some accessors for array elements, but
the basic scheme should work just fine.

Paul T.

Hi,

I have a C library and a VB .NET app using the C library
that I want to use on Windows CE 4.2. I have installed
EVC++ 4.0 and VS .NET 2003. Unfortunately they both
deploy to different emulators. Is it possible to set the
target emulator? Or could I install some other compact
framework environment and compile both programs for that
environment?

Denis C

Here also is the question unanswered from my previous
post concerning C structs containing arrays:

Sorry I forgot to put my name on the last one. I'm
wondering if I'm on the right track for dealing with char
arrays from C in VB.

Here's what I'm going to try:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

Public Structure GetLongRTS
Public sval As StringPtr
Public lval As Integer
End Structure

Public Structure StringPtr
Private szString As IntPtr

Public Sub New(ByVal s As Integer)
Me.szString = Memory.AllocHLocal(2 * (1 + s))
End Sub

Public Overrides Function ToString() As String
Return Marshal.PtrToStringUni(Me.szString)
End Function

Public Sub Free()
Memory.FreeHLocal(Me.szString)
End Sub
End Structure

Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal ()
Dim obj As DBAccessFuncDecl.GetLongRTS
obj.sval = New DBAccessFuncDecl.StringPtr(30)
obj = CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),
DBAccessFuncDecl.GetLongRTS)


Is that the idea??

And then we also have some 2d arrays, char[20][20].
Can this approach work?

Thanks

Denis

.


.
 
P

Paul G. Tobey [eMVP]

VS.NET, if you've configured it to generate a Smart Device Application,
doesn't compile for a particular device type, to speak of. You can choose
Pocket PC or Windows CE.NET from the toolbar.

When you compile the DLL, you need to make sure that your target selection
is the device, and not an emulator of the device, and that you're compiling
for the proper processor type.

Other than that, it pretty much just works. If you're getting lots of
Missing Method exceptions, verify that you are properly exporting the
functions you are trying to call. The dependency viewer, depends.exe, will
tell you this when you run it on your DLL. Check the exported function
names and make sure that they don't have a bunch of decorations on them, as
they will if the DLL is written in C++ and the functions are not marked
'extern "C"'.

Yes, I'd say that, for structured types, you'll need a managed class for
each one. For char id[30], that's just an array of Byte, so you don't need
a special type for that, if you're passing the array directly (*that* can be
marshaled). However, if you have an array embedded in a structure, you'll
need to do something similar to the classes that Chris mentions.

OK, the missing code for StringPtr misled me.

Paul T.


Denis C said:
Okay. So if I can get the device from the company, if I
connect it evc and vs .net 2003 will compile for the
correct target? With the emulators I'm still getting a
MethodNotFound type exception and I am assuming this is
because the library isn't compiled correctly.

So, just to be sure I'm understanding you correctly, I'm
looking at creating a class for each C type I need to
handle? i.e. char id[30] would have its own class.

Or do you mean a class for each struct that contains
array types?

The structs without array types inside are posing no
problems. We hava declared "equivilent structures in VB
and can access the data inside them without problems.
This leads me to believe you are suggesting the first
option. If so, I thought that was what I was doing in
the attached example. Instead of a class I created a
struct called StringPtr that was allocated the space
necessary to hold the 30 charater array it would be
exected to hold when the C struct was assigned to it.

Am I making any sense?

Denis
-----Original Message-----
Don't use the emulator. Use the target device. You'll spend a lot of time
screwing around, only to find that you have to test on the real device,
anyway.

I think that your mapping of structure from C to VB.NET is incorrect. I
think that Chris suggested this previously, but you should look at the
OpenNetCF library source for some tips on doing this type of thing.
Specifically, as I recall, the time zone stuff does this sort of thing. It
creates a class which represents the C data type. Rather than storing the
fields, though, it stores an array of bytes to represent the C structure
memory layout. When you access the 'fields' from outside an instance of the
class, the class converts the value you are sending in to an array of bytes
and copies the bytes to the right offset in the byte array. The reverse is
done when you read a 'field' of the structure externally. When you need to
call a C API and pass the structure, you do something like this:

CFunction( csobject.ToByteArray() );

where the ToByteArray() method simply returns the structure's internal byte
array. The CFunction can even alter the structure contents and you'll be
able to see them in the managed code after the call.

For your case, you may want to build some accessors for array elements, but
the basic scheme should work just fine.

Paul T.

Hi,

I have a C library and a VB .NET app using the C library
that I want to use on Windows CE 4.2. I have installed
EVC++ 4.0 and VS .NET 2003. Unfortunately they both
deploy to different emulators. Is it possible to set the
target emulator? Or could I install some other compact
framework environment and compile both programs for that
environment?

Denis C

Here also is the question unanswered from my previous
post concerning C structs containing arrays:

Sorry I forgot to put my name on the last one. I'm
wondering if I'm on the right track for dealing with char
arrays from C in VB.

Here's what I'm going to try:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

Public Structure GetLongRTS
Public sval As StringPtr
Public lval As Integer
End Structure

Public Structure StringPtr
Private szString As IntPtr

Public Sub New(ByVal s As Integer)
Me.szString = Memory.AllocHLocal(2 * (1 + s))
End Sub

Public Overrides Function ToString() As String
Return Marshal.PtrToStringUni(Me.szString)
End Function

Public Sub Free()
Memory.FreeHLocal(Me.szString)
End Sub
End Structure

Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal ()
Dim obj As DBAccessFuncDecl.GetLongRTS
obj.sval = New DBAccessFuncDecl.StringPtr(30)
obj = CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),
DBAccessFuncDecl.GetLongRTS)


Is that the idea??

And then we also have some 2d arrays, char[20][20].
Can this approach work?

Thanks

Denis

.


.
 
C

Chris Tacke, eMVP

Let's say you have this:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

It would look something like this in C#:

class GetLongRTS
{
byte[] bytes = new byte[34];
public char[] sval
{
get
{
char[] b = new char[34];
Buffer.BlockCopy( b, 0, bytes, 0, 34);
return b;
}
}

public long lval
{
get { return BitConverter.ToUInt32(data, 34); }
}

public static implicit operator byte[]( WAVEHDR wh )
{
return bytes;
}
}
 
C

Chris Tacke, eMVP

obviously I made a mistake...

public static implicit operator byte[]( GetLongRTS rts )
{
return rts.bytes;
}
 
D

Denis C

Okay, the compiling sounds like I thought. For some
reason it's not working but that's another story:)

What I was getting at with the char id[30] was if it is
in a C struct can my VB Structure contain a managed class
with the Byte array as you were suggesting as well as any
other types I need, like the StringPtr example?

so lets say C struct like before:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

VB Structure as before:

Public Structure GetLongRTS
Public sval As **CharArrayClass**
Public lval As Integer
End Structure

Where CharArrayClass contains a byte array for holding
the data.

then:

Dim ptr As IntPtr = DBAccessFuncDecl.GetLongVal()
Dim obj As DBAccessFuncDecl.GetLongRTS

obj = CType(Marshal.PtrToStructure(ptr, GetType
(DBAccessFuncDecl.GetLongRTS)),DBAccessFuncDecl.GetLongRTS
)


Or rather a class GetLongRTS which contains a byte array
internally and methods like getSval() return the expected
String and getLval() returns the expected long?

then:

the same as above.

Am I understanding what you're saying or am I way off?

Denis
 
P

Paul G. Tobey [eMVP]

You can't just pass a pointer to a managed structure if the structure
contains an array, no. That's not able to be marshaled by the system and
might, for all I know, cause a missing method exception itself.

Paul T.
 
C

Chris Tacke, eMVP

You should accept the struct from C as a byte array, then add an
operator/ctor pair to go from a byte[] to a class:

public GetLongRTS(byte[] data)
{
Buffer.BlockCopy(data, 0, bytes, 0, data.Length);
}

public static implicit operator GetLongRTS(byte[] data)
{
return new GetLongRTS(data);
}

This makes a byte array and your struct pretty transportable from one to the
other and back. Keep in mind I used implicit operators. For clarity
explicit may be better so you have to do a cast. Depends on how many
different classes like this you have in your project. If it's 1 or 2,
implicit is fine.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Denis C said:
Thanks for your patience guys. I'm just finishing up my
CompSci degree and all the .NET stuff is new to me, let
alone the Compact Framework.

Anyways, the code is very helpfull. When I am receiving
the pointer to the C struct can I still receive it as an
IntPtr and then Marshall it to the appropriate class?

Denis
-----Original Message-----
Let's say you have this:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

It would look something like this in C#:

class GetLongRTS
{
byte[] bytes = new byte[34];
public char[] sval
{
get
{
char[] b = new char[34];
Buffer.BlockCopy( b, 0, bytes, 0, 34);
return b;
}
}

public long lval
{
get { return BitConverter.ToUInt32(data, 34); }
}

public static implicit operator byte[]( WAVEHDR wh )
{
return bytes;
}
}



--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


.
 
D

Denis C

Thanks for your patience guys. I'm just finishing up my
CompSci degree and all the .NET stuff is new to me, let
alone the Compact Framework.

Anyways, the code is very helpfull. When I am receiving
the pointer to the C struct can I still receive it as an
IntPtr and then Marshall it to the appropriate class?

Denis
 
C

Chris Tacke, eMVP

The operators I gave are for casting. You can create operator overloads for
anything. Say you made a class that logically could be "added" to another,
you could override the "+" operator to do that.

I don't spreche VB very well any more (odd since that's where I started,
eh?), so the best I can offer is to head to ragingsmurf or some similar site
and grind it through a translator.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Denis C said:
I'm not clear on the implicit operator. Is this like a
constructor? Keep in mind also that the application is
in VB.

So my functions would be like:

<dllimport...>
Public Shared GetLongRT() As Byte()

??

Denis
-----Original Message-----
You should accept the struct from C as a byte array, then add an
operator/ctor pair to go from a byte[] to a class:

public GetLongRTS(byte[] data)
{
Buffer.BlockCopy(data, 0, bytes, 0, data.Length);
}

public static implicit operator GetLongRTS(byte[] data)
{
return new GetLongRTS(data);
}

This makes a byte array and your struct pretty transportable from one to the
other and back. Keep in mind I used implicit operators. For clarity
explicit may be better so you have to do a cast. Depends on how many
different classes like this you have in your project. If it's 1 or 2,
implicit is fine.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Thanks for your patience guys. I'm just finishing up my
CompSci degree and all the .NET stuff is new to me, let
alone the Compact Framework.

Anyways, the code is very helpfull. When I am receiving
the pointer to the C struct can I still receive it as an
IntPtr and then Marshall it to the appropriate class?

Denis

-----Original Message-----
Let's say you have this:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

It would look something like this in C#:

class GetLongRTS
{
byte[] bytes = new byte[34];
public char[] sval
{
get
{
char[] b = new char[34];
Buffer.BlockCopy( b, 0, bytes, 0, 34);
return b;
}
}

public long lval
{
get { return BitConverter.ToUInt32(data, 34); }
}

public static implicit operator byte[]( WAVEHDR wh )
{
return bytes;
}
}



--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


.


.
 
D

Denis C

I'm not clear on the implicit operator. Is this like a
constructor? Keep in mind also that the application is
in VB.

So my functions would be like:

<dllimport...>
Public Shared GetLongRT() As Byte()

??

Denis
-----Original Message-----
You should accept the struct from C as a byte array, then add an
operator/ctor pair to go from a byte[] to a class:

public GetLongRTS(byte[] data)
{
Buffer.BlockCopy(data, 0, bytes, 0, data.Length);
}

public static implicit operator GetLongRTS(byte[] data)
{
return new GetLongRTS(data);
}

This makes a byte array and your struct pretty transportable from one to the
other and back. Keep in mind I used implicit operators. For clarity
explicit may be better so you have to do a cast. Depends on how many
different classes like this you have in your project. If it's 1 or 2,
implicit is fine.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


Thanks for your patience guys. I'm just finishing up my
CompSci degree and all the .NET stuff is new to me, let
alone the Compact Framework.

Anyways, the code is very helpfull. When I am receiving
the pointer to the C struct can I still receive it as an
IntPtr and then Marshall it to the appropriate class?

Denis
-----Original Message-----
Let's say you have this:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

It would look something like this in C#:

class GetLongRTS
{
byte[] bytes = new byte[34];
public char[] sval
{
get
{
char[] b = new char[34];
Buffer.BlockCopy( b, 0, bytes, 0, 34);
return b;
}
}

public long lval
{
get { return BitConverter.ToUInt32(data, 34); }
}

public static implicit operator byte[]( WAVEHDR wh )
{
return bytes;
}
}



--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


.


.
 
D

Denis C

Your help has been great.

Thanks again. Could you give me an example using your
operator? I just read it more carefully and the first one
is the constructor? and the implicit function creates the
casting operator?

Denis
-----Original Message-----
The operators I gave are for casting. You can create operator overloads for
anything. Say you made a class that logically could be "added" to another,
you could override the "+" operator to do that.

I don't spreche VB very well any more (odd since that's where I started,
eh?), so the best I can offer is to head to ragingsmurf or some similar site
and grind it through a translator.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


I'm not clear on the implicit operator. Is this like a
constructor? Keep in mind also that the application is
in VB.

So my functions would be like:

<dllimport...>
Public Shared GetLongRT() As Byte()

??

Denis
-----Original Message-----
You should accept the struct from C as a byte array, then add an
operator/ctor pair to go from a byte[] to a class:

public GetLongRTS(byte[] data)
{
Buffer.BlockCopy(data, 0, bytes, 0, data.Length);
}

public static implicit operator GetLongRTS(byte[] data)
{
return new GetLongRTS(data);
}

This makes a byte array and your struct pretty transportable from one to the
other and back. Keep in mind I used implicit operators. For clarity
explicit may be better so you have to do a cast. Depends on how many
different classes like this you have in your project. If it's 1 or 2,
implicit is fine.

--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


"Denis C" <[email protected]> wrote
in
message
Thanks for your patience guys. I'm just finishing
up
my
CompSci degree and all the .NET stuff is new to me, let
alone the Compact Framework.

Anyways, the code is very helpfull. When I am receiving
the pointer to the C struct can I still receive it
as
an
IntPtr and then Marshall it to the appropriate class?

Denis

-----Original Message-----
Let's say you have this:

typedef struct GetLongRTS *GetLongRT;
struct GetLongRTS{
char sval[30];
LONG lval;
};

It would look something like this in C#:

class GetLongRTS
{
byte[] bytes = new byte[34];
public char[] sval
{
get
{
char[] b = new char[34];
Buffer.BlockCopy( b, 0, bytes, 0, 34);
return b;
}
}

public long lval
{
get { return BitConverter.ToUInt32(data, 34); }
}

public static implicit operator byte[]( WAVEHDR wh )
{
return bytes;
}
}



--
Chris Tacke, eMVP
Co-Founder and Advisory Board Member
www.OpenNETCF.org
---
Windows CE Product Manager
Applied Data Systems
www.applieddata.net


.



.


.
 

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