To cast or not to cast?

I

Ilya Tumanov [MS]

I suspect bioData[3] is an object. If so, you should cast it to whatever
type it is (e.g. Int32).
Next, cast it to whatever type you need:

System.UInt16 formatowner = unchecked( (System.UInt16) ((Int32)bioData[3])
);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 03:03:36 +0100
Lines: 840
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:26929
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

can't get it through:/

I ended up trying to store the value as int, which I guess would make sense
because an unsigned value cannot be stored in Sql, and the next thing up I
could find after smallint was int. Please tell me if I'm wrong. It does make
sense to me..

However, casting back to UInt16 really does not work. I'm doing the
following right now :
System.UInt16 formatowner = (System.UInt16)(unchecked((bioData[3])));

I checked the value of bioData[3], it's always 53242:

bioData[3]
{53242}
System.ValueType: {System.Int32}
m_value: 53242
MaxValue: 2147483647
MinValue: -2147483648

I don't even think that I should use the unchecked() statement, I guess it's
just for reassurance. Why would the following raise an invalid cast
exception?

"Ilya Tumanov [MS]" said:
You can get data out of SQL CE as Int16 and cast it to UInt16 using
unchecked().
Similarly, cast UInt16 to Int16 to store it.

Another solution would be to declare valuables as signed and let native
code to do interpretation.
That might be problematic if you set values manually somewhere, but it can
be done with unchecked().
Or, you can use equivalent values. For example binary representations of
65535 unsigned and -1 signed for 16 bit values are exactly the same.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
Subject: Re: To cast or not to cast?
Date: Wed, 13 Apr 2005 02:00:11 +0100
Lines: 686
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:26864
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

the "unsafe" keyword worked great! vs doesn't complain anymore.

I'm trying to solve the UInt16 problem now. I followed your previous advice
of using unchecked(). It still says that the value is too small or too large
when trying to insert it in the DB using OleDbType.SmallInt (which
kind
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0
9.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26785
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I tried to follow you fixed() solution, but I get an error saying that
"Pointers can only be used in an unsafe context" and "Cannot implicitly
convert type void* to System.IntPtr.

I've got the following down. I think the bit I'm missing is the
assignment
to IntPtr? And probably the casting as well.. Could you explain though
what
the compiler means by the first error? I understand that this is an
unsafe
block, so why does it complain?

fixed(void* BiometricDataPtr = (byte[])(bioData[8]))
{
SecondBIRSample.BiometricData = BiometricDataPtr;
}


I can see two reasons:



1. No need to free the memory after call is done.

2. No time wasted making temporary copy of something you already have.



Best regards,



Ilya


This posting is provided "AS IS" with no warranties, and confers no
rights.

Why would one want to use unsafe blocks is beyond me, when it
is
so
easy
to call LocalAlloc and Marshal.Copy

--
Alex Feinman
---
Visit http://www.opennetcf.org
It won't, but managed array (retrieved from SQL CE) should be
already
allocated.



Best regards,



Ilya



This posting is provided "AS IS" with no warranties, and
confers
no
rights.


"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in message
Though fixed won't allocate any memory for you, so you're still
where
you were before.

-Chris


message
You don't need to do anything to pin memory with fixed(), fixed()
will
do
it for you:

fixed(void *ArrayPointer = MyManagedArray) {
// MyManagedArray is pinned within fixed block and ArrayPointer
points
to
it.
// Assign it to IntPtr (might need some casting) and call native
function
within this block.
}

// MyManagedArray in longer pinned...

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 20:37:43 +0100
Lines: 335
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26681
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I did a little bit of research because I'm a newbie regarding
pointers/memory allocation. I found an interesting post by Brian
http://groups-beta.google.com/group...28847ac90ac8a3/b92cb386364174f7?q=unmanaged+a
llocate+memory&rnum=10#b92cb386364174f7

I supose I could use LocalAlloc to allocate memory. The post
seems
to
be
similar to my problem but I still don't understand how to pin
managed
memory
(by using Fixed()?), and have a pointer point to the byte array
(which is
probably what I would want to do) ?

your help is really appreciated!

message
Creating a new pointer is not enough. You need to allocate
memory
and
assign value pointing to it to the pointer.
You might allocate native memory via P/Invoke (which you would
have
to
free
at some point).
Or, you can obtain a pointer to a managed array you already
have.
Before you pass this pointer to a native code, you have
to
pin
managed
memory so GC won't move the array.
You can use GCHandle class for it or you can use unsafe code.
I would recommend using Fixed() with unsafe pointer. That
would
pin
and
unpin the array automatically.
GCHandle has a bug (returns incorrect pointer - there's a
workaround)
and
it's not automatic.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 01:46:03 +0100
Lines: 240
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26658
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Okay, so I used the 1040 length, it doesn't seem to
complain,
which is
good
news I guess. To make sure it stores the data
correctly
I'm
trying
to
retrieve it and feed it back to the biometric template
matching
method.

Because it takes IntPtr's for BiometricData and Signature, I
tried
creating
new pointers and marshal copy the content that I retrieved
earlier
into
these pointers. I failed. I tried the following:

System.IntPtr BiometricDataPtr;//create new pointer
BiometricData = (byte[])bioData[8];//BiometricData is an
array
of
type
byte
Marshal.Copy(BiometricData, 0, BiometricDataPtr,
1040);//copy
content
of
BiometricData in an IntPtr

"Use of unassigned local variable 'BiometricDataPtr'."
Obvsiously it's unused, it's meant to point to something...
what
am I
missing here?

in
message
You should not guess the length, you should know it. It
might
be
fixed
or
native code could return it somehow.
Most likely you're correct and FirstBIRSample.Length
contains
this
size.
You should check out the documentation for the function
you're
calling
to
make sure it's correct.

If value of a pointer is 0, it's null pointer. It means
this
pointer
is
not
valid and points to nothing.
Since no data is available, you could store it as DBNull.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and
confers
no
rights.
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sat, 9 Apr 2005 22:32:29 +0100
Lines: 159
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID:
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26653
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks a lot for that! Before going to the UInt16
problem, I
better
try
to
solve that IntPtr problem.

Managed to use Marshal.Copy. Not sure how I should
'guess'
the
length
of
the
Byte array to copy to. When I type
FirstBIRSample.BiometricData
(which
is
the IntPtr) in the command window, it returns to me:
2372256 -
is
this
the
size?
I 'assumed' the size would be 1040 (which is
stored
in
FirstBIRSample.Length).

Second Part: Seems like the FirstBIRSample.Signature
(also a
pointer)
is
0,
how should I store this?

Thanks so much!

""Ilya Tumanov [MS]""
wrote
in
message
While storing UInt16 as Int is OK, it will take more
space.
The
problem
you're having is caused by range check. You might
consider
using
unchecked() to disable it.

System.Runtime.InteropServices.Marshal class allows
you
to
copy
data
from
the pointer to the array. Look into Copy()
methods
of
that
class.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties,
and
confers
no
rights.
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 8 Apr 2005 10:44:39 +0100
Lines: 91
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26572
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks so much for the replies Alex and Ilya!

I changed the types in sql. It wasn't happy with
Uint16
mapped
to
smallint
(sql says value is either too small or too large),
so
I
just
went
for
an
int.

Not sure how to retrieve the data data an
IntPtr
is
pointing
to
though.
How
can I get that data as a byte array?

thanks so much again!

""Ilya Tumanov [MS]""
wrote
in
message
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
 
J

Jeremy

correct - that did the trick! thanks for that. Now I can start eliminating
the problems.. There seems to be trouble with copying the biometricdata from
to a Byte array. As mentioned earlier, I thought the length of
FirstBIRSample.BiometricData was 1040, however when I marshal copy from the
pointer to Byte Array BiometricData that is of length [2000] (just for
testing purposes) and I check the content of let's say [1995], using
BiometricData[1995], guess what, it's not empty. It's a very sad thought.

How can I check the length of the data the pointer is pointing to so that I
can create a Byte array of the correct length ? I suspect that is the
problem.

"Ilya Tumanov [MS]" said:
I suspect bioData[3] is an object. If so, you should cast it to whatever
type it is (e.g. Int32).
Next, cast it to whatever type you need:

System.UInt16 formatowner = unchecked( (System.UInt16) ((Int32)bioData[3])
);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 03:03:36 +0100
Lines: 840
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:26929
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

can't get it through:/

I ended up trying to store the value as int, which I guess would make sense
because an unsigned value cannot be stored in Sql, and the next thing up I
could find after smallint was int. Please tell me if I'm wrong. It does make
sense to me..

However, casting back to UInt16 really does not work. I'm doing the
following right now :
System.UInt16 formatowner = (System.UInt16)(unchecked((bioData[3])));

I checked the value of bioData[3], it's always 53242:

bioData[3]
{53242}
System.ValueType: {System.Int32}
m_value: 53242
MaxValue: 2147483647
MinValue: -2147483648

I don't even think that I should use the unchecked() statement, I guess it's
just for reassurance. Why would the following raise an invalid cast
exception?

"Ilya Tumanov [MS]" said:
You can get data out of SQL CE as Int16 and cast it to UInt16 using
unchecked().
Similarly, cast UInt16 to Int16 to store it.

Another solution would be to declare valuables as signed and let native
code to do interpretation.
That might be problematic if you set values manually somewhere, but it can
be done with unchecked().
Or, you can use equivalent values. For example binary representations of
65535 unsigned and -1 signed for 16 bit values are exactly the same.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Wed, 13 Apr 2005 02:00:11 +0100
Lines: 686
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26864
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

the "unsafe" keyword worked great! vs doesn't complain anymore.

I'm trying to solve the UInt16 problem now. I followed your previous
advice
of using unchecked(). It still says that the value is too small or too
large
when trying to insert it in the DB using OleDbType.SmallInt (which
kind
of
makes sense seeing as the value is 53242).

Any other ideas that could help ?

Unsafe block won't eliminate type check (fortunately). You need to do
explicit cast:

SecondBIRSample.BiometricData = (IntPtr32)BiometricDataPtr;

First error means you need to declare this function with unsafe keyword
and
use /unsafe compiler switch (can be set in project properties).

public unsafe void DoUnsafeStuff() {
// Can do unsafe stuff here...
..
}

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Tue, 12 Apr 2005 00:36:01 +0100
Lines: 560
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0
9.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26785
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I tried to follow you fixed() solution, but I get an error
saying
that
"Pointers can only be used in an unsafe context" and "Cannot
implicitly
convert type void* to System.IntPtr.

I've got the following down. I think the bit I'm missing is the
assignment
to IntPtr? And probably the casting as well.. Could you explain though
what
the compiler means by the first error? I understand that this is an
unsafe
block, so why does it complain?

fixed(void* BiometricDataPtr = (byte[])(bioData[8]))
{
SecondBIRSample.BiometricData = BiometricDataPtr;
}


I can see two reasons:



1. No need to free the memory after call is done.

2. No time wasted making temporary copy of something you already
have.



Best regards,



Ilya


This posting is provided "AS IS" with no warranties, and
confers
no
rights.

Why would one want to use unsafe blocks is beyond me, when
it
http://groups-beta.google.com/group...28847ac90ac8a3/b92cb386364174f7?q=unmanaged+a
llocate+memory&rnum=10#b92cb386364174f7

I supose I could use LocalAlloc to allocate memory. The post
seems
to
be
similar to my problem but I still don't understand how
to
pin
managed
memory
(by using Fixed()?), and have a pointer point to the byte
array
(which is
probably what I would want to do) ?

your help is really appreciated!

""Ilya Tumanov [MS]"" <[email protected]>
wrote
in
message
Creating a new pointer is not enough. You need to allocate
memory
and
assign value pointing to it to the pointer.
You might allocate native memory via P/Invoke (which you
would
have
to
free
at some point).
Or, you can obtain a pointer to a managed array you already
have.
Before you pass this pointer to a native code, you
have
there's
a
workaround)
and
it's not automatic.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 01:46:03 +0100
Lines: 240
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26658
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Okay, so I used the 1040 length, it doesn't seem to
complain,
which is
good
news I guess. To make sure it stores the data correctly
I'm
trying
to
retrieve it and feed it back to the biometric template
matching
method.

