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.
Its signature is deliberately open-ended so that it can be highly generic.
Refer SDK for more.
I want to access the drive geommetry of an SD-Card via
IOCTL_DISK_GET_ DRIVE_GEOMETRY.
I've tried three different methods for calling DeviceIoControl .
Two work, but the third doesn't. I want to know why?
The three methods are presented below,
And are delimited by "//////////////////"
Cheers Steve
//////////////////
//
// Passing an appropriately defined DISK_GEOMETRY structure by ref works..
ie.
[DllImport("kern el32.dll", ExactSpelling = true, SetLastError = true,
CharSet = CharSet.Auto)]
private static extern int DeviceIoControl (
IntPtr hDevice, uint dwIoControlCode ,
IntPtr lpInBuffer, uint nInBufferSize,
ref DISK_GEOMETRY lpOutBuffer, uint nOutBufferSize,
out uint lpBytesReturned , IntPtr lpOverlapped);
....
DISK_GEOMETRY DiskGeometry = new DISK_GEOMETRY() ;
....
// This works
DeviceIoControl (
...,
ref DiskGeometry, // StructLayout sequential, packing = 1
(uint) Marshal.SizeOf( typeof(DISK_GEO METRY), // 24 bytes
...);
//////////////////
//
// Pinning an allocated byte[] and passing via an IntPtr through lpOutBuffer
works
// yes i know you can just pass a byte array by ref
ie.
[DllImport("kern el32.dll", ExactSpelling = true, SetLastError = true,
CharSet = CharSet.Auto)]
private static extern int DeviceIoControl (
IntPtr hDevice, uint dwIoControlCode ,
IntPtr lpInBuffer, uint nInBufferSize,
IntPtr lpOutBuffer, uint nOutBufferSize,
out uint lpBytesReturned , IntPtr lpOverlapped);
....
byte[] DiskGeometry = new byte[24];
GCHandle PinnedDiskGeome try = GCHandle.Alloc( DiskGeometry,
GCHandleType.Pi nned);
IntPtr pDiskGeometry = PinnedDiskGeome try.AddrOfPinne dObject();
....
// This works
DeviceIoControl (
...,
pDiskGeometry,
(uint) DiskGeometry.Le ngth,
...);
....
PinnedDiskGeome try.Free();
//////////////////
//
// Pinning an allocated DISK_GEOMETRY structure
// and passing via an IntPtr through lpOutBuffer DOES NOT WORK
//
// Why? Is it because structs are allocated on the stack?
//
ie.
[DllImport("kern el32.dll", ExactSpelling = true, SetLastError = true,
CharSet = CharSet.Auto)]
private static extern int DeviceIoControl (
IntPtr hDevice, uint dwIoControlCode ,
IntPtr lpInBuffer, uint nInBufferSize,
IntPtr lpOutBuffer, uint nOutBufferSize,
out uint lpBytesReturned , IntPtr lpOverlapped);
....
DISK_GEOMETRY DiskGeometry = new DISK_GEOMETRY() ;
GCHandle PinnedDiskGeome try = GCHandle.Alloc( DiskGeometry,
GCHandleType.Pi nned);
IntPtr pDiskGeometry = PinnedDiskGeome try.AddrOfPinne dObject();
....
DeviceIoControl (
...,
pDiskGeometry, // StructLayout sequential, packing = 1
(uint) Marshal.SizeOf( typeof(DISK_GEO METRY), // 24 bytes
...);
// Call succeeds but DiskGeometry is not updated. WHY???
....
PinnedDiskGeome try.Free();
Cheers,
Steve 2 7900
> // Call succeeds but DiskGeometry is not updated. WHY???
If DISK_GEOMETRY is declared as a struct, you'll end up pinning a
boxed copy in the GCHandle.Alloc call. That copy will get modified,
not the original DiskGeometry instance.
Mattias
--
Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Ahhhh.. caught by value sematics again
Thanks,
Steve
"Mattias Sjögren" wrote: // Call succeeds but DiskGeometry is not updated. WHY???
If DISK_GEOMETRY is declared as a struct, you'll end up pinning a boxed copy in the GCHandle.Alloc call. That copy will get modified, not the original DiskGeometry instance.
Mattias
-- Mattias Sjögren [C# MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com Please reply only to the newsgroup. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Ted Miller |
last post by:
Hi folks,
I've got an unmanaged routine I'm pinvoking to. It takes a pointer to an
array of 3 pointers to bytes, typed as byte**.
public static extern void foo(byte **p3pb);
unsafe {
byte pB = new byte;
|
by: TT (Tom Tempelaere) |
last post by:
Hi people
I am wrapping a C dll using PInvoke from C#. I need to wrap the following signature in C
int dma_start( const UCHAR* data, UINT data_length )
The function should start with a DMA operation, and I have to use it from my C# program. I am really unsure what type of marshalling I should use.
The data parameter is a pointer to an array of byte data. This data must not be copied when calling the function. Plus, the .NET...
|
by: promko |
last post by:
Hi!
I need to call the following unmanged method:
HFCI __cdecl FCICreate( void FAR* pv)
I have written the next managed declaration:
public static extern IntPtr FCICreate(
IntPtr pv
);
|
by: Sai Kit Tong |
last post by:
I have to interface managed application with my legacy dll. I have employed
the wrapper approach but I have to deal with the asynchronous callback from
the legacy dll, which likely goes through a thread other than the initial
calling thread. I got the idea from MSDN and other articles
on using the delegate. However, for garabage collection issue, I need to pin
the delegate. Since my callback is asynchronous, I have been thinking about...
|
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 sub-object defined in a managed object has the effect of
pinning the entire object. For example, if any element of an array is pinned,
then the whole array is also pinned. There are no extensions to the language
for declaring a pinned array. To pin an...
| |
by: jim4u |
last post by:
I am porting a vb library to vb.net. The vb library has an external
call to an unmanaged dll.
Existing code:
//External function declaration
Private Declare Function Uncompress& Lib "COMPR.DLL" (ByVal SrcSt$,
ByVal SrcLen&, Dest As Any, ByVal DestLen&)
//Invocation of external function
|
by: jim4u |
last post by:
I am porting a vb library to vb.net. The vb library has an external
call to an unmanaged dll.
Existing code:
//External function declaration
Private Declare Function Uncompress& Lib "COMPR.DLL" (ByVal SrcSt$,
ByVal SrcLen&, Dest As Any, ByVal DestLen&)
//Invocation of external function
|
by: _iycrd |
last post by:
After numerous problems, I'm having second thoughts about using
C++/CLI to wrap a native DLL. On the other hand, PInvoke seems like
it will take a huge amount of work, if it will work at all.
The native DLL uses raw data buffers that are normally handed to it
(via pointer) from another native function. If I'm piloting this with
C#/PInvoke, how could those raw data buffers be created and
immobilized? In C++/CLI, there is pin_ptr to...
|
by: mike1reynolds |
last post by:
I am calling legacy Btrieve database functions using StructureToPtr and
PtrToStructure, but I think that I am getting memory corruption from improper
marshalling, and I am unclear about when memory is being copied vs memory
being pinned. Does StructureToPtr pin, or just copy?
I've seen references to pinning memory using GCHandles while googling the
topic, and I suspect that I might do better using GCHandle functions rather
than Marshal...
|
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,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |