Hello:
I have a question on pinning types via 'fixed'. In "Com and .NET
interoperability" by Andrew Troelsen, he provides a simple example of
..NET -> Native interoperability:
int[] theVals={1,2,3,4};
MyCustomDLLWrapper.AddArray(theVals,theVals.Length )
where MyCustomDLLWrapper wraps a native (C++) .dll:
public class MyCustomDLLWrapper
{
[DllImport("MyCustomDll.dll")]
public static extern int AddNumbers(int x[], int size);
}
Where AddNumbers is defined (obviously) in MyCustomDll.dll
Now, for correctness, shouldn't he be locking the reference to the
array in memory so it isn't garbage collected? So, shouldn't it be:
int[] theVals={1,2,3,4};
fixed(int* pinnedAry = theVals)
{
MyCustomDLLWrapper.AddArray(pinnedAry,theVals.Leng th)
}
?
Thanks!
Chris Finlayson 2 1423
> I have a question on pinning types via 'fixed'. In "Com and .NET interoperability" by Andrew Troelsen, he provides a simple example of .NET -> Native interoperability:
int[] theVals={1,2,3,4}; MyCustomDLLWrapper.AddArray(theVals,theVals.Length )
where MyCustomDLLWrapper wraps a native (C++) .dll:
public class MyCustomDLLWrapper { [DllImport("MyCustomDll.dll")] public static extern int AddNumbers(int x[], int size); }
Where AddNumbers is defined (obviously) in MyCustomDll.dll
Now, for correctness, shouldn't he be locking the reference to the array in memory so it isn't garbage collected? So, shouldn't it be:
int[] theVals={1,2,3,4}; fixed(int* pinnedAry = theVals) { MyCustomDLLWrapper.AddArray(pinnedAry,theVals.Leng th) }
The array cannot be garbage collected since there is still a refererence on
it. But it might be moved in memory when not beeing fixed so it is IMHO
always nesseccary to fix objects when passing them to native functions.
--
cody
[Freeware, Games and Humor] www.deutronium.de.vu || www.deutronium.tk
Arrays of blittable types are automatically pinned by the P/Invoke
infrastructure when you pass them to unmanaged export functions.
However, they are only pinned for the duration of the call.
-Rob Teixeira [MVP]
"Chris Finlayson" <fi********@yahoo.com> wrote in message
news:9d**************************@posting.google.c om... Hello:
I have a question on pinning types via 'fixed'. In "Com and .NET interoperability" by Andrew Troelsen, he provides a simple example of .NET -> Native interoperability:
int[] theVals={1,2,3,4}; MyCustomDLLWrapper.AddArray(theVals,theVals.Length )
where MyCustomDLLWrapper wraps a native (C++) .dll:
public class MyCustomDLLWrapper { [DllImport("MyCustomDll.dll")] public static extern int AddNumbers(int x[], int size); }
Where AddNumbers is defined (obviously) in MyCustomDll.dll
Now, for correctness, shouldn't he be locking the reference to the array in memory so it isn't garbage collected? So, shouldn't it be:
int[] theVals={1,2,3,4}; fixed(int* pinnedAry = theVals) { MyCustomDLLWrapper.AddArray(pinnedAry,theVals.Leng th) }
?
Thanks!
Chris Finlayson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: demofo |
last post by:
Does the pinning pointer safe?since the Read function will change the
ppBuffer points to unmanaged C++ heap,how does the dot net runtime knows
they will not move the allocated memory (pointed by...
|
by: Ioannis Vranos |
last post by:
Consider the code:
wchar_t __pin *p= &(someCommand->ToCharArray());
_wsystem(p);
p=0;
|
by: Brian Victor |
last post by:
I have a situation where I have a vector<gcroot<ManagedWrapperClass*> >
whose contents I need to pass to an unmanaged function. Is there a way
to pin all the pointers in the vector for that...
|
by: Hexar Anderson |
last post by:
I have two questions:
a) From documentation located at
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/vcManagedExtensionsSpec_7_7.asp,
it says, "Pinning a...
|
by: Maxwell |
last post by:
Hello,
Newbie question here. I have a VS.NET 2003 MC++ (not C++/cli) project
where I have a managed class reference in a unmanaged class...simple
enough. To keep things short I am for the most...
|
by: steve |
last post by:
Hi all,
I want to understand more about how the pinvoke pinning process works.
I'm writing some code that calls DeviceIoControl.
DeviceIoControl provides a generic interface to device drivers....
|
by: Greg Young |
last post by:
Ok so I think everyone can agree that creating buffers on the fly in
an async socket server is bad ... there is alot of literature
available on the problems this will cause with the heap. I am...
|
by: DaTurk |
last post by:
Hi,
I have a rather interesting problem. I have a unmanged c++ class
which needs to communicate information to managed c++ via callbacks,
with a layer of c# on top of the managed c++ ultimatley...
|
by: =?Utf-8?B?VGhlTWFkSGF0dGVy?= |
last post by:
I am very sorry to bring a topic up that has been beaten with an ugly
stick..... but...
If I want to "fix" an byte array for the life time of a program would it be
better allocating it on the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
| |