/// <summary> /// FetchAndDisplayError - Display error in status bar /// </summary> public void FetchAndDisplayError() { strBuffer = string.Empty; // Is this a RAPI error? int err = Rapi.CeRapiGetError(); if (err != Rapi.S_OK) { strBuffer = "RAPI Error (0x" + ((int)err).ToString("x") + ")"; } else { // Check for CE error. err = Rapi.CeGetLastError(); if (err != (int)Rapi.RAPI_ERROR.ERROR_FILE_NOT_FOUND) { strBuffer = "CE Error (code = " + err.ToString("x") + ")"; } } if (strBuffer != string.Empty) { itReason = INVOKE_FINDFILES.STATUS_MESSAGE; m_ctlInvokeTarget.Invoke(m_deleCallback); } // Trigger that thread has ended. m_thrd = null; }
/// <summary> /// ThreadMainStartup - Start RAPI connection. /// </summary> private void ThreadMainStartup() { // Allocate structure for call to CeRapiInitEx Rapi.RAPIINIT ri = new Rapi.RAPIINIT(); ri.cbSize = Marshal.SizeOf(ri); // Call init function int hr = Rapi.CeRapiInitEx(ref ri); // Wrap event handle in corresponding .NET object ManualResetEvent mrev = new ManualResetEvent(false); mrev.Handle = ri.heRapiInit; // Wait five seconds, then fail. if (mrev.WaitOne(5000, false) && ri.hrRapiInit == Rapi.S_OK) { // Notify caller that connection established. itReason = INVOKE_STARTUP.STARTUP_SUCCESS; m_ctlInvokeTarget.Invoke(m_deleCallback); } else { // On failure, disconnect from RAPI. Rapi.CeRapiUninit(); strBuffer = "Timeout - no device present."; itReason = INVOKE_STARTUP.STATUS_MESSAGE; m_ctlInvokeTarget.Invoke(m_deleCallback); // Notify caller that connection failed. itReason = INVOKE_STARTUP.STARTUP_FAILED; m_ctlInvokeTarget.Invoke(m_deleCallback); } }
private void cmdDisconnect_Click(object sender, System.EventArgs e) { // Disconnect from RAPI. Rapi.CeRapiUninit(); EnableButtons(false); m_bRapiConnected = false; }
private void ThreadMainEnumDB() { if (m_bVolume) // Enumerate volumes. { Guid guid = new Guid("ffffffffffffffffffffffffffffffff"); int cch = 32; string str = new String('\0', cch); while (Rapi.CeEnumDBVolumes(ref guid, str, cch) == Rapi.RAPI_TRUE && m_bContinue) { strBuffer = str; itReason = INVOKE_ENUMDB.ENUMDB_NEWVOLUME; m_ctlInvokeTarget.Invoke(m_deleCallback); } } else // Enumerate databases. { short cRecords = 0; IntPtr pfdbAll = IntPtr.Zero; Rapi.CeFindAllDatabases(0, Rapi.FAD.FAD_NAME | Rapi.FAD.FAD_NUM_RECORDS, ref cRecords, ref pfdbAll); IntPtr pfdb = pfdbAll; while (cRecords > 0) { // Set pointer to next record. Rapi.CEDB_FIND_DATA dbfd = (Rapi.CEDB_FIND_DATA) Marshal.PtrToStructure( pfdb, typeof(Rapi.CEDB_FIND_DATA)); // Post name to listbox. strBuffer = dbfd.DbInfo.szDbaseName + " (" + dbfd.DbInfo.wNumRecords + " records)"; itReason = INVOKE_ENUMDB.ENUMDB_NEWDATABASE; m_ctlInvokeTarget.Invoke(m_deleCallback); // Get ready for next loop. pfdb = (IntPtr)((int)pfdb + Marshal.SizeOf(dbfd)); cRecords--; } // while // Free memory returned by CeFindAllDatabases. Rapi.CeRapiFreeBuffer(pfdbAll); } // if // Notify main thread that we are done. itReason = INVOKE_ENUMDB.ENUMDB_COMPLETE; m_ctlInvokeTarget.Invoke(m_deleCallback); // Mark thread as done. m_thrd = null; }
cmdDisconnect_Click(object sender, System.EventArgs e) { if (m_bRapiConnected) { Rapi.CeRapiUninit(); m_bRapiConnected = false; } cmdDisconnect.Enabled = false; cmdFetch.Enabled = false; cmdConnect.Enabled = true; } // cmdDisconnect_Click
} // cmdDisconnect_Click private void FormMain_Closed(object sender, System.EventArgs e) { // If threads are running, trigger shutdown. if (this.m_thrdStartup != null) { this.m_thrdStartup.bThreadContinue = false; } if (m_bRapiConnected) { Rapi.CeRapiUninit(); m_bRapiConnected = false; } } // FormMain_Closed
} // cmdInvoke_Click private void cmdDisconnect_Click(object sender, System.EventArgs e) { // Disconnect from RAPI. Rapi.CeRapiUninit(); // Change UI. cmdInvoke.Enabled = false; cmdDisconnect.Enabled = false; cmdConnect.Enabled = true; textInput.Text = String.Empty; textInput.Enabled = false; // Set flag that we are disconnected m_bRapiConnected = false; } // cmdDisconnect_Click
cmdInvoke_Click(object sender, System.EventArgs e) { // Set up data to send to DLL string strHello = textInput.Text; int cbInput = (strHello.Length + 1) * 2; int cbOutput = 0; IntPtr ipInput = Marshal.StringToHGlobalUni(strHello); IntPtr ipOutput = IntPtr.Zero; try { // Call device-side DLL int hr = Rapi.CeRapiInvoke(@"\windows\SimpleBlockModeInvoke.dll", "UpperCaseInvoke", cbInput, ipInput, ref cbOutput, ref ipOutput, 0, 0); if (hr == Rapi.S_OK) { // Convert return value to a string. string strOutput = Marshal.PtrToStringUni(ipOutput, cbOutput); // Free memory returned from call to CeRapiInvoke. Marshal.FreeHGlobal(ipOutput); // Display resulting string. MessageBox.Show(strOutput, "CallDeviceDll"); } else { throw (new System.Exception()); } } catch { // In case of error, free memory we allocated. Marshal.FreeHGlobal(ipInput); MessageBox.Show("Error in calling device DLL.\n\n" + "Download SimpleBlockModeInvoke.dll " + "to the device.\nThen try again.", "CallDeviceDll"); } } // cmdInvoke_Click
cmdRun_Click(object sender, System.EventArgs e) { sbarMain.Text = "Attempting to run program..."; int iItem = lboxPrograms.SelectedIndex; string strProg = lboxPrograms.Items[iItem].ToString(); if (strProg.Length > 0) { Rapi.PROCESS_INFORMATION pi = new Rapi.PROCESS_INFORMATION(); Rapi.CeCreateProcess(strProg, 0, 0, 0, 0, 0, 0, 0, 0, ref pi); Rapi.CeCloseHandle(pi.hProcess); Rapi.CeCloseHandle(pi.hThread); } sbarMain.Text = "Ready"; }
cmdDisconnect_Click(object sender, System.EventArgs e) { sbarMain.Text = "Disconnecting..."; // Trigger thread to stop running. if (m_thrdFindFiles != null) { m_thrdFindFiles.bThreadContinue = false; } // Disconnect from RAPI. Rapi.CeRapiUninit(); ResetUI(); // Clear out previous contents of file listbox. lboxPrograms.Items.Clear(); m_bRapiConnected = false; }
private bool copyFromDevice(string localPath, string remotePath, bool useActiveSync) { _Log.DebugFormat("Copying file from device:[{1}]->[{0}]", localPath, remotePath); if (useActiveSync && Rapi != null) { if (Rapi.DevicePresent && Rapi.Connected) { Rapi.CopyFileFromDevice(localPath, remotePath, true); return(true); } else { return(false);//TODO Fehlermeldung/Logging } } else { if (System.IO.File.Exists(remotePath)) { try { System.IO.File.Copy(remotePath, localPath, true); return(true); } catch (IOException ex) { _Log.ErrorFormat("Copy Failure: [{0}]", ex.Message != null ? ex.Message : ""); return(false); } } else { _Log.ErrorFormat("Copy Failure: Path Not Found [{0}]", remotePath.nullToEmpty()); return(false); } } }
private bool copyToDevice(string localPath, string remotePath, bool useActiveSync) { _Log.DebugFormat("Copying file to device: [{0}]->[{1}]", localPath, remotePath); if (useActiveSync && Rapi != null) { if (Rapi.DevicePresent && Rapi.Connected) { Rapi.CopyFileToDevice(localPath, remotePath, true); return(true); } else { return(false);//TODO Fehlermeldung/Logging } } else { if (System.IO.File.Exists(localPath)) { try { System.IO.File.Copy(localPath, remotePath, true); return(true); } catch (IOException ex) { _Log.ErrorFormat("Copying file to device failed:\n[{0}]\n[{1}]->[{2}]", ex, localPath.nullToEmpty(), remotePath.nullToEmpty()); return(false); } } else { _Log.ErrorFormat("Copying file to device failed: Source does not exist! [{0}]", localPath.nullToEmpty()); return(false); } } }
/// <summary> /// AddProgramsInDirectory - Recursive function to search /// into directory tree. /// </summary> /// <param name="strDir">Starting directory</param> /// <returns></returns> private bool AddProgramsInDirectory(string strDir) { Trace.WriteLine("FindPrograms: " + "AddProgramsInDirectory (" + strDir + ")"); // Update status bar through delegate function. strBuffer = "Searching in " + strDir + "..."; itReason = INVOKE_FINDFILES.STATUS_MESSAGE; m_ctlInvokeTarget.Invoke(m_deleCallback); // As we add programs, store directory names. ArrayList alDirectories = new ArrayList(); Rapi.CE_FIND_DATA fd = new Rapi.CE_FIND_DATA(); // Start our search. string strSearch = strDir + "*.*"; IntPtr hff = Rapi.CeFindFirstFile(strSearch, ref fd); if ((int)hff == -1) { FetchAndDisplayError(); } else { do { string strFileName = fd.cFileName; int iFlag = (int) Rapi.FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY; if ((fd.dwFileAttributes & iFlag) == iFlag) { alDirectories.Add(strDir + fd.cFileName); } else { if (strFileName.EndsWith(".EXE") || strFileName.EndsWith(".exe")) { m_cFiles++; strBuffer = strDir + fd.cFileName; itReason = INVOKE_FINDFILES.FINDFILE_NEWFILE; m_ctlInvokeTarget.Invoke(m_deleCallback); } } } while (bThreadContinue && Rapi.CeFindNextFile(hff, ref fd) != 0); if (bThreadContinue && m_bIncludeSubDirs) { foreach (string str in alDirectories) { AddProgramsInDirectory(str + "\\"); } } } Rapi.CeFindClose(hff); return(true); }
/// <summary> /// AddProgramsInDirectory - Recursive function to search /// into directory tree. /// </summary> /// <param name="strDir">Starting directory</param> /// <returns></returns> private bool AddProgramsInDirectory(string strDir) { Trace.WriteLine("FindPrograms: " + "AddProgramsInDirectory (" + strDir + ")"); // Update status bar through delegate function. strBuffer = "Searching in " + strDir + "..."; itReason = INVOKE_FINDFILES.STATUS_MESSAGE; m_ctlInvokeTarget.Invoke(m_deleCallback); // As we add programs, store directory names. ArrayList alDirectories = new ArrayList(); // Start our search. string strSearch = strDir + "*.*"; IntPtr pfdAllFiles = IntPtr.Zero; // Return pointer. int cFound = 0; // Return count of files. // Call looking for all files in current directory. Rapi.CeFindAllFiles(strSearch, Rapi.FAF.FAF_ATTRIBUTES | Rapi.FAF.FAF_NAME, ref cFound, ref pfdAllFiles); // Loop through all files found. IntPtr pfd = pfdAllFiles; while (cFound > 0) { // // Here is the secret sauce. This function uses a // Win32 pointer to create a .NET object // Rapi.CE_FIND_DATA fd = // Output .NET object (Rapi.CE_FIND_DATA) // Always cast this Marshal.PtrToStructure( // The function pfd, // Input Win32 ptr typeof(Rapi.CE_FIND_DATA)); // Output type string strFileName = fd.cFileName; int iFlag = (int) Rapi.FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY; if ((fd.dwFileAttributes & iFlag) == iFlag) { alDirectories.Add(strDir + fd.cFileName); } else { if (strFileName.EndsWith(".EXE") || strFileName.EndsWith(".exe")) { m_cFiles++; strBuffer = strDir + fd.cFileName; itReason = INVOKE_FINDFILES.FINDFILE_NEWFILE; m_ctlInvokeTarget.Invoke(m_deleCallback); } } // Get ready for next loop. pfd = (IntPtr)((int)pfd + Marshal.SizeOf(fd)); cFound--; } // Free memory returned by CeFindAllFiles. Rapi.CeRapiFreeBuffer(pfdAllFiles); if (bThreadContinue && m_bIncludeSubDirs) { foreach (string str in alDirectories) { AddProgramsInDirectory(str + "\\"); } } return(true); }
/// <summary> /// ThreadMainEnumReg - Enumerate registry values. /// </summary> private void ThreadMainEnumReg() { // Open registry key. IntPtr hkeySearchNode = IntPtr.Zero; int iResult = Rapi.CeRegOpenKeyEx( this.m_hkeyRoot, this.m_strRegNode, 0, 0, ref hkeySearchNode); if (iResult != Rapi.ERROR_SUCCESS && m_bContinue) { // Send error message. itReason = INVOKE_ENUMREG.STATUS_MESSAGE; strBuffer = "Error accessing registry key"; this.m_ctlInvokeTarget.Invoke(m_deleCallback); // Trigger end of enumeration. itReason = INVOKE_ENUMREG.ENUMREG_ENDED; this.m_ctlInvokeTarget.Invoke(m_deleCallback); // Trigger that shutdown is complete. thrd = null; return; } // Keys or values? if (this.m_bKeys) // Enumerate keys. { int iIndex = 0; while (iResult == Rapi.ERROR_SUCCESS && m_bContinue) { string strKeyName = new string('\0', 32); int cbLength = 32; iResult = Rapi.CeRegEnumKeyEx( hkeySearchNode, iIndex++, strKeyName, ref cbLength, 0, 0, 0, 0); if (iResult == Rapi.ERROR_SUCCESS && m_bContinue) { itReason = INVOKE_ENUMREG.ENUMREG_NEWKEY; strBuffer = strKeyName; this.m_ctlInvokeTarget.Invoke(m_deleCallback); } } // while } else // Enumerate values. { int iIndex; for (iIndex = 0; iResult == Rapi.ERROR_SUCCESS && m_bContinue; iIndex++) { int cbName = 32; string strName = new string('\0', 16); Rapi.REGTYPE iType = 0; int cbLength = 0; // Enumerate key names only (not values) iResult = Rapi.CeRegEnumValue(hkeySearchNode, iIndex, strName, ref cbName, 0, ref iType, IntPtr.Zero, ref cbLength); if (iResult == Rapi.ERROR_SUCCESS && m_bContinue) { if (iType == Rapi.REGTYPE.REG_SZ) // string values. { int cbData = 32; string str = new string(new char[cbData]); Rapi.CeRegQueryValueEx(hkeySearchNode, strName, 0, ref iType, str, ref cbData); char [] ach = { '\0' }; strBuffer = strName.Trim(ach) + " = " + str.Trim(ach); } else if (iType == Rapi.REGTYPE.REG_BINARY) { // Fetch binary array of short values char [] ach = { '\0' }; strBuffer = strName.Trim(ach) + " = "; // Allocate buffer of short values. Int16 sh = 0; IntPtr iptr; int cbSizeOfShort = Marshal.SizeOf(sh); int cbData = cbSizeOfShort * 5; iptr = Marshal.AllocCoTaskMem(cbData); // Fetch array of short values. Rapi.CeRegQueryValueEx(hkeySearchNode, strName, 0, ref iType, iptr, ref cbData); // Copy array to managed array. int cElements = cbData / cbSizeOfShort; Int16 [] ash = new Int16 [cElements]; Marshal.Copy(iptr, ash, 0, cElements); Marshal.FreeCoTaskMem(iptr); // Add values to string for display. for (int i = 0; i < cElements; i++) { strBuffer = strBuffer + ash[i] + " "; } } else { strBuffer = strName + " not expected type"; } itReason = INVOKE_ENUMREG.ENUMREG_NEWVALUE; this.m_ctlInvokeTarget.Invoke(m_deleCallback); } } // for } // if Rapi.CeRegCloseKey(hkeySearchNode); // Trigger end of enumeration. itReason = INVOKE_ENUMREG.ENUMREG_ENDED; this.m_ctlInvokeTarget.Invoke(m_deleCallback); // Trigger that shutdown is complete. thrd = null; }