Showing posts with label copy. Show all posts
Showing posts with label copy. Show all posts

Saturday, April 8, 2017

How to copy ROM zip file to the freshly wiped device

How to copy ROM zip file to the freshly wiped device


Consider the following code:

using System;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Text;

namespace SmartDeviceProject5
{
class Program
{
static void Main(string[] args)
{
Foo foo = new Foo();
try
{
SomeMethod(foo); // KABOOM! NotSupportedException
}
catch (Exception e)
{
}
try
{
AnotherMethod("hello"); // KABOOM! NotSupportedException
}
catch (Exception e)
{
}
}

[DllImport("SomeDLL")]
extern static void SomeMethod(Foo foo);

[DllImport("SomeDLL")]
extern static void AnotherMethod([MarshalAs(UnmanagedType.LPStr)] string someString);
}

public struct Foo
{
public string Bar;
public string Borked;
}

}

When attempting to run this program, it will throw an exception upon calling the SomeMethod PInvoke. Attempting to call the "AnotherMethod" function will fail just as well.
This happens because C# automatically marshals all strings in structures as LPTStr (which for some reason is LPStr in Windows Mobile). However, in methods, strings are marshalled as LPWStr. I dont get it. Anyways, .Net Compact Framework does not support marshalling as LPStr in structures or method calls for whatever reason, even though the code to make it happen is all there (as I will show you).
Changing the code by adding the MarshalAs attribute to explicitly Marshal them as LPWStr would make it work:



 [DllImport("SomeDLL")]
extern static void AnotherMethod([MarshalAs(UnmanagedType.LPWStr)] string someString);
}

public struct Foo
{
[MarshalAs(UnmanagedType.LPWStr)]
public string Bar;
[MarshalAs(UnmanagedType.LPWStr)]
public string Borked;
}


Although LPWStr (Unicode) is more or less the standard now, but if you really really want/need to Marshal it as a LPStr, tough cookies, you get a NotSupportedException.
Generally, if you need to marshal a structure with strings, its highly unlikely that you are going to need to mix LPWStr and LPStr in the same structure. To that end, I wrote a custom MarshalAnsi class that marshals a structure and all its contents; any strings found are marshalled as LPStr:


