473,396 Members | 1,864 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,396 software developers and data experts.

Activator::CreateInstance( unmanaged type )

Hi,

VC++ 2008 /clr:

I have an unmanaged class: "class U" and managed one: "class ref M{ M(
const U* ); }". In other words, the constructor of the managed class M
takes the pointer to the unmanaged class U. I can call the constructor
directly no problem. I want to instantiate M via reflection.

Activator::CreateInstance() takes an array of parameters to be passed to the
constructor:

array< System::Object^ >^ pParams = gcnew array< System::Object^ >( 1 );
const U* pu = new U;
pParams[ 0 ] = pu;

The last line gives error C2440: '=' : cannot convert from 'const U *' to
'System::Object ^

My question is, how do I pass a pointer to an unmanaged class as a
constructor parameter to Activator::CreateInstance()?

Thank you,

Andrew

Aug 19 '08 #1
2 1541
Hi muriwai!
array< System::Object^ >^ pParams = gcnew array< System::Object^ >( 1 );
const U* pu = new U;
pParams[ 0 ] = pu;
For "pointers" you should use "System::IntPtr"

Greetings
Jochen
Aug 19 '08 #2
Thanks Jochen. I tried the following:

const U* pu = new U;
pParams[ 0 ] = gcnew System::IntPtr( ( PVOID) pu );
Activator.CreateInstance( type, pParams );

but the last line throws an exception because it can't find the constructor
(the constructor is "internal" by the way, and I am calling from the same
DLL).

I ended up with type->GetConstructors() and then explicitly invoking the
constructor with IntPtr as a parameter.

Cheers
"Jochen Kalmbach [MVP]" <no********************@holzma.dewrote in message
news:uY*************@TK2MSFTNGP06.phx.gbl...
Hi muriwai!
>array< System::Object^ >^ pParams = gcnew array< System::Object^ >( 1 );
const U* pu = new U;
pParams[ 0 ] = pu;

For "pointers" you should use "System::IntPtr"

Greetings
Jochen
Aug 19 '08 #3

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

Similar topics

2
by: shmeian | last post by:
I have the following code which works fine. However I want to pass the object I'm instantiating a string for its constructor. I can't get the syntax right. Can someone give me an example of...
3
by: Guy | last post by:
im running some code to instantiate a form, based on the name selected at runtime. Im using System.Activator.CreateInstance to load a form by its fully typed name, here is the code.... string...
2
by: Frank Pleyer via .NET 247 | last post by:
Hi, I got the following problem : I have an defined an Array of different Actions: public PRootActions AllActions = new PRootActions ; I got a dynamic method where all Actions or other...
3
by: Doug Riley | last post by:
I am using CreateInstance to create an instance of a class and invoke a function of that class. I really need it to execute in a single line of code (long story, but I want to execute this code in...
1
by: Nancy Sui | last post by:
I am trying to dynamically load a dll which specifies different rules for evalution with the following code. DataRow dr = ds.Tables.Rows; string fileName = Convert.ToString(dr); assemblyInstance...
2
by: jcomber | last post by:
Hi, I'd be grateful for some help... I'm trying to create a late bound object using: Activator.CreateInstance(Type, ConstructorParameters()) The constructor of the object takes a delegate...
1
by: Ryan.Mohammed | last post by:
I have a problem that does not happen without .NET 1.1 however it does happen on machines that have .NET 1.1 SP1 installed. Createinstance attempts to create an instance of ICEBOX.dll which...
1
by: Johnny R | last post by:
Hello, I'm loading a Class from Assemly DLL using Activator.CreateInstance. That loaded Class is executed in a worker Thread with no loop. What actually happends when class is loaded using...
0
by: midhunmathe | last post by:
We have a remote server application called from a client application. The server application is a .NET application but built with COM supportability and included as a COM+ service on one system....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...
0
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,...

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.