deniedbysystem Windows.Devices - c#

I'm making a Windows store device app to go onto the auto play when my device is plugged in, However when I try to access any of the data from the app I keep getting deniedBySystemand I'm not sure how to access my device.
private async void OpenDevice()
{
UInt32 vid = 1234;/*stand in vid and pid not real ones i use in my code*/
UInt32 pid = 5678;
lstResults.Items.Clear();
string aqs = UsbDevice.GetDeviceSelector(vid, pid);
int counter = 0;
var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync();
//var myDevices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(aqs); /*I was using this but it wasn't returning any device*/
for (int i = 0; i < myDevices.Count; i++)
{
try
{
if (myDevices[i].Id[4] == 'U' && myDevices[i].Id[8] == 'V' && myDevices[i].Id.Substring(12, 4) == vid.ToString())/* this if statements makes it so I only get the device I want*/
{
lstResults.Items.Add("VID " + myDevices[i].Id.Substring(12, 4) +
" PID " + myDevices[i].Id.Substring(21, 4));
counter++;
var deciceAccessInfo = Windows.Devices.Enumeration.DeviceAccessInformation.CreateFromId(myDevices[i].Id);
lstResults.Items.Add(deciceAccessInfo.CurrentStatus.ToString()); // this line informs me access is denied
// lstResults.Items.Add(myDevices[i].Id);
var usbDevice = await UsbDevice.FromIdAsync(myDevices[i].Id);//this line returns usbDevice to equal null
}
}
catch (Exception exception)
{
lstResults.Items.Add(exception.Message.ToString());
}
}
lstResults.Items.Add(counter + " out of " + myDevices.Count + " Have the correct VID");
}
In the Package.appxmanifestI have checked all the capabilities in case it was one of them denieing me access but it wasn't. I have also tried running the program as an admin to see if that gave me access but no joy. so my question is what is denying me access and is there a way around it?

Related

c# Cross-Platform device information from local network from lan scan

I have currently wrote a code which scans the local network with a function called "bool ping()". I want to add a capability to provide basic device information when the function returns 'true'. I've found 'ManagementObjectSearcher'.
At first it looked perfect but when It gets used for a non-windows device it crash. Therefore I suppose that this method cannot be used for non-windows devices.
As I want to add the below code ( or login , after enough polishing ), to an android app that scans the local network and returns the
A) IP address and
B) (one of the following )
the device type ( desktop , laptop , smartphone)
and/or
the OS type ( android , windows , linux , tvOS )
Is there a valid way I can do what I am looking for? I believe I have experienced apps that do stuff like that, though I don't know what language they were based on.
namespace LanConsole
{
class Program
{
static void Main(string[] args)
{
string host = "192.168.1.10"; // android smartphone IP
string temp = null;
// arguments I found
string[] _searchClass = { "Win32_ComputerSystem", "Win32_OperatingSystem", "Win32_BaseBoard", "Win32_BIOS" };
string[] param = { "UserName", "Caption", "Product", "Description" };
bool v = ping(host, 10, 900); // bool function send ping to host
if (v == true)
{
Console.WriteLine("true");
for (int i = 0; i <= _searchClass.Length - 1; i++)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("\\\\" + host + "\\root\\CIMV2", "SELECT *FROM " + _searchClass[i]);
foreach (ManagementObject obj in searcher.Get())
{
temp += obj.GetPropertyValue(param[i]).ToString() + "\n";
if (i == _searchClass.Length - 1)
{
Console.WriteLine(temp, "Hostinfo: " + host);
break;
}
}
Console.WriteLine("");
}
}
else
Console.WriteLine("false");
}
public static bool ping(string host, int attempts, int timeout)
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply pingReply;
for (int i = 0; i < attempts; i++)
{
try
{
pingReply = ping.Send(host, timeout);
// If there is a successful ping, return true.
if (pingReply != null &&
pingReply.Status == System.Net.NetworkInformation.IPStatus.Success)
{
return true;
}
}
catch
{
// supressing errors
}
}
// Return false if ping fales "attempts" times
return false;
}
}
}

