Click here to Skip to main content
15,895,423 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am newbiew in C++/CLI. I am using C++/CLI to create a wrapper for a managed
library. I almost successfull in creating it.

This is how it works.

Native C++ -> (Calls) -> C++/CLI Wrapper -> (Calls) -> C# library and returns the result back.


The wrapper header is a pure C++ file. I am loading Wrapper in Native library using static linking.

The header looks like this...

C++
class NATIVEDLL_API Wrapper
{
public:
   // constructor
   Wrapper(LPCTSTR uid);
   virtual ~Wrapper();

   int GetInfo(MyStruct* struct_out);
};

struct MyStruct
{
	int length;
	MyEnum enum;
}

I am using this MyStruct (it's a struct) as a out parameter. It works for me, struct gets filled after the function returns.

But when we try to call this method continuously for about 1000 times it fails inside this function without any exception.

Is this happens because of the pointer is getting cleaned by GC? Can you suggest a better way to write this method.


Thanks
Basanth
Posted
Updated 30-Nov-10 7:34am
v2

1 solution

//calling API
String^ uid = gcnew String( "...." );
// Marshal the string to pointer before passing to native API
Wrapper* pWrapper = new Wrapper( static_cast<TCHAR*>(Marshal::StringToHGlobalUni(uid).ToPointer());

MyStruct* pStruct = new MyStruct();
Wrapper->GetInfo( pStruct );

// After usage delete the poinetrs
delete pStruct;
pStruct = 0;

delete pWrapper;
pWrapper = 0;


This may help you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900