Because it takes IntPtr's for BiometricData and
Signature, I
tried
creating
new pointers and marshal copy the content that I retrieved
earlier
into
these pointers. I failed. I tried the following:

System.IntPtr BiometricDataPtr;//create new pointer
BiometricData = (byte[])bioData[8];//BiometricData
is
an
array
of
type
byte
Marshal.Copy(BiometricData, 0, BiometricDataPtr,
1040);//copy
content
of
BiometricData in an IntPtr

"Use of unassigned local variable 'BiometricDataPtr'."
Obvsiously it's unused, it's meant to point to
something...
what
am I
missing here?

in
message
You should not guess the length, you should know
it.
It
might
be
fixed
or
native code could return it somehow.
Most likely you're correct and FirstBIRSample.Length
contains
this
size.
You should check out the documentation for the function
you're
calling
to
make sure it's correct.

If value of a pointer is 0, it's null pointer. It means
this
pointer
is
not
valid and points to nothing.
Since no data is available, you could store it as
DBNull.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sat, 9 Apr 2005 22:32:29 +0100
Lines: 159
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26653
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks a lot for that! Before going to the UInt16
problem, I
better
try
to
solve that IntPtr problem.

Managed to use Marshal.Copy. Not sure how I should
'guess'
the
length
of
the
Byte array to copy to. When I type
FirstBIRSample.BiometricData
(which
is
the IntPtr) in the command window, it returns to me:
2372256 -
is
this
the
size?
I 'assumed' the size would be 1040 (which is
stored
in
FirstBIRSample.Length).

Second Part: Seems like the FirstBIRSample.Signature
(also a
pointer)
is
0,
how should I store this?

Thanks so much!

""Ilya Tumanov [MS]""
wrote
in
message
While storing UInt16 as Int is OK, it will
take
more
space.
The
problem
you're having is caused by range check. You might
consider
using
unchecked() to disable it.

System.Runtime.InteropServices.Marshal class allows
you
to
copy
data
from
the pointer to the array. Look into Copy() methods
of
that
class.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties,
and
confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 8 Apr 2005 10:44:39 +0100
Lines: 91
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<#t8wc#[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26572
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks so much for the replies Alex and Ilya!

I changed the types in sql. It wasn't happy with
Uint16
mapped
to
smallint
(sql says value is either too small or too large),
so
I
just
went
for
an
int.

Not sure how to retrieve the data data an IntPtr
is
pointing
to
though.
How
can I get that data as a byte array?

thanks so much again!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
definition
and likely
you types
into
 
I

Ilya Tumanov [MS]

Assuming your array is 1040 (gee, that reminds me of something…) bytes in
size, why should byte 1995 at this pointer be empty (zero)?
This array is located in memory and probably surrounded by other stuff
allocated by the same or other programs.
If you get out of array bounds, you would read (or worse - write to) stuff
which is not yours, but it does not have to be empty.

That's a very common problem in C/C++ development because there's no way to
know the size of memory pointer is pointing to.
Usually this size is either fixed or provided separately (as separate
argument or filed in the structure).
You should use a spec of the function you're calling to find out the right
size.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 10:57:53 +0100
Lines: 988
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:26946
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

correct - that did the trick! thanks for that. Now I can start eliminating
the problems.. There seems to be trouble with copying the biometricdata from
to a Byte array. As mentioned earlier, I thought the length of
FirstBIRSample.BiometricData was 1040, however when I marshal copy from the
pointer to Byte Array BiometricData that is of length [2000] (just for
testing purposes) and I check the content of let's say [1995], using
BiometricData[1995], guess what, it's not empty. It's a very sad thought.

How can I check the length of the data the pointer is pointing to so that I
can create a Byte array of the correct length ? I suspect that is the
problem.

"Ilya Tumanov [MS]" said:
I suspect bioData[3] is an object. If so, you should cast it to whatever
type it is (e.g. Int32).
Next, cast it to whatever type you need:

System.UInt16 formatowner = unchecked( (System.UInt16) ((Int32)bioData[3])
);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 03:03:36 +0100
Lines: 840
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:26929
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

can't get it through:/

I ended up trying to store the value as int, which I guess would make sense
because an unsigned value cannot be stored in Sql, and the next thing
up
I
could find after smallint was int. Please tell me if I'm wrong. It
does
make
sense to me..

However, casting back to UInt16 really does not work. I'm doing the
following right now :
System.UInt16 formatowner = (System.UInt16)(unchecked((bioData[3])));

I checked the value of bioData[3], it's always 53242:

bioData[3]
{53242}
System.ValueType: {System.Int32}
m_value: 53242
MaxValue: 2147483647
MinValue: -2147483648

I don't even think that I should use the unchecked() statement, I
guess
it's
just for reassurance. Why would the following raise an invalid cast
exception?

You can get data out of SQL CE as Int16 and cast it to UInt16 using
unchecked().
Similarly, cast UInt16 to Int16 to store it.

Another solution would be to declare valuables as signed and let native
code to do interpretation.
That might be problematic if you set values manually somewhere, but
it
can
be done with unchecked().
Or, you can use equivalent values. For example binary
representations
previous
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0
9.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26785
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I tried to follow you fixed() solution, but I get an error saying
that
"Pointers can only be used in an unsafe context" and "Cannot
implicitly
convert type void* to System.IntPtr.

I've got the following down. I think the bit I'm missing is the
assignment
to IntPtr? And probably the casting as well.. Could you explain
though
what
the compiler means by the first error? I understand that this
is
an
unsafe
block, so why does it complain?

fixed(void* BiometricDataPtr = (byte[])(bioData[8]))
{
SecondBIRSample.BiometricData = BiometricDataPtr;
}


I can see two reasons:



1. No need to free the memory after call is done.

2. No time wasted making temporary copy of something you already
have.



Best regards,



Ilya


This posting is provided "AS IS" with no warranties, and confers
no
rights.

message
Why would one want to use unsafe blocks is beyond me, when
it
is
so
easy
to call LocalAlloc and Marshal.Copy
should
be
already
allocated.



Best regards,



Ilya



This posting is provided "AS IS" with no warranties, and
confers
no
rights.


"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in message
Though fixed won't allocate any memory for you, so you're
still
where
you were before.

-Chris


""Ilya Tumanov [MS]"" <[email protected]>
wrote
in
message
You don't need to do anything to pin memory with fixed(),
fixed()
will
do
it for you:

fixed(void *ArrayPointer = MyManagedArray) {
// MyManagedArray is pinned within fixed block and
ArrayPointer
points
to
it.
// Assign it to IntPtr (might need some casting) and call
native
function
within this block.
}

// MyManagedArray in longer pinned...

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and
confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 20:37:43 +0100
Lines: 335
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26681
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