Where do I have memory leaks and how to fix it? Why memory consumption increases?

I am struggling a few days already with a problem of growing memory consumption by console application in .Net Core 2.2, and just now I ran out of ideas what else I could improve.
Im my application I have a method that triggers StartUpdatingAsync method:
public MenuViewModel()
{
if (File.Exists(_logFile))
File.Delete(_logFile);
try
{
StartUpdatingAsync("basic").GetAwaiter().GetResult();
}
catch (ArgumentException aex)
{
Console.WriteLine($"Caught ArgumentException: {aex.Message}");
}
Console.ReadKey();
}
StartUpdatingAsync creates 'repo' and instance is getting from DB a list of objects to be updated (around 200k):
private async Task StartUpdatingAsync(string dataType)
{
_repo = new DataRepository();
List<SomeModel> some_list = new List<SomeModel>();
some_list = _repo.GetAllToBeUpdated();
await IterateStepsAsync(some_list, _step, dataType);
}
And now, within IterateStepsAsync we are getting updates, parsing them with existing data and updating DB. Inside of each while I was creating new instances of all new classes and lists, to be sure that old ones are releasing memory, but it didnt help. Also I was GC.Collect() at the end of the method, what also is not helping. Please note, that method below triggers lots of parralel Tasks, but they supposed to be disposed within it, am I right?:
private async Task IterateStepsAsync(List<SomeModel> some_list, int step, string dataType)
{
List<Area> areas = _repo.GetAreas();
int counter = 0;
while (counter < some_list.Count)
{
_repo = new DataRepository();
_updates = new HttpUpdates();
List<Task> tasks = new List<Task>();
List<VesselModel> vessels = new List<VesselModel>();
SemaphoreSlim throttler = new SemaphoreSlim(_degreeOfParallelism);
for (int i = counter; i < step; i++)
{
int iteration = i;
bool skip = false;
if (dataType == "basic" && (some_list[iteration].Mmsi == 0 || !some_list[iteration].Speed.HasValue)) //if could not be parsed with "full"
skip = true;
tasks.Add(Task.Run(async () =>
{
string updated= "";
await throttler.WaitAsync();
try
{
if (!skip)
{
Model model= await _updates.ScrapeSingleModelAsync(some_list[iteration].Mmsi);
while (Updating)
{
await Task.Delay(1000);
}
if (model != null)
{
lock (((ICollection)vessels).SyncRoot)
{
vessels.Add(model);
scraped = BuildData(model);
}
}
}
else
{
//do nothing
}
}
catch (Exception ex)
{
Log("Scrape error: " + ex.Message);
}
finally
{
while (Updating)
{
await Task.Delay(1000);
}
Console.WriteLine("Updates for " + counter++ + " of " + some_list.Count + scraped);
throttler.Release();
}
}));
}
try
{
await Task.WhenAll(tasks);
}
catch (Exception ex)
{
Log("Critical error: " + ex.Message);
}
finally
{
_repo.UpdateModels(vessels, dataType, counter, some_list.Count, _step);
step = step + _step;
GC.Collect();
}
}
}
Inside of the method above, we are calling _repo.UpdateModels, where is updated DB. I tryed two approaches, with using EC Core and SqlConnection. Both with similar results. Below you can find both of them.
EF Core
internal List<VesselModel> UpdateModels(List<Model> vessels, string dataType, int counter, int total, int _step)
{
for (int i = 0; i < vessels.Count; i++)
{
Console.WriteLine("Parsing " + i + " of " + vessels.Count);
Model existing = _context.Vessels.Where(v => v.id == vessels[i].Id).FirstOrDefault();
if (vessels[i].LatestActivity.HasValue)
{
existing.LatestActivity = vessels[i].LatestActivity;
}
//and similar parsing several times, as above
}
Console.WriteLine("Saving ...");
_context.SaveChanges();
return new List<Model>(_step);
}
SqlConnection
internal List<VesselModel> UpdateModels(List<Model> vessels, string dataType, int counter, int total, int _step)
{
if (vessels.Count > 0)
{
using (SqlConnection connection = GetConnection(_connectionString))
using (SqlCommand command = connection.CreateCommand())
{
connection.Open();
StringBuilder querySb = new StringBuilder();
for (int i = 0; i < vessels.Count; i++)
{
Console.WriteLine("Updating " + i + " of " + vessels.Count);
//PARSE
VesselAisUpdateModel existing = new VesselAisUpdateModel();
if (vessels[i].Id > 0)
{
//find existing
}
if (existing != null)
{
//update for basic data
querySb.Append("UPDATE dbo." + _vesselsTableName + " SET Id = '" + vessels[i].Id+ "'");
if (existing.Mmsi == 0)
{
if (vessels[i].MMSI.HasValue)
{
querySb.Append(" , MMSI = '" + vessels[i].MMSI + "'");
}
}
//and similar parsing several times, as above
querySb.Append(" WHERE Id= " + existing.Id+ "; ");
querySb.AppendLine();
}
}
try
{
Console.WriteLine("Sending SQL query to " + counter);
command.CommandTimeout = 3000;
command.CommandType = CommandType.Text;
command.CommandText = querySb.ToString();
command.ExecuteNonQuery();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
connection.Close();
}
}
}
return new List<Model>(_step);
}
Main problem is, that after tenths/hundreds of thousands of updated models my console application memory consumption increases continuously. And I have no idea why.
SOLUTION my problem was inside of ScrapeSingleModelAsync method, where I was using incorrectly HtmlAgilityPack, what I could debug thanks to cassandrad.
Your code is messy, with huge amount of different objects with unknown lifetime. It's hardly possible to figure out the problem just looking at it.
Consider using profiling tools, for example Visual Studio's Diagnostic Tools, they will help you to find what objects are living too long in the heap. Here is overview of its functions related to memory profiling. Highly recomended to be read.
In short, you need to take two snapshots and look at what objects are taking the most memory. Let's look at simple example.
int[] first = new int[10000];
Console.WriteLine(first.Length);
int[] secod = new int[9999];
Console.WriteLine(secod.Length);
Console.ReadKey();
Take the first snapshot when your function works at least once. In my case, I took snapshot when the first huge space has been alocated.
After that, let your app be working some time so the difference in memory usage become noticeable, take the second memory snapshot.
You'll notice that another snapshot is added with info about how much is the difference. To get more specific info, click on one or another blue label of the latest snapshot to open snapshots comparison.
Following my example, we can see that there is change in count of int arrays. By default int[] wasn't visible in the table, so I had to uncheck Just My Code in filtration options.
So, this is what needs to be done. After you figure out what objects increase in count or size over time, you can locate where these objects are create and optimize this operation.

