GSM Shop GSM Shop
GSM-Forum  

Welcome to the GSM-Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features.
Only registered members may post questions, contact other members or search our database of over 8 million posts.

Registration is fast, simple and absolutely free so please - Click to REGISTER!

If you have any problems with the registration process or your account login, please contact contact us .

Go Back   GSM-Forum > Other Gsm/Mobile Related Forums > GSM Programming & Reverse Engineering


GSM Programming & Reverse Engineering Here you can post all Kind of GSM Programming and Reverse Engineering tools and Secrets.

Reply
 
LinkBack Thread Tools Display Modes
Old 11-23-2010, 11:51   #1 (permalink)
No Life Poster
 
angel25dz's Avatar
 
Join Date: Jul 2006
Location: ..::DZ-25::..
Posts: 529
Member: 315181
Status: Offline
Sonork: 100.1593455
Thanks Meter: 301
LG Sgold USB comminication in c#


many ppls asked me about usb communication with LG sgold phones using widows api's :-)

here my source in c#

When phone is connected without battery, it will be in Flash interface for a short time ==> phone is connected with path : \\.\USB1

1- Import windows dll's SETUPAPI.dll and KERNEL32.dll

Code:
 //*********************** API **********************
  
   public const uint DIGCF_PRESENT = 0x00000002;
   public const uint DIGCF_INTERFACE_DEVICE = 0x00000010;
   public const int FILE_FLAG_OVERLAPPED = 0x40000000;
   public static IntPtr INVALID_HANDLE = new IntPtr(-1);
   SafeFileHandle deviceHandle;
   int numberOfBytesWritten = 0; int numberOfBytesRead = 0;
   IntPtr unManagedBuffer;
   IntPtr unManagedOverlapped;
   Boolean success;
   //______________setupapi_______________
          [DllImport("setupapi.dll", SetLastError = true)]
   public static extern IntPtr SetupDiGetClassDevs(ref Guid ClassGuid,
   uint Enumerator, IntPtr hwndParent, uint Flags);
  
          [DllImport("setupapi.dll", SetLastError = true)]
   public static extern bool SetupDiEnumDeviceInterfaces(IntPtr DeviceInfoList,
   uint DeviceInfoData, ref Guid InterfaceClassGuid, uint MemberIndex,
   SP_DEVICE_INTERFACE_DATA DevInterfaceData);
  
          [DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Ansi)]
   public static extern bool SetupDiGetDeviceInterfaceDetail
              (IntPtr DeviceInfoSet, SP_DEVICE_INTERFACE_DATA DevInterfaceData,
   byte[] DetailData, int DataSize, ref int RequiredSize,
   SP_DEVINFO_DATA DeviceInfoData);
  
          [DllImport("setupapi.dll", SetLastError = true)]
   public static extern bool SetupDiDestroyDeviceInfoList
              (IntPtr DeviceInfoSet);
  
   //______________endsetupapi____________
   //______________kernel32_____________
  
          [DllImport("Kernel32.dll")]
   static extern SafeFileHandle CreateFile(string filename,
          [MarshalAs(UnmanagedType.U4)]FileAccess fileaccess,
          [MarshalAs(UnmanagedType.U4)]FileShare fileshare,
   int securityattributes,
          [MarshalAs(UnmanagedType.U4)]FileMode creationdisposition,
   int flags, IntPtr template);
  
          [DllImport("kernel32.dll", SetLastError = true)]
   public static extern bool CloseHandle(IntPtr hObject);
  
          [DllImport("kernel32.dll", SetLastError = true)]
   public static extern bool DeviceIoControl(
   IntPtr hDevice, uint IoControlCode,
   byte[] InBuffer, int InBufSize,
   byte[] OutBuffer, int OutBufSize,
   ref int BytesReturned, OVERLAPPED OverLapped);
  
          [DllImport("kernel32.dll", SetLastError = true)]
   public static extern bool WriteFile(SafeFileHandle hFile, byte[] lpBuffer,
   Int32 nNumberOfBytesToWrite, ref Int32 lpNumberOfBytesWritten,
   IntPtr lpOverlapped);
  
          [DllImport("kernel32.dll", SetLastError = true)]
   public static extern bool ReadFile(SafeFileHandle hFile, IntPtr lpBuffer,
   Int32 nNumberOfBytesToWrite, ref Int32 lpNumberOfBytesWritten,
  IntPtr lpOverlapped);       
   //______________endkernel32__________