I did a little bit of research because I'm a newbie
regarding
pointers/memory allocation. I found an interesting
post
http://groups-beta.google.com/group...28847ac90ac8a3/b92cb386364174f7?q=unmanaged+a
llocate+memory&rnum=10#b92cb386364174f7

I supose I could use LocalAlloc to allocate memory.
The
post
seems
to
be
similar to my problem but I still don't understand how to
pin
managed
memory
(by using Fixed()?), and have a pointer point to the byte
array
(which is
probably what I would want to do) ?

your help is really appreciated!

in
message
Creating a new pointer is not enough. You need to allocate
memory
and
assign value pointing to it to the pointer.
You might allocate native memory via P/Invoke (which you
would
have
to
free
at some point).
Or, you can obtain a pointer to a managed array you
already
have.
Before you pass this pointer to a native code, you
have
to
pin
managed
memory so GC won't move the array.
You can use GCHandle class for it or you can use unsafe
code.
I would recommend using Fixed() with unsafe pointer. That
would
pin
and
unpin the array automatically.
GCHandle has a bug (returns incorrect pointer -
there's
a
workaround)
and
it's not automatic.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and
confers
no
rights.
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 01:46:03 +0100
Lines: 240
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID:
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26658
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Okay, so I used the 1040 length, it doesn't seem to
complain,
which is
good
news I guess. To make sure it stores the data correctly
I'm
trying
to
retrieve it and feed it back to the biometric template
matching
method.

Because it takes IntPtr's for BiometricData and
Signature, I
tried
creating
new pointers and marshal copy the content that I
retrieved
earlier
into
these pointers. I failed. I tried the following:

System.IntPtr BiometricDataPtr;//create new pointer
BiometricData = (byte[])bioData[8];//BiometricData
is
an
array
of
type
byte
Marshal.Copy(BiometricData, 0, BiometricDataPtr,
1040);//copy
content
of
BiometricData in an IntPtr

"Use of unassigned local variable 'BiometricDataPtr'."
Obvsiously it's unused, it's meant to point to
something...
what
am I
missing here?

""Ilya Tumanov [MS]""
wrote
in
message
You should not guess the length, you should know it.
It
might
be
fixed
or
native code could return it somehow.
Most likely you're correct and FirstBIRSample.Length
contains
this
size.
You should check out the documentation for the
function
you're
calling
to
make sure it's correct.

If value of a pointer is 0, it's null pointer. It
means
this
pointer
is
not
valid and points to nothing.
Since no data is available, you could store it as
DBNull.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties,
and
confers
no
rights.
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sat, 9 Apr 2005 22:32:29 +0100
Lines: 159
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26653
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks a lot for that! Before going to the UInt16
problem, I
better
try
to
solve that IntPtr problem.

Managed to use Marshal.Copy. Not sure how I should
'guess'
the
length
of
the
Byte array to copy to. When I type
FirstBIRSample.BiometricData
(which
is
the IntPtr) in the command window, it returns
to
me:
2372256 -
is
this
the
size?
I 'assumed' the size would be 1040 (which is stored
in
FirstBIRSample.Length).

Second Part: Seems like the FirstBIRSample.Signature
(also a
pointer)
is
0,
how should I store this?

Thanks so much!

""Ilya Tumanov [MS]""
wrote
in
message
While storing UInt16 as Int is OK, it will take
more
space.
The
problem
you're having is caused by range check. You might
consider
using
unchecked() to disable it.

System.Runtime.InteropServices.Marshal class
allows
you
to
copy
data
from
the pointer to the array. Look into Copy() methods
of
that
class.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 8 Apr 2005 10:44:39 +0100
Lines: 91
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<#t8wc#[email protected]>
Newsgroups:

microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26572
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks so much for the replies Alex and Ilya!

I changed the types in sql. It wasn't
happy
with
Uint16
mapped
to
smallint
(sql says value is either too small or too
large),
so
I
just
went
for
an
int.

Not sure how to retrieve the data data an IntPtr
is
pointing
to
though.
How
can I get that data as a byte array?

thanks so much again!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ them).
I prior
to
 
J

Jeremy

I appreciate your patience, really.

I'm certain (I think..) that the biometricdata is not correctly inserted in
SQL. I know that the marshal copying from pointer to byte[] went ok (tested
that out).

This is what I get for biometricdata when in debug mode as soon as I get it
out of SQL:

bioData[8]
{Length=1}
[0]: 144

This is not normal, right? (this is more of a rethorical question I
guess..) - I insert data in the SQL using OleDbType.VarBinary (as suggested)
and simply provide the data as an object (no casting). Did I skip a crucial
step somewhere ?


"Ilya Tumanov [MS]" said:
Assuming your array is 1040 (gee, that reminds me of something.) bytes in
size, why should byte 1995 at this pointer be empty (zero)?
This array is located in memory and probably surrounded by other stuff
allocated by the same or other programs.
If you get out of array bounds, you would read (or worse - write to) stuff
which is not yours, but it does not have to be empty.

That's a very common problem in C/C++ development because there's no way to
know the size of memory pointer is pointing to.
Usually this size is either fixed or provided separately (as separate
argument or filed in the structure).
You should use a spec of the function you're calling to find out the right
size.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 10:57:53 +0100
Lines: 988
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:26946
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

correct - that did the trick! thanks for that. Now I can start eliminating
the problems.. There seems to be trouble with copying the biometricdata from
to a Byte array. As mentioned earlier, I thought the length of
FirstBIRSample.BiometricData was 1040, however when I marshal copy from the
pointer to Byte Array BiometricData that is of length [2000] (just for
testing purposes) and I check the content of let's say [1995], using
BiometricData[1995], guess what, it's not empty. It's a very sad thought.

How can I check the length of the data the pointer is pointing to so
that
I
can create a Byte array of the correct length ? I suspect that is the
problem.

"Ilya Tumanov [MS]" said:
I suspect bioData[3] is an object. If so, you should cast it to whatever
type it is (e.g. Int32).
Next, cast it to whatever type you need:

System.UInt16 formatowner = unchecked( (System.UInt16) ((Int32)bioData[3])
);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 03:03:36 +0100
Lines: 840
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26929
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

can't get it through:/

I ended up trying to store the value as int, which I guess would make
sense
because an unsigned value cannot be stored in Sql, and the next
thing
up
I
could find after smallint was int. Please tell me if I'm wrong. It does
make
sense to me..

However, casting back to UInt16 really does not work. I'm doing the
following right now :
System.UInt16 formatowner = (System.UInt16)(unchecked((bioData[3])));

I checked the value of bioData[3], it's always 53242:

bioData[3]
{53242}
System.ValueType: {System.Int32}
m_value: 53242
MaxValue: 2147483647
MinValue: -2147483648

I don't even think that I should use the unchecked() statement, I guess
it's
just for reassurance. Why would the following raise an invalid cast
exception?

You can get data out of SQL CE as Int16 and cast it to UInt16 using
unchecked().
Similarly, cast UInt16 to Int16 to store it.

Another solution would be to declare valuables as signed and let native
code to do interpretation.
That might be problematic if you set values manually somewhere,
but
it representations
previous or
too
need
to confers
no
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0 this
is
an
unsafe
block, so why does it complain?

fixed(void* BiometricDataPtr = (byte[])(bioData[8]))
{
SecondBIRSample.BiometricData = BiometricDataPtr;
}


message
I can see two reasons:



1. No need to free the memory after call is done.

2. No time wasted making temporary copy of something you already
have.



Best regards,



Ilya


This posting is provided "AS IS" with no warranties, and confers
no
rights.

message
Why would one want to use unsafe blocks is beyond me,
when
it
is
so
easy
to call LocalAlloc and Marshal.Copy
should
be
already
allocated.



Best regards,



Ilya



This posting is provided "AS IS" with no warranties, and
confers
no
rights.


"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in message
Though fixed won't allocate any memory for you, so you're
still
where
you were before.

-Chris


in
message
You don't need to do anything to pin memory with fixed(),
fixed()
will
do
it for you:

fixed(void *ArrayPointer = MyManagedArray) {
// MyManagedArray is pinned within fixed block and
ArrayPointer
points
to
it.
// Assign it to IntPtr (might need some casting) and call
native
function
within this block.
}

// MyManagedArray in longer pinned...

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and
confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 20:37:43 +0100
Lines: 335
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26681
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

I did a little bit of research because I'm a newbie
regarding
pointers/memory allocation. I found an interesting
post
http://groups-beta.google.com/group...28847ac90ac8a3/b92cb386364174f7?q=unmanaged+a
llocate+memory&rnum=10#b92cb386364174f7

I supose I could use LocalAlloc to allocate memory. The
post
seems
to
be
similar to my problem but I still don't understand
how
to
pin
managed
memory
(by using Fixed()?), and have a pointer point to
the
byte
array
(which is
probably what I would want to do) ?

your help is really appreciated!

in
message
Creating a new pointer is not enough. You need to
allocate
memory
and
assign value pointing to it to the pointer.
You might allocate native memory via P/Invoke
(which
you
would
have
to
free
at some point).
Or, you can obtain a pointer to a managed array you
already
have.
Before you pass this pointer to a native code, you have
to
pin
managed
memory so GC won't move the array.
You can use GCHandle class for it or you can use unsafe
code.
I would recommend using Fixed() with unsafe pointer.
That
would
pin
and
unpin the array automatically.
GCHandle has a bug (returns incorrect pointer - there's
a
workaround)
and
it's not automatic.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 01:46:03 +0100
Lines: 240
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26658
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Okay, so I used the 1040 length, it doesn't seem to
complain,
which is
good
news I guess. To make sure it stores the data
correctly
I'm
trying
to
retrieve it and feed it back to the biometric template
matching
method.

