473,695 Members | 2,600 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing c# class as parameter to managed c++ method

I have a managed C++ method that wraps unmanaged code and creates a
managed object holding data retrieved form an unmanged one. I want
create an instance of this managed class in C#, pass it to this method
and have it set the instance to hold the right data.
From what I've read it seems I should be able to pass C# objects to

managed C++ methods and it should just work; however, when I try it, my
C# instance comes out null. If I step into the C++ method, the
debugger says the parameter is out of scope, but it doesn't throw an
exception when the parameter is referred to in the code -- I assume
this is a debugger problem. But I still don't understand why it
doesn't come out correctly set.

Sorry if this question is unclear. Here's my code, hopefully it will
be more illuminating:

// C# code
public int GetLevel1()
{
// create the managed object in C#
BWCMsgLevel1 msg = null;
// if I instantiate it, then I can set the individual fields
// in the C++ code (see note below)
// BWCMsgLevel1 msg = new BWCMsgLevel1();

// call the managed C++ code, passing the managed object as a
parameter
int result = _DataConnection .GetLevel1(m_Sy mbol, msg);
return result;
}

// managed C++ code
BOOL Wrapper::GetLev el1(String* sSymbol, BWCMsgLevel1*
pMsgLevel1Manag ed)
{
// put the String into an LPCTSTR
LPCTSTR lpctSymbol;
const char * c = (const
char*)(System:: Runtime::Intero pServices::Mars hal::StringToHG lobalAnsi(sSymb ol)).ToPointer( );
std::string str = c;

System::Runtime ::InteropServic es::Marshal::Fr eeHGlobal(Syste m::IntPtr((void *)c));
lpctSymbol = str.c_str();

// create a local instance of the managed object
BWCMsgLevel1* pTest;

// create a local instance of the unmanaged object
CMsgLevel1* pMsgLevel1 = new CMsgLevel1();

// call unmanaged code to fill msgLevel1 with correct data
int nResult = m_pCStockCache->GetLevel1(lpct Symbol, pMsgLevel1, 0, 0);

if(nResult != 0)
{
// if successful getting data out of cache, put data into a
BWCMsgLevel1 object

// create a managed array of bytes
byte arr __gc[] = new byte __gc[pMsgLevel1->GetSize()];

// copy bytes from unmanaged msgLevel1 to managed arr
Marshal::Copy(m sgLevel1, arr, 0, pMsgLevel1->GetSize());

// set pTest to an instance of the managed class
// at this point, I can see pTest and check its members and they are
all set correctly
pTest = new BWCMsgLevel1(ar r, true);

// set parameter pMsgLevel1Manag ed equal to pTest
pMsgLevel1Manag ed = pTest;

// copy fields from unmanaged c++ level1 object to managed .NET
level1 object.
// This code works if pMsgLevel1Manag ed isn't null, but I would
prefer
// not to have to write code to copy each field because there are
going to be
// many, many more fields.
// The debugger still says pMsgLevel1Manag ed is out of scope, but I
can
// step through these lines without an error, and when I inspect the
object
// in C# the fields are set correctly.
//pMsgLevel1Manag ed->m_bDecimal = pMsgLevel1->m_bDecimal;
//pMsgLevel1Manag ed->m_cAskMC = pMsgLevel1->m_cAskMC;
//pMsgLevel1Manag ed->m_cBidMC = pMsgLevel1->m_cBidMC;
}

return nResult;
}

BWCMsgLevel1 is defined in a C# library that both this C# project and
the managed C++ project can see.
I'm not a C++ programmer so maybe I'm missing something. If there is a
better place to post this, please let me know. Thanks for your help.

Nov 16 '05 #1
2 4674
>// managed C++ code
BOOL Wrapper::GetLev el1(String* sSymbol, BWCMsgLevel1* >pMsgLevel1Mana ged)


If you want to return a new BWCMsgLevel1 instance you have to add an
additional level of indirection here (BWCMsgLevel1** ), effectively
making it a ref parameter in C#.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
That works. Thanks very much.

Nov 16 '05 #3

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

Similar topics

5
36407
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
1
3534
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
7
4746
by: Ken Allen | last post by:
I have a .net client/server application using remoting, and I cannot get the custom exception class to pass from the server to the client. The custom exception is derived from ApplicationException and is defined in an assembly common to the client and server components. The custom class merely defines three (3) constructors -- the null constructor; one with a string parameter; and one with a string and innner exception parameter -- that...
3
3789
by: Mark | last post by:
Hi From what I understand, you can pass arrays from classic ASP to .NET using interop, but you have to change the type of the.NET parameter to object. This seems to be because classic ASP passes a variant containing an array, and interop expects a parameter of type object if you are passing a variant (you are expected to cast it to the correct type in your code). I'd like to find a way of passing arrays so that you don't need to change...
7
10238
by: Tim | last post by:
When there is a need to pass some dynamic information between 2 managed assemblies, the "Dictionary object" in Generic form can be used as a method parameter to pass the information. The information that needs to be passed can be stored as Key-Value pairs, and the method signature remains the same. That way, handling future requirements of passing additional details to the callee can be handled without changing the method signature. Is...
1
1829
by: matthew breedlove | last post by:
I have a Managed VC++ WinForms app in VC8 calling a member function of a class in a native DLL. My managed code is similar to this: System::Void btnTest_Click(System::Object^ sender, System::EventArgs^ e) { Receiver r; std::vector<char> v; v.push_back('A');
12
11365
by: Haxan | last post by:
Hi, I have my main application class(unmanaged) that has a none static member function that I need to pass as a delegate to managed C# method. In one of the methods of this class(unmamanged), I am calling a managed C# method(I use gcnew to instantiate the managed class). One of the parameters of this C# method is a delegate. I need to pass the none static member function as a delegate(function pointer) as a parameter in the managed C#...
7
3303
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
17
7247
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need to show the array data to the end user. Can I do that? How?
0
8649
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
8586
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
9137
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...
0
9004
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8864
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
8838
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...
1
6506
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...
0
5842
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4592
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.