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

when to pin?

In the following code snippet, I am calling into an unmanaged static .lib
file from a managed class. I belive that I have to pin the pointer before
making the call into the unmanaged code, but I don't completely understand
when a pin is required. Any thoughts?

void p4dn::ClientUser::OutputError( System::String* errString )
{
System::IntPtr ptr = Marshal::StringToHGlobalAnsi( errString );

// does this need to be pinned?
char __pin* str = static_cast< char * >( ptr.ToPointer() );

// unmanged code from static .lib
_clientUser->OutputError( str );

Marshal::FreeHGlobal( ptr );
}
Nov 16 '05 #1
3 2912
No, you don't need to pin this.

You only need pinning when you are going to pass the address of a member of
a __gc type over to unmanaged code I.e. treating it at a __nogc *).
Marshal::StringToHGLobalAnsi directly returns a __nogc *. It does this by
allocating memory from the NT heap and translating/copyign the string data
into that buffer.

Ronald Laeremans
Visual C++ team

"Jacob Gladish" <ja**********@yahoo.com> wrote in message
news:OQ**************@TK2MSFTNGP10.phx.gbl...
In the following code snippet, I am calling into an unmanaged static .lib
file from a managed class. I belive that I have to pin the pointer before
making the call into the unmanaged code, but I don't completely understand
when a pin is required. Any thoughts?

void p4dn::ClientUser::OutputError( System::String* errString )
{
System::IntPtr ptr = Marshal::StringToHGlobalAnsi( errString );

// does this need to be pinned?
char __pin* str = static_cast< char * >( ptr.ToPointer() );

// unmanged code from static .lib
_clientUser->OutputError( str );

Marshal::FreeHGlobal( ptr );
}

Nov 16 '05 #2
Jacob Gladish wrote:
System::IntPtr ptr = Marshal::StringToHGlobalAnsi( errString );
char __pin* str = static_cast< char * >( ptr.ToPointer() );


In this case you do not need pinning...
But for a better and faster solution see (PtrToStringChars; which needs
pinning):
HOW TO: Convert from System::String* to Char* in Visual C++ .NET
http://support.microsoft.com/?kbid=311259
--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
Nov 16 '05 #3
Ok, I understand the previous section of code, and I have found examples on
the msdn website that shows examples of this exact same code snippet. Here's
yet another problem I am unsure of. I have the following class structure,
and need to pass a reference or pointer of an managed class into an
unmanaged class. The reason is that I have an unmanaged class that would
normally be sub-classed and then passed into yet another unmanaged class and
have it's methods called as callbacks. The solution I have come up with is
to have a managed class with an instance of an unmanged class as it's member
which would simply call it's parent's mirrored methods.

__gc class ExposedManagedClass {

ExposedManagedClass() {
// need to pass managed pointer to self to unmanaged class. does
passing in a reference work?
_myCallbackDeletegate = new UnmanagedCallbackInterface( *this );
}

// programmer overrides this method.
void Foo( ManagedObj mo );

private:
UnmagedClallBackInterface* _myCallbackDeletegate;

private:
__nogc UnmanagedCallbackInterface : CallbackInterface {
public:
UnmanagedCallbackInterface( ExposedManagedClass& emc ) :
_parent(emc) { }

virtual void Foo( UnManagedObj uo ) {
ManagedObj mo = FromUnmangedObjToManagedObj( uo );
_parent.Foo( mo );
private:
ExposedManagedClass& _parent;
};
};

"Ronald Laeremans [MSFT]" <ro*****@online.microsoft.com> wrote in message
news:ue**************@tk2msftngp13.phx.gbl...
No, you don't need to pin this.

You only need pinning when you are going to pass the address of a member of a __gc type over to unmanaged code I.e. treating it at a __nogc *).
Marshal::StringToHGLobalAnsi directly returns a __nogc *. It does this by
allocating memory from the NT heap and translating/copyign the string data
into that buffer.

Ronald Laeremans
Visual C++ team

"Jacob Gladish" <ja**********@yahoo.com> wrote in message
news:OQ**************@TK2MSFTNGP10.phx.gbl...
In the following code snippet, I am calling into an unmanaged static ..lib file from a managed class. I belive that I have to pin the pointer before making the call into the unmanaged code, but I don't completely understand when a pin is required. Any thoughts?

void p4dn::ClientUser::OutputError( System::String* errString )
{
System::IntPtr ptr = Marshal::StringToHGlobalAnsi( errString );

// does this need to be pinned?
char __pin* str = static_cast< char * >( ptr.ToPointer() );

// unmanged code from static .lib
_clientUser->OutputError( str );

Marshal::FreeHGlobal( ptr );
}


Nov 16 '05 #4

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

Similar topics

4
by: Nuno | last post by:
Is there any SQL Error? Or I have to use Select case in VB code to control SQL instead. Thank you for any ans. Nuno
13
by: elad | last post by:
Hi The Menu doesn't work properly when I have 2 frame and the Menu popup frame=document target frame, when I choose item in the menu the doc opened and the menu get stuck. Here is the code...
24
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
7
by: sql-db2-dba | last post by:
Does DB2 just fudge it when it is an empty table? Is there a "formula" for average row size when you have variable length records. Or you really have to know what your application is packing into...
7
by: Nicolae Fieraru | last post by:
Hi All, I am trying to change the rowsource of a combobox when I click on it. I played with many events, associated with the form and the combobox, but still haven't figured out what is the way...
4
by: Peter Row | last post by:
Hi, I have created a UserControl which is subsequently hosted on a standard form. My control has a TabControl on it but it has no TabPages configured. At runtime I create X pages and put a...
5
by: AAguiar | last post by:
I have an asp.net project where the code behind the aspx page calls a c# class which makes calls to a managed static C++ class. The C# class works fine when the asp net worker process starts, when...
8
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
44
by: Smokey Grindle | last post by:
I have a list box on my form, but I need to databind it to a data table that is a private member of the form's class... so I basically have Public Class MyForm priate m_MyTable as new datatable...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.