Because it takes IntPtr's for BiometricData and
Signature, I
tried
creating
new pointers and marshal copy the content that I
retrieved
earlier
into
these pointers. I failed. I tried the following:

System.IntPtr BiometricDataPtr;//create new pointer
BiometricData =
(byte[])bioData[8];//BiometricData
is
an
array
of
type
byte
Marshal.Copy(BiometricData, 0, BiometricDataPtr,
1040);//copy
content
of
BiometricData in an IntPtr

"Use of unassigned local variable 'BiometricDataPtr'."
Obvsiously it's unused, it's meant to point to
something...
what
am I
missing here?

""Ilya Tumanov [MS]""
wrote
in
message
You should not guess the length, you should
know
it.
It
might
be
fixed
or
native code could return it somehow.
Most likely you're correct and FirstBIRSample.Length
contains
this
size.
You should check out the documentation for the
function
you're
calling
to
make sure it's correct.

If value of a pointer is 0, it's null pointer. It
means
this
pointer
is
not
valid and points to nothing.
Since no data is available, you could store it as
DBNull.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties,
and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sat, 9 Apr 2005 22:32:29 +0100
Lines: 159
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26653
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks a lot for that! Before going to the UInt16
problem, I
better
try
to
solve that IntPtr problem.

Managed to use Marshal.Copy. Not sure how I should
'guess'
the
length
of
the
Byte array to copy to. When I type
FirstBIRSample.BiometricData
(which
is
the IntPtr) in the command window, it
returns
to
me:
2372256 -
is
this
the
size?
I 'assumed' the size would be 1040 (which is
stored
in
FirstBIRSample.Length).

Second Part: Seems like the
FirstBIRSample.Signature
(also a
pointer)
is
0,
how should I store this?

Thanks so much!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
While storing UInt16 as Int is OK, it will take
more
space.
The
problem
you're having is caused by range check. You
might
consider
using
unchecked() to disable it.

System.Runtime.InteropServices.Marshal class
allows
you
to
copy
data
from
the pointer to the array. Look into Copy()
methods
of
that
class.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 8 Apr 2005 10:44:39 +0100
Lines: 91
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<#t8wc#[email protected]>
Newsgroups:

microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26572
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks so much for the replies Alex and Ilya!

I changed the types in sql. It wasn't happy
with
Uint16
mapped
to
smallint
(sql says value is either too small or too
large),
so
I
just
went
for
an
int.

Not sure how to retrieve the data data an
IntPtr
is
pointing
to
though.
How
can I get that data as a byte array?

thanks so much again!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
news:[email protected]...
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ None
of them). would
be
 
I

Ilya Tumanov [MS]

Hmm... Not sure. I would cast it to byte array just in case. You can also
try SqlDbType.Image.
And make sure array size is OK before it's added to the SQL.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 15 Apr 2005 01:41:18 +0100
Lines: 1172
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
2.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.compactframework:75583
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I appreciate your patience, really.

I'm certain (I think..) that the biometricdata is not correctly inserted in
SQL. I know that the marshal copying from pointer to byte[] went ok (tested
that out).

This is what I get for biometricdata when in debug mode as soon as I get it
out of SQL:

bioData[8]
{Length=1}
[0]: 144

This is not normal, right? (this is more of a rethorical question I
guess..) - I insert data in the SQL using OleDbType.VarBinary (as suggested)
and simply provide the data as an object (no casting). Did I skip a crucial
step somewhere ?


"Ilya Tumanov [MS]" said:
Assuming your array is 1040 (gee, that reminds me of something.) bytes in
size, why should byte 1995 at this pointer be empty (zero)?
This array is located in memory and probably surrounded by other stuff
allocated by the same or other programs.
If you get out of array bounds, you would read (or worse - write to) stuff
which is not yours, but it does not have to be empty.

That's a very common problem in C/C++ development because there's no way to
know the size of memory pointer is pointing to.
Usually this size is either fixed or provided separately (as separate
argument or filed in the structure).
You should use a spec of the function you're calling to find out the right
size.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 10:57:53 +0100
Lines: 988
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.compactframework:26946
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

correct - that did the trick! thanks for that. Now I can start eliminating
the problems.. There seems to be trouble with copying the
biometricdata
from
to a Byte array. As mentioned earlier, I thought the length of
FirstBIRSample.BiometricData was 1040, however when I marshal copy
from
the
pointer to Byte Array BiometricData that is of length [2000] (just for
testing purposes) and I check the content of let's say [1995], using
BiometricData[1995], guess what, it's not empty. It's a very sad thought.

How can I check the length of the data the pointer is pointing to so
that
I
can create a Byte array of the correct length ? I suspect that is the
problem.

I suspect bioData[3] is an object. If so, you should cast it to whatever
type it is (e.g. Int32).
Next, cast it to whatever type you need:

System.UInt16 formatowner = unchecked( (System.UInt16) ((Int32)bioData[3])
);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 03:03:36 +0100
Lines: 840
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26929
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

can't get it through:/

I ended up trying to store the value as int, which I guess would make
sense
because an unsigned value cannot be stored in Sql, and the next
thing
up
I
could find after smallint was int. Please tell me if I'm wrong. It does
make
sense to me..

However, casting back to UInt16 really does not work. I'm doing the
following right now :
System.UInt16 formatowner = (System.UInt16)(unchecked((bioData[3])));

I checked the value of bioData[3], it's always 53242:

bioData[3]
{53242}
System.ValueType: {System.Int32}
m_value: 53242
MaxValue: 2147483647
MinValue: -2147483648

I don't even think that I should use the unchecked() statement, I guess
it's
just for reassurance. Why would the following raise an invalid cast
exception?

You can get data out of SQL CE as Int16 and cast it to UInt16 using
unchecked().
Similarly, cast UInt16 to Int16 to store it.

Another solution would be to declare valuables as signed and let
native
code to do interpretation.
That might be problematic if you set values manually somewhere,
but
it
can
be done with unchecked().
Or, you can use equivalent values. For example binary representations
of
65535 unsigned and -1 signed for 16 bit values are exactly the same.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Wed, 13 Apr 2005 02:00:11 +0100
Lines: 686
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26864
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

the "unsafe" keyword worked great! vs doesn't complain anymore.

I'm trying to solve the UInt16 problem now. I followed your previous

advice
of using unchecked(). It still says that the value is too
small
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0
9.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26785
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I tried to follow you fixed() solution, but I get an error
saying
that
"Pointers can only be used in an unsafe context" and "Cannot
implicitly
convert type void* to System.IntPtr.

I've got the following down. I think the bit I'm missing
is
the
assignment
to IntPtr? And probably the casting as well.. Could you explain
though
what
the compiler means by the first error? I understand that
this
is
an
unsafe
block, so why does it complain?

fixed(void* BiometricDataPtr = (byte[])(bioData[8]))
{
SecondBIRSample.BiometricData = BiometricDataPtr;
}


message
I can see two reasons:



1. No need to free the memory after call is done.

2. No time wasted making temporary copy of something you
already
have.



Best regards,



Ilya


This posting is provided "AS IS" with no warranties, and
confers
no
rights.

message
Why would one want to use unsafe blocks is beyond me, when
it
is
so
easy
to call LocalAlloc and Marshal.Copy
wrote
in
message
It won't, but managed array (retrieved from SQL CE) should
be
already
allocated.



Best regards,



Ilya



This posting is provided "AS IS" with no warranties, and
confers
no
rights.


"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in message
Though fixed won't allocate any memory for you, so you're
still
where
you were before.

-Chris


in
message
You don't need to do anything to pin memory with fixed(),
fixed()
will
do
it for you:

fixed(void *ArrayPointer = MyManagedArray) {
// MyManagedArray is pinned within fixed block and
ArrayPointer
points
to
it.
// Assign it to IntPtr (might need some casting)
and
call
native
function
within this block.
}

// MyManagedArray in longer pinned...

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 20:37:43 +0100
Lines: 335
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26681
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

I did a little bit of research because I'm a newbie
regarding
pointers/memory allocation. I found an interesting post
http://groups-beta.google.com/group...28847ac90ac8a3/b92cb386364174f7?q=unmanaged+a
llocate+memory&rnum=10#b92cb386364174f7

I supose I could use LocalAlloc to allocate
memory.
The
post
seems
to
be
similar to my problem but I still don't understand how
to
pin
managed
memory
(by using Fixed()?), and have a pointer point to the
byte
array
(which is
probably what I would want to do) ?

your help is really appreciated!

""Ilya Tumanov [MS]""
wrote
in
message
Creating a new pointer is not enough. You need to
allocate
memory
and
assign value pointing to it to the pointer.
You might allocate native memory via P/Invoke (which
you
would
have
to
free
at some point).
Or, you can obtain a pointer to a managed array you
already
have.
Before you pass this pointer to a native code, you
have
to
pin
managed
memory so GC won't move the array.
You can use GCHandle class for it or you can use
unsafe
code.
I would recommend using Fixed() with unsafe pointer.
That
would
pin
and
unpin the array automatically.
GCHandle has a bug (returns incorrect pointer -
there's
a
workaround)
and
it's not automatic.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties,
and
confers
no
rights.
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 01:46:03 +0100
Lines: 240
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26658
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Okay, so I used the 1040 length, it doesn't
seem
to
complain,
which is
good
news I guess. To make sure it stores the data
correctly
I'm
trying
to
retrieve it and feed it back to the biometric
template
matching
method.

