Re: A failing oif C#?
- From: "Willy Denoyette [MVP]" <willy.denoyette@xxxxxxxxxx>
- Date: Tue, 9 Aug 2005 22:26:21 +0200
"Matt" <matttelles@xxxxxxxxxxx> wrote in message
news:1123615773.907470.209150@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> Ken Allen wrote:
>> wackyphill@xxxxxxxxx wrote:
>> > You can infact mark a section of your c# code unsafe and use pointers
>> > just like c++.
>> >
>> How does one mark a section unsafe and have the code before and after
>> that section as safe (managed) code?
>>
>> How does one use pointers at all in C#? Even in unsafe code? If I have a
>> buffer, which is probably defined as a byte array, how do I 'cast' a
>> subset of that array to be treated as a structure with data members? The
>> only way I have seen is to marshal a (cast as IntPtr) specific subset of
>> the array into a structure.
>>
>> -ken
>
> You can do anything in unsafe code, including writing C++ (more or
> less) code:
>
> unsafe
> {
> char s[200];
> int x = 10;
> memcpy(
>
No it's not, you can't call (not directly) unmanaged code like memcpy(...),
calling unmanaged code from C# is only possible through PInvoke and COM
interop.
unsafe doesn't mean "native" it's simply an indication that the block may
contains native C style pointers and can perform pointer arithmetics but
that's it, the code remains managed (compiled to IL) code and the data like
a char array is still GC heap allocated and JIT tracked data.
unsafe {
// allocates a managed array of type char
char[] s = new char[200];
// pin the array object and take the address of the first char in the array
fixed (char* ptr = &s[0])
{
// use ptr f.i as an argument in a call to unmanaged code using
PInvoke interop
}// un-pin the char array!!!
}
Willy.
.
- Follow-Ups:
- Re: A failing oif C#?
- From: Matt
- Re: A failing oif C#?
- References:
- A failing oif C#?
- From: Ken Allen
- Re: A failing oif C#?
- From: wackyphill
- Re: A failing oif C#?
- From: Ken Allen
- Re: A failing oif C#?
- From: Matt
- A failing oif C#?
- Prev by Date: Re: Getting Null Value Error when Inserting record
- Next by Date: Re: newbie : save files not in virtual path
- Previous by thread: Re: A failing oif C#?
- Next by thread: Re: A failing oif C#?
- Index(es):
Relevant Pages
|