Entity Framework - How To Handle Batch SaveChanges Failure

In my C# program I am using Entity Framework to synchronize a local SQL Server database with QuickBooks data. Getting the data from QuickBooks does not seem to have any issues. However I am running into a stumbling block when doing batch commits of entities.
Currently I am building up the DataContext with a configurable number of entities and then committing the entities in batch. So far the batch has not failed, but what if it does? My idea to combat this would be to iterate over the batch and submit each entity one at a time and then log the one(s) that is/are causing the commit failure.
However I do not see a way to do this with the data context since it appears to be an all or nothing matter when using SaveChanges(). Is there a way to handle what I am trying to accomplish, or should I be going about dealing with the failures in a completely different way?
Here is the code that I currently have, in case you want to take a look at it:
int itemsCount = 0;
int itemsSynced = 0;
int itemsFailed = 0;
ArrayList exceptions = new ArrayList();
int batchSliceCount = Properties.Settings.Default.SyncBatchSize; //Getting the max batch size from the settings
int index = 1; //Index used for keeping track of current batch size on data context
List<Customer> currentBatch = new List<Customer>(); // List to hold curent batch
db = new DataContext(DatabaseHelper.GetLocalDatabaseConnectionString());
foreach (var customer in QBResponse.customers)
{
itemsCount++;
try
{
string debugMsg = "Saving Customer with the Following Details....." + Environment.NewLine;
debugMsg += "ListId: " + customer.CustomerListId + Environment.NewLine;
debugMsg += "FullName: " + customer.FullName + Environment.NewLine;
int progressPercentage = (itemsCount * 100) / opResponse.retCount;
UpdateStatus(Enums.LogLevel.Debug, debugMsg, progressPercentage);
var dbCustomer = db.Customers.FirstOrDefault(x => x.CustomerListId == customer.CustomerListId);
if (dbCustomer == null)
{
// customer.CopyPropertiesFrom(customer, db);
Customer newCustomer = new Customer();
newCustomer.CopyCustomer(customer, db);
newCustomer.AddBy = Enums.OperationUser.SyncOps;
newCustomer.AddDateTime = DateTime.Now;
newCustomer.EditedBy = Enums.OperationUser.SyncOps;
newCustomer.EditedDateTime = DateTime.Now;
newCustomer.SyncStatus = true;
db.Customers.Add(newCustomer);
currentBatch.Add(newCustomer);
}
else
{
//dbCustomer.CopyPropertiesFrom(customer, db);
dbCustomer.CopyCustomer(customer, db);
dbCustomer.EditedBy = Enums.OperationUser.SyncOps;
dbCustomer.EditedDateTime = DateTime.Now;
dbCustomer.SyncStatus = true;
currentBatch.Add(dbCustomer);
}
try
{
if (index % batchSliceCount == 0 || index == opResponse.customers.Count()) //Time to submit the batch
{
UpdateStatus(Enums.LogLevel.Information, "Saving Batch of " + batchSliceCount + "Customers to Local Database");
db.SaveChanges();
itemsSynced += currentBatch.Count();
currentBatch = new List<Customer>();
db.Dispose();
db = new DataContext(DatabaseHelper.GetLocalDatabaseConnectionString());
}
}
catch (Exception ex)
{
string errorMsg = "Error occured submitting batch. Itterating and submitting one at a time. " + Environment.NewLine;
errorMsg += "Error Was: " + ex.GetBaseException().Message + Environment.NewLine + "Stack Trace: " + ex.GetBaseException().StackTrace;
UpdateStatus(Enums.LogLevel.Debug, errorMsg, progressPercentage);
//What to do here? Is there a way to properly iterate over the context and submit a change one at a time?
}
}
catch (Exception ex)
{
//Log exception and restart the data context
db.Dispose();
db = new DataContext(DatabaseHelper.GetLocalDatabaseConnectionString());
}
Thread.Sleep(Properties.Settings.Default.SynchronizationSleepTimer);
index++;
}
it depends on the exceptions you want to recover from...
If you are just looking for a way to retry if the connection was interrupted you could use a custom Execution Strategy based on DbExecutionStrategy that retries if specific errors occur as demonstrated in this CodeProject article.