Because it takes IntPtr's for BiometricData and
Signature, I
tried
creating
new pointers and marshal copy the content that I
retrieved
earlier
into
these pointers. I failed. I tried the following:

System.IntPtr BiometricDataPtr;//create new pointer
BiometricData = (byte[])bioData[8];//BiometricData
is
an
array
of
type
byte
Marshal.Copy(BiometricData, 0, BiometricDataPtr,
1040);//copy
content
of
BiometricData in an IntPtr

"Use of unassigned local variable
'BiometricDataPtr'."
Obvsiously it's unused, it's meant to point to
something...
what
am I
missing here?

""Ilya Tumanov [MS]""
wrote
in
message
You should not guess the length, you should know
it.
It
might
be
fixed
or
native code could return it somehow.
Most likely you're correct and
FirstBIRSample.Length
contains
this
size.
You should check out the documentation for the
function
you're
calling
to
make sure it's correct.

If value of a pointer is 0, it's null
pointer.
It
means
this
pointer
is
not
valid and points to nothing.
Since no data is available, you could store
it
as
DBNull.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sat, 9 Apr 2005 22:32:29 +0100
Lines: 159
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26653
X-Tomcat-NG:

microsoft.public.dotnet.framework.compactframework

Thanks a lot for that! Before going to the
UInt16
problem, I
better
try
to
solve that IntPtr problem.

Managed to use Marshal.Copy. Not sure how I
should
'guess'
the
length
of
the
Byte array to copy to. When I type
FirstBIRSample.BiometricData
(which
is
the IntPtr) in the command window, it
returns
to
me:
2372256 -
is
this
the
size?
I 'assumed' the size would be 1040 (which is
stored
in
FirstBIRSample.Length).

Second Part: Seems like the
FirstBIRSample.Signature
(also a
pointer)
is
0,
how should I store this?

Thanks so much!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
While storing UInt16 as Int is OK, it will
take
more
space.
The
problem
you're having is caused by range check. You
might
consider
using
unchecked() to disable it.

System.Runtime.InteropServices.Marshal class
allows
you
to
copy
data
from
the pointer to the array. Look into Copy()
methods
of
that
class.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 8 Apr 2005 10:44:39 +0100
Lines: 91
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<#t8wc#[email protected]>
Newsgroups:

microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26572
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks so much for the replies Alex and
Ilya!

I changed the types in sql. It wasn't happy
with
Uint16
mapped
to
smallint
(sql says value is either too small or too
large),
so
I
just
went
for
an
int.

Not sure how to retrieve the data data an
IntPtr
is
pointing
to
though.
How
can I get that data as a byte array?

thanks so much again!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
pointless
to
with
 
J

Jeremy

getting there...

Before I insert the byte array in Sql I checked it, and the values from 0 to
99 (these are the ones that I can see easily check when in debug mode) are
filled. So that's okay.

When I extract the same data out of Sql and I do the same thing again, I
check rdr[8] (which is the biometricdata entry) and what do I get? Length of
1040, that's ok. But only the first position in the array is filled. And
guess what, with the same value as the byte array.

What I mean is:

BiometricData[0] = 144
rdr[8][0] = 144

That's totally fine. But from the second position up to the end, the values
are 0 in rdr[8]. I reckon that the array is not inserted properly in Sql.
Somehow, only the first value of the array is inserted and the rest...
well.. went in the air:p (at least I can still laugh about it, I don't know
if you can)

I couldn't try OleDbType.Image because I can't change the length in Sql from
16 to 1040 (don't know why). I did try OleDbType.binary when inserting, same
thing.

This is what I'm using now:
cmd.Parameters.Add("@BiometricData", OleDbType.VarBinary);
cmd.Parameters[8].Value = (byte[])(BiometricData);

There must be some casting problem. It's definitely in the insertion (or
maybe an incorrect extraction?!) ?

thanks again...

"Ilya Tumanov [MS]" said:
Hmm... Not sure. I would cast it to byte array just in case. You can also
try SqlDbType.Image.
And make sure array size is OK before it's added to the SQL.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 15 Apr 2005 01:41:18 +0100
Lines: 1172
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
2.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.compactframework:75583
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I appreciate your patience, really.

I'm certain (I think..) that the biometricdata is not correctly inserted in
SQL. I know that the marshal copying from pointer to byte[] went ok (tested
that out).

This is what I get for biometricdata when in debug mode as soon as I get it
out of SQL:

bioData[8]
{Length=1}
[0]: 144

This is not normal, right? (this is more of a rethorical question I
guess..) - I insert data in the SQL using OleDbType.VarBinary (as suggested)
and simply provide the data as an object (no casting). Did I skip a crucial
step somewhere ?


"Ilya Tumanov [MS]" said:
Assuming your array is 1040 (gee, that reminds me of something.) bytes in
size, why should byte 1995 at this pointer be empty (zero)?
This array is located in memory and probably surrounded by other stuff
allocated by the same or other programs.
If you get out of array bounds, you would read (or worse - write to) stuff
which is not yours, but it does not have to be empty.

That's a very common problem in C/C++ development because there's no
way
to
know the size of memory pointer is pointing to.
Usually this size is either fixed or provided separately (as separate
argument or filed in the structure).
You should use a spec of the function you're calling to find out the right
size.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 10:57:53 +0100
Lines: 988
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26946
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

correct - that did the trick! thanks for that. Now I can start eliminating
the problems.. There seems to be trouble with copying the biometricdata
from
to a Byte array. As mentioned earlier, I thought the length of
FirstBIRSample.BiometricData was 1040, however when I marshal copy from
the
pointer to Byte Array BiometricData that is of length [2000] (just for
testing purposes) and I check the content of let's say [1995], using
BiometricData[1995], guess what, it's not empty. It's a very sad thought.

How can I check the length of the data the pointer is pointing to so that
I
can create a Byte array of the correct length ? I suspect that is the
problem.

I suspect bioData[3] is an object. If so, you should cast it to whatever
type it is (e.g. Int32).
Next, cast it to whatever type you need:

System.UInt16 formatowner = unchecked( (System.UInt16)
((Int32)bioData[3])
);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 03:03:36 +0100
Lines: 840
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26929
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

can't get it through:/

I ended up trying to store the value as int, which I guess would make
sense
because an unsigned value cannot be stored in Sql, and the next thing
up
I
could find after smallint was int. Please tell me if I'm wrong. It
does
make
sense to me..

However, casting back to UInt16 really does not work. I'm doing the
following right now :
System.UInt16 formatowner = (System.UInt16)(unchecked((bioData[3])));

I checked the value of bioData[3], it's always 53242:

bioData[3]
{53242}
System.ValueType: {System.Int32}
m_value: 53242
MaxValue: 2147483647
MinValue: -2147483648

I don't even think that I should use the unchecked() statement, I
guess
it's
just for reassurance. Why would the following raise an invalid cast
exception?

You can get data out of SQL CE as Int16 and cast it to UInt16 using
unchecked().
Similarly, cast UInt16 to Int16 to store it.

Another solution would be to declare valuables as signed and let
native
code to do interpretation.
That might be problematic if you set values manually
somewhere,
but
it
can
be done with unchecked().
Or, you can use equivalent values. For example binary
representations
of
65535 unsigned and -1 signed for 16 bit values are exactly the same.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Wed, 13 Apr 2005 02:00:11 +0100
Lines: 686
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26864
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

the "unsafe" keyword worked great! vs doesn't complain anymore.

I'm trying to solve the UInt16 problem now. I followed your
previous

advice
of using unchecked(). It still says that the value is too
small
or
too
large
when trying to insert it in the DB using OleDbType.SmallInt (which
kind
of
makes sense seeing as the value is 53242).

Any other ideas that could help ?

message
Unsafe block won't eliminate type check (fortunately). You need
to
do
explicit cast:

SecondBIRSample.BiometricData = (IntPtr32)BiometricDataPtr;

First error means you need to declare this function with unsafe
keyword
and
use /unsafe compiler switch (can be set in project properties).

public unsafe void DoUnsafeStuff() {
// Can do unsafe stuff here...
..
}

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Tue, 12 Apr 2005 00:36:01 +0100
Lines: 560
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0
9.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26785
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

I tried to follow you fixed() solution, but I get an error
saying
that
"Pointers can only be used in an unsafe context" and "Cannot
implicitly
convert type void* to System.IntPtr.

I've got the following down. I think the bit I'm missing is
the
assignment
to IntPtr? And probably the casting as well.. Could you
explain
though
what
the compiler means by the first error? I understand that this
is
an
unsafe
block, so why does it complain?

fixed(void* BiometricDataPtr = (byte[])(bioData[8]))
{
SecondBIRSample.BiometricData = BiometricDataPtr;
}


message
I can see two reasons:



1. No need to free the memory after call is done.

2. No time wasted making temporary copy of something you
already
have.



Best regards,



Ilya


This posting is provided "AS IS" with no warranties, and
confers
no
rights.

"Alex Feinman [MVP]" <[email protected]>
wrote
in
message
Why would one want to use unsafe blocks is beyond
me,
when
it
is
so
easy
to call LocalAlloc and Marshal.Copy