using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; using System.Reflection; namespace System.Runtime.InteropServices { /// <summary> /// .NET Compact framework does not support marshalling strings to ascii. /// Need to do it manually. /// </summary> static class MarshalAnsi { /// <summary> /// This Dictionary maintains all the strings allocated by an IntPtr that a structure /// was Marshalled to. /// </summary> static Dictionary<IntPtr, List<IntPtr>> myStringsForObject = new Dictionary<IntPtr,List<IntPtr>>(); public static IntPtr StructureToPtr(object structure) { Type type = structure.GetType(); var fieldInfos = type.GetFields(BindingFlags.Instance | BindingFlags.GetField | BindingFlags.Public | BindingFlags.NonPublic); // determine the total size of the structure. Need to special case strings and bools int totalSize = 0; foreach (FieldInfo field in fieldInfos) { totalSize += field.FieldType == typeof(string) ? Marshal.SizeOf(typeof(IntPtr)) : field.FieldType == typeof(bool) ? Marshal.SizeOf(typeof(int)) : Marshal.SizeOf(field.FieldType); } // allocate the pointer, and create its list of allocated strings IntPtr ret = Marshal.AllocHGlobal(totalSize); List<IntPtr> strings = new List<IntPtr>(); myStringsForObject.Add(ret, strings); // structure pointer offset, which is incremented as we write to the structure int ofs = 0; foreach (FieldInfo field in fieldInfos) { object toWrite = null; if (field.FieldType == typeof(string)) { // allocate memory for the string if need be, and add it to the // pointers string allocation list string str = field.GetValue(structure) as string; IntPtr strPtr; if (str == null) strPtr = IntPtr.Zero; else { byte[] bytes = Encoding.ASCII.GetBytes(str); strPtr = Marshal.AllocHGlobal(bytes.Length + 2); strings.Add(strPtr); Marshal.Copy(bytes, 0, strPtr, bytes.Length); Marshal.WriteInt16(strPtr, bytes.Length, 0); } toWrite = strPtr; } else if (field.FieldType == typeof(bool)) { // need to write this as an int, not a bool. // BOOL in C/C++ is really an int, which is of size 4. toWrite = (bool)field.GetValue(structure) ? 1 : 0; } else { // just do the default behavior toWrite = field.GetValue(structure); } Marshal.StructureToPtr(toWrite, (IntPtr)((int)ret + ofs), false); // increment the structure pointer offset ofs += Marshal.SizeOf(toWrite); } return ret; } /// <summary> /// Destroy the memory allocated by a structure, and all strings as well. /// </summary> /// <param name="ptr"></param> public static void DestroyStructure(IntPtr ptr) { List<IntPtr> strings = myStringsForObject[ptr]; myStringsForObject.Remove(ptr); foreach (IntPtr strPtr in strings) { Marshal.FreeHGlobal(strPtr); } Marshal.FreeHGlobal(ptr); } } } 

Usage:
Simply pass a structure into this class and it will return a pointer with the marshalled data. All strings are marshalled as Ansi strings.
Remember to call MarshalAnsi.DestroyStructure with the pointer returned from MarshalAnsi.StructureToPtr when it is no longer in use.


Go to link for download

Read more »

Friday, March 24, 2017

SORRY SHIVAM ALWAR u are not able to be Author Coz of your Copy Paste Work

SORRY SHIVAM ALWAR u are not able to be Author Coz of your Copy Paste Work


In Android device, custom recovery has to perform lots of tasks such as flash custom ROM, flash the mod zip file, flash Super Su and much more. Without the help of custom recovery, you cannot do such types of task on your android smartphone. All android phones and tablets come with preinstalled stock recovery but this stock recovery have only basic functions and it cannot flash any type of ROMs on your phone. To perform such type of advanced level actions, we need a custom recovery. There are many custom recoveries available such as Clockwork Mod recovery, Philz, Safestrap, TWRP, CWM, etc. One of the powerful recoveries is the Team wins TWRP recovery. It has a simple user interface and easy to use functions.
twrp image showing alt text

How to get TWRP for your android?
No matter what smartphone you have, you can easily get TWRP recovery for your device. This is the universal method of installing TWRP on any android phone or tablet. You dont need to have the knowledge of coding, any person having the basic knowledge of smartphone can proceed with this method. Recently TWRP team have released their official app through which you can easily install a custom recovery on your android devices.

Disclaimer: Perform this action at your own risk.  It is your responsibility to make sure that each step is performed carefully.

Requirements:
  1. Your device must be rooted. It is mandatory to root your device, as it gives root access to the app which helps to flash the TWRP on android device.
  2. Your device must have unlocked the bootloader. Almost every device comes with the locked bootloader. It is not possible to provide a tutorial to unlock the bootloader of every android so you can take help of google and unlock it.
If you have the above-mentioned prerequisites then you are good to go.

Steps to Flash TWRP:
  • Open up the play store and download an app called Official TWRP and install it.
  • Once you are done with the installation process, open the app and provide root access if it asks for.
  • Click on TWRP flash option and select device. There are tons of devices which are officially supported, you can easily find your build from the list. Type the name of your smartphone at the top, select your device and it shows the list of TWRP images, grab the latest version and download it in your internal storage.
flash twrp image showing alt text




  • Once the download is completed, go back to the app and select a file to flash then select the image file and tap on flash. It starts installing TWRP in a fraction of a second.
  • Now, to confirm if TWRP is installed or not, switch your phone to recovery mode. To switch into recovery mode, different devices have different key combination, find the key combination of your device from google. 
  • This is the easiest method to get TWRP on your android phone or tablet.
I have made this tutorial as easy as possible.  If you still face any issues then ping me on any social media platform.

Go to link for download

Read more »