Sharepoint Client Object Model The property or field has not been initialized

I have a C# program that manages Sharepoint lists using the Sharepoint Client Object Model. Occasionally we will have server issues which will prevent the program from accessing the sharepoint server. I am using a helper class to run the ExecuteQuery method and have exception handling to continue to execute until there is no exception.
private void ExecuteContextQuery(ref ClientContext siteContext)
{
int timeOut = 10000;
int numberOfConnectionErrors = 0;
int maxNumberOfRetry = 60;
while (numberOfConnectionErrors < maxNumberOfRetry)
{
try
{
siteContext.ExecuteQuery();
break;
}
catch (Exception Ex)
{
numberOfConnectionErrors++;
Service.applicationLog.WriteLine("Unable to connect to the sharepoint site. Retrying in " + timeOut);
Service.applicationLog.WriteLine("Exception " + Ex.Message + " " + Ex.StackTrace);
System.Threading.Thread.Sleep(timeOut);
if (numberOfConnectionErrors == maxNumberOfRetry)
{
throw Ex;
}
}
}
}
However I getting an error messages
The property or field 'LoginName' has not been initialized.
and
collection has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
The error messages seem to be related to methods where I call the Load method. here is an example of my code that calls the method above.
List sharepointList = siteContext.Web.Lists.GetByTitle(this._listName);
CamlQuery query = CamlQuery.CreateAllItemsQuery();
items = sharepointList.GetItems(query);
siteContext.Load(items);
//siteContext.ExecuteQuery();
ExecuteContextQuery(ref siteContext);
Do I need to reload the site context with every call to ExecuteQuery? Is that why I am seeing the error message above?
Here is the function I am using for getting the Login ID which is generating the error
public String getLoginIDbyUserId(int userID)
{
ClientContext siteContext = getClientContextObject();
User _getUser = siteContext.Web.SiteUsers.GetById(userID);
siteContext.Load(_getUser);
//siteContext.ExecuteQuery();
ExecuteContextQuery(ref siteContext);
String loginID = String.Empty;
String formatedLoginID = String.Empty;
loginID = _getUser.LoginName;
if (loginID.Contains('|'))
{
formatedLoginID = loginID.Substring(loginID.IndexOf('|') + 1);
}
siteContext.Dispose();
return formatedLoginID;
}
Please try to load LoginName property of user while loading user object. And after excutequery method, try to consume LoginName property of User
siteContext.Load(_getUser, u => u.LoginName);
And after this change your code should look like this
public String getLoginIDbyUserId(int userID)
{
ClientContext siteContext = getClientContextObject();
User _getUser = siteContext.Web.SiteUsers.GetById(userID);
siteContext.Load(_getUser, u => u.LoginName);
//siteContext.ExecuteQuery();
ExecuteContextQuery(ref siteContext);
String loginID = String.Empty;
String formatedLoginID = String.Empty;
loginID = _getUser.LoginName;
if (loginID.Contains('|'))
{
formatedLoginID = loginID.Substring(loginID.IndexOf('|') + 1);
}
siteContext.Dispose();
return formatedLoginID;
}