--
Alex Feinman
---
Visit http://www.opennetcf.org
in
message
It won't, but managed array (retrieved from SQL CE)
should
be
already
allocated.



Best regards,



Ilya



This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.


"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in message
Though fixed won't allocate any memory for you, so
you're
still
where
you were before.

-Chris


""Ilya Tumanov [MS]""
wrote
in
message
You don't need to do anything to pin memory with
fixed(),
fixed()
will
do
it for you:

fixed(void *ArrayPointer = MyManagedArray) {
// MyManagedArray is pinned within fixed block and
ArrayPointer
points
to
it.
// Assign it to IntPtr (might need some casting) and
call
native
function
within this block.
}

// MyManagedArray in longer pinned...

Best regards,

Ilya

This posting is provided "AS IS" with no warranties,
and
confers
no
rights.
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 20:37:43 +0100
Lines: 335
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26681
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

I did a little bit of research because I'm a newbie
regarding
pointers/memory allocation. I found an interesting
post
by
Brian
Smith:
http://groups-beta.google.com/group...28847ac90ac8a3/b92cb386364174f7?q=unmanaged+a
llocate+memory&rnum=10#b92cb386364174f7

I supose I could use LocalAlloc to allocate memory.
The
post
seems
to
be
similar to my problem but I still don't
understand
how
to
pin
managed
memory
(by using Fixed()?), and have a pointer point
to
the
byte
array
(which is
probably what I would want to do) ?

your help is really appreciated!

""Ilya Tumanov [MS]""
wrote
in
message
Creating a new pointer is not enough. You need to
allocate
memory
and
assign value pointing to it to the pointer.
You might allocate native memory via P/Invoke (which
you
would
have
to
free
at some point).
Or, you can obtain a pointer to a managed
array
you
already
have.
Before you pass this pointer to a native code, you
have
to
pin
managed
memory so GC won't move the array.
You can use GCHandle class for it or you can use
unsafe
code.
I would recommend using Fixed() with unsafe pointer.
That
would
pin
and
unpin the array automatically.
GCHandle has a bug (returns incorrect pointer -
there's
a
workaround)
and
it's not automatic.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties,
and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 01:46:03 +0100
Lines: 240
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<#[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26658
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Okay, so I used the 1040 length, it doesn't seem
to
complain,
which is
good
news I guess. To make sure it stores the data
correctly
I'm
trying
to
retrieve it and feed it back to the biometric
template
matching
method.

Because it takes IntPtr's for BiometricData and
Signature, I
tried
creating
new pointers and marshal copy the content that I
retrieved
earlier
into
these pointers. I failed. I tried the following:

System.IntPtr BiometricDataPtr;//create new
pointer
BiometricData = (byte[])bioData[8];//BiometricData
is
an
array
of
type
byte
Marshal.Copy(BiometricData, 0, BiometricDataPtr,
1040);//copy
content
of
BiometricData in an IntPtr

"Use of unassigned local variable
'BiometricDataPtr'."
Obvsiously it's unused, it's meant to point to
something...
what
am I
missing here?

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
You should not guess the length, you
should
know
it.
It
might
be
fixed
or
native code could return it somehow.
Most likely you're correct and
FirstBIRSample.Length
contains
this
size.
You should check out the documentation for the
function
you're
calling
to
make sure it's correct.

If value of a pointer is 0, it's null pointer.
It
means
this
pointer
is
not
valid and points to nothing.
Since no data is available, you could
store
how
(which
is
stored
in
FirstBIRSample.Length).

Second Part: Seems like the
FirstBIRSample.Signature
(also a
pointer)
is
0,
how should I store this?

Thanks so much!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
While storing UInt16 as Int is OK, it will
take
more
space.
The
problem
you're having is caused by range
check.
You
might
consider
using
unchecked() to disable it.

System.Runtime.InteropServices.Marshal class
allows
you
to
copy
data
from
the pointer to the array. Look into Copy()
methods
of
that
class.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.
Subject: Re: To cast or not to cast?
Date: Fri, 8 Apr 2005 10:44:39 +0100
Lines: 91
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<#t8wc#[email protected]>
Newsgroups:

microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26572
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks so much for the replies Alex and
Ilya!

I changed the types in sql. It wasn't
happy
with
Uint16
mapped
to
smallint
(sql says value is either too small
or
too
large),
so
I
just
went
for
an
int.

Not sure how to retrieve the data
data
an
IntPtr
is
pointing
to
though.
How
can I get that data as a byte array?

thanks so much again!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
 
I

Ilya Tumanov [MS]

Since array is OK before it's inserted, this is not really a CF issue, but
a desktop SQL client/SQL Server issue.
Have you tried asking in desktop ADO.Net/SQL Server NG?

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<OUSV#[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 15 Apr 2005 19:37:13 +0100
Lines: 1377
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
2.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.compactframework:75635
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

getting there...

Before I insert the byte array in Sql I checked it, and the values from 0 to
99 (these are the ones that I can see easily check when in debug mode) are
filled. So that's okay.

When I extract the same data out of Sql and I do the same thing again, I
check rdr[8] (which is the biometricdata entry) and what do I get? Length of
1040, that's ok. But only the first position in the array is filled. And
guess what, with the same value as the byte array.

What I mean is:

BiometricData[0] = 144
rdr[8][0] = 144

That's totally fine. But from the second position up to the end, the values
are 0 in rdr[8]. I reckon that the array is not inserted properly in Sql.
Somehow, only the first value of the array is inserted and the rest...
well.. went in the air:p (at least I can still laugh about it, I don't know
if you can)

I couldn't try OleDbType.Image because I can't change the length in Sql from
16 to 1040 (don't know why). I did try OleDbType.binary when inserting, same
thing.

This is what I'm using now:
cmd.Parameters.Add("@BiometricData", OleDbType.VarBinary);
cmd.Parameters[8].Value = (byte[])(BiometricData);

There must be some casting problem. It's definitely in the insertion (or
maybe an incorrect extraction?!) ?

thanks again...

"Ilya Tumanov [MS]" said:
Hmm... Not sure. I would cast it to byte array just in case. You can also
try SqlDbType.Image.
And make sure array size is OK before it's added to the SQL.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 15 Apr 2005 01:41:18 +0100
Lines: 1172
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
2.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.compactframework:75583
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I appreciate your patience, really.

I'm certain (I think..) that the biometricdata is not correctly
inserted
in
SQL. I know that the marshal copying from pointer to byte[] went ok (tested
that out).

This is what I get for biometricdata when in debug mode as soon as I
get
it
out of SQL:

bioData[8]
{Length=1}
[0]: 144

This is not normal, right? (this is more of a rethorical question I
guess..) - I insert data in the SQL using OleDbType.VarBinary (as suggested)
and simply provide the data as an object (no casting). Did I skip a crucial
step somewhere ?


Assuming your array is 1040 (gee, that reminds me of something.)
bytes
in
size, why should byte 1995 at this pointer be empty (zero)?
This array is located in memory and probably surrounded by other stuff
allocated by the same or other programs.
If you get out of array bounds, you would read (or worse - write to) stuff
which is not yours, but it does not have to be empty.

That's a very common problem in C/C++ development because there's no way
to
know the size of memory pointer is pointing to.
Usually this size is either fixed or provided separately (as separate
argument or filed in the structure).
You should use a spec of the function you're calling to find out the right
size.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 10:57:53 +0100
Lines: 988
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26946
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

correct - that did the trick! thanks for that. Now I can start
eliminating
the problems.. There seems to be trouble with copying the biometricdata
from
to a Byte array. As mentioned earlier, I thought the length of
FirstBIRSample.BiometricData was 1040, however when I marshal copy from
the
pointer to Byte Array BiometricData that is of length [2000] (just for
testing purposes) and I check the content of let's say [1995], using
BiometricData[1995], guess what, it's not empty. It's a very sad
thought.

How can I check the length of the data the pointer is pointing to so
that
I
can create a Byte array of the correct length ? I suspect that is the
problem.

I suspect bioData[3] is an object. If so, you should cast it to
whatever
type it is (e.g. Int32).
Next, cast it to whatever type you need:

System.UInt16 formatowner = unchecked( (System.UInt16)
((Int32)bioData[3])
);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 03:03:36 +0100
Lines: 840
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26929
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

can't get it through:/

I ended up trying to store the value as int, which I guess would
make
sense
because an unsigned value cannot be stored in Sql, and the next
thing
up
I
could find after smallint was int. Please tell me if I'm
wrong.
It
does
make
sense to me..

However, casting back to UInt16 really does not work. I'm
doing
the
following right now :
System.UInt16 formatowner =
(System.UInt16)(unchecked((bioData[3])));

I checked the value of bioData[3], it's always 53242:

bioData[3]
{53242}
System.ValueType: {System.Int32}
m_value: 53242
MaxValue: 2147483647
MinValue: -2147483648

I don't even think that I should use the unchecked()
statement,
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0
9.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26785
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

I tried to follow you fixed() solution, but I get an error
saying
that
"Pointers can only be used in an unsafe context" and "Cannot
implicitly
convert type void* to System.IntPtr.

I've got the following down. I think the bit I'm
missing
is
the
assignment
to IntPtr? And probably the casting as well.. Could you
explain
though
what
the compiler means by the first error? I understand that
this
is
an
unsafe
block, so why does it complain?

fixed(void* BiometricDataPtr = (byte[])(bioData[8]))
{
SecondBIRSample.BiometricData = BiometricDataPtr;
}


