Re: To cast or not to cast?
- From: ilyatum@xxxxxxxxxxxxxxxxxxxx ("Ilya Tumanov [MS]")
- Date: Sun, 10 Apr 2005 21:33:33 GMT
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" <jezonline@xxxxxxxxxxx>
> References: <umEpDi8OFHA.3880@xxxxxxxxxxxxxxxxxxxx>
<64$0lp9OFHA.1564@xxxxxxxxxxxxxxxxxxxxx>
<#t8wc#BPFHA.1268@xxxxxxxxxxxxxxxxxxxx>
<tz30okGPFHA.920@xxxxxxxxxxxxxxxxxxxxx>
<eTuV9uUPFHA.2568@xxxxxxxxxxxxxxxxxxxx>
<SN73xxVPFHA.920@xxxxxxxxxxxxxxxxxxxxx>
<#Hr63aWPFHA.3336@xxxxxxxxxxxxxxxxxxxx>
<VMZf79WPFHA.920@xxxxxxxxxxxxxxxxxxxxx>
> 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: <Oo3kPTgPFHA.580@xxxxxxxxxxxxxxxxxxxx>
> 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/microsoft.public.dotnet.framework.compac
tframework/browse_frm/thread/d628847ac90ac8a3/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]"" <ilyatum@xxxxxxxxxxxxxxxxxxxx> wrote in message
> news:VMZf79WPFHA.920@xxxxxxxxxxxxxxxxxxxxxxxx
> > 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" <jezonline@xxxxxxxxxxx>
> > > References: <umEpDi8OFHA.3880@xxxxxxxxxxxxxxxxxxxx>
> > <64$0lp9OFHA.1564@xxxxxxxxxxxxxxxxxxxxx>
> > <#t8wc#BPFHA.1268@xxxxxxxxxxxxxxxxxxxx>
> > <tz30okGPFHA.920@xxxxxxxxxxxxxxxxxxxxx>
> > <eTuV9uUPFHA.2568@xxxxxxxxxxxxxxxxxxxx>
> > <SN73xxVPFHA.920@xxxxxxxxxxxxxxxxxxxxx>
> > > 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: <#Hr63aWPFHA.3336@xxxxxxxxxxxxxxxxxxxx>
> > > 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]"" <ilyatum@xxxxxxxxxxxxxxxxxxxx> wrote in message
> > > news:SN73xxVPFHA.920@xxxxxxxxxxxxxxxxxxxxxxxx
> > > > 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" <jezonline@xxxxxxxxxxx>
> > > > > References: <umEpDi8OFHA.3880@xxxxxxxxxxxxxxxxxxxx>
> > > > <64$0lp9OFHA.1564@xxxxxxxxxxxxxxxxxxxxx>
> > > > <#t8wc#BPFHA.1268@xxxxxxxxxxxxxxxxxxxx>
> > > > <tz30okGPFHA.920@xxxxxxxxxxxxxxxxxxxxx>
> > > > > 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: <eTuV9uUPFHA.2568@xxxxxxxxxxxxxxxxxxxx>
> > > > > 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]"" <ilyatum@xxxxxxxxxxxxxxxxxxxx> wrote in
> message
> > > > > news:tz30okGPFHA.920@xxxxxxxxxxxxxxxxxxxxxxxx
> > > > > > 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" <jezonline@xxxxxxxxxxx>
> > > > > > > References: <umEpDi8OFHA.3880@xxxxxxxxxxxxxxxxxxxx>
> > > > > > <64$0lp9OFHA.1564@xxxxxxxxxxxxxxxxxxxxx>
> > > > > > > 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#BPFHA.1268@xxxxxxxxxxxxxxxxxxxx>
> > > > > > > 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]"" <ilyatum@xxxxxxxxxxxxxxxxxxxx> wrote in
> > > message
> > > > > > > news:64$0lp9OFHA.1564@xxxxxxxxxxxxxxxxxxxxxxxx
> > > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > >
> > >
> >
>
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
> > > > > > > > frlrfsystemdatasqldbtypeclasstopic.asp
> > > > > > > >
> > > > > > > > UInt32: Int will do. Signed and Unsigned integers are
actually
> > the
> > > > > same;
> > > > > > > > just cast them to the right type.
> > > > > > > > UInt16: SmallInt, see warning above
> > > > > > > > Byte: TinyInt
> > > > > > > > BioAPI_*: look up underlying type definition and use it.
Enums
> > are
> > > > > Int32
> > > > > > > by
> > > > > > > > default.
> > > > > > > > IntPtr: it's Int32, so store it as Int.
> > > > > > > >
> > > > > > > > Note, however, it's probably pointless to store pointers in
> the
> > > data
> > > > > > base.
> > > > > > > > Pointer itself is not important, most likely you need to
store
> > > > > whatever
> > > > > > > > it's pointing to.
> > > > > > > >
> > > > > > > > Best regards,
> > > > > > > >
> > > > > > > > Ilya
> > > > > > > >
> > > > > > > > This posting is provided "AS IS" with no warranties, and
> confers
> > > no
> > > > > > > rights.
> > > > > > > >
> > > > > > > > --------------------
> > > > > > > > > From: "Jeremy" <jezonline@xxxxxxxxxxx>
> > > > > > > > > Subject: To cast or not to cast?
> > > > > > > > > Date: Fri, 8 Apr 2005 00:21:17 +0100
> > > > > > > > > Lines: 25
> > > > > > > > > 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: <umEpDi8OFHA.3880@xxxxxxxxxxxxxxxxxxxx>
> > > > > > > > > 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:26546
> > > > > > > > > X-Tomcat-NG:
> > microsoft.public.dotnet.framework.compactframework
> > > > > > > > >
> > > > > > > > > Hi,
> > > > > > > > >
> > > > > > > > > I'm trying to convert the following types into types that
I
> > can
> > > > > insert
> > > > > > > in
> > > > > > > > an
> > > > > > > > > SQL database (via a webservice). None of these however are
> > > > accepted
> > > > > by
> > > > > > > Sql
> > > > > > > > > (i.e., there is no OleDbType for them). I could only use
> > integer
> > > > > type
> > > > > > > for
> > > > > > > > > UInt32, that's about it. I'm totally unsure about the
other
> > > types.
> > > > > > What
> > > > > > > > I've
> > > > > > > > > tried doing so far is casting them prior to sending them
to
> > the
> > > > > > > > webservice.
> > > > > > > > > When inserting them I get errors though. Would anyone
> > know/could
> > > > > point
> > > > > > > me
> > > > > > > > to
> > > > > > > > > types that I could use? Any help would be really
> appreciated,
> > > i'm
> > > > > > lost!
> > > > > > > > >
> > > > > > > > > Thanks so much!
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > System.UInt32 Length;
> > > > > > > > > System.Byte HeaderVersion;
> > > > > > > > > BioAPI_BIR_DATA_TYPE Type;
> > > > > > > > > System.UInt16 FormatOwner;
> > > > > > > > > System.UInt16 FormatID;
> > > > > > > > > System.Byte Quality; /*INT8 BioAPI_QUALITY*/
> > > > > > > > > BioAPI_BIR_PURPOSE Purpose;
> > > > > > > > > BioAPI_BIR_AUTH_FACTORS FactorsMask;
> > > > > > > > > System.IntPtr BiometricData;
> > > > > > > > > System.IntPtr Signature;
> > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > > >
> > > > > >
> > > > >
> > > > >
> > > > >
> > > >
> > >
> > >
> > >
> >
>
>
>
.
- Follow-Ups:
- Re: To cast or not to cast?
- From: <ctacke/>
- Re: To cast or not to cast?
- References:
- To cast or not to cast?
- From: Jeremy
- RE: To cast or not to cast?
- From: "Ilya Tumanov [MS]"
- Re: To cast or not to cast?
- From: Jeremy
- Re: To cast or not to cast?
- From: "Ilya Tumanov [MS]"
- Re: To cast or not to cast?
- From: Jeremy
- Re: To cast or not to cast?
- From: "Ilya Tumanov [MS]"
- Re: To cast or not to cast?
- From: Jeremy
- Re: To cast or not to cast?
- From: "Ilya Tumanov [MS]"
- Re: To cast or not to cast?
- From: Jeremy
- To cast or not to cast?
- Prev by Date: How to get user input
- Next by Date: Re: To cast or not to cast?
- Previous by thread: Re: To cast or not to cast?
- Next by thread: Re: To cast or not to cast?
- Index(es):
Relevant Pages
|
Loading