Using COM component fails as admin

I have a .NET wrapper over a native dll.
I try to register the component only for current user based on this : link
This runs fine, except if I right click the executable and say "Run as admin.." the tool will fail that it cannot find the class.
This is the code I use, is there something I am missing , I think it might be related to :
link
Thanks!
public int Register(ServerAction Action, string ServerName)
{
int hr = 0;
string szFunction = (Action == ServerAction.Register) ? "DllRegisterServer" : "DllUnregisterServer";
logger.Info("FileReader->Try to " + szFunction + " " + ServerName);
try
{
IntPtr hMod = LoadLibrary(ServerName);
if (hMod.ToInt32() != 0)
{
IntPtr registerDllServer_ProcAddress = GetProcAddress(hMod, szFunction);
DllRegisterServerInvoker pfnDllProc = (DllRegisterServerInvoker)Marshal.GetDelegateForFunctionPointer(registerDllServer_ProcAddress, typeof(DllRegisterServerInvoker));
if (pfnDllProc != null)
{
if (isUserAdminAndProcessElevated())
{
logger.Info("User is admin and process is elevated!");
}
else
{
logger.Info("User is not admin!");
hr = OverrideClassesRoot(HKEY_CURRENT_USER, "Software\\Classes");
}
}
hr = pfnDllProc();
pfnDllProc = null;
FreeLibrary(hMod);
}
else
{
hr = this.GetHresultFromWin32();
}
}
catch
{
Console.WriteLine("FileReader->" + szFunction + " " + ServerName + " FAILED!");
}
string errorMessage = new Win32Exception(hr).Message;
logger.Fatal(errorMessage);
return hr;
}
private Int32 OverrideClassesRoot(UIntPtr hKeyBase, string OverrideKey)
{
UIntPtr hkey = new UIntPtr();
int l = RegOpenKeyA(hKeyBase, OverrideKey, ref hkey);
if (l == 0)
{
l = RegOverridePredefKey(HKEY_CLASSES_ROOT, hkey);
RegCloseKey(hkey);
}
return this.GetHresultFromWin32();
}
Still haven't figured this out but I fixed it with : MSDN link

Categories