Click here to Skip to main content
       

Managed C++/CLI

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page  Show 
QuestionHow to call a static libray in a dll?memberLongFangFang21 Jan '13 - 19:38 
AnswerRe: How to call a static libray in a dll?mvpRichard MacCutchan21 Jan '13 - 22:32 
Questionfrom unmanaged c++ to managed: passing int& parametermemberacastrucc19 Dec '12 - 4:30 
I have a third party unmanaged c++ library.
I created a managed c++ wrapper for it since I need to call it from c# code. Problem is one of native c++ functions passes an int by reference. I do not know how to route this through the managed wrapper to the c# code. For example
 
class A //unmnaged SDK class
{
public:
void Foo(int& i);
}
 
//managed c++ wrapper
public ref class A_Wrapper
{
public:
A_Wrapper() {_unmanaged = new A();};
~A_Wrapper() {delete unmanaged; };
void Foo(int& i) { _unmanaged->Foo(i)};
private:
A * _unmanaged;
}
 
In my c# (yes #) code I try to call A_Wrapper::Foo() but I get compile errors
 
c# code:
public static void Main()
{
A_Wrapper aw = new A_Wrapper();
int id = 0;
aw.Foo(&id);
}
error CS0214: Pointers and fixed size buffers may only be used in an unsafe context
 
I don't really want to wrap it in an unsafe {} block. How do I route the pass by reference from c++ to c# properly?
 

Update: Answered. (I figured it out) I had to change the wrapper class to use tracking references, i.e.
 
//managed c++ wrapper
public ref class A_Wrapper
{
public:
A_Wrapper() {_unmanaged = new A();};
~A_Wrapper() {delete unmanaged; };
void Foo(int% i)
{
int ret=0;
_unmanaged->Foo(ret);
i = ret;
};
private:
A * _unmanaged;
}
 
c# code:
public static void Main()
{
A_Wrapper aw = new A_Wrapper();
int id = 0;
aw.Foo(ref id);
}
QuestionCompilation Problem [modified]membertcnm12 Dec '12 - 9:18 
QuestionRe: Compilation ProblemmvpRichard MacCutchan12 Dec '12 - 22:56 
Answer-Re: Compilation Problem [modified]membertcnm14 Dec '12 - 6:42 
GeneralRe: -Re: Compilation ProblemmvpRichard MacCutchan14 Dec '12 - 21:43 
QuestionWhy c is so important?memberriceshoots10 Dec '12 - 4:47 
AnswerRe: Why c is so important?mvpRichard MacCutchan10 Dec '12 - 5:52 
QuestionPlease vote for C++/CLI debug visualizer supportmemberJohn Schroedl7 Dec '12 - 4:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid