473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to convert class instance to IntPtr

I have two C# classes (Interop & Assembly)which is shown below

Expand|Select|Wrap|Line Numbers
  1. public class CTest
  2. {
  3.    public CTest()
  4.    {
  5.    }
  6.  
  7.    public void SimpleFunction()
  8.    {
  9.    }
  10. }
  11.  
  12. public class CGetSimpleFunctionTest
  13. {
  14.    public CGetSimpleFunctionTest()
  15.    {
  16.    }
  17.  
  18.    public IntPtr GetSimpleFunctionClass()
  19.    {
  20.        CTest test = new CTest();
  21.  
  22.        IntPtr pTest = new IntPtr();
  23.        GCHandle gch = GCHandle.Alloc(test, GCHandleType.Pinned);
  24.    }
  25. }
  26.  
I have a unmanged c++ client and c++ client creates an instance of CGetSimpleFunctionTest through COM. it calls GetSimpleFunctionClass()to get the CTest object. Then it calls SimpleFunction() on CTest object.


Problem is when GetSimpleFunctionClass() is called, GCHandle.Alloc is throwing an exception "Object contains non-primitive or non-blittable data." . I don't have any data members in CTest class just one function SimpleFunction().


I searched entire net to solve the above problem, but i could not get any solution.

Please Please can anyone helpme to solve this problem
Dec 12 '10 #1
1 7032
Plater
7,872 Expert 4TB
Have you tried working with Marshal.StructureToPtr()?

EDIT: Ok it produces the same message pretty much.
For Marshal.SizeOf() I see this:
"You can only use Marshal.SizeOf to get the size of a value type (struct) or
a class that has an explicit layout declaration (StructLayoutAttribute)"

I recomend using: [StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
That will allow marshalling of your class, and pack=1 means it will allign the data bytes on single byte offsets (my default seems to be 4 bytes)
Dec 14 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Matthew Bell | last post by:
Hi, I've got a question about whether there are any issues with directly calling attributes and/or methods of a threaded class instance. I wonder if someone could give me some advice on this. ...
0
by: Jeff | last post by:
I'm trying to pass a proxy class instance (SWIG generated) of CClass, to a python callback function from C++. The proxy class instance of CClass is created from a pointer to the C++ class CClass....
1
by: vipin | last post by:
My C# console application takes 5 parameters two of which are supposed to be inherently integers So I do COnvert.ToInt32(args) There is no problem with the console application when I run it...
2
by: Alberto | last post by:
why there isn't a Convert.ToFloat() method in the Convert class? Sometimes I can't work with a float field because there isn't this method and I need it to, for example, convert the input in a...
7
by: Steven.Xu | last post by:
Hello everyone! I have a problem about create an instance automatically in C#. Now I have a class: namespace1.namespace11.CClass01. I want to create it's instance in a function named...
7
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
3
by: Peter Oliphant | last post by:
Trying to save/load a class instance to a file is tough. The reason is because there is no way to preserve its type other than by conventon. For eample, one can save all the states of the members...
2
by: swartzbill2000 | last post by:
I want to PostMessage from a class instance to its containing form. I want the WPARAM to point to the class instance. I am using the declaration of PostMessage from www.pinvoke.net. WPARAM is an...
5
by: Joseph Geretz | last post by:
Of course, I can store a C# class instance to the Server Cache (this.Context.Cache). I've tried it. My question is, will this destroy the scalability of my application? My background is VB6....
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...

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.