From: AMercer on
I have a legacy C dll that I call from VB .net, and I'm unsure about when
pinning is necessary. The called functions in the dll run on a worker thread.

The case I am worried about is an array of doubles. I have an array 'dim
x(10) as double'. I call a C function with x(0) passed byref, and the C
function expects double*. This works fine, but I don't know if array x needs
to be pinned. In other words, I may have a bug that will eventually bite me
when GC and my call run concurrently. Does anyone know whether pinning is
necessary or not in this setting?

From: Armin Zingler on
Am 27.05.2010 22:24, schrieb AMercer:
> I have a legacy C dll that I call from VB .net, and I'm unsure about when
> pinning is necessary. The called functions in the dll run on a worker thread.
>
> The case I am worried about is an array of doubles. I have an array 'dim
> x(10) as double'. I call a C function with x(0) passed byref, and the C
> function expects double*. This works fine, but I don't know if array x needs
> to be pinned. In other words, I may have a bug that will eventually bite me
> when GC and my call run concurrently. Does anyone know whether pinning is
> necessary or not in this setting?

IIRC, if you pass an array item ByRef to a native function, the whole array
is pinned automatically. I'm still looking for the source of this information
(I remember I once read it) but I'm pretty sure it's true.

--
Armin
From: Herfried K. Wagner [MVP] on
Armin --

Am 27.05.2010 23:05, schrieb Armin Zingler:
>> I have a legacy C dll that I call from VB .net, and I'm unsure about when
>> pinning is necessary. The called functions in the dll run on a worker thread.
>>
>> The case I am worried about is an array of doubles. I have an array 'dim
>> x(10) as double'. I call a C function with x(0) passed byref, and the C
>> function expects double*. This works fine, but I don't know if array x needs
>> to be pinned. In other words, I may have a bug that will eventually bite me
>> when GC and my call run concurrently. Does anyone know whether pinning is
>> necessary or not in this setting?
>
> IIRC, if you pass an array item ByRef to a native function, the whole array
> is pinned automatically. I'm still looking for the source of this information
> (I remember I once read it) but I'm pretty sure it's true.

IIRC this is at least the case if the function is declared using VB's
'Declare' statement.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
From: AMercer on
> > IIRC, if you pass an array item ByRef to a native function, the whole array
> > is pinned automatically. I'm still looking for the source of this information
> > (I remember I once read it) but I'm pretty sure it's true.

> IIRC this is at least the case if the function is declared using VB's
> 'Declare' statement.

I am using 'Declare', and the sub parameter is declared like 'byref x as
double'. In my vb code, I have 'dim x(10) as double', and I call the sub
passing x(0). So, it sounds like I am safe. I have also searched high and
low for info on this subject, and I couldn't find anything definite.

 | 
Pages: 1
Prev: display var value
Next: best solution