"Ilya Tumanov [MS]" <[email protected]>
wrote
in
message
I can see two reasons:



1. No need to free the memory after call is done.

2. No time wasted making temporary copy of something you
already
have.



Best regards,



Ilya


This posting is provided "AS IS" with no warranties, and
confers
no
rights.

in
message
Why would one want to use unsafe blocks is beyond me,
when
it
is
so
easy
to call LocalAlloc and Marshal.Copy

--
Alex Feinman
---
Visit http://www.opennetcf.org
in
message
It won't, but managed array (retrieved from SQL CE)
should
be
already
allocated.



Best regards,



Ilya



This posting is provided "AS IS" with no warranties,
and
confers
no
rights.


"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in message
Though fixed won't allocate any memory for you, so
you're
still
where
you were before.

-Chris


""Ilya Tumanov [MS]""
wrote
in
message
You don't need to do anything to pin memory with
fixed(),
fixed()
will
do
it for you:

fixed(void *ArrayPointer = MyManagedArray) {
// MyManagedArray is pinned within fixed block and
ArrayPointer
points
to
it.
// Assign it to IntPtr (might need some
casting)
and
call
native
function
within this block.
}

// MyManagedArray in longer pinned...

Best regards,

Ilya

This posting is provided "AS IS" with no warranties,
and
confers
no
rights.
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 20:37:43 +0100
Lines: 335
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26681
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

I did a little bit of research because I'm a newbie
regarding
pointers/memory allocation. I found an interesting
post
by
Brian
Smith:
http://groups-beta.google.com/group...28847ac90ac8a3/b92cb386364174f7?q=unmanaged+a
llocate+memory&rnum=10#b92cb386364174f7

I supose I could use LocalAlloc to allocate memory.
The
post
seems
to
be
similar to my problem but I still don't understand
how
to
pin
managed
memory
(by using Fixed()?), and have a pointer point to
the
byte
array
(which is
probably what I would want to do) ?

your help is really appreciated!

""Ilya Tumanov [MS]""
wrote
in
message
Creating a new pointer is not enough. You
need
to
allocate
memory
and
assign value pointing to it to the pointer.
You might allocate native memory via P/Invoke
(which
you
would
have
to
free
at some point).
Or, you can obtain a pointer to a managed array
you
already
have.
Before you pass this pointer to a native
code,
you
have
to
pin
managed
memory so GC won't move the array.
You can use GCHandle class for it or you can use
unsafe
code.
I would recommend using Fixed() with unsafe
pointer.
That
would
pin
and
unpin the array automatically.
GCHandle has a bug (returns incorrect pointer -
there's
a
workaround)
and
it's not automatic.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 01:46:03 +0100
Lines: 240
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<#[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:


TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26658
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Okay, so I used the 1040 length, it
doesn't
seem
to
complain,
which is
good
news I guess. To make sure it stores the data
correctly
I'm
trying
to
retrieve it and feed it back to the biometric
template
matching
method.

Because it takes IntPtr's for
BiometricData
and
Signature, I
tried
creating
new pointers and marshal copy the content that I
retrieved
earlier
into
these pointers. I failed. I tried the following:

System.IntPtr BiometricDataPtr;//create new
pointer
BiometricData =
(byte[])bioData[8];//BiometricData
is
an
array
of
type
byte
Marshal.Copy(BiometricData, 0, BiometricDataPtr,
1040);//copy
content
of
BiometricData in an IntPtr

"Use of unassigned local variable
'BiometricDataPtr'."
Obvsiously it's unused, it's meant to
point
to
something...
what
am I
missing here?

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
You should not guess the length, you should
know
it.
It
might
be
fixed
or
native code could return it somehow.
Most likely you're correct and
FirstBIRSample.Length
contains
this
size.
You should check out the documentation
for
the
function
you're
calling
to
make sure it's correct.

If value of a pointer is 0, it's null pointer.
It
means
this
pointer
is
not
valid and points to nothing.
Since no data is available, you could
store
it
as
DBNull.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sat, 9 Apr 2005 22:32:29 +0100
Lines: 159
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26653
X-Tomcat-NG:

microsoft.public.dotnet.framework.compactframework

Thanks a lot for that! Before going to the
UInt16
problem, I
better
try
to
solve that IntPtr problem.

Managed to use Marshal.Copy. Not sure
how
I
should
'guess'
the
length
of
the
Byte array to copy to. When I type
FirstBIRSample.BiometricData
(which
is
the IntPtr) in the command window, it
returns
to
me:
2372256 -
is
this
the
size?
I 'assumed' the size would be 1040
(which
is
stored
in
FirstBIRSample.Length).

Second Part: Seems like the
FirstBIRSample.Signature
(also a
pointer)
is
0,
how should I store this?

Thanks so much!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message

While storing UInt16 as Int is OK,
it
will
take
more
space.
The
problem
you're having is caused by range check.
You
might
consider
using
unchecked() to disable it.

System.Runtime.InteropServices.Marshal
class
allows
you
to
copy
data
from
the pointer to the array. Look into Copy()
methods
of
that
class.

Best regards,

Ilya

This posting is provided "AS IS"
with
no
Alex
and
small
or
too
large),
so
I
just
went
for
an
int.

Not sure how to retrieve the data data
an
IntPtr
is
pointing
to
though.
How
can I get that data as a byte array?

thanks so much again!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
 
J

Jeremy

I just did to framework.adonet.. see you there:p
will post back here as soon as I have more info:)

in the meantime thanks!

"Ilya Tumanov [MS]" said:
Since array is OK before it's inserted, this is not really a CF issue, but
a desktop SQL client/SQL Server issue.
Have you tried asking in desktop ADO.Net/SQL Server NG?

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<OUSV#[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 15 Apr 2005 19:37:13 +0100
Lines: 1377
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
2.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl microsoft.public.dotnet.framework.compactframework:75635
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

getting there...

Before I insert the byte array in Sql I checked it, and the values from
0
to
99 (these are the ones that I can see easily check when in debug mode) are
filled. So that's okay.

When I extract the same data out of Sql and I do the same thing again, I
check rdr[8] (which is the biometricdata entry) and what do I get?
Length
of
1040, that's ok. But only the first position in the array is filled. And
guess what, with the same value as the byte array.

What I mean is:

BiometricData[0] = 144
rdr[8][0] = 144

That's totally fine. But from the second position up to the end, the values
are 0 in rdr[8]. I reckon that the array is not inserted properly in Sql.
Somehow, only the first value of the array is inserted and the rest...
well.. went in the air:p (at least I can still laugh about it, I don't know
if you can)

I couldn't try OleDbType.Image because I can't change the length in Sql from
16 to 1040 (don't know why). I did try OleDbType.binary when inserting, same
thing.

This is what I'm using now:
cmd.Parameters.Add("@BiometricData", OleDbType.VarBinary);
cmd.Parameters[8].Value = (byte[])(BiometricData);

There must be some casting problem. It's definitely in the insertion (or
maybe an incorrect extraction?!) ?

thanks again...

"Ilya Tumanov [MS]" said:
Hmm... Not sure. I would cast it to byte array just in case. You can also
try SqlDbType.Image.
And make sure array size is OK before it's added to the SQL.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]> <[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<OUSV#[email protected]>
Subject: Re: To cast or not to cast?
Date: Fri, 15 Apr 2005 01:41:18 +0100
Lines: 1172
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP1
2.phx.gbl
Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.compactframework:75583
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

I appreciate your patience, really.

I'm certain (I think..) that the biometricdata is not correctly inserted
in
SQL. I know that the marshal copying from pointer to byte[] went ok
(tested
that out).

This is what I get for biometricdata when in debug mode as soon as I get
it
out of SQL:

bioData[8]
{Length=1}
[0]: 144

This is not normal, right? (this is more of a rethorical question I
guess..) - I insert data in the SQL using OleDbType.VarBinary (as
suggested)
and simply provide the data as an object (no casting). Did I skip a
crucial
step somewhere ?


Assuming your array is 1040 (gee, that reminds me of something.) bytes
in
size, why should byte 1995 at this pointer be empty (zero)?
This array is located in memory and probably surrounded by other stuff
allocated by the same or other programs.
If you get out of array bounds, you would read (or worse - write to)
stuff
which is not yours, but it does not have to be empty.

That's a very common problem in C/C++ development because there's
no
way
to
know the size of memory pointer is pointing to.
Usually this size is either fixed or provided separately (as separate
argument or filed in the structure).
You should use a spec of the function you're calling to find out the
right
size.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no
rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 10:57:53 +0100
Lines: 988
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26946
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

correct - that did the trick! thanks for that. Now I can start
eliminating
the problems.. There seems to be trouble with copying the
biometricdata
from
to a Byte array. As mentioned earlier, I thought the length of
FirstBIRSample.BiometricData was 1040, however when I marshal copy
from
the
pointer to Byte Array BiometricData that is of length [2000]
(just
for
testing purposes) and I check the content of let's say [1995], using
BiometricData[1995], guess what, it's not empty. It's a very sad
thought.

How can I check the length of the data the pointer is pointing
to
so
that
I
can create a Byte array of the correct length ? I suspect that
is
the
problem.

I suspect bioData[3] is an object. If so, you should cast it to
whatever
type it is (e.g. Int32).
Next, cast it to whatever type you need:

System.UInt16 formatowner = unchecked( (System.UInt16)
((Int32)bioData[3])
);

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Thu, 14 Apr 2005 03:03:36 +0100
Lines: 840
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26929
X-Tomcat-NG: microsoft.public.dotnet.framework.compactframework

