473,659 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using GCHandle with Enums

Why can't you pin an enumeration? For example, the with the following code:

public enum MyEnum : byte
{
ValueOne = 1,
ValueTwo = 2
}

public class MyClass
{
public MyFunc()
{
MyEnum m = MyEnum.ValueOne ;
GCHandle myGch = GCHandle.Alloc( m, GCHandleType.Pi nned);
}
}

When you run MyFunc(), then a System.Argument Exception is thrown: "Object
contains non-primitive or non-blittable data." This seems quite odd to me;
if the underlying type of an enum is blittable (byte) then shouldn't the enum
itself be blittable too? How does one pin an enum then?

The second part of my question is, what's the best way to pin a 2D array of
enums, eg:

public MyFunc2()
{
MyEnum[,] array = new MyEnum[3, 3];
GCHandle h = GCHandle.Alloc( array[0,0], GCHandleType.Pi nned); // Same
exception
}
Nov 17 '05 #1
1 2603
take a look at below:

http://msdn.microsoft.com/library/de...ingpinning.asp

"Hexar Anderson" <He***********@ discussions.mic rosoft.com> wrote in message
news:B1******** *************** ***********@mic rosoft.com...
Why can't you pin an enumeration? For example, the with the following
code:

public enum MyEnum : byte
{
ValueOne = 1,
ValueTwo = 2
}

public class MyClass
{
public MyFunc()
{
MyEnum m = MyEnum.ValueOne ;
GCHandle myGch = GCHandle.Alloc( m, GCHandleType.Pi nned);
}
}

When you run MyFunc(), then a System.Argument Exception is thrown: "Object
contains non-primitive or non-blittable data." This seems quite odd to
me;
if the underlying type of an enum is blittable (byte) then shouldn't the
enum
itself be blittable too? How does one pin an enum then?

The second part of my question is, what's the best way to pin a 2D array
of
enums, eg:

public MyFunc2()
{
MyEnum[,] array = new MyEnum[3, 3];
GCHandle h = GCHandle.Alloc( array[0,0], GCHandleType.Pi nned); // Same
exception
}

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
3706
by: Marquee | last post by:
Hello, This is my first program c#, my background is c++. One thing I would like to do is put binary data (e.g. a record from disk, or network packet) in a struct. In C++ I would create the struct and memcpy() the data into it thus creating a structured overlay. The struct can have variable length e.g. struct CRecord //Just for example {
3
4178
by: Raj | last post by:
I want to pass a C structure from a windows server to the C# client using the Sockets. Will there be requirements to cast the data types in the C# client?
47
3514
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen); It is meant to be called by C, but I would rather use it from C#. So I wrote the following C# program to test it, but it failed to work. The call to compress doesn't return or throw an exception, it simply...
0
1103
by: Lonewolf | last post by:
Hi, please pardon me if I sound stupid. I want to check the status of the GChandle to see if a previous call to Alloc is successful, or whether it has been called before hand, before I call the Free function. I tried the following code, GCHandle gch; gch=GCHandle::Alloc(Mydelegate); <== How do I check if this is successful? gch.Free();
2
7900
by: steve | last post by:
Hi all, I want to understand more about how the pinvoke pinning process works. I'm writing some code that calls DeviceIoControl. DeviceIoControl provides a generic interface to device drivers. Its signature is deliberately open-ended so that it can be highly generic. Refer SDK for more. I want to access the drive geommetry of an SD-Card via
6
14065
by: Bart Burkhardt | last post by:
Hi, I could use some help in setting a C# callback function that an external unmanaged dll will call on a event. Using a delegate and use the external callback set function doesn't work. The carbage collector says hello here (-; I have pasted some code below, any help is greatly appreciated! //Bart
3
8271
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0 build with these methods, will appear to encrypt and decrypt, but the resulting decrypted file will be corrupted. I tried encrypting a .bmp file and then decrypting, the resulting decrypted file under .NET 2.0 is garbage, the .NET 1.1 build works...
1
4613
by: =?Utf-8?B?SkE=?= | last post by:
I use a method for threading that instantiates an object that is a wrapper to a DLL (written in C). The wrapper class is passed a byte array, and then does GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); IntPtr ptr = (IntPtr)(handle.AddrOfPinnedObject().ToInt32() + buffer.Length-1); It passes ptr to the DLL function FunctionX().
7
6433
by: DaTurk | last post by:
Hi, I'm coding up an application that has a native c++ layer, asynchronously calling callback in a CLI layer. We originally did this with static inline methods in the CLI layer, but this solution only works with singleton objects. So I have to explore other solutions. So beside pinning pointers, I've been looking at GCHandle. I was
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8850
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.