I am making a console application that displays some data in windows form popups primarily for development or to configure it. However, when deployed it is to be ran in a command line only environment, which will not be able to display the forms. I would like to know if there is a way to detect whether or not it is possible to display these forms so I can display the data in a different format or print an explanation if the data cannot be displayed (one of the forms is an image preview).
Yes sir, i would like to give you two Methods, for the case "Mono":
This Checks, if your in Mono Runtime:
private Boolean IsMonoRuntime()
{
return (Type.GetType("Mono.Runtime") != null);
}
This gives you the Mono-Version (Linux Package) or String.Empty:
private String GetMonoRuntime()
{
Type type = Type.GetType("Mono.Runtime");
if (type != null)
{
MethodInfo displayName = type.GetMethod("GetDisplayName", BindingFlags.NonPublic | BindingFlags.Static);
if (displayName == null)
return "Unix/Linux + Mono";
else
return "Unix/Linux + Mono " + displayName.Invoke(null, null);
}
return String.Empty;
}
I hope its whay ou needed.
Related
I am trying to read the System Event Logs in C# .NET 3.5 with the following method EventLog.GetEventLogs. This seems to be working perfectly for all kinds of Event Logs that I want to take a look at. There is but one exception: Microsoft-Windows-Kernel-Power Event Logs which can be read but produces the following Message:
Microsoft-Windows-Kernel-Power The description for Event ID 'X' in
Source 'Microsoft-Windows-Kernel-Power' cannot be found. The local
computer may not have the necessary registry information or message
DLL files to display the message, or you may not have permission to
access them. The following information is part of the event:'Y', 'Z'
instead of the correct Message displayed in the Windows Event Viewer.
Code looks like this
var myEventLogs = new List<myModels.EventLogEntry>();
foreach (var eventLog in EventLog.GetEventLogs())
{
foreach (var entry in eventLog.Entries)
{
if (entry.Source.IndexOf("kernel-power", StringComparison.OrdinalIgnoreCase) == -1 &&
entry.Message.IndexOf("kernel-power", StringComparison.OrdinalIgnoreCase) == -1)
continue;
myEventLogs.Add(new myModels.EventLogEntry(entry.Source, entry.Message))
}
}
This is happening even if I run the application as administrator. I am at a loss here. I have searched far and wide all over the internet and found a couple posts that seem to have similar issues but most are for writing Event Logs instead of having problems reading them. Is anyone familiar with that kind of problem or can point me in the right direction? All my registry keys etc seem to be set up correctly (and it is also showing the same results on a different PC).
EDIT: Windows Version 10.0.18363 Build 18363 but it is happening on multiple PCs (I am not sure what Windows version the others are using). In fact, I have not found a single one which is working (tested 5 so far).
I couldn't find any specification neither, but I got a hack which shows plausible messages here. The issue looks like either a bug in EventLogEntry from CLR or a non-uniform message handling of kernel-power events.
I reproduced the issue, and what was happening under the hood on my machine:
E.g. there is a 109 (0x6D) event in the event log:
EventLogEntry gets a path to a dll with string resources from HKLM\SYSTEM\CurrentControlSet\Services\EventLog\Application\Microsoft-Windows-Kernel-Power\EventMessageFile (in CLR sources of EventLogEntry as here and here)
Dll path on my machine is: %systemroot%\system32\microsoft-windows-kernel-power-events.dll, it's successfully found and loaded, but there is no 0x6D string id inside. However there is a string with the same text but with 0x02 00 00 00 prefix in its id (string resources from native dll were enumerated using this article)
ID 0x0200006d (33554541) Language: 0409
The kernel power manager has initiated a shutdown transition.
So if set 0x02 00 00 00 bit manually in event id, it might produce meaningful messages. Code below does this, but again this is an ugly hack, it should not be used in real life software, as it's based solely on assumptions and not tested in all cases, and it manipulates private state of EventLogEntry:
var myEventLogs = new List<myModels.EventLogEntry>();
foreach (var eventLog in EventLog.GetEventLogs())
{
foreach (EventLogEntry entry in eventLog.Entries)
{
if (entry.Source.IndexOf("kernel-power", StringComparison.OrdinalIgnoreCase) == -1 &&
entry.Message.IndexOf("kernel-power", StringComparison.OrdinalIgnoreCase) == -1)
continue;
var dataBuf = entry.GetPrivateField<byte[]>("dataBuf");
var bufOffset = entry.GetPrivateField<int>("bufOffset");
byte previousMagicByte = dataBuf[bufOffset + EVENTID + 3];
try
{
dataBuf[bufOffset + EVENTID + 3] |= 0x02; //as strings in microsoft-windows-kernel-power-events.dll have 0x02****** ids
myEventLogs.Add(new myModels.EventLogEntry(entry.Source, entry.Message))
}
finally
{
dataBuf[bufOffset + EVENTID + 3] = previousMagicByte;
}
}
}
...
internal const int EVENTID = 20;
public static T GetPrivateField<T>(this object obj, string fieldName)
{
if (fieldName == null)
throw new ArgumentNullException(nameof(fieldName));
var fieldInfo = obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
if (fieldInfo == null)
throw new ArgumentException($"Type {obj.GetType().FullName} doesn't have {fieldName} private instance field");
object result = fieldInfo.GetValue(obj);
return (T)result;
}
i am new to WIA. And i have been asked to make scaning service scan faster and duplex. My current service scan one page, then put it in pdf and so on untill there is less then 20 pages(this number just a crutch used before me, will be glad if someone explane how to get "if there is any paper in there" variable). I started to dig and found docs on MSDN describing properties and after found this post describing duplex sanning, but with mysterious 5 in set. After I found this and figured out what i need WIA_DPS_DOCUMENT_HANDLING_SELECT to set to 0x205(FEEDER + DUPLEX + AUTO_ADVANCE). So I tried to setup them like this:
private static void SetProperty(Property property, int value)
{
IProperty x = (IProperty)property;
Object val = value;
x.set_Value(ref val);
}
...some code...
foreach (Property prop in device.Properties)
{
//LOGGER.Warn(prop.Name);
//LOGGER.Warn(prop.PropertyID);
switch ((Int32)prop.PropertyID)
{
// Document Handling Select
case 3088:
SetProperty(prop, 517);
break;
// Pages
case 3096:
SetProperty(prop, 1);
break;
}
}
And it did't worked for me... It just stuck on setting... Can Somebody explain how to setup AUTO_ADVANCE and DUPLEX props? Or maybe "make scanning faster and duplex" need something more then just AUTO_ADVANCE and DUPLEX and my perception about them is wrong? Or I should considering "ISIS / TWAIN (Windows XP / Vista / 7 / 8 / 8.1 / 10)" string in my scan description and use other libraries?
(Window 10, Canon DR-M160||, DR-M160 & DR-M160II Driver for Windows)
and also here is the current fetch function:
public List<ImageFile> FetchImageList()
{
List<ImageFile> imageList = new List<ImageFile>();
//bool hasMorePages = true;
int testcount = 0;
while (testcount >= 0)
{
testcount--;
WIA.Device device = FindDevice(_deviceId);
if (device == null)
{
LOGGER.Warn("Scanner device not found");
return null;
}
// get item
WIA.Item scanItem = device.Items[1] as WIA.Item;
LOGGER.Debug($"ScanItem: {scanItem.ItemID}");
try
{
foreach (Property prop in device.Properties)
{
//LOGGER.Warn(prop.Name);
//LOGGER.Warn(prop.PropertyID);
switch ((Int32)prop.PropertyID)
{
// Document Handling Select
case 3088:
LOGGER.Warn("here");
SetProperty(prop, 517);
LOGGER.Warn("here");
break;
// Pages
case 3096:
SetProperty(prop, 1);
break;
}
}
// scan image
WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
WIA.ImageFile image = (WIA.ImageFile)scanItem.Transfer(WIA.FormatID.wiaFormatPNG);
imageList.Add(image);
LOGGER.Warn("Front");
//get back side
image = (WIA.ImageFile)scanItem.Transfer(WIA.FormatID.wiaFormatPNG);
imageList.Add(image);
LOGGER.Warn("Back");
}
catch (Exception e)
{
throw (e);
}
}
return imageList;
}
Well... I tried to make duplex scan without AUTO_ADVANCE and got HRESULT: 0x8000FFFF (E_UNEXPECTED) on Transfer call. According to this post(even though that was on Windows 7) I guess there is no way to solve this for me by using WIA, still hope there will other suggestions...
Solved problem
I used saraff.twain and it worked for me:
- git page :https://github.com/saraff-9EB1047A4BEB4cef8506B29BA325BD5A/Saraff.Twain.NET
good library with grate wiki page.(Also have similar library for .net 4.6.1)
I started using CodeFluentRuntimeClient to replace Interop.MSScriptControl.dll.
I succeed here by tweeking a bit the dll to make it work.
We started using the dll in production. On one of the machines that we installed on it (windows server 2012), we are having a Sytem.AccessViolationException.
Here's the stack trace of the event viewer:
Do CodeFluent requieres any other dlls?
EDIT
Here's the code:
public dynamic EvaluateVBScript(string token, string key, string script, IDictionary<string, object> parameterValuePair = null)
{
try
{
using (ScriptEngine engine = new ScriptEngine(ScriptEngine.VBScriptLanguage))
{
List<object> parameters = new List<object>() { string.IsNullOrEmpty(token) ? string.Empty : ServiceManager.GetService<IServiceInstance>().GetService<IContextManager>(token).UserName };
string extraParameters = string.Empty;
if (parameterValuePair != null && parameterValuePair.Count > 0)
{
extraParameters = "," + string.Join(",", parameterValuePair.Select(e => e.Key));
foreach (var para in parameterValuePair)
parameters.Add(para.Value);
}
string parsedScript = string.Format(#"Function {0}(NecUserProfile {2})
{1}
End Function", key, script, extraParameters);
ParsedScript parsed = engine.Parse(parsedScript);
dynamic value = parsed.CallMethod(key, parameters.ToArray());
return (value != null) ? value.ToString() : string.Empty;
}
}
catch
{
throw;
}
}
After some tests, we found out that the client had an antivirus (Kaspersky) installed on his server. Even after disabling the antivirus, the access violation error was still occurring.
After uninstalling the antivirus, we were finally able to execute the JavaScript. We still don't know what rule was set in the antivirus that was blocking the script to be parsed.
I didn't test in the suggested solution by Simon Mounier. I don't know if it would have solved the problem.
The solution was to drop out the CodeFluent.Runtime.Client.dll and use directly the source code provided here. Also add MarshalAs(UnmanagedType.LPWStr)] around the string parameters that are going to be used by the parse function, like in here.
So, I have a basic LAMP server that I use to run my asp.net websites (running mono). When the user accesses my website, I record all of their information in my database.
When testing the application from code, it works like a charm. However, if I publish my website to my server and access the website, it records nothing.
My desktop runs Win7 and the server is Ubuntu 64bit w/ Apache2. My first thought was maybe I wasn't doing it right. So, I ran the code from here http://msdn.microsoft.com/en-us/library/3yekbd5b.aspx and everything was false. I'm lead to believe that the issue isn't necessarily my code but possibly with apache2.
From what you can see, I do code around this a little.
public string GetBrowserType()
{
var value = String.Empty;
if (HttpContext.Current.Request.Browser.Type != null && HttpContext.Current.Request.Browser.Type != "*0")
{
value = bc.Type;
}
return value;
}
public string GetBrowserName()
{
var value = String.Empty;
if (HttpContext.Current.Request.Browser.Browser != null && HttpContext.Current.Request.Browser.Browser != "*")
{
value = bc.Browser;
}
return value;
}
public string GetOs()
{
var value = String.Empty;
if (HttpContext.Current.Request.Browser.Platform != null && HttpContext.Current.Request.Browser.Platform != "unknown")
{
value = bc.Platform;
//value = HttpContext.Current.Request.Browser.Platform;
}
return value;
}
So would the issue be with Apache? Is there a mod that I need to enable to allow it to log everything?
Probably you have not update/install the browser detection files on the asp.net running pool.
to make Request.Browser works you must have configure the files on \Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers\
I want to check from my code if a particular version of flash player is installed or not.
I used the following code
using Microsoft.Win32
RegistryKey RK = Registry.CurrentUser.OpenSubKey("HKEY_LOCAL_MACHINE\\SOFTWARE\\Macromedia\\FlashPlayer");
if (RK != null)
{
// It's there
}
else
{
// It's not there
}
In registry If I search for flash player with version 10.2.161.23, the location
"HKEY_LOCAL_MACHINE\SOFTWARE\Macromedia"
is having 2 folders:
FlashPlayer and
FlashPlayerActiveX.
But, my above code is not working.
Kindly let me know how to check if a particular version of flash player is installed in a system or not USING C#.NET.
Adobe's old (pre 10) IE Flash detection code used to test in VBScript if it could instantiate object ShockwaveFlash.ShockwaveFlash.<major version>. If it's just the major version you want to test, you can check for those keys under HKCR, e.g. HKEY_CLASSES_ROOT\ShockwaveFlash.ShockwaveFlash.10.
SWFObject instantiates the version-less object name, ShockwaveFlash.ShockwaveFlash, and queries its $version property. To do this in C#:
// Look up flash object type from registry
var type = Type.GetTypeFromProgID("ShockwaveFlash.ShockwaveFlash");
if (type == null)
{
// No flash
return;
}
// Create a flash object to query
// (should probably try/catch around CreateInstance)
var flashObject = Activator.CreateInstance(type);
var versionString = flashObject.GetType()
.InvokeMember("GetVariable", BindingFlags.InvokeMethod,
null, flashObject, new object[] {"$version"})
as string;
// e.g. "WIN 10,2,152,26"
// Clean up allocated COM Object
Marshal.ReleaseComObject(flashObject);