473,569 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert class instance to IntPtr

2 New Member
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 CGetSimpleFunct ionTest through COM. it calls GetSimpleFuncti onClass()to get the CTest object. Then it calls SimpleFunction( ) on CTest object.


Problem is when GetSimpleFuncti onClass() 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 7073
Plater
7,872 Recognized Expert Expert
Have you tried working with Marshal.Structu reToPtr()?

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 (StructLayoutAt tribute)"

I recomend using: [StructLayoutAtt ribute(LayoutKi nd.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
2407
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. Generally, the documentation suggests that queues or similar constructs should be used for thread inter-process comms. I've had a lot of success...
0
1715
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. Using the code below, I receive the error message: "AttributeError: 'PySwigObject' object has no attribute 'GetName'" The python callback...
1
2145
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 at the command prompt But the above statement raises an exception when I use CreateProcess(NULL,CommandLin ....) from a C++ code I have the c#...
2
22166
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 textBox to a float field. Thank you
7
7898
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 CreateObjInstance. And the function not only generate just CClass01. So I must sent the class type(whit it's namespace) into the function. There has two...
7
6602
by: Baski | last post by:
Base class: class AssetBase { string _clli; public string CLLI { get
3
1410
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 of a class instance to a file. But when loading it up the program can only read in the values stored. It has no idea what kind of class they came...
2
2126
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 IntPtr. VB2005 won't let me do this: CType(Me, IntPtr) How can I pass Me as the WPARAM arg? Bill
5
2155
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. With VB6 we learned very early on that you do *NOT* store VB6 class instances on the server. That is because a VB6 class can only be accessed on the...
12
3097
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 only ever ONE instantiation of this class, and this outside method in a separate thread has to access it. How do i do this?
0
7698
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...
0
7612
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...
0
7924
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. ...
0
8122
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1213
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
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...

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.