NLua and generic methods - c#

I am trying to run following lua code with DoFile() in NLua :
import('TestStack.White');
src=luanet.import_type('TestStack.White.UIItems.Finders.SearchCriteria')
application=Application.Launch('C:\\windows\\system32\\calc.exe')
win = application:GetWindow("Calculator");
win:WaitWhileBusy();
btnOne=win:Get(src.ByText("1")) <<---Exception here
btnOne:Click();
win:WaitWhileBusy();
I get following exception while executing this :
{Late bound operations cannot be performed on types or methods for which ContainsGenericParameters is true.}
Equaivalent .net code which works fine is :
Application application = Application.Launch(#"C:\windows\system32\calc.exe");
Window window = application.GetWindow("Calculator");
window.WaitWhileBusy();
var btnOne = window.Get(SearchCriteria.ByText("1"));
btnOne.Click();
window.WaitWhileBusy();
Please help getting rid of this exception.
Additionally, I would also like to know "
Is there a way to load a dll using lua , not already loaded in my .net application?"

Related

WF4.5- Expression Activity type 'CSharpValue`1' requires compilation in order to run

Background
What I'm trying to do is to have a scoped variable of one of my models in the xaml.
In my workflow project "MyProject.Workflows" I have created model classes, code activities and Xaml files. They are all under same namespace. In another project ("Engine"), I load and execute these workflows.
To load the workflows in the "Engine", I use ActivityXamlServices with ActivityXamlServicesSettings including CompileExpressions = true.
When loading the ActivityXamlServices, I use a XamlXmlReader with XamlXmlReaderSettings where I actually point to the "MyProject.Workflows" dll.
Since Both these projects are in the same solution I actually referred MyProject.Workflows in the "Engine".
Because Earlier, they were in different solutions, So when I tried to do this It gave me It cant find the "MyProject.Workflows" dll even though I point it in the XamlXmlReaderSettings.
Then I tried to load the dll to the app domain and then it worked.But I did not want to deal with App Domains so I decided to get both projects under one solution so I can refer the "MyProject.Workflows" in the "Engine".
Issue:
If I use one of those models inside of the Xaml as an expression like "Assign Activity" the Workflow isn't getting compiled when I try to execute this.
For example if I use this in an "Assign" activity having a scoped variable of type MyObject
Newtonsoft.Json.JsonConvert.DeserializeObject<MyProject.Workflows.Models.MyObject>(inputString);
I will get the below error message when I run the workflow.
NotSupportedException:'Expression Activity type 'CSharpValue`1' requires compilation in order to run. Please ensure that the workflow has been compiled.
If I remove these objects and just deal with strings or ints, it works fine.
Things I found in my research:
I found this was a bug in .Net Framework 4.5. But Im using 4.6
Even though I used CompileExpressions = true , I tried this compile method I found. But did not change a thing.
private static void Compile(DynamicActivity dynamicActivity)
{
TextExpressionCompilerSettings settings = new TextExpressionCompilerSettings
{
Activity = dynamicActivity,
Language = "C#",
ActivityName = dynamicActivity.Name.Split('.').Last() + "_CompiledExpressionRoot",
ActivityNamespace = string.Join(".", dynamicActivity.Name.Split('.').Reverse().Skip(1).Reverse()),
RootNamespace = null,
GenerateAsPartialClass = false,
AlwaysGenerateSource = true,
};
TextExpressionCompilerResults results =
new TextExpressionCompiler(settings).Compile();
if (results.HasErrors)
{
throw new Exception("Compilation failed.");
}
ICompiledExpressionRoot compiledExpressionRoot =
Activator.CreateInstance(results.ResultType,
new object[] { dynamicActivity }) as ICompiledExpressionRoot;
CompiledExpressionInvoker.SetCompiledExpressionRootForImplementation(
dynamicActivity, compiledExpressionRoot);
}
I read that some people faced this problem and they had to actually move the models to a diffrent namespace. I did that too. Didn't fix the problem.
My Xaml file has this entry added at the top.
xmlns:local="clr-namespace:MyProject.Workflows.Models"
Can someone please help me to get through this?

Executing python script with zmq from C# using IronPython not working

I'm using ZeroMQ for inter-process communication between C# managed application and python script. When calling script from C#, using IronPython I get following error :
An unhandled exception of type
IronPython.Runtime.Exceptions.ImportException occurred in
Microsoft.Dynamic.dll
Additional information: cannot import constants from
zmq.backend.cython
Python code (test.py file):
import sys, zmq, time
def execute(input):
context = zmq.Context()
publisher = context.socket(zmq.PUB)
publisher.bind("tcp://*:18800")
time.sleep(1)
publisher.send(input) #just echo input parameter
And this is how I execute .py code from my C# app :
static void Main(string[] args)
{
var options = new Dictionary<string, object>();
options["Frames"] = true;
options["FullFrames"] = true;
ScriptEngine _engine = Python.CreateEngine(options);
ScriptRuntime _runtime = _engine.Runtime;
ICollection<string> _searchPaths = _engine.GetSearchPaths();
_searchPaths.Add(#"C:\Python27\");
_searchPaths.Add(#"C:\Python27\Lib\");
_searchPaths.Add(#"C:\Python27\Scripts\");
_searchPaths.Add(#"C:\Python27\libs\");
_searchPaths.Add(#"C:\Python27\DLLs\");
_searchPaths.Add(#"C:\Python27\include\");
_searchPaths.Add(#"C:\Python27\lib\site-packages");
_engine.SetSearchPaths(_searchPaths);
dynamic wrapperObj = _runtime.UseFile("test.py");
wrapperObj.execute("test");
}
Note that some code is omitted for brevity, and not actually relevant for this scenario. When manually invoking .py script through the command line, everything works fine, I'm able to pass data around and my C# app is receiving messages.
Anyone knows what is this error and how can be solved?
EDIT : While I was desperate and almost gave up, I tried to write quick named-pipes support, and guess what - similar error:
An unhandled exception of type IronPython.Runtime.Exceptions.ImportException occurred in
Microsoft.Dynamic.dll
Additional information: No module named win32file
This time, my test.py looks like this:
import win32file
def execute(input):
handle = win32file.CreateFile(r"\\.\pipe\test_pipe",
win32file.GENERIC_WRITE,
0, None,
win32file.OPEN_EXISTING,
0, None)
if handle:
win32file.WriteFile(self.handle, input, None)
win32file.FlushFileBuffers(self.handle)
win32file.SetFilePointer(self.handle, 0, win32file.FILE_BEGIN)
I'm not really sure if I have more options for this kind of interoperability, but I'm starting to think that IronPython can just cover basic usage scenarios (when calling pure python code from C#), not to mention matplotlib, numpy or astropy that I will certanly need later on.
Thanks in advance!
Regards,
Civa

C# run piece of code under different credential

Referring to THIS QUESTION I was able to execute some code as different user.
Now I'm trying to execute a piece of code as a user with administrator privileges and I get a missing file error. The code is this:
using (new Impersonator("domainAdmin", "DOMAIN", "myStup1dPa$$w0rd"))
{
GetXAApplicationByName apps = new GetXAApplicationByName();
apps.BrowserName = new string[] { "*" };
IEnumerable<XAApplication> result = CitrixRunspaceFactory.DefaultRunspace.ExecuteCommand(apps);
// other code...
}
The error is thrown at last reported line, the one starting with IEnumerable<XAApplication> and is:
FileNotFoundException not handled C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
I don't report code for Impersonator class, the basic schema is reported on linked question (and it worked also in other applications).
Am I missing something?
Looks like there is no problem with impersonating but your code somehow tries to load the assembly System.Management.Automation.dll which is not in GAC , can you try registering this assembly in GAC and try it out again .

System.AccessViolationException error when accessing COM DLL written in Visual FoxPro

I have a C# WinForms .NET 3.5 application that I need to call a COM DLL written in Visual FoxPro 7. I have added the COM object to the C# project fine, and can
view the objects and its members in the object browser fine.
However when I attempt to call any methods or access any properties from the COM object I get the following exception thrown :
System.AccessViolationException was unhandled
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=Interop.emscosting
StackTrace:
at emscosting.ComBaseClass.ShellExecute(String tcItem, String tcAction)
...
Below is some example C# code I am using :
emscosting.ComBaseClass com = new emscosting.ComBaseClass();
com.ShellExecute(file, "TRUE"); <--- The exception is thrown here
Also when I try and access any of the public properties of the COM object in the debugger watch window I get the following appear in the Value column :
'com.DatabaseUserName' threw an exception of type 'System.AccessViolationException'
I am already using this COM object without any issues from other VFP applications, Office applications, and in Javascript/Classic ASP.
Can someone help me resolve this issue please ?
I do have the original VFP source code for the COM still and below is a snippet of the code where I am declaring the public properties, however I am trying to
avoid rewriting the COM DLL if possible!
#If BUILD_AS_COM_OBJECT
Define Class emsCosting As Combase OlePublic
#Else
Define Class emsCosting As Combase
#Endif
CostingsPath = COSTINGS_PATH
Dimension CostingsPath_COMATTRIB[5]
CostingsPath_COMATTRIB[1] = COMATTRIB_NONE
CostingsPath_COMATTRIB[2] = "Contains the path of where the costings get saved" && HelpString
CostingsPath_COMATTRIB[3] = "CostingsPath" && Capitalisation
CostingsPath_COMATTRIB[4] = "String" && Property Type
CostingsPath_COMATTRIB[5] = 0 && Number of parameters
CostingsMaster = COSTINGS_MASTER
Dimension CostingsMaster_COMATTRIB[5]
CostingsMaster_COMATTRIB[1] = COMATTRIB_NONE
CostingsMaster_COMATTRIB[2] = "Contains the filename of the costings master (usually costings.xls)" && HelpString
CostingsMaster_COMATTRIB[3] = "CostingsMaster" && Capitalisation
CostingsMaster_COMATTRIB[4] = "String" && Property Type
CostingsMaster_COMATTRIB[5] = 0 && Number of parameters
Function SetCostingsPath(tcPath As String) As VOID HelpString "This is a test function"
CostingsPath = tcPath
EndFunc
Function TestFunctionNoParam() AS String HelpString "This is a helpstring"
Return "\\TEST\ContractReviewCostings\"
EndFunc
......
NOTE: Both the two test functions defined above fail with the exception System.AccessViolationException
You have to compile your .Net Application for x86, if you have a x64 system.
When you compile it with "Any CPU" or "x64", you can not access the x32 FoxPro-DLL.

Saving collection using PhoneApplicationService.Current.State

I'm trying to save a collection where a store a diagnostic log temporarily when navigating away from my app - I've looked at other sample code and it seems pretty basic as I've done below:
Saving:
PhoneApplicationService.Current.State["DiagnosticLog"] = DiagnosticLog;
Loading:
if (PhoneApplicationService.Current.State.ContainsKey("DiagnosticLog"))
DiagnosticLog = (ObservableCollection<DiagnosticLogEntry>)
PhoneApplicationService.Current.State["DiagnosticLog"];
However I get this error:
A first chance exception of type
'System.Runtime.Serialization.InvalidDataContractException' occurred
in System.Runtime.Serialization.dll
Any suggestions please?
Usually, when this happens, it means you do not have a default public constructor on your Diagnostic class (or one of it's contained classes).

Categories

Resources