473,783 Members | 2,546 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do we have to keep the object alive while doing pinvoke?

Hello,

After reading a few articles,
http://blogs.gotdotnet.com/cbrumme/P...0-f0fa27ab6cc0
http://blogs.gotdotnet.com/anathan/c...7-1b6c677214cb

i have the following question:

__gc class C
{
C(){ ptr = new Unmanaged(); }
~C(){ delete ptr; ptr = 0; }

void DoWork()
{

//Do we have to HandleRef ref(this, IntPtr(ptr));
ptr->UnmnagedCall() ;
//or call GC::KeepAlive(t his)
}

Unmanaged __nogc* ptr;
};

Do we have to do
HandleRef ref(this, IntPtr(ptr)) or GC::KeepAlive?
No one seems to talk about this. Does MC++ take care of
this problem?

Thanx for any input
Nov 16 '05 #1
1 1866
Hello
The rules are very simple .

Consider a simple sync unmanaged call from a managed function.
What kind of data are you passing to the unmanaged function? Nothing ? You
have nothing to worrry about !
If you are only passing addresses of any managed data like a string
pointer, a garbage collection cycle can change them(remember the GC will
move the objected) and so using them from unmanaged functions is not safe.
That is why you will pin the variable. see code below

#using <mscorlib.dll >
using namespace System;
#include <vcclr.h>
#include <stdio.h>
int main()
{
String* s = "hello";
const __wchar_t __pin* pinned_ptr = PtrToStringChar s(s);
//s will be alive till pinned_ptr is in scope. Sufficient for
Synchronous functions
wprintf(pinned_ ptr); //unmanaged call

}

On the other hand , if you are making a async call, you want to keep a
Pinned GCHandle alive till the async call is complete. This is for
unmanaged functions manipulating the managed heap directly and so the need
to not move them .

THis is using IJW in MC++ . If you are using PINVOKE like in C# , the
pinvoke layer will take care of this for sync calls.
http://blogs.gotdotnet.com/cbrumme/d...05-06T00:00:00
also has some data.

--------------------
From: cp*****@yahoo.c om (cppdev)
Newsgroups: microsoft.publi c.dotnet.langua ges.vc
Subject: Do we have to keep the object alive while doing pinvoke?
Date: 25 Jul 2003 11:56:07 -0700
Organization : http://groups.google.com/
Lines: 30
Message-ID: <fc************ **************@ posting.google. com>
NNTP-Posting-Host: 68.164.38.125
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google. com 1059159368 24658 127.0.0.1 (25 Jul 2003 18:56:08 GMT)X-Complaints-To: gr**********@go ogle.com
NNTP-Posting-Date: 25 Jul 2003 18:56:08 GMT
Path: cpmsftngxa06.ph x.gbl!TK2MSFTNG P08.phx.gbl!new sfeed00.sul.t-online.de!t-onlin
e.de!news-spur1.maxwell.s yr.edu!news.max well.syr.edu!sn-xit-03!sn-xit-06!sn-
xit-09!supernews.co m!postnews1.goo gle.com!not-for-mailXref: cpmsftngxa06.ph x.gbl microsoft.publi c.dotnet.langua ges.vc:26553
X-Tomcat-NG: microsoft.publi c.dotnet.langua ges.vc

Hello,

After reading a few articles,
http://blogs.gotdotnet.com/cbrumme/P...71-48b9-b360-f 0fa27ab6cc0http://blogs.gotdotnet.com/anathan/c...6290-4500-a8c7 -1b6c677214cb
i have the following question:

__gc class C
{
C(){ ptr = new Unmanaged(); }
~C(){ delete ptr; ptr = 0; }

void DoWork()
{

//Do we have to HandleRef ref(this, IntPtr(ptr));
ptr->UnmnagedCall() ;
//or call GC::KeepAlive(t his)
}

Unmanaged __nogc* ptr;
};

Do we have to do
HandleRef ref(this, IntPtr(ptr)) or GC::KeepAlive?
No one seems to talk about this. Does MC++ take care of
this problem?

Thanx for any input


Nov 16 '05 #2

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

Similar topics

0
1064
by: xeoicq | last post by:
I wrote a COM server in Python where all the clients use the same global object(test_obj). So far it works, but when the last client is closed the Python COM enviornment is closed and the global object is lost. How can I prevent that? I need that new clients use the same global object and not a new created one. I figured out a workaround, but there must be another solution. The code looks like:
1
2818
by: Robin Tucker | last post by:
Just a quick question about connection management. My application will never need more than 1 or 2 connections about at any given time. Also, I do not expect many users to be connected at any given time. For efficiency, I would like to keep connections alive throughout the lifetime of the objects requiring them, rather than opening a new connection, executing code and then closing it again. What is the most efficient way of doing this?...
0
1834
by: Gil Strauss | last post by:
When using .net framework HttpWebRequest object, how can we set the connection header to "keep-alive" Acording to Microsoft ducomentation, setting the keepAlive property to true, should do the job, but it doesn't seem to do so When trying to change the header manually (using httpWebRequestObject.Connection = "Keep-Alive") we get the following error Argument Exception: "Keep-Alive and Close may not be set with this property. We try the...
2
7296
by: David Rasmussen | last post by:
Someone once asked about this an got no answer: http://groups.google.dk/group/comp.lang.python/browse_frm/thread/c3cc0b8d7e9cbc2/ff11efce3b1776cf?lnk=st&q=python+http+%22keep+alive%22&rnum=84&hl=da#ff11efce3b1776cf Maybe I am luckier :) Does anyone know how to do Keep-Alive with urllib2, that is, how to get a persistent HTTP connection, instead of a new connection being opened for every request?
0
933
by: Dody Suria Wijaya | last post by:
I'm looking for a RPC library in Python that can keep its TCP connection alive for the duration of a user's login session. The reason is, each user login to the application server will impose a single dedicated database connection, and a TCP disconnect will indiciate application server to then destroy the database connection properly. Thanks. background:
19
2446
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; //every class may have this statement self.hello = function() {
6
5679
by: laredotornado | last post by:
Hi, When a user logs into our site, we create a session variable to denote the session is active and another to denote who is logged in. Once the user closes the browser and re-opens it, the session is destroyed and the variables are gone. How can I keep the session alive for 24 hours even if the user closes and re-opens the browser?
1
1297
by: Laura D. | last post by:
website transactions? Hi all. I've working on http://www.picturesoffood.org about pictures of food. Yummy! Anyway, I upload all the files using FTP, but then I use php to process them, either the server times out or maybe the server runs out of memory space for my account. I'm using shared webhosting. Anyway, is there a way to keep the connection alive while the code processes the files. It's just a simple for loop and I call php's image...
0
1509
by: Ramchandar | last post by:
In my aspx page i want the lot of informations to be filled by the user. But wen the user takes some break to fill the form n resumes after some time the page gets expired. So to keep the session alive i used the following javascript code but it didnt work. function KeepMeAlive() { var myImg; myImg =document.getElementById('keepAliveIMG'); if (myImg) myImg.src =...
0
9480
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
10147
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
10083
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
8968
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
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
6737
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
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.