Author: Apriorit (Device Team)
Permanent link: apriorit(dot)com/our-company/dev-blog/62-marshalling-data-in-cf
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 than one programming language in our product and also use data marshaling in it.
In this article we will review some aspects of using data types and ways of using them during marshalling data to and from unmanaged code.
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.
Bitable 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#:
Code:
[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++:
Code:
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#:
Code:
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
public delegate int ProgressEventHandler(int progressValue);
Code C++:
Code:
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).
Code:
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:
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 :
Code:
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:
Code:
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:
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:
Code:
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.
Related
Hello friends,
First off, please check below code. Well, I am developing printing functionality from the windows mobile device using vc++. (I am novice to the vc++ ). For printing, we are using third party DLLs. Below are important snippet required to explain the complete picture. Currently, the problem I am more concerning is printing multiple pages. For this, we have API and everything. While printing using “mpPreviewDialog” API (provided by third party in form of DLL) and when it recognize more than one pages required, it raises/passes “MP_EVENT_REQ_NEW_PAGE” message from printing library, ie mpPreviewDialog itself, to the handle hWnd of “mpPreviewDialog(hWnd,__)”. Now, to handle this message, we have put all logic portion in one class which is inherited from CWnd so that we can override “DefWindowProc” function to achieve our goal. But while I run the application and command print for multiple page, it only prints the first page perfectly and then just get hanged. Thus, it fails to handle the “MP_EVENT_REQ_NEW_PAGE” message of “DefWindowProc” function. What all I need is to handle this message and execute the case MP_EVENT_REQ_NEW_PAGE: while printing and multiple page required. Because in this case it issues the message but appropriate case never get executed. I hope you got my point. Please let me know how to solve this. Am I missing something? Thank you very much for your time in advance.
class CFxPrinterWnd : public CWnd
{
DECLARE_DYNCREATE(CFxPrinterWnd)
public:
CFxPrinterWnd(); // protected constructor used by dynamic creation
virtual ~CFxPrinterWnd();
public:
#ifdef _DEBUG
virtual void AssertValid() const;
#ifndef _WIN32_WCE
virtual void Dump(CDumpContext& dc) const;
#endif
#endif
protected:
DECLARE_MESSAGE_MAP()
public:
int PrintReport(HWND hwnd, int flg);
protected:
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam);
};
Int CFxPrinterWnd:rintReport (HWND hWnd, int flg)
{
_______
This contains “mpPreviewDialog” API call which starts printing and issue “MP_EVENT_REQ_NEW_PAGE” message if more than one page required.
}
LRESULT CFxPrinterWnd:efWindowProc(___,___,___)
{
Switch (message)
{
___
Case MP_EVENT_REQ_NEW_PAGE: //This debug point never get focused while printing and new page required.
___This point is never get executed
___
}
}
Button1 Click Event
{
hWnd = ::FindWindow(NULL, _T(“del”));
CFxPrinterWnd wnd;
Wnd.Create(_______________________);
Ret = wnd.PrintReport(hWnd, 0);
___
___
mpPreviewDialog (hWnd____); //This is the printing API where MP_EVENT_REQ_NEW_PAGE message get raised while printing if new page
//required.
wnd.DestroyWindow();
}
---
Kind Regards,
Sachin Patel
Hi,
I try to install a Keyboard Hook to get volume key presses in .NET 3.5.
But it fails. Does someone know how to do it?
This is my VB .NET code:
Code:
Imports System.Runtime.InteropServices
Public Delegate Function HookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As intptr) As Integer
Public Class Form1
<MarshalAs(UnmanagedType.FunctionPtr)> _
Private callbackKeyboard As HookCallback
Private Shared hHookKeyboard As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If hHookKeyboard.Equals(0) Then
callbackKeyboard = New HookCallback(AddressOf KeyboardHookProc)
hHookKeyboard = WinApi.SetWindowsHookEx(WinApi.WH_KEYBOARD_LL, callbackKeyboard, WinApi.GetModuleHandle(Nothing), 0)
If hHookKeyboard.Equals(0) Then
MessageBox.Show("Keyboard Hook Failed:" & vbCrLf & hHookKeyboard.ToString() & vbCrLf & Marshal.GetLastWin32Error(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
End If
End If
End Sub
End Class
'API Functions
Public Class WinApi
Public Const WH_KEYBOARD_LL As Integer = 13
Public Structure KBDLLHOOKSTRUCT
Public vkCode As Integer
Public scanCode As Integer
Public flags As Integer
Public time As Integer
Public dwExtraInfo As Integer
End Structure
<DllImport("coredll.dll")> _
Public Shared Function SetWindowsHookEx( _
ByVal idHook As Integer, ByVal HookProc As HookCallback, _
ByVal hInstance As IntPtr, ByVal wParam As Integer) As Integer
End Function
<DllImport("coredll.dll")> _
Public Shared Function GetModuleHandle(ByVal mod2 As String) As IntPtr
End Function
<DllImport("coredll.dll")> _
Public Shared Function CallNextHookEx( _
ByVal idHook As Integer, ByVal nCode As Integer, _
ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
End Function
End Class
glad to claim not vb knowlege at all but I can advice you to search
for c# solutions as everything which can be don in c# can be don as
far as I know in vb.net even if the syntes is horrid
thing is most people who you want to get advice from would never
use vb but if you read their stuff you can with a bit of skill convert it
from c# to vb.net even if it makes baby jesus cry
Well, this Code is inspired by C# code from here.
I know that most .NET developers use C# and not VB so I searched for C# code too. I've already used keyboard hooks in desktop apps but this does not work for .NET CF.
Very strange, I tried the same code in C# and it worked!
So I'll write my app in C#
Whatever language you have written the code in should not make any difference. Take the C# generated .EXE file or .DLL that works, and load it into RED GATE's .NET Reflector.
http://www.red-gate.com/products/reflector/
A freeware download, just agree to the disclaimer etc.
Load your program in it and navigate down until the code appears in the main window. It will show you the C# code that was used to create it. It won't know the variable names but it will replace them with int1,int2,string1,float1 etc.
Here's the clever bit, use the drop down box to switch the language to VB, and it will display the VB code to do the same job. You should be able to spot the difference in the code compared to your own VB original.
I'm stuck. I am building a custom Settings.apk for a new tablet and I've run across an error I can't find.
Any dialog that displays a SeekBar or variation simply FC's with a null pointer exception. I have a reference source tree that is a virgin copy from the android git. If I compile the stock Settings.apk, it does the same thing at the same places as my custom version.
The error occurs in the BrightnessPreference.java and the RingerVolumePreference.java files any time the dialog containing a SeekBar or Volumizer tries to display.
Here's an example of the code:
protected void onBindDialogView(View view) {
super.onBindDialogView(view);
mSeekBar = getSeekBar(view);
mSeekBar.setMax(MAXIMUM_BACKLIGHT - mMinBrightness); <-- errors here
It errors at setMax with a null pointer error, like the setMax method doesn't exist. My build environment seems fine. Everything I've built runs on the target devices, but this one has me stumped. Has anyone else tried building a Settings.apk lately, and does it display the volume (in sound) and brightness (in Display) dialogs?
BTW, the target is Froyo
Thanks,
--- Jem
Are you sure the mMinBrightness variable is defined? BrightnessPreference.java as shown here instead uses MINIMUM_BACKLIGHT and is defined as:
Code:
private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;
It is then used to set the seekbar max:
Code:
mSeekBar.setMax(MAXIMUM_BACKLIGHT - MINIMUM_BACKLIGHT);
You can also look at the onBindDialogView in RingerVolumePreference.java here. If you want to check whether the java files change from the version I linked(2.2.1 Froyo), just use the arrows on either side of the currently listed version.
I appreciate the response. Yes it is defined and initialized earlier as referenced below.
=== Code ===
private int mMinBrightness;
private boolean mAutomaticAvailable;
// Backlight range is from 0 - 255. Need to make sure that user
// doesn't set the backlight to 0 and get stuck
private static final int MINIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_DIM + 10;
private static final int MAXIMUM_BACKLIGHT = android.os.Power.BRIGHTNESS_ON;
public BrightnessPreference(Context context, AttributeSet attrs) {
super(context, attrs);
mAutomaticAvailable = context.getResources().getBoolean(
com.android.internal.R.bool.config_automatic_brightness_available);
setDialogLayoutResource(R.layout.preference_dialog_brightness);
setDialogIcon(R.drawable.ic_settings_display);
mMinBrightness = MINIMUM_BACKLIGHT;
if (Settings.System.getInt(context.getContentResolver(),
Settings.System.LIGHT_SENSOR_CUSTOM, 0) != 0) {
mMinBrightness = Settings.System.getInt(context.getContentResolver(),
Settings.System.LIGHT_SCREEN_DIM, mMinBrightness);
}
}
=== code ===
Thanks,
--- Jem
It might be useful to rule out an issue with mMinBrightness not getting set or getting set incorrectly by either replacing it with MINIMUM_BACKLIGHT in the setMax line or using a toast message to display the value of mMinBrightness before setMax. If that doesn't reveal anything, could you paste the relevant logcat output for the error/fc and also upload your BrightnessPreference.java file somewhere so it could be downloaded.
I saved the dir's with my modified package and blew away the entire repo structure. I then reloaded the official Android tree from kernel.org. I have compiled the Settings.apk app ALL STOCK, and I get the same errors in the brightness and volume areas. A copy of the brightnesspreference.java and a log screenshot of the error tracking can be found at:
mlsoft dot sytes dot net/brightness.zip (I can't post the direct link)
The only change in the error is the line number (same call though).
At this point, I am wondering if anyone can build the Settings.apk from the repo and get it to display a seekbar or variant in brightness or volume settings using 2.2.1 (froyo). I am comfortable at this point thinking that it is in the tree structure and not my build environment. At least I have a place to hunt if you can reproduce the error on your end.
I really appreciate your help.
--- Jem
android:id="@*android
Yeah, same thing happened to me and I found answer.
I hope this might help.
You will find that two xml files using "@*android".
"It gives access to internal resources for platform apps.
It is NOT safe to build apps with such declarations unless you are building a bundled app within a full system image."
- from stackoverflow
"What is the purpose of the star in the ID string?"
Sorry that I can not post URL. I'm new user.
Sorry about not yet having built and tested the Settings.apk on my end but I haven't found the time to do so yet. Your issue could very well be what You Kim posted about and I'm sure I wouldn't have noticed that for awhile if at all. Here's the link to the stack overflow topic You Kim referenced. The two xml files in the Settings package this occurs in are res/layout/preference_dialog_brightness.xml and res/layout/preference_dialog_ringervolume.xml. That part looks like this:
Code:
<!-- In brightness layout -->
<[COLOR="Red"]SeekBar android:id="@*android:id/seekbar"[/COLOR]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dip" />
<!-- In ringervolume layout -->
<!-- Used for the ring volume. This is what the superclass VolumePreference uses. -->
<[COLOR="red"]SeekBar android:id="@*android:id/seekbar"[/COLOR]
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingLeft="20dip"
android:paddingRight="20dip" />
Thank you both. I have been through those files more than once and didn't notice the declarations. Outstanding work guys. I'm off to edit/recompile/test.
Thanks again,
--- Jem
Unfortunately, these changes didn't stop the null pounter errors. Something, somewhere HAS to be uninitialized, but damned if I can find it in the absseekbar, progressbar, or any other class in the seekbar hierarchy. I even tried setting a value of 255 in setMax to rule out variable problems, but that produces the same error at the same line.
I know there is a fix, Settings works on millions of Android devices, so it has to be something stupid I'm overlooking.
--- Jem
Well, it's almost 3am, but I fixed the problem in the brightness preference. SeekBarPreference handles the getSeekBar function, but it doesn't work as expected.
It assigns a static system res ID that obviously doesn't work as intended. So, I overrode the routine just above the call to onBindDialogView. Here is the routine in the seekbarpreference file:
(at sign)Override
protected static SeekBar getSeekBar(View dialogView) {
return (SeekBar) dialogView.findViewById(com.android.internal.R.id.seekbar);
}
Overridden in the brightnes preference file to this:
(at sign)Override
protected static SeekBar getSeekBar(View dialogView) {
return (SeekBar) dialogView.findViewById(R.id.seekbar);
}
I'll go back and write the original to accept a res ID as an argument later. For now at least the brightness control works. Tomorrow I'll go after the volume controls.
--- Jem
I have created a Database class file to use SQLite db in Android app. Since I am beginner and I am having problem understanding what should i put in context when I instantiate Database class to another class The database code is as follows:
public Database(@nullable Context context) {
super(context,DATABASE_NAME,null,DATABASE_VERSION);
database=getWritableDatabase();
How can I instantiate it to an another class. When doing instantiation Database db = new Database(); what should be put under context
Database db =new Database(Combiner.this);. as well as Database db= new Database(this);I tried this but its showing Database(android.content,Context) in Database cannot be applied to (com.example.data.Combiner)
Are you sure that you require a context parameter in the construct function?
If it is unnecessary, just let it be null as it is nullable.
The command line client for Stagefright
https://android.googlesource.com/platform/frameworks/av/+/master/cmds/stagefright/stagefright.cpp
Is using MediaSource to play the Video.
How can I set MediaSource to be from byte array (char *), where I can specify it. I want to load the whole file from memory from array (very small video, not need for file streaming)
https://android.googlesource.com/pl.../master/cmds/stagefright/stagefright.cpp#1234
I think MediaSource is an Interface, it has read() methods etc, but nothing to consturct MediaSource from bytearray, char array (char *)
How can I construct proper MediaSource? So that I can just make
playSource(mediaSource);