can't get it through:/

I ended up trying to store the value as int, which I guess would
make
sense
because an unsigned value cannot be stored in Sql, and the next
thing
up
I
could find after smallint was int. Please tell me if I'm
wrong.
It
does
make
sense to me..

However, casting back to UInt16 really does not work. I'm doing
the
following right now :
System.UInt16 formatowner =
(System.UInt16)(unchecked((bioData[3])));

I checked the value of bioData[3], it's always 53242:

bioData[3]
{53242}
System.ValueType: {System.Int32}
m_value: 53242
MaxValue: 2147483647
MinValue: -2147483648

I don't even think that I should use the unchecked()
statement,
I
guess
it's
just for reassurance. Why would the following raise an invalid
cast
exception?

message
You can get data out of SQL CE as Int16 and cast it to UInt16
using
unchecked().
Similarly, cast UInt16 to Int16 to store it.

Another solution would be to declare valuables as signed
and
let
native
code to do interpretation.
That might be problematic if you set values manually somewhere,
but
it
can
be done with unchecked().
Or, you can use equivalent values. For example binary
representations
of
65535 unsigned and -1 signed for 16 bit values are exactly the
same.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Wed, 13 Apr 2005 02:00:11 +0100
Lines: 686
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com 84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26864
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

the "unsafe" keyword worked great! vs doesn't complain
anymore.

I'm trying to solve the UInt16 problem now. I followed your
previous

advice
of using unchecked(). It still says that the value is too
small
or
too
large
when trying to insert it in the DB using OleDbType.SmallInt
(which
kind
of
makes sense seeing as the value is 53242).

Any other ideas that could help ?

""Ilya Tumanov [MS]"" <[email protected]>
wrote
in
message
Unsafe block won't eliminate type check (fortunately). You
need
to
do
explicit cast:

SecondBIRSample.BiometricData = (IntPtr32)BiometricDataPtr;

First error means you need to declare this function with
unsafe
keyword
and
use /unsafe compiler switch (can be set in project
properties).

public unsafe void DoUnsafeStuff() {
// Can do unsafe stuff here...
..
}

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and
confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References: <[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Tue, 12 Apr 2005 00:36:01 +0100
Lines: 560
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478
Message-ID: <[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host: host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTFEED01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP0
9.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.compactframework:26785
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

I tried to follow you fixed() solution, but I get an error
saying
that
"Pointers can only be used in an unsafe context" and
"Cannot
implicitly
convert type void* to System.IntPtr.

I've got the following down. I think the bit I'm missing
is
the
assignment
to IntPtr? And probably the casting as well.. Could you
explain
though
what
the compiler means by the first error? I understand that
this
is
an
unsafe
block, so why does it complain?

fixed(void* BiometricDataPtr = (byte[])(bioData[8]))
{
SecondBIRSample.BiometricData = BiometricDataPtr;
}


in
message
I can see two reasons:



1. No need to free the memory after call is done.

2. No time wasted making temporary copy of
something
you
already
have.



Best regards,



Ilya


This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.

in
message
Why would one want to use unsafe blocks is
beyond
me,
when
it
is
so
easy
to call LocalAlloc and Marshal.Copy
wrote
in
message
It won't, but managed array (retrieved from SQL CE)
should
be
already
allocated.



Best regards,



Ilya



This posting is provided "AS IS" with no warranties,
and
confers
no
rights.


"<ctacke/>" <ctacke_AT_OpenNETCF_com> wrote in
message
Though fixed won't allocate any memory for
you,
so
you're
still
where
you were before.

-Chris


""Ilya Tumanov [MS]""
wrote
in
message
You don't need to do anything to pin memory with
fixed(),
fixed()
will
do
it for you:

fixed(void *ArrayPointer = MyManagedArray) {
// MyManagedArray is pinned within fixed
block
and
ArrayPointer
points
to
it.
// Assign it to IntPtr (might need some casting)
and
call
native
function
within this block.
}

// MyManagedArray in longer pinned...

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.
--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<#[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 20:37:43 +0100
Lines: 335
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP15.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26681
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

I did a little bit of research because I'm a
newbie
regarding
pointers/memory allocation. I found an interesting
http://groups-beta.google.com/group...28847ac90ac8a3/b92cb386364174f7?q=unmanaged+a
llocate+memory&rnum=10#b92cb386364174f7

I supose I could use LocalAlloc to allocate
memory.
The
post
seems
to
be
similar to my problem but I still don't understand
how
to
pin
managed
memory
(by using Fixed()?), and have a pointer
point
to
the
byte
array
(which is
probably what I would want to do) ?

your help is really appreciated!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
Creating a new pointer is not enough. You need
to
allocate
memory
and
assign value pointing to it to the pointer.
You might allocate native memory via P/Invoke
(which
you
would
have
to
free
at some point).
Or, you can obtain a pointer to a managed array
you
already
have.
Before you pass this pointer to a native code,
you
have
to
pin
managed
memory so GC won't move the array.
You can use GCHandle class for it or you
can
use
unsafe
code.
I would recommend using Fixed() with unsafe
pointer.
That
would
pin
and
unpin the array automatically.
GCHandle has a bug (returns incorrect pointer -
there's
a
workaround)
and
it's not automatic.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.

--------------------
From: "Jeremy" <[email protected]>
References:
<[email protected]>
<[email protected]>
<#t8wc#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
Subject: Re: To cast or not to cast?
Date: Sun, 10 Apr 2005 01:46:03 +0100
Lines: 240
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<#[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:


TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26658
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Okay, so I used the 1040 length, it doesn't
seem
to
complain,
which is
good
news I guess. To make sure it stores the data
correctly
I'm
trying
to
retrieve it and feed it back to the biometric
template
matching
method.

Because it takes IntPtr's for BiometricData
and
Signature, I
tried
creating
new pointers and marshal copy the content
that I
retrieved
earlier
into
these pointers. I failed. I tried the
following:

System.IntPtr BiometricDataPtr;//create new
pointer
BiometricData =
(byte[])bioData[8];//BiometricData
is
an
array
of
type
byte
Marshal.Copy(BiometricData, 0,
BiometricDataPtr,
1040);//copy
content
of
BiometricData in an IntPtr

"Use of unassigned local variable
'BiometricDataPtr'."
Obvsiously it's unused, it's meant to
point
to
something...
what
am I
missing here?

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
You should not guess the length, you should
know
it.
It
might
be
fixed
or
native code could return it somehow.
Most likely you're correct and
FirstBIRSample.Length
contains
this
size.
You should check out the documentation for
the
function
you're
calling
to
make sure it's correct.

If value of a pointer is 0, it's null
pointer.
It
means
this
pointer
is
not
valid and points to nothing.
Since no data is available, you could store
it
as
DBNull.

Best regards,

Ilya

This posting is provided "AS IS" with no
warranties,
and
confers
no
rights.
Subject: Re: To cast or not to cast?
Date: Sat, 9 Apr 2005 22:32:29 +0100
Lines: 159
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE
V6.00.2800.1478
Message-ID:
<[email protected]>
Newsgroups:
microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:

TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26653
X-Tomcat-NG:

microsoft.public.dotnet.framework.compactframework

Thanks a lot for that! Before going
to
the
UInt16
problem, I
better
try
to
solve that IntPtr problem.

Managed to use Marshal.Copy. Not
sure
how
I
should
'guess'
the
length
of
the
Byte array to copy to. When I type
FirstBIRSample.BiometricData
(which
is
the IntPtr) in the command window, it
returns
to
me:
2372256 -
is
this
the
size?
I 'assumed' the size would be 1040 (which
is
stored
in
FirstBIRSample.Length).

Second Part: Seems like the
FirstBIRSample.Signature
(also a
pointer)
is
0,
how should I store this?

Thanks so much!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message

While storing UInt16 as Int is OK, it
will
take
more
space.
The
problem
you're having is caused by range check.
You
might
consider
using
unchecked() to disable it.

System.Runtime.InteropServices.Marshal
class
allows
you
to
copy
data
from
the pointer to the array. Look into
Copy()
methods
of
that
class.

Best regards,

Ilya

This posting is provided "AS IS"
with
no
warranties,
and
confers
no
rights.
Subject: Re: To cast or not to cast?
Date: Fri, 8 Apr 2005 10:44:39 +0100
Lines: 91
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook
Express
6.00.2800.1478
X-MimeOLE: Produced By Microsoft
MimeOLE
V6.00.2800.1478
Message-ID:
<#t8wc#[email protected]>
Newsgroups:

microsoft.public.dotnet.framework.compactframework
NNTP-Posting-Host:
host-84-9-101-26.bulldogdsl.com
84.9.101.26
Path:


TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
Xref: TK2MSFTNGXA01.phx.gbl

microsoft.public.dotnet.framework.compactframework:26572
X-Tomcat-NG:
microsoft.public.dotnet.framework.compactframework

Thanks so much for the replies Alex
and
Ilya!

I changed the types in sql. It wasn't
happy
with
Uint16
mapped
to
smallint
(sql says value is either too
small
or
too
large),
so
I
just
went
for
an
int.

Not sure how to retrieve the
data
data
an
IntPtr
is
pointing
to
though.
How
can I get that data as a byte array?

thanks so much again!

""Ilya Tumanov [MS]""
<[email protected]>
wrote
in
message
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/ it
as casting
them
 

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

Similar Threads


Top