Pin Data
Pin Data
![]() |
| OEM USB DATA TRANSFER CABLE FOR SMASUNG 20 PIN PHONES | |
![]() |
$4.49 Time Remaining: 31m Buy It Now for only: $4.49 Buy It Now |
| RCA AH731R Data power cable USB 4 pin USB Type A M mini USB T AH731R | |
![]() |
$9.99 Time Remaining: 37m Buy It Now for only: $9.99 Buy It Now |
| 98cm Length External eSATA 8 pin Data Female Extendable Cable | |
![]() |
$6.44 Time Remaining: 46m Buy It Now for only: $6.44 Buy It Now |
| High Speed USB 20 A to Mini B 5 pin Male USB data Cable 5P Lead New UK | |
![]() |
$1.55 Time Remaining: 53m Buy It Now for only: $2.18 Buy It Now | Bid now |
| 25 PIN D connector METAL HOOD 17 1657 25 mil spec NEW wpi data comm db25 | |
![]() |
$6.00 Time Remaining: 1h 14m Buy It Now for only: $6.00 Buy It Now |
| 30Pin Sync Data Extension Charger USB Cable For Apple iPod Nano Touch 4 3G EA214 | |
![]() |
$0.10 Time Remaining: 1h 16m |
| D USB to Mini 5 Pin Data Charging Cable Line for MP3 MP4 | |
![]() |
$0.94 Time Remaining: 1h 23m |
| High Quality 6 Pin USB Data Sync Cable Charger For Ipad 1 2 3 iPHONE 4 4S 3GS | |
![]() |
$0.02 Time Remaining: 1h 58m |
| USB A to Mini B 5 Pin Data Cable Adapter Male M MP3 DC | |
![]() |
$0.99 Time Remaining: 2h 3m Buy It Now for only: $0.99 Buy It Now |
| DB9 9 Pin Serial Female to 25 Pin Male Data Cable 2m | |
![]() |
$6.77 Time Remaining: 2h 24m Buy It Now for only: $6.77 Buy It Now |
| BAFO 8740 25S 25 Pin 4 way Data Transfer Switch | |
![]() |
$31.12 Time Remaining: 2h 41m Buy It Now for only: $31.12 Buy It Now |
| Extension Data Cable 7 Pin Female Serial ATA SATA | |
![]() |
$2.94 Time Remaining: 3h 5m Buy It Now for only: $2.94 Buy It Now |
| Retractable USB Male A to Micro 5 Pin USB Sync Transfer Data Cable New | |
![]() |
$3.15 Time Remaining: 3h 9m Buy It Now for only: $3.50 Buy It Now | Bid now |
| 100 Pcs 25cm USB 20 Type A Male to Mini B 5 Pin Male MP3 MP4 Data Charger Cable | |
![]() |
$67.42 Time Remaining: 3h 16m Buy It Now for only: $67.42 Buy It Now |
| BRAND NEW USB 20 A Male TO 5 PIN MINI USB B DATA CABLE 12 LONG | |
![]() |
$1.99 Time Remaining: 3h 29m Buy It Now for only: $1.99 Buy It Now |
| New iFrogz Unique Sync 30 Pin USB Data Cable for Apple iPhone iPod iPad PURPLE | |
![]() |
$14.50 Time Remaining: 3h 30m Buy It Now for only: $14.50 Buy It Now |
| 30pin Extension Data Sync USB Cable For Apple iPhone iPod Touch new iPad 3 AC35B | |
![]() |
$0.01 (1 Bid) Time Remaining: 3h 37m |
| New iFrogz Unique Sync 30 Pin USB Data Cable for Apple iPhone iPod iPad WHITE | |
![]() |
$14.50 Time Remaining: 4h 1m Buy It Now for only: $14.50 Buy It Now |
| Brand New Data Link Cable BMW 20pin to obd2 16 Pin Connector Free Shippping | |
![]() |
$6.55 Time Remaining: 4h 4m |
| Dell 17 inch 34 Pin Floppy Drive Data Cable W5775 | |
![]() |
$3.00 Time Remaining: 4h 44m Buy It Now for only: $3.00 Buy It Now |
| Lot of 50 New 2 USB 20 A to 5 pin Mini B USB data charging mp3 phone mp4 cable | |
![]() |
$29.99 Time Remaining: 4h 48m Buy It Now for only: $29.99 Buy It Now |
| Lot of 100 New USB A to 5 pin Mini B data charging cable for mp3 phone camera | |
![]() |
$39.99 Time Remaining: 4h 52m Buy It Now for only: $39.99 Buy It Now |
| 15 FT High Speed USB 20 A Male to Micro B 5 Pin M M Data Charging Cable 15FT | |
![]() |
$4.49 Time Remaining: 4h 58m Buy It Now for only: $4.49 Buy It Now |
| 10 FT FireWire Cable 6 to 4 PIN IEEE 1394 iLINK DV 6P 4P M M PC MAC Data10FT | |
![]() |
$4.45 Time Remaining: 4h 58m Buy It Now for only: $4.45 Buy It Now |
| 15 FT IEEE 1394 4P 4 Pin IEEE 1394 iLink FireWire Cable 4P 4P M M Data 15FT | |
![]() |
$5.49 Time Remaining: 4h 58m Buy It Now for only: $5.49 Buy It Now |
| Serial SATA ATA Data Power Y Splitter Cable 7 + 15 Pin | |
![]() |
$10.47 Time Remaining: 5h 18m Buy It Now for only: $10.47 Buy It Now |
| Phone 1M Black Micro 5pin USB Data Charger Cord Cable | |
![]() |
$3.86 Time Remaining: 5h 20m Buy It Now for only: $3.86 Buy It Now |
| USB 8 Pins Digital Camera Data Cable for EasyShare C310 | |
![]() |
$6.29 Time Remaining: 6h 13m Buy It Now for only: $6.29 Buy It Now |
| For PC Male to Male Serial SATA 22Pin Data Power Cable | |
![]() |
$6.99 Time Remaining: 6h 38m Buy It Now for only: $6.99 Buy It Now |
| IOMEGA ZIP EXT DRIVE PARALLEL DATA CABLE 25 PIN 3 FT | |
![]() |
$8.95 Time Remaining: 6h 55m Buy It Now for only: $8.95 Buy It Now |
| USB A Male to Mini USB B 5 Pin Female Data Cable Adapter Converter Coupler | |
![]() |
$0.40 (1 Bid) Time Remaining: 7h 4m |
| 50 LOT USB A to Micro B 5 Pin USB Data Charger Cable | |
![]() |
$71.25 Time Remaining: 7h 7m Buy It Now for only: $71.25 Buy It Now |
| 6 Feet 40 Pin USB Data Charger Cable for Asus Eee Pad Transformer TF101 Tablet | |
![]() |
$8.99 Time Remaining: 7h 16m |
| 5x High Speed USB 20 5 Pin Mini B to A Data Cable for MP3 MP4 Camera Adapter | |
![]() |
$2.71 Time Remaining: 7h 18m Buy It Now for only: $2.71 Buy It Now |
| New High Speed USB 20 5 Pin Mini B to A Data Cable for MP3 MP4 Camera Adapter | |
![]() |
$0.99 Time Remaining: 7h 19m |
| 394 USB 20 Type A to Mini 5 Pin Extension Data Cable Black | |
![]() |
$5.19 Time Remaining: 7h 21m Buy It Now for only: $5.19 Buy It Now |
| USB Data U4 U 4 Cable Cord 4 pin for Kodak EasyShare DX4330 DX4530 CX6230 CX6445 | |
![]() |
$3.28 Time Remaining: 7h 27m |
| 2 PCS 22 Pin SATA Power Data to IDE + Serial ATA Cable | |
![]() |
$5.75 Time Remaining: 7h 37m Buy It Now for only: $5.75 Buy It Now |
| Mini 5 pin USB MIC to USB11 USB 20 AM OTG Adapter Data Cord Cable Kable New | |
![]() |
$1.55 Time Remaining: 7h 39m Buy It Now for only: $1.55 Buy It Now |
| Female eSATA to eSATA 7 Pin Data Extension Cable 118 | |
![]() |
$9.62 Time Remaining: 7h 44m Buy It Now for only: $9.62 Buy It Now |
| USB Mini 5Pin Data Charger 11M Cable for Motorola L6 | |
![]() |
$8.85 Time Remaining: 8h 1m Buy It Now for only: $8.85 Buy It Now |
| Mint Original OEM HTC Flyer DC500 12Pin ExtMicro USB Sync Data Cable Charger | |
![]() |
$9.99 Time Remaining: 8h 14m Buy It Now for only: $9.99 Buy It Now |
| 15+7 Pin Power Data to 4 pin IDE Power SATA Date Cable | |
![]() |
$4.18 Time Remaining: 8h 16m Buy It Now for only: $4.18 Buy It Now |
| 4 PCS 15ft 7pin SERIAL ATA SATA RAID DATA HDD HARD DRIVE Data Transfer CABLE | |
![]() |
$5.98 Time Remaining: 8h 26m |
| 4 pcs x 7 Copper Pin feMale to Female Series ATA SATA Data Cable hdd Drive | |
![]() |
$5.98 Time Remaining: 8h 26m |
| Mint Original OEM HTC Flyer Tablet DC500 12Pin USB Data Cable+Home Wall Charger | |
![]() |
$10.99 Time Remaining: 8h 28m Buy It Now for only: $10.99 Buy It Now |
| Oki Data Dot Matrix 18 pin Printer Pacemark 4410 Large Format Continuous Feed | |
![]() |
$875.00 Time Remaining: 8h 30m Buy It Now for only: $875.00 Buy It Now |
| USB 20 Type A to Micro B 5 Pin MIni Data Male Cable | |
![]() |
$3.90 Time Remaining: 8h 53m Buy It Now for only: $3.90 Buy It Now |
| 15+7 22 Pin Serial ATA Power Data Cable for SATA HDD | |
![]() |
$4.30 Time Remaining: 8h 57m Buy It Now for only: $4.30 Buy It Now |
| USB 20 Data Charger Cable for Mp3 Mp4 camera new 5 pin | |
![]() |
$4.47 Time Remaining: 9h 8m Buy It Now for only: $4.47 Buy It Now |
| 1pc USB 20 Charger Charging To Micro 5Pin Data Cable For Mp3 Cell Phone Blue | |
![]() |
$0.99 Time Remaining: 9h 12m |
| 7 Pin Serial ATA SATA Female Data Cable w Angle for HDD | |
![]() |
$2.68 Time Remaining: 9h 15m Buy It Now for only: $2.68 Buy It Now |
| 7 Pin HDD Hard Drive HD Serial ATA SATA Raid DATA Cable | |
![]() |
$1.31 Time Remaining: 9h 15m Buy It Now for only: $1.31 Buy It Now |
| Blue Mini USB 5 pin male to Mini usb 5p Male Data charge Cable for GPS Car AUX | |
![]() |
$2.50 Time Remaining: 9h 37m Buy It Now for only: $2.50 Buy It Now |
| Light Pink Micro 5 Pin USB Data Charger Cable Blackberry Cord | |
![]() |
$4.99 Time Remaining: 9h 42m Buy It Now for only: $4.99 Buy It Now |
| New Black USB A Female to Mini USB B 5 Pin Female Data Cable Adapter Converter C | |
![]() |
$0.96 (5 Bids) Time Remaining: 9h 43m |
| 15M USB DATA Charger Cable 40 pin for Asus Eee Pad Transformer TF101 201 TABLET | |
![]() |
$9.99 Time Remaining: 9h 49m Buy It Now for only: $9.99 Buy It Now |
| 30 PIN Dock Extender Extension Data Sync Cable for iPad 2 iPod iPhone 4 4s white | |
![]() |
$5.93 Time Remaining: 10h 4m Buy It Now for only: $5.93 Buy It Now |
| 6pin USB Sync Data Charging Charger Cable Cord for Apple iPhone 4 4S 4G 4th 3ft | |
![]() |
$0.06 (2 Bids) Time Remaining: 10h 18m |
| Black Color Micro 5 Pin USB Data Charger Cable Blackberry Cord | |
![]() |
$4.99 Time Remaining: 10h 24m Buy It Now for only: $4.99 Buy It Now |
| 18 7 4 Pin SATA Power Data Cable for Slim DVD Adapter | |
![]() |
$5.50 Time Remaining: 10h 52m Buy It Now for only: $5.50 Buy It Now |
| SATA Male to Male 7+15 Pin Serial ATA Data Cable | |
![]() |
$7.24 Time Remaining: 11h 21m Buy It Now for only: $7.24 Buy It Now |
| New USB 20 Type A To B Micro 5 Pin Sync Data Charger Flexible Cable | |
![]() |
$2.69 Time Remaining: 12h 9m Buy It Now for only: $2.69 Buy It Now |
| 6+7 Pin SATA to IDE Power SATA Serial ATA Data Cable | |
![]() |
$6.39 Time Remaining: 12h 11m Buy It Now for only: $6.39 Buy It Now |
| 13 PIN DIN DATA CABLE MP3 DISC CHANGER CD SHUTTLE C BUS | |
![]() |
$14.95 Time Remaining: 12h 21m Buy It Now for only: $14.95 Buy It Now |
| 15+7 Pin Male to Female SATA Data Power Cable Cord | |
![]() |
$5.31 Time Remaining: 12h 29m Buy It Now for only: $5.31 Buy It Now |
| 15M USB 20 A Male to Mini B 5 Pin Data Charging Cable Cord | |
![]() |
$5.51 Time Remaining: 13h 2m Buy It Now for only: $5.51 Buy It Now |
| USB A Male to A B Mini 5 Pin Power Data Y Cable for HDD | |
![]() |
$22.99 Time Remaining: 13h 22m Buy It Now for only: $22.99 Buy It Now |
| Blue USB 20 Male to Mini 5 Pin Male MP3 MP4 Data Cable | |
![]() |
$2.80 Time Remaining: 13h 25m Buy It Now for only: $2.80 Buy It Now |
| BRAND NEW USB 20 A Male TO 5 PIN MINI B DATA CABLE FOR MP3 MP4 CAMERA FREE SHIP | |
![]() |
$0.99 Time Remaining: 13h 53m Buy It Now for only: $0.99 Buy It Now |
| 7 Pin SATA Serial ATA to ESATA External Data Cord Cable | |
![]() |
$3.79 Time Remaining: 13h 54m Buy It Now for only: $3.79 Buy It Now |
| New USB A to Mini 5 Pin Data Cable Adapter Male M | |
![]() |
$1.79 Time Remaining: 14h 30m Buy It Now for only: $1.79 Buy It Now |
| Mini SAS to 4 x 7 Pin SATA Serial ATA Data Cable Cord | |
![]() |
$11.72 Time Remaining: 14h 54m Buy It Now for only: $11.72 Buy It Now |
| 1M USB A Type Male to Mini 5Pin Male Data Transfer Cable Adapter | |
![]() |
$5.18 Time Remaining: 15h 8m Buy It Now for only: $5.18 Buy It Now |
| Mini USB 5Pin OTG To USB2O Female Host Sync Data Cable for Tablet PC Smartphone | |
![]() |
$1.34 Time Remaining: 15h 48m Buy It Now for only: $1.34 Buy It Now |
| USB DATA 40pin Charger Cable for Asus Eee Pad Transformer TF101 TF201 TABLET new | |
![]() |
$7.22 Time Remaining: 16h 1m |
| USB Data Sync Charging Cable Cord for iphone 3G 3GS 4 4S ipod Touch 30 pin Dock | |
![]() |
$0.49 Time Remaining: 16h 4m |
| Wholesale 100 USB 20 A to Mini 5 Pin B Male Data Cable | |
![]() |
$89.99 Time Remaining: 17h 31m Buy It Now for only: $89.99 Buy It Now |
| 18M RS232 Female to DB9 Pin DATA Transfer Virtual Serial Camera LCD TV Cable PC | |
![]() |
$143.96 Time Remaining: 17h 37m Buy It Now for only: $143.96 Buy It Now |
| Black Mini 5 Pin USB 20 to Type A Extension 394 Data Cable | |
![]() |
$5.02 Time Remaining: 19h 27m Buy It Now for only: $5.02 Buy It Now |
| 7 Copper Pin Male to Female Series ATA SATA Data Cable | |
![]() |
$4.14 Time Remaining: 20h 29m Buy It Now for only: $4.14 Buy It Now |
| USB Data Cable 2 in 1 Sync 5 Pin for Blackberry HTC | |
![]() |
$2.60 Time Remaining: 20h 48m Buy It Now for only: $2.60 Buy It Now |
| ML591 OKI Oki Data ML 591 24pin 360cps Turbo Parrellel Dot Matrix Printer | |
![]() |
$262.50 Time Remaining: 21h 12m Buy It Now for only: $262.50 Buy It Now |
| LOT100 USB 20 A Male to Mini B 5 Pin Male Data Charging Cable | |
![]() |
$42.00 Time Remaining: 21h 13m Buy It Now for only: $42.00 Buy It Now |
| 4 Pin to 7 + 15 Serial ATA SATA Data IDE Cable Orange | |
![]() |
$5.75 Time Remaining: 21h 24m Buy It Now for only: $5.75 Buy It Now |
| TOP GRADE 6 Pin USB 20 Data Sync Cable Charger For Ipad 1 2 3 iPHONE 4 4S 3GS | |
![]() |
$0.02 Time Remaining: 21h 28m |
| New 50cm 7 Pins SATA Male to SATA Female Extension Data Cable Red | |
![]() |
$2.05 Time Remaining: 21h 44m Buy It Now for only: $2.05 Buy It Now |
| New 50cm 7 Pins SATA Female to SATA Female Extension Data Cable Yellow | |
![]() |
$2.05 Time Remaining: 21h 44m Buy It Now for only: $2.05 Buy It Now |
| For Computer ATA SATA 22Pin Data Power Extension Cable | |
![]() |
$7.15 Time Remaining: 22h 3m Buy It Now for only: $7.15 Buy It Now |
| 9 to 9 pin Firewire IEEE1394 B B 800 800 1 Meter Data Connection Cable | |
![]() |
$6.99 Time Remaining: 22h 14m Buy It Now for only: $6.99 Buy It Now |
| 9 to 6 pin Firewire IEEE1394 A B 800 400 1 Meter Data Connection Cable | |
![]() |
$6.99 Time Remaining: 22h 14m Buy It Now for only: $6.99 Buy It Now |
| Mini USB 5pin male to Mini usb 5pin Male Data charge Cable for GPS Car AUX line | |
![]() |
$3.30 Time Remaining: 22h 24m Buy It Now for only: $3.30 Buy It Now |
I'm creating a power cable for a USB device. Is it ok to leave the data pins floating or do they need ground?
Working on a dev board that has a USB interface, which is where we want to power it from. If we connect only the two power pins up to it, will it be ok if the data pins are floating, or do they need to be tied to ground (or high) to prevent issues? I know this is a problem with TTL chips, didn't know if it would apply in this case. Thanks.
It would be best to ground them, however it is not nessary. Depends on what the usb device is and how sensitive it is.
Marshalling data in Compact Framework
In many situations when we create applications for different embedded systems or mobile platforms we can’t develop all parts of the product using managed code only.
For example we need several modules written in native language which perform some low level operations or we already have these libraries written on C++. So we have to use more that one programming language in our product and also use data marshaling in it.
Making your interop calls more efficient
Marshaling is the act of taking data from one environment to another. In the context of .NET marshalling refers to transferring data from the app-domain you are in to somewhere else, outside.
You should remember that such Platform Invoke calls are slower than direct native calls and than regular managed calls. The speed depends on types marshaled between managed and native code, but nevertheless you should avoid using Platform Invoke calls if you have a chance to do this. Also it is recommended to use calls with some amount of transferred data than several “small” Platform Invoke calls.
Blittable types
It is recommended to use simple data types (int, byte, boolean, characters and strings). It makes the call more efficient and helps to avoid any convertions and copying. These blittable types have identical representation in both managed and unmanaged memory. But you should remember that in Compact Framework during marshaling boolean type is represented as 1-byte integer value (instead of 4-byte integer value in the full .NET Framework), character type (char) is always represented as 2-bytes Unicode character and String type is always treated as a Unicode array (in full .NET Framework it may be treated as a Unicode or ANSI array, or a BSTR).
Method Inlining
The JIT compiler can inline some methods in order to make the calls more efficient. You can not force a method to be inlined by the compiler, but you can make it NOT to be inlined. In order to avoid inlining you can:
- make the method virtual;
- add branching to the method’s body;
- define local variables in the method;
- use 2-bit floating point arguments (or return value).
Disabling method inlining can help to detect a problem during Platform Invoke calls.
Sequential layout
In the Compact Framework all structures and classes always have sequential layout (the managed value type has the same memory layout as the unmanaged structure). This behavior can be specified by setting attribute LayoutKind.Sequential. You don’t need to specify this attribute in Compact Framework, but if you use these pieces of code in both full .NET Framework and Compact Framework you have to set it to avoid different behavior on two platforms.
The following sample shows how to send some pointers from C# code for storing them in the native module.
Code C#:
[StructLayout(LayoutKind.Sequential)]
public class BasePointers // you can use the struct too
{
public IntPtr pointer1;
public IntPtr pointer2;
}
[DllImport("NativeDLL.dll", CallingConvention = CallingConvention.Winapi)]
// Cdecl
public static extern int TransferStruct(BasePointers pointers);
Code C++:
struct BasePointers
{
unsigned int pointer1;
unsigned int pointer2;
}
extern "C" __declspec(dllexport) int CDECL TransferArray(BasePointers*
pointers);
One Calling Convention
The Calling Convention determines the order in which parameters are passed to the function, and who is responsible for the stack cleaning. The .NET Compact Framework supports only the Winapi value (Cdecl on this platform) of Calling Convention. It defines the calling convention for C and C++ (instead of the full .NET Framework which supports three different calling conventions). To avoid crashes of your application you should make sure that your calling conventions in both managed and native declarations are same.
If you specify the attribute to preserve signature of functions ([PreserveSig]) then the returned value will contain 32-bit HRESULT value that will give you more data to analyze errors during the native function execution. The Calling Convention can be specified by adding the attribute CallingConvention to the declaration of your function. As it was mentioned the .NET Compact Framework supports only “Winapi” Calling Convention that corresponds to Cdecl:
Code C#:
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate int ProgressEventHandler(int progressValue);
Code C++:
typedef void (CDECL *ProgressEventHandler)(int progressValue);
Data Alignment
In some situations we need to transfer data between the managed and unmanaged code in the structures. As it’s written above all structures have sequential layout in the Compact Framework, but you should remember about representation of structs in the managed in unmanaged code. The way of packing structures depends on a platform and on a way how the members of structure are aligned. On ARM platform this value for alignment is four (all values in structures are aligned to 4 bytes).
typedef struct OurStruct
{
unsigned char valueChar;
usingned int valueInt;
} ourStruct_;
This structure could be perfectly acceptable in desktop code, but if you use such structure on the Windows Mobile platform then you might receive valueInt at the offset 4. If you use such structures in both desktop and device's side code you have to use them carefully during marshaling.
During marshaling data you might receive such errors as “Datatype misalignment” (0x80000002) or “Access violation” (0x80000005). It indicates that you are using wrong pointers or try to access to the wrong offset of data. For example, you transfer array of bytes from C# code to the native module and define you functions as:
C# code:
[DllImport("NativeDLL.dll", CallingConvention = CallingConvention.Winapi)]
// Cdecl
public static extern int TransferArray(IntPtr src, int srcSize);
C++ Native Module code:
extern "C" __declspec(dllexport) int CDECL TransferArray(byte* srcArr,
int srcSize);
If you try to use the pointer “srcArr” as the pointer on integer (int*) and then try to use the corresponding value you will receive an error :
int value = *(int*)srcArr; // Datatype misalignment
The simple way to avoid this problem is to change declaration of C++ function and change the pointer on array of bytes to the pointer on array of integer and use it without any problems:
extern "C" __declspec(dllexport) int CDECL TransferArray(int* srcArr,
int srcSize);
Marshal class
You can use methods in the class Marshal to manually convert managed objects and perform conversions between IntPtrs. These methods are PtrToStructure, GetComInterfaceForObject, PtrToStringBSTR, GetFunctionPointerForDelegate and others. It allows you to control marshaling. These methods are also useful for debugging issues with marshaling parameters where the runtime is not able to convert a particular argument.
You cannot pass delegate directly to the native module as parameter of you function because the .NET Compact Framework does not support marshaling of delegates. Instead you should use method Marshal.GetFunctionPointerForDelegate for getting function pointer which you can pass to the native code and call it.
Code:
class MainClass
{
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate int ProgressEventHandler(int progressValue);
...
void OnProgressChanged(int progressValue)
{
...
}
…
…
[DllImport("NativeDLL.dll", CallingConvention = CallingConvention.Winapi)]
// Cdecl
public static extern int SetCallbackFunction(IntPtr functionPointer);
}
// Passing function pointer
Delegate d = new ProgressEventHandler(OnProgressChanged);
IntPtr progressChanged = Marshal.GetFunctionPointerForDelegate(d);
int result = SetCallbackFunction(progressChanged);
But you should be aware of Garbage Collector (GC) in such situation. The GC might collect you delegates and your function pointers will become invalid. It may happen when you passed the function pointer to the native code as callback method in order to call it later - GC might think that there are no references to it in the managed code. To avoid this situation you should keep reference to this delegate. For example, you can store it in the classes variable or create some delegates pool, in which you can keep references to the several delegates.
GCHandle
Since we're passing a pointer to some data we need to allocate memory for that data and make sure that the GC will not remove that memory. One of the possible ways to manage this situation is to use GCHandle.
If you want to pass some class (or array of bytes) to the unmanaged code and you need to pin memory for the proper work with it in the unmanaged code you can write:
class SampleClass
{
...
}
SampleClass classSample = new SampleClass();
GCHandle classHandle = GCHandle.Alloc(classSample, GCHandleType.Pinned);
IntPtr ptrToClass = classHandle.AddrOfPinnedObject();
int result = PassPtrToUnmanagedCode(ptrToClass); // our function
You can also make an instance of GCHandle as a member of the class to avoid deleting them by GC. Also you should remember that the structure is value-type. And pinning it to the memory will cause a problem, because structure will be copied and GCHandle will handle a reference to created “boxed” copy of the object. It will be hard to find such problem in the future.
Conclusion
During marshaling data you may face with the problems described above. Very often you may get “NotSupportedException” and other exceptions. To find a problem you can enable logging of setting the registry keys. One of the logging components is “Interop ”. The log provides information about Platform Invoke calls and marshaling. You can read MSDN for more information about Creating Log Files.
With the .NET Compact Framework 2.0 you can use Platform Invoke calls in managed application, even though there are a few limitations. You should remember all differences and limitations between full .NET Framework and the Compact Framework to avoid problems in your applications.
Resources:
See on the Apriorit site
About the Author
Eugene Kordin,
Software Developer of Device Team
Apriorit Inc.
GWAVA Retain makes BlackBerry PIN, SMS, Messenger text messages and Phone Data ready for Discovery.





























































