2- some needed functions (not all needed for LG)

Code:
 //___________________________________
          [StructLayout(LayoutKind.Sequential, Pack = 1)]
   public class SP_DEVINFO_DATA
          {
   public int cbSize;
   public Guid ClassGuid;
   public uint DevInst;
   public uint Reserved;
          }
  
  
          [StructLayout(LayoutKind.Sequential, Pack = 1)]
   public class SP_DEVICE_INTERFACE_DATA
          {
   public int cbSize;
   public Guid InterfaceClassGuid;
   public uint Flags;
   public uint Reserved;
          }
  
          [StructLayout(LayoutKind.Sequential, Pack = 1)]
   public class OVERLAPPED
          {
   public uint Internal;
   public uint InternalHigh;
   public uint Offset;
   public uint OffsetHigh;
  public IntPtr hEvent;
          }
  //___________________________________
3 - Read/write HEX data (managed buffer) :

Code:
 public string read_write_hex1(SafeFileHandle devicehandle, byte[] command, int commandlenght, byte[] inputReportBuffer)
          {
  
              unManagedBuffer = Marshal.AllocHGlobal(inputReportBuffer.Length);
              numberOfBytesRead = 0;
              numberOfBytesWritten = 0;
  
              success = WriteFile(devicehandle, command, commandlenght, ref numberOfBytesWritten,
                  unManagedOverlapped);
   Thread.Sleep(5);
  
              success = ReadFile(devicehandle, unManagedBuffer, inputReportBuffer.Length,
   ref numberOfBytesRead, IntPtr.Zero);
  
   Marshal.Copy(unManagedBuffer, inputReportBuffer, 0, numberOfBytesRead);
   byte[] tempReDim = new byte[inputReportBuffer.Length];
   if (inputReportBuffer != null)
              {
                  System.Array.Copy(inputReportBuffer, tempReDim,
                      System.Math.Min(inputReportBuffer.Length, tempReDim.Length));
                  inputReportBuffer = tempReDim;
              }
  
   string hex = "";
   string h = "";
   foreach (byte inputReportBuffer_element in inputReportBuffer)
              {
                  h = inputReportBuffer_element.ToString("X");
   if (h.Length < 2)
                  {
                      hex = hex + "0" + h;
                  }
   else
                  {
                      hex = hex + h;
                  }
              }
   return hex;
  }
  
   //_____________end readwrite_hex1________
4- Read/Write string data :

Code:
  public string read_write_str(SafeFileHandle devicehandle, byte[] command, int commandlenght, byte[] inputReportBuffer)
          {
              unManagedBuffer = Marshal.AllocHGlobal(inputReportBuffer.Length);
              numberOfBytesRead = 0;
              numberOfBytesWritten = 0;
  
              success = WriteFile(devicehandle, command, commandlenght, ref numberOfBytesWritten,
   IntPtr.Zero);
   Thread.Sleep(5);
  
              success = ReadFile(devicehandle, unManagedBuffer, inputReportBuffer.Length,
   ref numberOfBytesRead, unManagedOverlapped);
  
   Marshal.Copy(unManagedBuffer, inputReportBuffer, 0, numberOfBytesRead);
   byte[] tempReDim = new byte[inputReportBuffer.Length];
   if (inputReportBuffer != null)
              {
                  System.Array.Copy(inputReportBuffer, tempReDim,
                      System.Math.Min(inputReportBuffer.Length, tempReDim.Length));
                  inputReportBuffer = tempReDim;
              }
   string rest = "";
   string hex = "";
   int n = 0;
   foreach (byte inputReportBuffer_element in inputReportBuffer)
              {
   if (n > commandlenght - 1)
                  {
                      hex = hex + inputReportBuffer_element.ToString("X");
   if (inputReportBuffer_element <= 0x20)
                      {
                          rest = rest + " ";
                      }
   else
                      {
                          rest = rest + Convert.ToChar(inputReportBuffer_element);
  }
                  }
                  n += 1;
              }
   return rest.Trim();
          }
  
   //_____________end readwrite_str________
5 - use of thoese functions :

as I said the phone is connected with path \\.\USB1, we need to open this path with CreateFile function :
part of my code :
Code:
string devpath = "\\\\.\\USB1";
    int i = 0; progressBar1.Visible = true;
                       progressBar1.Value = 0; progressBar1.Maximum = 100;
    while (i <= 2000000) //waiting for phone connexion

                       {
                           deviceHandle = CreateFile(devpath, FileAccess.ReadWrite,
    FileShare.ReadWrite, 0, FileMode.Open, 128, IntPtr.Zero);
    if (deviceHandle.IsInvalid == false)
                           {
    break;
                           }
                           progressBar1.Value = i / 20000;
                           i = i + 1;
   }
read/write

Code:
byte[] iRB = new byte[20];
SR = read_write_hex1(deviceHandle, pr2, pr2.Length, iRB);
pr2: our hex data which we want to send to phone;
phone response is readed as hex and returned in SR;
iRB: our buffer;

PS: we can use separated functions to read/write (one for read and one for write), it's better I think, in my soft I use a separated functions

for more infos about windows api's : pinvoke.net: the interop wiki!

./wbr
  Reply With Quote
The Following 4 Users Say Thank You to angel25dz For This Useful Post:
Show/Hide list of the thanked
Old 11-27-2010, 11:41   #2 (permalink)
Freak Poster
 
Join Date: Oct 2009
Posts: 283
Member: 1144205
Status: Offline
Thanks Meter: 75
thanks good work mate and good link
  Reply With Quote
Old 11-28-2010, 07:58   #3 (permalink)
Banned
 
Join Date: Dec 2006
Location: ALGERIE ORAN
Age: 50
Posts: 428
Member: 415170
Status: Offline
Thanks Meter: 1,042
good work AnGeL25dZ..............
  Reply With Quote
Old 08-29-2011, 13:21   #4 (permalink)
No Life Poster
 
[Shadab_M]'s Avatar
 
Join Date: Mar 2006
Location: .: India :. Heaven on Earth
Posts: 2,496
Member: 238812
Status: Offline
Sonork: 100.1602669
Thanks Meter: 1,443
So I can use function posted for BB5 by frensis but only change GUID to \\\\.\\USB1 ?

I am out of my place so cant check anything.

Br,
Shadab Ahmad
  Reply With Quote
Old 08-29-2011, 22:04   #5 (permalink)
No Life Poster
 
angel25dz's Avatar
 
Join Date: Jul 2006
Location: ..::DZ-25::..
Posts: 529
Member: 315181
Status: Offline
Sonork: 100.1593455
Thanks Meter: 301
Quote:
Originally Posted by shadab_a4u View Post
So I can use function posted for BB5 by frensis but only change GUID to \\\\.\\USB1 ?

I am out of my place so cant check anything.

Br,
Shadab Ahmad
not need GUID in this case, just use connection using device path \\.\USB1
./br
  Reply With Quote
The Following User Says Thank You to angel25dz For This Useful Post:
Reply

Bookmarks


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add a language in 51xx/61xx tati Nokia Legacy Phones ( DCT-1 ,2 ,3 ,L ) 8 05-21-2013 19:20
imei in 6110 PIOTRS Nokia Legacy Phones ( DCT-1 ,2 ,3 ,L ) 0 11-16-1999 18:01
blue Display in Nokia Cellphones TheDon Nokia Hardware & Hardware Repair 0 10-29-1999 18:28
New menu item in 5110 GeoMan Nokia Legacy Phones ( DCT-1 ,2 ,3 ,L ) 3 09-23-1999 20:24
Lock 2 in nokia 51xx/61xx Pablo Nokia Legacy Phones ( DCT-1 ,2 ,3 ,L ) 1 07-31-1999 04:35

 



All times are GMT +1. The time now is 09:35.



Powered by Searchlight © 2024 Axivo Inc.
vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
- GSM Hosting Ltd. - 1999-2023 -
Page generated in 0.33981 seconds with 9 queries

SEO by vBSEO